Browse Source

Add Harmony and GC internal call PoC

tags/v0.0.0a
NGnius 5 years ago
parent
commit
d5a2911e41
3 changed files with 68 additions and 2 deletions
  1. +4
    -0
      TestMod/TestMod.csproj
  2. +37
    -0
      TestMod/TestPatch.cs
  3. +27
    -2
      TestMod/TestPlugin.cs

+ 4
- 0
TestMod/TestMod.csproj View File

@@ -4,6 +4,10 @@
<TargetFramework>net45</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Lib.Harmony" Version="1.2.0.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="CommandLine">
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\CommandLine.dll</HintPath>


+ 37
- 0
TestMod/TestPatch.cs View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using RobocraftX.GUI.CommandLine;
using Harmony;
using UnityEngine;

namespace TestMod
{
[HarmonyPatch]
class TestPatch
{
static void Prefix(string name, Action func, string description = "")
{
Debug.Log("Registering "+name);
//uREPL.Log.Output("This is an unhelpful message");
}

[HarmonyTargetMethod]
static MethodBase HTargetMethod(HarmonyInstance instance)
{
Type CLUtility = AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility");
MethodInfo[] methods = CLUtility.GetMethods();
foreach (MethodInfo m in methods)
{
if (m.Name == "Register") {
return m;
}
}
Debug.Log("Defaulting to "+methods[0].Name);
return methods[0];
}
}
}

+ 27
- 2
TestMod/TestPlugin.cs View File

@@ -2,11 +2,15 @@
using IllusionPlugin;
using UnityEngine;
using uREPL;
using Harmony;
using System.Reflection;

namespace TestMod
{
public class Plugin : IllusionPlugin.IEnhancedPlugin
{
public static HarmonyInstance harmony { get; protected set; }

public string[] Filter { get; } = new string[] { "RobocraftX", "Gamecraft" };

public string Name { get; } = "TestPlugin";
@@ -21,7 +25,18 @@ namespace TestMod
public void OnApplicationStart()
{
Debug.Log("Hello World!");
RuntimeCommands.Register<object>("Log", LogCommand,"Write a message to Player.log (Runtime version)");
if (harmony == null)
{
harmony = HarmonyInstance.Create("org.git.exmods.gamecraft.testmod.testplugin");
harmony.PatchAll(Assembly.GetExecutingAssembly());
}
MethodInfo commandHelp = Harmony.AccessTools.Method(Harmony.AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility"), "SaveCommandHelp", new Type[] { typeof(string), typeof(string)});
uREPL.RuntimeCommands.Register<string>("Log", LogCommand, "this description is not shown anywhere");
commandHelp.Invoke(null, new string[] { "Log", "Write a string to Player.log" });
uREPL.RuntimeCommands.Register<string>("Echo", EchoCommand, "this doesn't matter");
commandHelp.Invoke(null, new string[] { "Echo", "Write a message to the command line" });
uREPL.RuntimeCommands.Register("Exit", QuitCommand, "Close Gamecraft");
commandHelp.Invoke(null, new string[] { "Exit", "Forcefully exit Gamecraft immediately" });
Debug.Log("TestPlugin startup complete");
}

@@ -52,9 +67,19 @@ namespace TestMod
}

/* Log command method. This called by uREPL when the Log <object> command is called from RCX */
private void LogCommand(object msg)
private void LogCommand(string msg)
{
Debug.Log(msg);
}

private void EchoCommand(string msg)
{
uREPL.Log.Output(msg);
}

private void QuitCommand()
{
UnityEngine.Application.Quit();
}
}
}