From 06b8c3664e188ba1bde50e0361390e4356a8b037 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Wed, 13 May 2020 22:34:26 -0400 Subject: [PATCH] Add Event builders --- GamecraftModdingAPI/Events/EmitterBuilder.cs | 105 +++++++++++ GamecraftModdingAPI/Events/EventExceptions.cs | 11 ++ GamecraftModdingAPI/Events/HandlerBuilder.cs | 165 ++++++++++++++++++ .../Tests/GamecraftModdingAPIPluginTest.cs | 68 +++++--- 4 files changed, 328 insertions(+), 21 deletions(-) create mode 100644 GamecraftModdingAPI/Events/EmitterBuilder.cs create mode 100644 GamecraftModdingAPI/Events/HandlerBuilder.cs diff --git a/GamecraftModdingAPI/Events/EmitterBuilder.cs b/GamecraftModdingAPI/Events/EmitterBuilder.cs new file mode 100644 index 0000000..48a6cba --- /dev/null +++ b/GamecraftModdingAPI/Events/EmitterBuilder.cs @@ -0,0 +1,105 @@ +using System; + +using Svelto.ECS; + +namespace GamecraftModdingAPI.Events +{ + public class EmitterBuilder + { + private string name; + + private int? type; + + /// + /// Create a new event emitter builder. + /// + public EmitterBuilder() + { + } + + /// + /// Create a new event emitter builder. + /// This is equivalent to new EmitterBuilder().Name(name) + /// + /// The emitter name. + public EmitterBuilder(string name) + { + this.name = name; + } + + /// + /// Create and return an event emitter builder. + /// + /// The builder. + public static EmitterBuilder Builder() + { + return new EmitterBuilder(); + } + + /// + /// Create and return an event emitter builder. + /// This is equivalent to Builder().Name(name) + /// + /// The builder. + /// The emitter name. + public static EmitterBuilder Builder(string name) + { + return new EmitterBuilder(name); + } + + /// + /// Name the event emitter. + /// + /// The builder. + /// The event emitter name. + public EmitterBuilder Name(string name) + { + this.name = name; + return this; + } + + /// + /// Set the type of event to handle. + /// + /// The builder. + /// The event type. + public EmitterBuilder Handle(EventType eventType) + { + return Handle((int)eventType); + } + + /// + /// Set the type of event to handle. + /// + /// The builder. + /// The event type. + public EmitterBuilder Handle(int eventType) + { + this.type = eventType; + return this; + } + + /// + /// Build the event emitter. + /// + /// The event emitter. + /// Automatically register the event emitter with EventManager.AddEventemitter(). + public IEventEmitterEngine Build(bool register = true) + { + if (string.IsNullOrWhiteSpace(name)) + { + throw new EventParameterMissingException("Event emitter name must be defined before Build() is called"); + } + if (!type.HasValue) + { + throw new EventParameterMissingException("Event emitter event type must be defined before Build() is called"); + } + SimpleEventEmitterEngine result = new SimpleEventEmitterEngine(type.Value, name); + if (register) + { + EventManager.AddEventEmitter(result); + } + return result; + } + } +} diff --git a/GamecraftModdingAPI/Events/EventExceptions.cs b/GamecraftModdingAPI/Events/EventExceptions.cs index ee29480..b4458bc 100644 --- a/GamecraftModdingAPI/Events/EventExceptions.cs +++ b/GamecraftModdingAPI/Events/EventExceptions.cs @@ -52,4 +52,15 @@ namespace GamecraftModdingAPI.Events { } } + + public class EventParameterMissingException : EventException + { + public EventParameterMissingException() + { + } + + public EventParameterMissingException(string message) : base(message) + { + } + } } diff --git a/GamecraftModdingAPI/Events/HandlerBuilder.cs b/GamecraftModdingAPI/Events/HandlerBuilder.cs new file mode 100644 index 0000000..d5d9879 --- /dev/null +++ b/GamecraftModdingAPI/Events/HandlerBuilder.cs @@ -0,0 +1,165 @@ +using System; + +using Svelto.ECS; + +namespace GamecraftModdingAPI.Events +{ + public class HandlerBuilder + { + private string name; + + private int? type; + + private Action activated; + + private Action destroyed; + + /// + /// Create a new event handler builder. + /// + public HandlerBuilder() + { + } + + /// + /// Create a new event handler builder. + /// This is equivalent to new HandlerBuilder().Name(name) + /// + /// The handler name. + public HandlerBuilder(string name) + { + this.name = name; + } + + /// + /// Create and return an event handler builder. + /// + /// The builder. + public static HandlerBuilder Builder() + { + return new HandlerBuilder(); + } + + /// + /// Create and return an event handler builder. + /// This is equivalent to Builder().Name(name) + /// + /// The builder. + /// The handler name. + public static HandlerBuilder Builder(string name) + { + return new HandlerBuilder(name); + } + + /// + /// Name the event handler. + /// + /// The builder. + /// The event handler name. + public HandlerBuilder Name(string name) + { + this.name = name; + return this; + } + + /// + /// Set the action to perform on when the activated event occurs. + /// + /// The builder. + /// The activated event action. + public HandlerBuilder OnActivation(Action action) + { + return OnActivation((_) => { action(); }); + } + + /// + /// Set the action to perform on when the activated event occurs. + /// + /// The builder. + /// The activated event action. + public HandlerBuilder OnActivation(Action action) + { + this.activated = action; + return this; + } + + /// + /// Set the action to perform when the destroyed event occurs. + /// + /// The builder. + /// The destroyed event action. + public HandlerBuilder OnDestruction(Action action) + { + return OnDestruction((_) => { action(); }); + } + + /// + /// Set the action to perform when the destroyed event occurs. + /// + /// The builder. + /// The destroyed event action. + public HandlerBuilder OnDestruction(Action action) + { + this.destroyed = action; + return this; + } + + /// + /// Set the type of event to handle. + /// + /// The builder. + /// The event type. + public HandlerBuilder Handle(EventType eventType) + { + return Handle((int)eventType); + } + + /// + /// Set the type of event to handle. + /// + /// The builder. + /// The event type. + public HandlerBuilder Handle(int eventType) + { + this.type = eventType; + return this; + } + + /// + /// Build the event handler. + /// + /// The event handler. + /// Automatically register the event handler with EventManager.AddEventHandler(). + public IEventHandlerEngine Build(bool register = true) + { + if (string.IsNullOrWhiteSpace(name)) + { + throw new EventParameterMissingException("Event handler name must be defined before Build() is called"); + } + if (activated == null && destroyed == null) + { + throw new EventParameterMissingException("Event handler destruction or activated event action must be defined before Build() is called"); + } + if (!type.HasValue) + { + throw new EventParameterMissingException("Event handler event type must be defined before Build() is called"); + } + Action validActivated = activated; + if (validActivated == null) + { + validActivated = (_) => { }; + } + Action validDestroyed = destroyed; + if (validDestroyed == null) + { + validDestroyed = (_) => { }; + } + SimpleEventHandlerEngine result = new SimpleEventHandlerEngine(validActivated, validDestroyed, type.Value, name); + if (register) + { + EventManager.AddEventHandler(result); + } + return result; + } + } +} diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index 183b4f0..307b3f6 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -61,28 +61,54 @@ namespace GamecraftModdingAPI.Tests Utility.VersionTracking.Enable(); - // debug/test handlers - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("App Inited event!"); }, () => { }, - EventType.ApplicationInitialized, "appinit API debug")); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Activated event!"); }, - () => { Logging.Log("Menu Destroyed event!"); }, - EventType.Menu, "menuact API debug")); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Switched To event!"); }, () => { }, - EventType.MenuSwitchedTo, "menuswitch API debug")); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Activated event!"); }, - () => { Logging.Log("Game Destroyed event!"); }, - EventType.Game, "gameact API debug")); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Reloaded event!"); }, () => { }, - EventType.GameReloaded, "gamerel API debug")); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Switched To event!"); }, () => { }, - EventType.GameSwitchedTo, "gameswitch API debug")); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Mode Simulation Switched To event!"); }, () => { }, - EventType.SimulationSwitchedTo, "simulationswitch API debug")); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Mode Build Switched To event!"); }, () => { }, - EventType.BuildSwitchedTo, "buildswitch API debug")); + // debug/test handlers + HandlerBuilder.Builder() + .Name("appinit API debug") + .Handle(EventType.ApplicationInitialized) + .OnActivation(() => { Logging.Log("App Inited event!"); }) + .Build(); - EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { throw new Exception(""); }, () => {}, - EventType.Menu, "menu activated API error thrower test")); + HandlerBuilder.Builder("menuact API debug") + .Handle(EventType.Menu) + .OnActivation(() => { Logging.Log("Menu Activated event!"); }) + .OnDestruction(() => { Logging.Log("Menu Destroyed event!"); }) + .Build(); + + HandlerBuilder.Builder("menuswitch API debug") + .Handle(EventType.MenuSwitchedTo) + .OnActivation(() => { Logging.Log("Menu Switched To event!"); }) + .Build(); + + HandlerBuilder.Builder("gameact API debug") + .Handle(EventType.Menu) + .OnActivation(() => { Logging.Log("Game Activated event!"); }) + .OnDestruction(() => { Logging.Log("Game Destroyed event!"); }) + .Build(); + + HandlerBuilder.Builder("gamerel API debug") + .Handle(EventType.GameReloaded) + .OnActivation(() => { Logging.Log("Game Reloaded event!"); }) + .Build(); + + HandlerBuilder.Builder("gameswitch API debug") + .Handle(EventType.GameSwitchedTo) + .OnActivation(() => { Logging.Log("Game Switched To event!"); }) + .Build(); + + HandlerBuilder.Builder("simulationswitch API debug") + .Handle(EventType.SimulationSwitchedTo) + .OnActivation(() => { Logging.Log("Game Mode Simulation Switched To event!"); }) + .Build(); + + HandlerBuilder.Builder("buildswitch API debug") + .Handle(EventType.BuildSwitchedTo) + .OnActivation(() => { Logging.Log("Game Mode Build Switched To event!"); }) + .Build(); + + HandlerBuilder.Builder("menu activated API error thrower test") + .Handle(EventType.Menu) + .OnActivation(() => { throw new Exception("Event Handler always throws an exception!"); }) + .Build(); // debug/test commands if (Dependency.Hell("ExtraCommands"))