Fix commands not being disposed properly and disabled MoveBlocks

This commit is contained in:
NGnius 2019-10-23 22:42:10 -04:00
parent 064f719f82
commit 9071fccf76
4 changed files with 18 additions and 20 deletions

View file

@ -16,6 +16,8 @@ namespace ExtraCommands
{ {
static void Postfix(UnityContext<FullGameCompositionRoot> contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters) static void Postfix(UnityContext<FullGameCompositionRoot> 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) }); 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()) foreach (Type t in typeof(CustomCommandEngine).Assembly.GetTypes())
{ {
@ -32,16 +34,19 @@ namespace ExtraCommands
.Invoke(new object[] { contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters }); .Invoke(new object[] { contextHolder, enginesRoot, physicsWorld, reloadGame, multiplayerParameters });
// add to engineRoot // add to engineRoot
enginesRoot.AddEngine(inst); enginesRoot.AddEngine(inst);
engineCount++;
} }
// add to Gamecraft help command // add to Gamecraft help command
commandHelp.Invoke(null, new string[] { attr.Name, attr.Description }); 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 TargetMethod(HarmonyInstance instance)
static MethodBase HTargetMethod(HarmonyInstance instance)
{ {
return _ComposeMethodInfo(CommandLineCompositionRoot.Compose<CommandLineContext>); return _ComposeMethodInfo(CommandLineCompositionRoot.Compose<CommandLineContext>);
} }

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>net45</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -72,7 +72,6 @@
<Reference Include="uREPL"> <Reference Include="uREPL">
<HintPath>..\ref\uREPL.dll</HintPath> <HintPath>..\ref\uREPL.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -14,7 +14,7 @@ using RobocraftX;
namespace ExtraCommands.Building namespace ExtraCommands.Building
{ {
[CustomCommand("MoveBlocks", "Move blocks from their original position")] //[CustomCommand("MoveBlocks", "Move blocks from their original position")]
class MoveBlocksCommandEngine : CustomCommandEngine class MoveBlocksCommandEngine : CustomCommandEngine
{ {
public MoveBlocksCommandEngine(UnityContext<FullGameCompositionRoot> ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) public MoveBlocksCommandEngine(UnityContext<FullGameCompositionRoot> ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams)

View file

@ -11,12 +11,15 @@ using Svelto.Context;
namespace ExtraCommands namespace ExtraCommands
{ {
[HarmonyPatch] class UnregisterCommandEngine : CustomCommandEngine
class CommandLineCompositionRootUnregisterCommandPatch
{ {
static void Prefix() public UnregisterCommandEngine(UnityContext<FullGameCompositionRoot> 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()) foreach (Type t in typeof(CustomCommandEngine).Assembly.GetTypes())
{ {
CustomCommandAttribute[] attributes = (CustomCommandAttribute[])t.GetCustomAttributes(typeof(CustomCommandAttribute), false); CustomCommandAttribute[] attributes = (CustomCommandAttribute[])t.GetCustomAttributes(typeof(CustomCommandAttribute), false);
@ -26,20 +29,11 @@ namespace ExtraCommands
{ {
// remove Gamecraft help command // remove Gamecraft help command
commandRemoveHelp.Invoke(null, new string[] { attr.Name }); commandRemoveHelp.Invoke(null, new string[] { attr.Name });
count++;
} }
} }
} }
} Debug.Log($"Removed {count} custom commands");
[HarmonyTargetMethod]
static MethodBase HTargetMethod(HarmonyInstance instance)
{
return _OnContextDestroyedMethodInfo((new CommandLineCompositionRoot()).OnContextDestroyed);
}
private static MethodInfo _OnContextDestroyedMethodInfo(Action a)
{
return a.Method;
} }
} }
} }