From f19dc4601082599930e12a1f8c18fd63b42af359 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Tue, 22 Oct 2019 20:40:04 -0400 Subject: [PATCH] Create untested basic commands --- extracommands/ChainCommandEngine.cs | 69 +++++++++++++++++++ ...andLineCompositionRootSaveCommandPatch.cs} | 3 +- ...neCompositionRootUnregisterCommandPatch.cs | 45 ++++++++++++ extracommands/ExitCommandEngine.cs | 37 ++++++++++ extracommands/SetFOVCommandEngine.cs | 37 ++++++++++ .../SetTargetFramerateCommandEngine.cs | 37 ++++++++++ ...nt.cs => TeleportWaypointCommandEngine.cs} | 4 +- extracommands/WaitCommandEngine.cs | 37 ++++++++++ 8 files changed, 265 insertions(+), 4 deletions(-) create mode 100644 extracommands/ChainCommandEngine.cs rename extracommands/{CommandLineCompositionRootPatch.cs => CommandLineCompositionRootSaveCommandPatch.cs} (96%) create mode 100644 extracommands/CommandLineCompositionRootUnregisterCommandPatch.cs create mode 100644 extracommands/ExitCommandEngine.cs create mode 100644 extracommands/SetFOVCommandEngine.cs create mode 100644 extracommands/SetTargetFramerateCommandEngine.cs rename extracommands/{CreateWaypoint.cs => TeleportWaypointCommandEngine.cs} (93%) create mode 100644 extracommands/WaitCommandEngine.cs diff --git a/extracommands/ChainCommandEngine.cs b/extracommands/ChainCommandEngine.cs new file mode 100644 index 0000000..000d51b --- /dev/null +++ b/extracommands/ChainCommandEngine.cs @@ -0,0 +1,69 @@ +using System; +using RobocraftX.GUI.CommandLine; +using RobocraftX.Multiplayer; +using RobocraftX.StateSync; +using RobocraftX.Character; +using Svelto.ECS; +using Unity.Entities; +using UnityEngine; +using uREPL; +using Svelto.Context; +using RobocraftX; + +namespace ExtraCommands.Basics +{ + [CustomCommand("Chain", "Run two commands, one after the other")] + [CustomCommand("ChainNoFail", "Run two commands, one after the other even if the first one is invalid")] + [CustomCommand("ChainQuiet", "Run two commands, one after the other quietly")] + class ChainCommandEngine : CustomCommandEngine + { + public ChainCommandEngine(UnityContext ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) + { + } + + public override void Ready() + { + uREPL.RuntimeCommands.Register("Chain", ChainCommand, "Run two commands, one after the other"); + uREPL.RuntimeCommands.Register("ChainNoFail", ChainCommand, "Run two commands, one after the other even if the first one is invalid"); + uREPL.RuntimeCommands.Register("ChainQuiet", ChainCommand, "Run two commands, one after the other quietly"); + } + + private void ChainCommand(string command1, string command2) + { + bool success1 = uREPL.RuntimeCommands.ConvertIntoCodeIfCommand(command1); + if (!success1) { + uREPL.Log.Error("First command was not executed successfully"); + return; + } + bool success2 = uREPL.RuntimeCommands.ConvertIntoCodeIfCommand(command2); + if (!success2) { + uREPL.Log.Error("Second command was not executed successfully"); + } + } + + private void ChainNoFailCommand(string command1, string command2) + { + bool success1 = uREPL.RuntimeCommands.ConvertIntoCodeIfCommand(command1); + if (!success1) { + uREPL.Log.Error("First command was not executed successfully"); + } + bool success2 = uREPL.RuntimeCommands.ConvertIntoCodeIfCommand(command2); + if (!success2) { + uREPL.Log.Error("Second command was not executed successfully"); + } + } + + private void ChainQuietCommand(string command1, string command2) + { + uREPL.RuntimeCommands.ConvertIntoCodeIfCommand(command1); + uREPL.RuntimeCommands.ConvertIntoCodeIfCommand(command2); + } + + public override void Dispose() + { + uREPL.RuntimeCommands.Unregister("Chain"); + uREPL.RuntimeCommands.Unregister("ChainNoFail"); + uREPL.RuntimeCommands.Unregister("ChainQuiet"); + } + } +} diff --git a/extracommands/CommandLineCompositionRootPatch.cs b/extracommands/CommandLineCompositionRootSaveCommandPatch.cs similarity index 96% rename from extracommands/CommandLineCompositionRootPatch.cs rename to extracommands/CommandLineCompositionRootSaveCommandPatch.cs index 2b9a804..0898ef5 100644 --- a/extracommands/CommandLineCompositionRootPatch.cs +++ b/extracommands/CommandLineCompositionRootSaveCommandPatch.cs @@ -12,7 +12,7 @@ using Svelto.Context; namespace ExtraCommands { [HarmonyPatch] - class CommandLineCompositionRootPatch + class CommandLineCompositionRootSaveCommandPatch { static void Postfix(UnityContext contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters) { @@ -38,7 +38,6 @@ namespace ExtraCommands } } } - Debug.Log("Postfix"); } [HarmonyTargetMethod] diff --git a/extracommands/CommandLineCompositionRootUnregisterCommandPatch.cs b/extracommands/CommandLineCompositionRootUnregisterCommandPatch.cs new file mode 100644 index 0000000..76575d3 --- /dev/null +++ b/extracommands/CommandLineCompositionRootUnregisterCommandPatch.cs @@ -0,0 +1,45 @@ +using System; +using System.Reflection; +using Harmony; +using UnityEngine; +using Unity.Entities; +using RobocraftX; +using RobocraftX.GUI.CommandLine; +using RobocraftX.Multiplayer; +using Svelto.ECS; +using Svelto.Context; + +namespace ExtraCommands +{ + [HarmonyPatch] + class CommandLineCompositionRootUnregisterCommandPatch + { + static void Prefix() + { + MethodInfo commandRemoveHelp = Harmony.AccessTools.Method(Harmony.AccessTools.TypeByName("RobocraftX.GUI.CommandLine.CommandLineUtility"), "SaveCommandHelp", new Type[] { typeof(string) }); + foreach (Type t in typeof(CustomCommandEngine).Assembly.GetTypes()) + { + CustomCommandAttribute[] attributes = (CustomCommandAttribute[])t.GetCustomAttributes(typeof(CustomCommandAttribute), false); + foreach (CustomCommandAttribute attr in attributes) + { + if (attr != null && t.IsSubclassOf(typeof(CustomCommandEngine))) + { + // remove Gamecraft help command + commandRemoveHelp.Invoke(null, new string[] { attr.Name }); + } + } + } + } + + [HarmonyTargetMethod] + static MethodBase HTargetMethod(HarmonyInstance instance) + { + return _OnContextDestroyedMethodInfo((new CommandLineCompositionRoot()).OnContextDestroyed); + } + + private static MethodInfo _OnContextDestroyedMethodInfo(Action a) + { + return a.Method; + } + } +} diff --git a/extracommands/ExitCommandEngine.cs b/extracommands/ExitCommandEngine.cs new file mode 100644 index 0000000..5647e91 --- /dev/null +++ b/extracommands/ExitCommandEngine.cs @@ -0,0 +1,37 @@ +using System; +using RobocraftX.GUI.CommandLine; +using RobocraftX.Multiplayer; +using RobocraftX.StateSync; +using RobocraftX.Character; +using Svelto.ECS; +using Unity.Entities; +using UnityEngine; +using uREPL; +using Svelto.Context; +using RobocraftX; + +namespace ExtraCommands.Basics +{ + [CustomCommand("Exit", "Forcefully close Gamecraft")] + class ExitCommandEngine : CustomCommandEngine + { + public ExitCommandEngine(UnityContext ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) + { + } + + public override void Ready() + { + uREPL.RuntimeCommands.Register("Exit", ExitCommand, "Forcefully close Gamecraft"); + } + + private void ExitCommand() + { + Application.Quit(); + } + + public override void Dispose() + { + uREPL.RuntimeCommands.Unregister("Exit"); + } + } +} diff --git a/extracommands/SetFOVCommandEngine.cs b/extracommands/SetFOVCommandEngine.cs new file mode 100644 index 0000000..f72cb0e --- /dev/null +++ b/extracommands/SetFOVCommandEngine.cs @@ -0,0 +1,37 @@ +using System; +using RobocraftX.GUI.CommandLine; +using RobocraftX.Multiplayer; +using RobocraftX.StateSync; +using RobocraftX.Character; +using Svelto.ECS; +using Unity.Entities; +using UnityEngine; +using uREPL; +using Svelto.Context; +using RobocraftX; + +namespace ExtraCommands.Basics +{ + [CustomCommand("SetFieldOfView", "Set the camera's field of view")] + class SetFOVCommandEngine : CustomCommandEngine + { + public SetFOVCommandEngine(UnityContext ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) + { + } + + public override void Ready() + { + uREPL.RuntimeCommands.Register("SetFieldOfView", SetFieldOfViewCommand, "Set the camera's field of view"); + } + + private void SetFieldOfViewCommand(float newFoV) + { + Camera.main.fieldOfView = newFoV; + } + + public override void Dispose() + { + uREPL.RuntimeCommands.Unregister("SetFieldOfView"); + } + } +} diff --git a/extracommands/SetTargetFramerateCommandEngine.cs b/extracommands/SetTargetFramerateCommandEngine.cs new file mode 100644 index 0000000..f05e98c --- /dev/null +++ b/extracommands/SetTargetFramerateCommandEngine.cs @@ -0,0 +1,37 @@ +using System; +using RobocraftX.GUI.CommandLine; +using RobocraftX.Multiplayer; +using RobocraftX.StateSync; +using RobocraftX.Character; +using Svelto.ECS; +using Unity.Entities; +using UnityEngine; +using uREPL; +using Svelto.Context; +using RobocraftX; + +namespace ExtraCommands.Basics +{ + [CustomCommand("SetTargetFPS", "Set Gamecraft's target FPS")] + class SetTargetFramerateCommandEngine : CustomCommandEngine + { + public SetFOVCommandEngine(UnityContext ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) + { + } + + public override void Ready() + { + uREPL.RuntimeCommands.Register("SetTargetFPS", SetFramerateCommand, "Set Gamecraft's target FPS"); + } + + private void SetFramerateCommand(int newFoV) + { + Application.targetFrameRate = newFoV; + } + + public override void Dispose() + { + uREPL.RuntimeCommands.Unregister("SetTargetFPS"); + } + } +} diff --git a/extracommands/CreateWaypoint.cs b/extracommands/TeleportWaypointCommandEngine.cs similarity index 93% rename from extracommands/CreateWaypoint.cs rename to extracommands/TeleportWaypointCommandEngine.cs index 101f718..b55763a 100644 --- a/extracommands/CreateWaypoint.cs +++ b/extracommands/TeleportWaypointCommandEngine.cs @@ -23,8 +23,8 @@ namespace ExtraCommands.Waypoints public override void Ready() { - uREPL.RuntimeCommands.Register("CreateWaypoint", CreateWaypointCommand); - uREPL.RuntimeCommands.Register("TeleportPlayerWaypoint", TeleportToWaypointCommand); + uREPL.RuntimeCommands.Register("CreateWaypoint", CreateWaypointCommand, "Create a waypoint in your current location"); + uREPL.RuntimeCommands.Register("TeleportPlayerWaypoint", TeleportToWaypointCommand, "Teleport to a waypoint"); } private void CreateWaypointCommand(object name) diff --git a/extracommands/WaitCommandEngine.cs b/extracommands/WaitCommandEngine.cs new file mode 100644 index 0000000..328f56b --- /dev/null +++ b/extracommands/WaitCommandEngine.cs @@ -0,0 +1,37 @@ +using System; +using RobocraftX.GUI.CommandLine; +using RobocraftX.Multiplayer; +using RobocraftX.StateSync; +using RobocraftX.Character; +using Svelto.ECS; +using Unity.Entities; +using UnityEngine; +using uREPL; +using Svelto.Context; +using RobocraftX; + +namespace ExtraCommands.Basics +{ + [CustomCommand("Wait", "Delay execution for a length of time (ms)")] + class WaitCommandEngine : CustomCommandEngine + { + public WaitCommandEngine(UnityContext ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams) + { + } + + public override void Ready() + { + uREPL.RuntimeCommands.Register("Wait", WaitCommand, "Delay execution for a length of time (ms)"); + } + + private void WaitCommand(int ms) + { + System.Threading.Thread.Sleep(ms); + } + + public override void Dispose() + { + uREPL.RuntimeCommands.Unregister("Wait"); + } + } +}