From 047f0bb34456e564e781db1639ac29eb46628937 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sat, 14 Dec 2019 23:20:20 -0800 Subject: [PATCH] Add command support --- .../Commands/CommandEngineFactory.cs | 17 +++++ .../Commands/CommandManager.cs | 56 ++++++++++++++++ GamecraftModdingAPI/Commands/CommandPatch.cs | 9 ++- .../Commands/CommandRegistrationHelper.cs | 67 +++++++++++++++++++ .../Commands/ICustomCommandEngine.cs | 13 +++- .../Commands/SimpleCustomCommandEngine.cs | 56 ++++++++++++++++ .../Commands/SimpleCustomCommandEngine1.cs | 47 +++++++++++++ .../Commands/SimpleCustomCommandEngine2.cs | 47 +++++++++++++ .../Commands/SimpleCustomCommandEngine3.cs | 47 +++++++++++++ .../Events/EventEngineFactory.cs | 59 ++++++++++++++++ .../Events/{Manager.cs => EventManager.cs} | 5 +- .../Events/GameActivatedPatch.cs | 4 +- .../Events/GameReloadedPatch.cs | 2 +- .../Events/GameSwitchedToPatch.cs | 2 +- .../Events/MenuActivatedPatch.cs | 6 +- .../Events/MenuSwitchedToPatch.cs | 2 +- .../Events/SimpleEventEmitterEngine.cs | 2 +- .../Events/SimpleEventHandlerEngine.cs | 21 ++++-- GamecraftModdingAPI/Main.cs | 12 ++-- .../Tests/GamecraftModdingAPIPluginTest.cs | 24 +++++-- GamecraftModdingAPI/Utility/Logging.cs | 8 +-- ref2 | 1 - 22 files changed, 465 insertions(+), 42 deletions(-) create mode 100644 GamecraftModdingAPI/Commands/CommandEngineFactory.cs create mode 100644 GamecraftModdingAPI/Commands/CommandManager.cs create mode 100644 GamecraftModdingAPI/Commands/CommandRegistrationHelper.cs create mode 100644 GamecraftModdingAPI/Commands/SimpleCustomCommandEngine.cs create mode 100644 GamecraftModdingAPI/Commands/SimpleCustomCommandEngine1.cs create mode 100644 GamecraftModdingAPI/Commands/SimpleCustomCommandEngine2.cs create mode 100644 GamecraftModdingAPI/Commands/SimpleCustomCommandEngine3.cs create mode 100644 GamecraftModdingAPI/Events/EventEngineFactory.cs rename GamecraftModdingAPI/Events/{Manager.cs => EventManager.cs} (96%) delete mode 120000 ref2 diff --git a/GamecraftModdingAPI/Commands/CommandEngineFactory.cs b/GamecraftModdingAPI/Commands/CommandEngineFactory.cs new file mode 100644 index 0000000..ddcdcb8 --- /dev/null +++ b/GamecraftModdingAPI/Commands/CommandEngineFactory.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GamecraftModdingAPI.Commands +{ + /// + /// UNIMPLEMENTED! + /// Convenient factories for command engines + /// + public static class CommandEngineFactory + { + //TODO + } +} diff --git a/GamecraftModdingAPI/Commands/CommandManager.cs b/GamecraftModdingAPI/Commands/CommandManager.cs new file mode 100644 index 0000000..6ff3046 --- /dev/null +++ b/GamecraftModdingAPI/Commands/CommandManager.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Svelto.ECS; + +namespace GamecraftModdingAPI.Commands +{ + /// + /// Keeps track of custom commands + /// This is used to add, remove and get command engines + /// + public static class CommandManager + { + private static Dictionary _customCommands = new Dictionary(); + + public static void AddCommand(ICustomCommandEngine engine) + { + _customCommands[engine.Name] = engine; + } + + public static bool ExistsCommand(string name) + { + return _customCommands.ContainsKey(name); + } + + public static bool ExistsCommand(ICustomCommandEngine engine) + { + return ExistsCommand(engine.Name); + } + + public static ICustomCommandEngine GetCommand(string name) + { + return _customCommands[name]; + } + + public static Dictionary GetCommands() + { + return _customCommands; + } + + public static void RemoveCommand(string name) + { + _customCommands.Remove(name); + } + + public static void RegisterEngines(EnginesRoot enginesRoot) + { + foreach (var key in _customCommands.Keys) + { + enginesRoot.AddEngine(_customCommands[key]); + } + } + } +} diff --git a/GamecraftModdingAPI/Commands/CommandPatch.cs b/GamecraftModdingAPI/Commands/CommandPatch.cs index fb4f320..bc75df5 100644 --- a/GamecraftModdingAPI/Commands/CommandPatch.cs +++ b/GamecraftModdingAPI/Commands/CommandPatch.cs @@ -15,14 +15,17 @@ using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI.Commands { + /// + /// Patch of RobocraftX.GUI.CommandLine.CommandLineCompositionRoot.Compose() + /// [HarmonyPatch] public static class CommandPatch { - public static void Prefix(UnityContext contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters) + public static void Postfix(UnityContext contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters) { - Logging.Log("Command Line was loaded"); + Logging.MetaDebugLog("Command Line was loaded"); // When a game is loaded, register the command engines - // TODO + CommandManager.RegisterEngines(enginesRoot); } public static MethodBase TargetMethod(HarmonyInstance instance) diff --git a/GamecraftModdingAPI/Commands/CommandRegistrationHelper.cs b/GamecraftModdingAPI/Commands/CommandRegistrationHelper.cs new file mode 100644 index 0000000..7e48dfc --- /dev/null +++ b/GamecraftModdingAPI/Commands/CommandRegistrationHelper.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using uREPL; +using RobocraftX.CommandLine.Custom; + +namespace GamecraftModdingAPI.Commands +{ + /// + /// Convenient methods for registering commands to Gamecraft. + /// All methods register to the command line and console block by default. + /// + public static class CommandRegistrationHelper + { + public static void Register(string name, Action action, string desc, bool noConsole = false) + { + RuntimeCommands.Register(name, action, desc); + if (noConsole) { return; } + ConsoleCommands.Register(name, action, desc); + } + + public static void Register(string name, Action action, string desc, bool noConsole = false) + { + Register(name, action, desc, noConsole); + } + + public static void Register(string name, Action action, string desc, bool noConsole = false) + { + Register(name, action, desc, noConsole); + } + + public static void Register(string name, Action action, string desc, bool noConsole = false) + { + Register(name, action, desc, noConsole); + } + + public static void Register(string name, Action action, string desc, bool noConsole = false) + { + RuntimeCommands.Register(name, action, desc); + if (noConsole) { return; } + ConsoleCommands.Register(name, action, desc); + } + + public static void Register(string name, Action action, string desc, bool noConsole = false) + { + RuntimeCommands.Register(name, action, desc); + if (noConsole) { return; } + ConsoleCommands.Register(name, action, desc); + } + + public static void Register(string name, Action action, string desc, bool noConsole = false) + { + RuntimeCommands.Register(name, action, desc); + if (noConsole) { return; } + ConsoleCommands.Register(name, action, desc); + } + + public static void Unregister(string name, bool noConsole = false) + { + RuntimeCommands.Unregister(name); + if (noConsole) { return; } + ConsoleCommands.Unregister(name); + } + } +} diff --git a/GamecraftModdingAPI/Commands/ICustomCommandEngine.cs b/GamecraftModdingAPI/Commands/ICustomCommandEngine.cs index 19a7537..ce0ba4d 100644 --- a/GamecraftModdingAPI/Commands/ICustomCommandEngine.cs +++ b/GamecraftModdingAPI/Commands/ICustomCommandEngine.cs @@ -7,12 +7,19 @@ using Svelto.ECS; namespace GamecraftModdingAPI.Commands { - public interface ICustomCommandEngine : IEngine, IQueryingEntitiesEngine + /// + /// Engine interface to handle command operations + /// + public interface ICustomCommandEngine : IEngine, IQueryingEntitiesEngine, IDisposable { + /// + /// The name of the command + /// string Name { get; } + /// + /// The command's description, shown in command help messages + /// string Description { get; } - - void ExecuteCommand(); } } diff --git a/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine.cs b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine.cs new file mode 100644 index 0000000..7427f82 --- /dev/null +++ b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine.cs @@ -0,0 +1,56 @@ +using Svelto.ECS; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GamecraftModdingAPI.Commands +{ + /// + /// A simple implementation of ICustomCommandEngine sufficient for most commands. + /// This version is for commands which take 0 argument(s) + /// + class SimpleCustomCommandEngine : ICustomCommandEngine + { + /// + /// The name of the command + /// + public string Name { get; } + + /// + /// The command's description, shown in command help messages + /// + public string Description { get; } + + /// + /// The operation to execute when the command is called by the player + /// + private Action runCommand; + + public IEntitiesDB entitiesDB { set; private get; } + + public void Dispose() + { + CommandRegistrationHelper.Unregister(this.Name); + } + + public void Ready() + { + CommandRegistrationHelper.Register(this.Name, this.runCommand, this.Description); + } + + /// + /// Construct the engine + /// + /// The command's operation + /// The name of the command + /// The command's description, shown in command help messages + public SimpleCustomCommandEngine(Action command, string name, string description) + { + this.runCommand = command; + this.Name = name; + this.Description = description; + } + } +} diff --git a/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine1.cs b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine1.cs new file mode 100644 index 0000000..268a808 --- /dev/null +++ b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine1.cs @@ -0,0 +1,47 @@ +using Svelto.ECS; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GamecraftModdingAPI.Commands +{ + /// + /// A simple implementation of ICustomCommandEngine sufficient for most commands. + /// This version is for commands which take 1 argument(s) + /// + class SimpleCustomCommandEngine : ICustomCommandEngine + { + public string Name { get; } + + public string Description { get; } + + private Action runCommand; + + public IEntitiesDB entitiesDB { set; private get; } + + public void Dispose() + { + CommandRegistrationHelper.Unregister(this.Name); + } + + public void Ready() + { + CommandRegistrationHelper.Register(this.Name, this.runCommand, this.Description); + } + + /// + /// Construct the engine + /// + /// The command's operation + /// The name of the command + /// The command's description, shown in command help messages + public SimpleCustomCommandEngine(Action command, string name, string description) + { + this.runCommand = command; + this.Name = name; + this.Description = description; + } + } +} diff --git a/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine2.cs b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine2.cs new file mode 100644 index 0000000..ee872de --- /dev/null +++ b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine2.cs @@ -0,0 +1,47 @@ +using Svelto.ECS; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GamecraftModdingAPI.Commands +{ + /// + /// A simple implementation of ICustomCommandEngine sufficient for most commands. + /// This version is for commands which take 2 argument(s) + /// + class SimpleCustomCommandEngine : ICustomCommandEngine + { + public string Name { get; } + + public string Description { get; } + + private Action runCommand; + + public IEntitiesDB entitiesDB { set; private get; } + + public void Dispose() + { + CommandRegistrationHelper.Unregister(this.Name); + } + + public void Ready() + { + CommandRegistrationHelper.Register(this.Name, this.runCommand, this.Description); + } + + /// + /// Construct the engine + /// + /// The command's operation + /// The name of the command + /// The command's description, shown in command help messages + public SimpleCustomCommandEngine(Action command, string name, string description) + { + this.runCommand = command; + this.Name = name; + this.Description = description; + } + } +} diff --git a/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine3.cs b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine3.cs new file mode 100644 index 0000000..b5c24b1 --- /dev/null +++ b/GamecraftModdingAPI/Commands/SimpleCustomCommandEngine3.cs @@ -0,0 +1,47 @@ +using Svelto.ECS; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GamecraftModdingAPI.Commands +{ + /// + /// A simple implementation of ICustomCommandEngine sufficient for most commands. + /// This version is for commands which take 3 argument(s) + /// + class SimpleCustomCommandEngine : ICustomCommandEngine + { + public string Name { get; } + + public string Description { get; } + + private Action runCommand; + + public IEntitiesDB entitiesDB { set; private get; } + + public void Dispose() + { + CommandRegistrationHelper.Unregister(this.Name); + } + + public void Ready() + { + CommandRegistrationHelper.Register(this.Name, this.runCommand, this.Description); + } + + /// + /// Construct the engine + /// + /// The command's operation + /// The name of the command + /// The command's description, shown in command help messages + public SimpleCustomCommandEngine(Action command, string name, string description) + { + this.runCommand = command; + this.Name = name; + this.Description = description; + } + } +} diff --git a/GamecraftModdingAPI/Events/EventEngineFactory.cs b/GamecraftModdingAPI/Events/EventEngineFactory.cs new file mode 100644 index 0000000..4a9edf7 --- /dev/null +++ b/GamecraftModdingAPI/Events/EventEngineFactory.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Svelto.ECS; + +namespace GamecraftModdingAPI.Events +{ + /// + /// Convenient factories for mod event engines + /// + public static class EventEngineFactory + { + /// + /// Factory method which automatically adds the SimpleEventHandlerEngine to the Manager + /// + /// The name of the engine + /// The type of event to handle + /// The operation to do when the event is created + /// The operation to do when the event is destroyed (if applicable) + /// The created object + public static SimpleEventHandlerEngine CreateAddSimpleHandler(string name, object type, Action onActivated, Action onDestroyed) + { + var engine = new SimpleEventHandlerEngine(onActivated, onDestroyed, type, name); + EventManager.AddEventHandler(engine); + return engine; + } + + /// + /// Factory method which automatically adds the SimpleEventHandlerEngine to the Manager + /// + /// The name of the engine + /// The type of event to handle + /// The operation to do when the event is created + /// The operation to do when the event is destroyed (if applicable) + /// The created object + public static SimpleEventHandlerEngine CreateAddSimpleHandler(string name, object type, Action onActivated, Action onDestroyed) + { + var engine = new SimpleEventHandlerEngine(onActivated, onDestroyed, type, name); + EventManager.AddEventHandler(engine); + return engine; + } + + /// + /// Factory method which automatically adds the SimpleEventEmitterEngine to the Manager + /// + /// The name of the engine + /// The type of event to emit + /// Will removing this engine not break your code? + /// The created object + public static SimpleEventEmitterEngine CreateAddSimpleEmitter(string name, object type, bool isRemovable = true) + { + var engine = new SimpleEventEmitterEngine(type, name, isRemovable); + EventManager.AddEventEmitter(engine); + return engine; + } + } +} diff --git a/GamecraftModdingAPI/Events/Manager.cs b/GamecraftModdingAPI/Events/EventManager.cs similarity index 96% rename from GamecraftModdingAPI/Events/Manager.cs rename to GamecraftModdingAPI/Events/EventManager.cs index f878148..bcb4e3c 100644 --- a/GamecraftModdingAPI/Events/Manager.cs +++ b/GamecraftModdingAPI/Events/EventManager.cs @@ -9,9 +9,9 @@ namespace GamecraftModdingAPI.Events { /// /// Keeps track of event handlers and emitters. - /// This class can be used to add, remove and get event handlers and emitters. + /// This is used to add, remove and get API event handlers and emitters. /// - public static class Manager + public static class EventManager { private static Dictionary _eventEmitters = new Dictionary(); @@ -98,6 +98,5 @@ namespace GamecraftModdingAPI.Events enginesRoot.AddEngine(_eventEmitters[key]); } } - } } diff --git a/GamecraftModdingAPI/Events/GameActivatedPatch.cs b/GamecraftModdingAPI/Events/GameActivatedPatch.cs index 3445d86..784e666 100644 --- a/GamecraftModdingAPI/Events/GameActivatedPatch.cs +++ b/GamecraftModdingAPI/Events/GameActivatedPatch.cs @@ -21,9 +21,9 @@ namespace GamecraftModdingAPI.Events { // A new EnginesRoot is always created when ActivateGame is called // so all event emitters and handlers must be re-registered. - Manager.RegisterEngines(____mainGameEnginesRoot); + EventManager.RegisterEngines(____mainGameEnginesRoot); Logging.Log("Dispatching Game Activated event"); - Manager.GetEventEmitter("GamecraftModdingAPIGameActivatedEventEmitter").Emit(); + EventManager.GetEventEmitter("GamecraftModdingAPIGameActivatedEventEmitter").Emit(); } } } diff --git a/GamecraftModdingAPI/Events/GameReloadedPatch.cs b/GamecraftModdingAPI/Events/GameReloadedPatch.cs index c331989..d1367bd 100644 --- a/GamecraftModdingAPI/Events/GameReloadedPatch.cs +++ b/GamecraftModdingAPI/Events/GameReloadedPatch.cs @@ -21,7 +21,7 @@ namespace GamecraftModdingAPI.Events { // Event emitters and handlers should already be registered by GameActivatedPatch Logging.Log("Dispatching Game Reloaded event"); - Manager.GetEventEmitter("GamecraftModdingAPIGameReloadedEventEmitter").Emit(); + EventManager.GetEventEmitter("GamecraftModdingAPIGameReloadedEventEmitter").Emit(); } } } diff --git a/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs b/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs index 81853da..6b192e8 100644 --- a/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs +++ b/GamecraftModdingAPI/Events/GameSwitchedToPatch.cs @@ -21,7 +21,7 @@ namespace GamecraftModdingAPI.Events { // Event emitters and handlers should already be registered by GameActivated event Logging.Log("Dispatching Game Switched To event"); - Manager.GetEventEmitter("GamecraftModdingAPIGameSwitchedToEventEmitter").Emit(); + EventManager.GetEventEmitter("GamecraftModdingAPIGameSwitchedToEventEmitter").Emit(); } } } diff --git a/GamecraftModdingAPI/Events/MenuActivatedPatch.cs b/GamecraftModdingAPI/Events/MenuActivatedPatch.cs index 1111fa8..a8aeb3d 100644 --- a/GamecraftModdingAPI/Events/MenuActivatedPatch.cs +++ b/GamecraftModdingAPI/Events/MenuActivatedPatch.cs @@ -23,15 +23,15 @@ namespace GamecraftModdingAPI.Events { // A new EnginesRoot is always created when ActivateMenu is called // so all event emitters and handlers must be re-registered. - Manager.RegisterEngines(____frontEndEnginesRoot); + EventManager.RegisterEngines(____frontEndEnginesRoot); if (firstLoad) { firstLoad = false; Logging.Log("Dispatching App Init event"); - Manager.GetEventEmitter("GamecraftModdingAPIApplicationInitializedEventEmitter").Emit(); + EventManager.GetEventEmitter("GamecraftModdingAPIApplicationInitializedEventEmitter").Emit(); } Logging.Log("Dispatching Menu Activated event"); - Manager.GetEventEmitter("GamecraftModdingAPIMenuActivatedEventEmitter").Emit(); + EventManager.GetEventEmitter("GamecraftModdingAPIMenuActivatedEventEmitter").Emit(); } } } diff --git a/GamecraftModdingAPI/Events/MenuSwitchedToPatch.cs b/GamecraftModdingAPI/Events/MenuSwitchedToPatch.cs index 806d8d5..7f19b32 100644 --- a/GamecraftModdingAPI/Events/MenuSwitchedToPatch.cs +++ b/GamecraftModdingAPI/Events/MenuSwitchedToPatch.cs @@ -21,7 +21,7 @@ namespace GamecraftModdingAPI.Events { // Event emitters and handlers should already be registered by MenuActivated event Logging.Log("Dispatching Menu Switched To event"); - Manager.GetEventEmitter("GamecraftModdingAPIMenuSwitchedToEventEmitter").Emit(); + EventManager.GetEventEmitter("GamecraftModdingAPIMenuSwitchedToEventEmitter").Emit(); } } } diff --git a/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs b/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs index b345b57..152a555 100644 --- a/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs +++ b/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs @@ -11,7 +11,7 @@ namespace GamecraftModdingAPI.Events /// /// A simple implementation of IEventEmitterEngine sufficient for most uses /// - class SimpleEventEmitterEngine : IEventEmitterEngine + public class SimpleEventEmitterEngine : IEventEmitterEngine { public string Name { get; set; } public object type { get; set; } diff --git a/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs b/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs index 2f53fa4..bc9b45a 100644 --- a/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs +++ b/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs @@ -10,7 +10,7 @@ namespace GamecraftModdingAPI.Events /// /// A simple implementation of IEventHandlerEngine sufficient for most uses /// - class SimpleEventHandlerEngine : IEventHandlerEngine + public class SimpleEventHandlerEngine : IEventHandlerEngine { public object type { get; set; } public string Name { get; set; } @@ -53,15 +53,22 @@ namespace GamecraftModdingAPI.Events } /// - /// + /// Construct the engine /// - /// - /// - /// - /// + /// The operation to do when the event is created + /// The operation to do when the event is destroyed (if applicable) + /// The type of event to handle + /// The name of the engine public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name) : this((IEntitiesDB _) => { activated.Invoke(); }, (IEntitiesDB _) => { removed.Invoke(); }, type, name) { } - + + /// + /// Construct the engine + /// + /// The operation to do when the event is created + /// The operation to do when the event is destroyed (if applicable) + /// The type of event to handler + /// The name of the engine public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name) { this.type = type; diff --git a/GamecraftModdingAPI/Main.cs b/GamecraftModdingAPI/Main.cs index a2330e3..dded179 100644 --- a/GamecraftModdingAPI/Main.cs +++ b/GamecraftModdingAPI/Main.cs @@ -22,12 +22,12 @@ namespace GamecraftModdingAPI harmony.PatchAll(currentAssembly); } // create default event emitters - Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false)); - Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false)); - Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false)); // TODO - Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false)); - Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false)); // TODO - Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false)); // TODO + EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false)); + EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false)); + EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false)); // TODO + EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false)); + EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false)); // TODO + EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false)); // TODO Logging.Log($"{currentAssembly.GetName().Name} {currentAssembly.GetName().Version} init & patch complete"); } diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 33415a2..84520f4 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -1,12 +1,18 @@ using System; using System.Reflection; using Harmony; + +using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Events; using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI.Tests { // unused by design + /// + /// Modding API implemented as a standalone IPA Plugin. + /// Ideally, GamecraftModdingAPI should be loaded by another mod; not itself + /// public class GamecraftModdingAPIPluginTest #if DEBUG : IllusionPlugin.IEnhancedPlugin @@ -37,21 +43,27 @@ namespace GamecraftModdingAPI.Tests //MinimumSpecsCheckPatch.ForcePassMinimumSpecCheck = true; // debug/test handlers - Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("App Inited event!"); }, () => { }, + EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("App Inited event!"); }, () => { }, EventType.ApplicationInitialized, "appinit API debug")); - Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Activated event!"); }, + EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Activated event!"); }, () => { Logging.Log("Menu Destroyed event!"); }, EventType.Menu, "menuact API debug")); - Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Switched To event!"); }, () => { }, + EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Switched To event!"); }, () => { }, EventType.MenuSwitchedTo, "menuswitch API debug")); - Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Activated event!"); }, + EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Activated event!"); }, () => { Logging.Log("Game Destroyed event!"); }, EventType.Game, "gameact API debug")); - Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Reloaded event!"); }, () => { }, + EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Reloaded event!"); }, () => { }, EventType.GameReloaded, "gamerel API debug")); - Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Switched To event!"); }, () => { }, + EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Switched To event!"); }, () => { }, EventType.GameSwitchedTo, "gameswitch API debug")); + // debug/test commands + CommandManager.AddCommand(new SimpleCustomCommandEngine(() => { UnityEngine.Application.Quit(); }, + "Exit", "Close Gamecraft without any prompts")); + CommandManager.AddCommand(new SimpleCustomCommandEngine((float d) => { UnityEngine.Camera.main.fieldOfView = d; }, + "SetFOV", "Set the player camera's field of view")); + } public void OnFixedUpdate() { } diff --git a/GamecraftModdingAPI/Utility/Logging.cs b/GamecraftModdingAPI/Utility/Logging.cs index ff5f1da..7dae150 100644 --- a/GamecraftModdingAPI/Utility/Logging.cs +++ b/GamecraftModdingAPI/Utility/Logging.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Reflection; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -158,14 +158,14 @@ namespace GamecraftModdingAPI.Utility } /// - /// Write a descriptive message to Gamecraft's log including the current time and the DLL's name + /// Write a descriptive message to Gamecraft's log including the current time and the calling method's name /// /// The object to log [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void MetaLog(object obj) { - var callAsm = Assembly.GetCallingAssembly(); - Log($"[{DateTime.Now.ToString()}][{callAsm.GetName().Name}]{obj.ToString()}"); + var method = (new StackTrace()).GetFrame(1).GetMethod(); + Log($"[{DateTime.Now.ToString()}][{method.DeclaringType.Name}.{method.Name}]{obj.ToString()}"); } } } diff --git a/ref2 b/ref2 deleted file mode 120000 index 72a1bb1..0000000 --- a/ref2 +++ /dev/null @@ -1 +0,0 @@ -Z:/ \ No newline at end of file