Skip to content

Commit

Permalink
Defer getting Active Mod ids to Utility.ActiveModId
Browse files Browse the repository at this point in the history
Fixed NullReferenceExceptions for CurrentMod.Identifier usage
Proper mod Unload and Reload
  • Loading branch information
Spartan322 committed May 9, 2017
1 parent f2b198e commit e0baa69
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 39 deletions.
21 changes: 14 additions & 7 deletions Pathfinder/Command/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace Pathfinder.Command
{
public static class Handler
{
internal static Dictionary<string, Tuple<Func<Hacknet.OS, List<string>, bool>, string>> commands =
new Dictionary<string, Tuple<Func<Hacknet.OS, List<string>, bool>, string>>();
internal static Dictionary<string, Func<Hacknet.OS, List<string>, bool>> commands =
new Dictionary<string, Func<Hacknet.OS, List<string>, bool>>();
internal static Dictionary<string, List<string>> modToCommands = new Dictionary<string, List<string>>();

private static Dictionary<string, string> help = new Dictionary<string, string>();

Expand All @@ -28,22 +29,28 @@ public static string RegisterCommand(string key,
string description = null,
bool autoComplete = false)
{
Logger.Verbose("Mod {0} is attempting to add command {1}", Pathfinder.CurrentMod.Identifier, key);
Logger.Verbose("Mod {0} is attempting to add command {1}", Utility.ActiveModId, key);
if (commands.ContainsKey(key))
return null;
commands.Add(key, new Tuple<Func<Hacknet.OS, List<string>, bool>, string>(function, Pathfinder.CurrentMod.Identifier));
commands.Add(key, function);
if(!modToCommands.ContainsKey(Utility.ActiveModId))
modToCommands.Add(Utility.ActiveModId, new List<string>());
modToCommands[Utility.ActiveModId].Add(key);
if (description != null)
//Helpfile.help.Add(key + "\n " + description);
help.Add(key, description);
if(autoComplete && !ProgramList.programs.Contains(key))
ProgramList.programs.Add(key);
return Pathfinder.CurrentMod.Identifier + '.' + key;
return Utility.ActiveModId + '.' + key;
}

internal static bool UnregisterCommand(string key)
{
Logger.Info("unreg 1 {0}", key);
if (!commands.ContainsKey(key))
return true;
Logger.Info("unreg 2");
modToCommands[Utility.ActiveModId].Remove(key);
help.Remove(key);
ProgramList.programs.Remove(key);
return commands.Remove(key);
Expand All @@ -68,13 +75,13 @@ public static bool AddCommand(string key, Func<Hacknet.OS, string[], bool> funct

internal static void CommandListener(CommandSentEvent e)
{
Tuple<Func<Hacknet.OS, List<string>, bool>, string> f;
Func<Hacknet.OS, List<string>, bool> f;
if (commands.TryGetValue(e.Arguments[0], out f))
{
e.IsCancelled = true;
try
{
e.Disconnects = f.Item1(e.OS, e.Arguments);
e.Disconnects = f(e.OS, e.Arguments);
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Pathfinder/Daemon/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static string RegisterDaemon(string id, IInterface inter)
{
id = Utility.GetId(id, throwFindingPeriod: true);
Logger.Verbose("Mod {0} attempting to add daemon interface {1} with id {2}",
Pathfinder.CurrentMod.Identifier,
Utility.ActiveModId,
inter.GetType().FullName,
id);
if (idToInterface.ContainsKey(id))
Expand Down
2 changes: 1 addition & 1 deletion Pathfinder/Event/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void RegisterListener(Type pathfinderEventType, Action<PathfinderE
if (String.IsNullOrEmpty(debugName))
debugName = "[" + Path.GetFileName(listener.Method.Module.Assembly.Location) + "] "
+ listener.Method.DeclaringType.FullName + "." + listener.Method.Name;
eventListeners[pathfinderEventType].Add(new Tuple<Action<PathfinderEvent>, string, string>(listener, debugName, Pathfinder.CurrentMod.Identifier));
eventListeners[pathfinderEventType].Add(new Tuple<Action<PathfinderEvent>, string, string>(listener, debugName, Utility.ActiveModId));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Pathfinder/Executable/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static string RegisterExecutable(string id, IInterface inter)
{
id = Utility.GetId(id, throwFindingPeriod: true);
Logger.Verbose("Mod '{0}' is attempting to add executable interface {1} with id {2}",
Pathfinder.CurrentMod.Identifier,
Utility.ActiveModId,
inter.GetType().FullName,
id);
if (idToInterface.ContainsKey(id))
Expand Down
2 changes: 1 addition & 1 deletion Pathfinder/Extension/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static string RegisterExtension(string id, Info extensionInfo)
{
id = Utility.GetId(id, throwFindingPeriod: true);
Logger.Verbose("Mod {0} attempting to register extension {1} with id {2}",
Pathfinder.CurrentMod.Identifier,
Utility.ActiveModId,
extensionInfo.GetType().FullName,
id);
if (idToInfo.ContainsKey(id))
Expand Down
18 changes: 13 additions & 5 deletions Pathfinder/GUI/PathfinderMainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ private static void PrepareButtons(DrawMainMenuEvent e)
unloadButtons.Clear();
foreach (var id in ids)
{
var mod = Pathfinder.GetMod(id);
var loc = mod.GetType().Assembly.Location;
unloadButtons[id] = new Button(-1, -1, 100, 30, "Unload")
{
DrawFinish = r => { if (r.JustReleased) { Pathfinder.UnloadMod(Pathfinder.GetMod(id)); disabledModIds.Add(id); } }
DrawFinish = r => {
if (r.JustReleased)
{
Pathfinder.UnloadMod(mod);
disabledModIds.Add(id);
}
}
};
reloadButtons[id] = new GUI.Button(-1, -1, 100, 30, "Load")
{
Expand All @@ -51,10 +59,10 @@ private static void PrepareButtons(DrawMainMenuEvent e)
{
try
{
Pathfinder.LoadMod(Pathfinder.GetMod(id).GetType().Assembly.Location, true);
Pathfinder.LoadMod(loc, true);
disabledModIds.Remove(id);
}
catch (Exception) { }
disabledModIds.Remove(id);
catch (Exception) {}
}
}
};
Expand Down Expand Up @@ -93,7 +101,7 @@ public static void DrawMainMenu(DrawMainMenuEvent e)

Gui.TextItem.doFontLabel(new Vector2(200, yPos), "Disabled Mods", GuiData.font, Color.White);
yPos += 50;

index = 0;
foreach (var id in disabledModIds)
{
b = reloadButtons[id];
Expand Down
4 changes: 2 additions & 2 deletions Pathfinder/Mission/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static string RegisterMissionGoal(string id, IMissionGoal inter)
{
id = Utility.GetId(id, throwFindingPeriod: true);
Logger.Verbose("Mod {0} attempting to add mission goal interface {1} with id {2}",
Pathfinder.CurrentMod.Identifier,
Utility.ActiveModId,
inter.GetType().FullName,
id);
if (goals.ContainsKey(id))
Expand All @@ -38,7 +38,7 @@ public static string RegisterMission(string id, IInterface inter)
{
id = Utility.GetId(id, throwFindingPeriod: true);
Logger.Verbose("Mod {0} attempting to add mission interface {1} with id {2}",
Pathfinder.CurrentMod.Identifier,
Utility.ActiveModId,
inter.GetType().FullName,
id);
if (goals.ContainsKey(id))
Expand Down
34 changes: 16 additions & 18 deletions Pathfinder/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,30 @@ internal static void UnloadMod(IPathfinderMod mod)
if (mod == null) return;

CurrentMod = mod;

string id = "";

for (var i = 0; ++i < Extension.Handler.idToInfo.Count; id = Extension.Handler.idToInfo.Keys.ElementAt(i))
if (id.IndexOf('.') != -1 && id.Remove(id.IndexOf('.')) == mod.Identifier)
Extension.Handler.UnregisterExtension(id);
foreach (var e in Extension.Handler.idToInfo.ToArray())
if (e.Key.IndexOf('.') != -1 && e.Key.Remove(id.IndexOf('.')) == mod.Identifier)
Extension.Handler.UnregisterExtension(e.Key);

for (var i = 0; ++i < Executable.Handler.idToInterface.Count; id = Executable.Handler.idToInterface.Keys.ElementAt(i))
if (id.IndexOf('.') != -1 && id.Remove(id.IndexOf('.')) == mod.Identifier)
foreach (var e in Executable.Handler.idToInterface.ToArray())
if (e.Key.IndexOf('.') != -1 && id.Remove(id.IndexOf('.')) == mod.Identifier)
Executable.Handler.UnregisterExecutable(id);

for (var i = 0; ++i < Daemon.Handler.idToInterface.Count; id = Daemon.Handler.idToInterface.Keys.ElementAt(i))
if (id.IndexOf('.') != -1 && id.Remove(id.IndexOf('.')) == mod.Identifier)
Daemon.Handler.UnregisterDaemon(id);
foreach (var d in Daemon.Handler.idToInterface.ToArray())
if (d.Key.IndexOf('.') != -1 && d.Key.Remove(d.Key.IndexOf('.')) == mod.Identifier)
Daemon.Handler.UnregisterDaemon(d.Key);

//var pair = new KeyValuePair<string, Tuple<Func<Hacknet.OS, List<string>, bool>, string>>();
Command.Handler.commands.Where(pair => pair.Value.Item2 == mod.Identifier)
.Select(pair => Command.Handler.UnregisterCommand(pair.Key));
foreach (var c in Command.Handler.modToCommands[mod.Identifier].ToArray())
Command.Handler.UnregisterCommand(c);

for (var i = 0; ++i < Mission.Handler.goals.Count; id = Mission.Handler.goals.Keys.ElementAt(i))
if (id.IndexOf('.') != -1 && id.Remove(id.IndexOf('.')) == mod.Identifier)
Mission.Handler.UnregisterMissionGoal(id);
foreach (var g in Mission.Handler.goals.ToArray())
if (g.Key.IndexOf('.') != -1 && g.Key.Remove(g.Key.IndexOf('.')) == mod.Identifier)
Mission.Handler.UnregisterMissionGoal(g.Key);

for (var i = 0; ++i < Mission.Handler.missions.Count; id = Mission.Handler.missions.Keys.ElementAt(i))
if (id.IndexOf('.') != -1 && id.Remove(id.IndexOf('.')) == mod.Identifier)
Mission.Handler.UnregisterMission(id);
foreach (var m in Mission.Handler.missions.ToArray())
if (m.Key.IndexOf('.') != -1 && m.Key.Remove(m.Key.IndexOf('.')) == mod.Identifier)
Mission.Handler.UnregisterMission(m.Key);

var events = new List<Tuple<Action<PathfinderEvent>, string, string>>();
foreach (var v in EventManager.eventListeners.Values)
Expand Down
2 changes: 1 addition & 1 deletion Pathfinder/Port/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Handler
public static string RegisterPort(string id, Type port)
{
id = Utility.GetId(id, throwFindingPeriod: true);
Logger.Verbose("Mod {0} attempting to register port [{1}] with id {2}", Pathfinder.CurrentMod.Identifier, port, id);
Logger.Verbose("Mod {0} attempting to register port [{1}] with id {2}", Utility.ActiveModId, port, id);
if (idToPortType.ContainsKey(id))
return null;
port.PortId = id;
Expand Down
2 changes: 1 addition & 1 deletion Pathfinder/Util/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public enum LogLevel
public static void Log(LogLevel level, params object[] input)
{
Tuple<LogLevel, string> t;
var prefix = IncludeModId ? Pathfinder.CurrentMod.Identifier+ " " : "";
var prefix = IncludeModId ? Utility.ActiveModId + " " : "";
if (input.Length > 1)
t = new Tuple<LogLevel, string>(level,
String.Format("{0}[{1}]: {2}",
Expand Down
4 changes: 3 additions & 1 deletion Pathfinder/Util/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static string ConvertToValidXmlAttributeName(string input)
return input;
}

public static string ActiveModId => Pathfinder.CurrentMod?.Identifier ?? "Pathfinder";

/// <summary>
/// Retrieves an identifier for the input.
/// </summary>
Expand All @@ -47,7 +49,7 @@ public static string GetId(string inputId, bool ignorePeriod = false, bool ignor

xmlString = ignoreValidXml ? xmlString : ConvertToValidXmlAttributeName(xmlString);
if (!ignorePeriod && inputId.IndexOf('.') == -1)
xmlString = Pathfinder.CurrentMod.Identifier + "." + xmlString;
xmlString = ActiveModId + "." + xmlString;
return inputId.IndexOf('.') != -1 ? inputId.Remove(inputId.LastIndexOf('.')+1) + xmlString : inputId;
}

Expand Down

0 comments on commit e0baa69

Please sign in to comment.