From aa276b8ecd75b456c547515b1820163d73eac867 Mon Sep 17 00:00:00 2001 From: NGnius Date: Mon, 21 Oct 2019 21:08:37 -0400 Subject: [PATCH] Test and fix waypoint system --- TestMod/TeleportEnginePatch.cs | 20 +++++------------ TestMod/TeleportPatch.cs | 41 +++++++++++++++++++++------------- TestMod/TestMod.csproj | 18 +++++++++++++++ TestMod/TestPlugin.cs | 4 ++-- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/TestMod/TeleportEnginePatch.cs b/TestMod/TeleportEnginePatch.cs index f4c2346..bb0b015 100644 --- a/TestMod/TeleportEnginePatch.cs +++ b/TestMod/TeleportEnginePatch.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Reflection; using RobocraftX.GUI.CommandLine; using Harmony; using UnityEngine; @@ -8,22 +6,16 @@ using UnityEngine; namespace TestMod.Waypoints { [HarmonyPatch(typeof(TeleportCharacterCommandEngine))] - [HarmonyPatch(typeof(new Type[] {}))] - class TeleportEnginePatch + [HarmonyPatch("Ready")] + [HarmonyPatch(new Type[] { })] + public static class TeleportEnginePatch { - static TeleportCharacterCommandEngine Instance {get; private set;} + public static TeleportCharacterCommandEngine Instance {get; private set;} - static void Postfix(TeleportCharacterCommandEngine __result, TeleportCharacterCommandEngine __instance) + static void Postfix(TeleportCharacterCommandEngine __instance) { // this works because this class should be a singleton; only one copy of it should exist at a time - if (__instance != null) - { - Instance = __instance; - Debug.Log("Instance caught from constructor"); - } else { - Instance = __result; - Debug.Log("Result caught from constructor") - } + Instance = __instance; Debug.Log("Caught TeleportCharacterCommandEngine"); } } diff --git a/TestMod/TeleportPatch.cs b/TestMod/TeleportPatch.cs index 20ef009..f75c5e5 100644 --- a/TestMod/TeleportPatch.cs +++ b/TestMod/TeleportPatch.cs @@ -1,29 +1,27 @@ +using System; +using System.Collections.Generic; using System.Reflection; -using System.Dictionary.Generic; using Harmony; using UnityEngine; -using Unity.Mathematics; using RobocraftX.Character; using RobocraftX.StateSync; -using RobocraftX.GUI.CommandLine; +using Svelto.ECS; using uREPL; namespace TestMod.Waypoints { - [HarmonyPatch(typeof(uREPL.RuntimeCommands))] - [HarmonyPatch("Register")] - [HarmonyPatch(new Type[] { typeof(string), typeof(Action), typeof(string) })] - class TeleportPatch + [HarmonyPatch] + public static class TeleportPatch { // TeleportPatch intercepts all Register(string, Action string) method calls // which allows it to catch Teleport Actions as they're registered as CLI commands - static Action TeleportAbsolute {get; private set;} + public static Action TeleportAbsolute {get; private set;} - static Action TeleportRelative {get; private set;} + public static Action TeleportRelative {get; private set;} - static Dictionary _waypoints {get;} = new Dictionnary(); + public static Dictionary _waypoints {get;} = new Dictionary(); - static void Prefix(string name, Action func, string description) + static void Prefix(string name, Action func, string description) { Debug.Log("Caught Register method call for "+name); if (name.Equals("TeleportPlayerAbsolute")) @@ -37,7 +35,18 @@ namespace TestMod.Waypoints } } - static void WaypointHereCommand(object name) + static MethodBase TargetMethod(HarmonyInstance instance) + { + // TODO: make less hacky + return _giveMeMethodInfoFFS(uREPL.RuntimeCommands.Register); + } + + private static MethodInfo _giveMeMethodInfoFFS(Action, string> a) + { + return a.Method; + } + + public static void WaypointHereCommand(object name) { // hopefully entitiesDB does not become private in the future... if (TeleportEnginePatch.Instance == null) @@ -49,12 +58,13 @@ namespace TestMod.Waypoints { _waypoints.Remove(name); } - ref RigidBodyEntityStruct reference = ref TeleportEnginePatch.Instance.entitiesDB.QueryEntity(0u, CharacterExclusiveGroups.CharacterGroup); - _waypoints.Add(new float[3]{reference.x, reference.y, reference.z}); + IEntitiesDB entitiesDB = Harmony.Traverse.Create(TeleportEnginePatch.Instance).Property("entitiesDB").GetValue(); + ref PhysicsEntityStruct reference = ref entitiesDB.QueryEntity(0u, CharacterExclusiveGroups.CharacterGroup); + _waypoints.Add(name, new float[3]{reference.position.x, reference.position.y, reference.position.z}); uREPL.Log.Output("Saved "+name.ToString()); } - static void TeleportToWaypointCommand(object name) + public static void TeleportToWaypointCommand(object name) { if (!_waypoints.ContainsKey(name)) { @@ -67,7 +77,6 @@ namespace TestMod.Waypoints return; } TeleportAbsolute(_waypoints[name][0], _waypoints[name][1], _waypoints[name][2]); - uREPL.Log.Output("Teleported player to "+name.ToString()); } } diff --git a/TestMod/TestMod.csproj b/TestMod/TestMod.csproj index ee78617..c37783f 100644 --- a/TestMod/TestMod.csproj +++ b/TestMod/TestMod.csproj @@ -15,6 +15,24 @@ ..\..\..\IPA\IPA\bin\Debug\IPA\Data\Managed\IllusionPlugin.dll + + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\RobocraftX.Character.dll + + + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\RobocraftX.StateSync.dll + + + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\Svelto.ECS.dll + + + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\Unity.Mathematics.dll + + + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\Unity.Mathematics.Extensions.dll + + + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\Unity.Mathematics.Extensions.Hybrid.dll + ..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Gamecraft\RobocraftX_Data\Managed\UnityEngine.dll diff --git a/TestMod/TestPlugin.cs b/TestMod/TestPlugin.cs index bc22d05..6a295a0 100644 --- a/TestMod/TestPlugin.cs +++ b/TestMod/TestPlugin.cs @@ -38,9 +38,9 @@ namespace TestMod uREPL.RuntimeCommands.Register("Exit", QuitCommand, "Close Gamecraft"); commandHelp.Invoke(null, new string[] { "Exit", "Forcefully exit Gamecraft immediately" }); // waypoint commands - uREPL.RuntimeCommands.Register("WaypointHere", Waypoints.WaypointHereCommand, "/shrug"); + uREPL.RuntimeCommands.Register("WaypointHere", Waypoints.TeleportPatch.WaypointHereCommand, "/shrug"); commandHelp.Invoke(null, new string[] { "WaypointHere", "Create a named waypoint in your current location" }); - uREPL.RuntimeCommands.Register("TeleportToWaypoint", Waypoints.TeleportToWaypoint, "Blame Josh"); + uREPL.RuntimeCommands.Register("TeleportToWaypoint", Waypoints.TeleportPatch.TeleportToWaypointCommand, "Blame Josh"); commandHelp.Invoke(null, new string[] { "TeleportToWaypoint", "Teleport to a waypoint by name" }); Debug.Log("TestPlugin startup complete"); }