From 9071fccf76d2ce5b2845051e1ddafc3cba5d8c9b Mon Sep 17 00:00:00 2001 From: NGnius Date: Wed, 23 Oct 2019 22:42:10 -0400 Subject: [PATCH] Fix commands not being disposed properly and disabled MoveBlocks --- ...mandLineCompositionRootSaveCommandPatch.cs | 9 +++++-- extracommands/ExtraCommands.csproj | 3 +-- extracommands/MoveBlocksCommandEngine.cs | 2 +- ...andPatch.cs => UnregisterCommandEngine.cs} | 24 +++++++------------ 4 files changed, 18 insertions(+), 20 deletions(-) rename extracommands/{CommandLineCompositionRootUnregisterCommandPatch.cs => UnregisterCommandEngine.cs} (66%) diff --git a/extracommands/CommandLineCompositionRootSaveCommandPatch.cs b/extracommands/CommandLineCompositionRootSaveCommandPatch.cs index 0898ef5..01ebf1a 100644 --- a/extracommands/CommandLineCompositionRootSaveCommandPatch.cs +++ b/extracommands/CommandLineCompositionRootSaveCommandPatch.cs @@ -16,6 +16,8 @@ namespace ExtraCommands { static void Postfix(UnityContext contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters) { + int commandCount = 0; + int engineCount = 0; MethodInfo commandHelp = Harmony.AccessTools.Method(Harmony.AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility"), "SaveCommandHelp", new Type[] { typeof(string), typeof(string) }); foreach (Type t in typeof(CustomCommandEngine).Assembly.GetTypes()) { @@ -32,16 +34,19 @@ namespace ExtraCommands .Invoke(new object[] { contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters }); // add to engineRoot enginesRoot.AddEngine(inst); + engineCount++; } // add to Gamecraft help command commandHelp.Invoke(null, new string[] { attr.Name, attr.Description }); + commandCount++; } } } + enginesRoot.AddEngine(new UnregisterCommandEngine(contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters)); + Debug.Log($"Added {commandCount} custom commands in {engineCount} engines"); } - [HarmonyTargetMethod] - static MethodBase HTargetMethod(HarmonyInstance instance) + static MethodBase TargetMethod(HarmonyInstance instance) { return _ComposeMethodInfo(CommandLineCompositionRoot.Compose); } diff --git a/extracommands/ExtraCommands.csproj b/extracommands/ExtraCommands.csproj index 1ec3ecc..db17a5c 100644 --- a/extracommands/ExtraCommands.csproj +++ b/extracommands/ExtraCommands.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + net45 @@ -72,7 +72,6 @@ ..\ref\uREPL.dll - diff --git a/extracommands/MoveBlocksCommandEngine.cs b/extracommands/MoveBlocksCommandEngine.cs index 1919c03..851ff41 100644 --- a/extracommands/MoveBlocksCommandEngine.cs +++ b/extracommands/MoveBlocksCommandEngine.cs @@ -14,7 +14,7 @@ using RobocraftX; namespace ExtraCommands.Building { - [CustomCommand("MoveBlocks", "Move blocks from their original position")] + //[CustomCommand("MoveBlocks", "Move blocks from their original position")] class MoveBlocksCommandEngine : CustomCommandEngine { public MoveBlocksCommandEngine(UnityContext ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) diff --git a/extracommands/CommandLineCompositionRootUnregisterCommandPatch.cs b/extracommands/UnregisterCommandEngine.cs similarity index 66% rename from extracommands/CommandLineCompositionRootUnregisterCommandPatch.cs rename to extracommands/UnregisterCommandEngine.cs index 76575d3..6546aec 100644 --- a/extracommands/CommandLineCompositionRootUnregisterCommandPatch.cs +++ b/extracommands/UnregisterCommandEngine.cs @@ -11,12 +11,15 @@ using Svelto.Context; namespace ExtraCommands { - [HarmonyPatch] - class CommandLineCompositionRootUnregisterCommandPatch + class UnregisterCommandEngine : CustomCommandEngine { - static void Prefix() + public UnregisterCommandEngine(UnityContext ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) { - MethodInfo commandRemoveHelp = Harmony.AccessTools.Method(Harmony.AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility"), "SaveCommandHelp", new Type[] { typeof(string) }); + } + public override void Dispose() + { + int count = 0; + MethodInfo commandRemoveHelp = Harmony.AccessTools.Method(Harmony.AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility"), "UnregisterCommandHelp", new Type[] { typeof(string) }); foreach (Type t in typeof(CustomCommandEngine).Assembly.GetTypes()) { CustomCommandAttribute[] attributes = (CustomCommandAttribute[])t.GetCustomAttributes(typeof(CustomCommandAttribute), false); @@ -26,20 +29,11 @@ namespace ExtraCommands { // remove Gamecraft help command commandRemoveHelp.Invoke(null, new string[] { attr.Name }); + count++; } } } - } - - [HarmonyTargetMethod] - static MethodBase HTargetMethod(HarmonyInstance instance) - { - return _OnContextDestroyedMethodInfo((new CommandLineCompositionRoot()).OnContextDestroyed); - } - - private static MethodInfo _OnContextDestroyedMethodInfo(Action a) - { - return a.Method; + Debug.Log($"Removed {count} custom commands"); } } }