Implement missing events

This commit is contained in:
NGnius (Graham) 2019-12-14 10:52:24 -08:00
parent 864efca755
commit fae054b72b
23 changed files with 353 additions and 79 deletions

View file

@ -16,7 +16,7 @@ using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Commands
{
[HarmonyPatch]
class CommandPatch
public static class CommandPatch
{
public static void Prefix(UnityContext<FullGameCompositionRoot> contextHolder, EnginesRoot enginesRoot, World physicsWorld, Action reloadGame, MultiplayerInitParameters multiplayerParameters)
{

View file

@ -7,7 +7,7 @@ using Svelto.ECS;
namespace GamecraftModdingAPI.Commands
{
interface ICustomCommandEngine : IEngine, IQueryingEntitiesEngine
public interface ICustomCommandEngine : IEngine, IQueryingEntitiesEngine
{
string Name { get; }

View file

@ -6,14 +6,16 @@ using System.Threading.Tasks;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Built-in event types.
/// These are configured to fire when the API is initialized.
/// </summary>
public enum EventType
{
ApplicationInitialized,
MenuActivated,
MenuDestroyed,
Menu,
MenuSwitchedTo,
GameActivated,
GameDestroyed,
Game,
GameReloaded,
GameSwitchedTo
}

View file

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Harmony;
using RobocraftX;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Patch of RobocraftX.FullGameCompositionRoot.ActivateGame()
/// </summary>
[HarmonyPatch(typeof(FullGameCompositionRoot), "ActivateGame")]
class GameActivatedPatch
{
public static void Postfix(ref EnginesRoot ____mainGameEnginesRoot)
{
// A new EnginesRoot is always created when ActivateGame is called
// so all event emitters and handlers must be re-registered.
Manager.RegisterEngines(____mainGameEnginesRoot);
Logging.Log("Dispatching Game Activated event");
Manager.GetEventEmitter("GamecraftModdingAPIGameActivatedEventEmitter").Emit();
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Harmony;
using RobocraftX;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Patch of RobocraftX.FullGameCompositionRoot.ReloadGame()
/// </summary>
[HarmonyPatch(typeof(FullGameCompositionRoot), "ReloadGame")]
class GameReloadedPatch
{
public static void Postfix()
{
// Event emitters and handlers should already be registered by GameActivatedPatch
Logging.Log("Dispatching Game Reloaded event");
Manager.GetEventEmitter("GamecraftModdingAPIGameReloadedEventEmitter").Emit();
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Harmony;
using RobocraftX;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Patch of RobocraftX.FullGameCompositionRoot.SwitchToGame()
/// </summary>
[HarmonyPatch(typeof(FullGameCompositionRoot), "SwitchToGame")]
class GameSwitchedToPatch
{
public static void Postfix()
{
// Event emitters and handlers should already be registered by GameActivated event
Logging.Log("Dispatching Game Switched To event");
Manager.GetEventEmitter("GamecraftModdingAPIGameSwitchedToEventEmitter").Emit();
}
}
}

View file

@ -7,13 +7,36 @@ using Svelto.ECS;
namespace GamecraftModdingAPI.Events
{
public interface IEventEmitterEngine : IEngine, IQueryingEntitiesEngine
/// <summary>
/// Engine interface to create a ModEventEntityStruct in entitiesDB when Emit() is called.
/// </summary>
public interface IEventEmitterEngine : IEngine, IQueryingEntitiesEngine, IDisposable
{
/// <summary>
/// The name of the IEventEmitterEngine
/// </summary>
string Name { get; }
/// <summary>
/// The type of event emitted
/// </summary>
object type { get; }
/// <summary>
/// Whether the emitter can be removed with Manager.RemoveEventEmitter(name)
/// </summary>
bool isRemovable { get; }
/// <summary>
/// The EntityFactory for the entitiesDB.
/// Use this to create a ModEventEntityStruct when Emit() is called.
/// </summary>
IEntityFactory Factory { set; }
/// <summary>
/// Emit the event so IEventHandlerEngines can handle it.
/// Call Emit() to trigger the IEventEmitterEngine's event.
/// </summary>
void Emit();
}
}

View file

@ -8,8 +8,14 @@ using Svelto.ECS.Internal;
namespace GamecraftModdingAPI.Events
{
public interface IEventHandlerEngine : IEngine, IQueryingEntitiesEngine, IReactOnAddAndRemove<ModEventEntityStruct>, IReactOnAddAndRemove
/// <summary>
/// Engine interface to handle ModEventEntityStruct events emitted by IEventEmitterEngines.
/// </summary>
public interface IEventHandlerEngine : IEngine, IQueryingEntitiesEngine, IReactOnAddAndRemove<ModEventEntityStruct>, IReactOnAddAndRemove, IDisposable
{
/// <summary>
/// The name of the IEventHandlerEngine
/// </summary>
string Name { get; }
}
}

View file

@ -78,7 +78,10 @@ namespace GamecraftModdingAPI.Events
public static void RemoveEventEmitter(string name)
{
_eventEmitters.Remove(name);
if (_eventEmitters[name].isRemovable)
{
_eventEmitters.Remove(name);
}
}
public static void RegisterEngines(EnginesRoot enginesRoot)

View file

@ -11,8 +11,11 @@ using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Patch of RobocraftX.FullGameCompositionRoot.ActivateMenu()
/// </summary>
[HarmonyPatch(typeof(FullGameCompositionRoot), "ActivateMenu")]
class GameInitPatch
class MenuActivatedPatch
{
private static bool firstLoad = true;

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Harmony;
using RobocraftX;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Patch of RobocraftX.FullGameCompositionRoot.SwitchToMenu()
/// </summary>
[HarmonyPatch(typeof(FullGameCompositionRoot), "SwitchToMenu")]
class MenuSwitchedToPatch
{
public static void Postfix()
{
// Event emitters and handlers should already be registered by MenuActivated event
Logging.Log("Dispatching Menu Switched To event");
Manager.GetEventEmitter("GamecraftModdingAPIMenuSwitchedToEventEmitter").Emit();
}
}
}

View file

@ -7,6 +7,9 @@ using Svelto.ECS;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// EntityDescriptor for creating ModEventEntityStructs
/// </summary>
public class ModEventEntityDescriptor : GenericEntityDescriptor<ModEventEntityStruct>
{
}

View file

@ -8,10 +8,14 @@ using Svelto.ECS.Hybrid;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// The event entity struct
/// </summary>
public struct ModEventEntityStruct : IEntityStruct
{
/// <summary>
/// The type of event that has been emitted
/// </summary>
public object type;
public EGID ID { get; set; }
}
}

View file

@ -8,36 +8,57 @@ using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// A simple implementation of IEventEmitterEngine sufficient for most uses
/// </summary>
class SimpleEventEmitterEngine : IEventEmitterEngine
{
public string Name { get; set; }
public object type { get; set; }
public bool isRemovable { get; }
public IEntityFactory Factory { private get; set; }
public IEntitiesDB entitiesDB { set; private get; }
public void Ready() { }
/// <summary>
/// Emit the event
/// </summary>
public void Emit()
{
Factory.BuildEntity<ModEventEntityDescriptor>(ApiExclusiveGroups.eventID++, ApiExclusiveGroups.eventsExclusiveGroup)
.Init(new ModEventEntityStruct
{
type = type
});
.Init(new ModEventEntityStruct { type = type });
}
public SimpleEventEmitterEngine(EventType type, string name)
public void Dispose() { }
/// <summary>
/// Construct the engine
/// </summary>
/// <param name="type">The EventType to use for ModEventEntityStruct.type</param>
/// <param name="name">The name of this engine</param>
/// <param name="isRemovable">Will removing this engine not break your code?</param>
public SimpleEventEmitterEngine(EventType type, string name, bool isRemovable = true)
{
this.type = type;
this.Name = name;
this.isRemovable = isRemovable;
}
public SimpleEventEmitterEngine(object type, string name)
/// <summary>
/// Construct the engine
/// </summary>
/// <param name="type">The object to use for ModEventEntityStruct.type</param>
/// <param name="name">The name of this engine</param>
/// <param name="isRemovable">Will removing this engine not break your code?</param>
public SimpleEventEmitterEngine(object type, string name, bool isRemovable = true)
{
this.type = type;
this.Name = name;
this.isRemovable = isRemovable;
}
}
}

View file

@ -7,12 +7,19 @@ using System.Threading.Tasks;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// A simple implementation of IEventHandlerEngine sufficient for most uses
/// </summary>
class SimpleEventHandlerEngine : IEventHandlerEngine
{
public object type { get; set; }
public string Name { get; set; }
private readonly Action<IEntitiesDB> onEvent;
private bool isActivated = false;
private readonly Action<IEntitiesDB> onActivated;
private readonly Action<IEntitiesDB> onDestroyed;
public IEntitiesDB entitiesDB { set; private get; }
@ -20,21 +27,47 @@ namespace GamecraftModdingAPI.Events
{
if (entityView.type.Equals(this.type))
{
onEvent.Invoke(entitiesDB);
isActivated = true;
onActivated.Invoke(entitiesDB);
}
}
public void Ready() { }
public void Remove(ref ModEventEntityStruct entityView, EGID egid) { }
public void Remove(ref ModEventEntityStruct entityView, EGID egid)
{
if (entityView.type.Equals(this.type) && isActivated)
{
isActivated = false;
onDestroyed.Invoke(entitiesDB);
}
}
public SimpleEventHandlerEngine(Action handleEvent, object type, string name) : this((IEntitiesDB db) => { handleEvent.Invoke(); }, type, name) { }
public void Dispose()
{
if (isActivated)
{
isActivated = false;
onDestroyed.Invoke(entitiesDB);
}
}
/// <summary>
///
/// </summary>
/// <param name="activated"></param>
/// <param name="removed"></param>
/// <param name="type"></param>
/// <param name="name"></param>
public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name)
: this((IEntitiesDB _) => { activated.Invoke(); }, (IEntitiesDB _) => { removed.Invoke(); }, type, name) { }
public SimpleEventHandlerEngine(Action<IEntitiesDB> handleEvent, object type, string name)
public SimpleEventHandlerEngine(Action<IEntitiesDB> activated, Action<IEntitiesDB> removed, object type, string name)
{
this.type = type;
this.Name = name;
this.onEvent = handleEvent;
this.onActivated = activated;
this.onDestroyed = removed;
}
}
}

View file

@ -537,17 +537,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Folder Include="Properties\" />
</ItemGroup>
<!--End Dependencies-->

View file

@ -21,12 +21,14 @@ namespace GamecraftModdingAPI
harmony = HarmonyInstance.Create(currentAssembly.GetName().Name);
harmony.PatchAll(currentAssembly);
}
// create default event objects
Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("App Inited event!"); },
EventType.ApplicationInitialized, "appinit API debug"));
Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter"));
Manager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuActivated, "GamecraftModdingAPIMenuActivatedEventEmitter"));
Logging.Log($"{currentAssembly.GetName().Name} {currentAssembly.GetName().Version} start & patch complete");
// 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
Logging.Log($"{currentAssembly.GetName().Name} {currentAssembly.GetName().Version} init & patch complete");
}
public static void Shutdown()

View file

@ -1,26 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace GamecraftModdingAPI.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.4.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
}
}

View file

@ -1,6 +0,0 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
</SettingsFile>

View file

@ -1,20 +1,24 @@
using System;
using UnityEngine;
using Harmony;
using System.Reflection;
using Harmony;
using GamecraftModdingAPI.Events;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Tests
{
// unused by design
public class GamecraftModdingAPIPluginTest //: IllusionPlugin.IEnhancedPlugin
public class GamecraftModdingAPIPluginTest
#if DEBUG
: IllusionPlugin.IEnhancedPlugin
#endif
{
public static HarmonyInstance harmony { get; protected set; }
public string[] Filter { get; } = new string[] { "Gamecraft" };
public string Name { get; } = "Gamecraft Modding API";
public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
public string Version { get; } = "v0.1.0.A";
public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
public string HarmonyID { get; } = "org.git.exmods.modtainers.gamecraftmoddingapi";
@ -26,6 +30,28 @@ namespace GamecraftModdingAPI.Tests
public void OnApplicationStart()
{
GamecraftModdingAPI.Main.Init();
// in case Steam is not installed/running
// this will crash the game slightly later during startup
//SteamInitPatch.ForcePassSteamCheck = true;
// in case running in a VM
//MinimumSpecsCheckPatch.ForcePassMinimumSpecCheck = true;
// debug/test handlers
Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("App Inited event!"); }, () => { },
EventType.ApplicationInitialized, "appinit API debug"));
Manager.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!"); }, () => { },
EventType.MenuSwitchedTo, "menuswitch API debug"));
Manager.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!"); }, () => { },
EventType.GameReloaded, "gamerel API debug"));
Manager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Switched To event!"); }, () => { },
EventType.GameSwitchedTo, "gameswitch API debug"));
}
public void OnFixedUpdate() { }

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -135,7 +136,7 @@ namespace GamecraftModdingAPI.Utility
}
/// <summary>
/// Write a message to stdout (usually the terminal which is running, like Command Prompt or PowerShell)
/// Write a message to stdout (ie the terminal, like Command Prompt or PowerShell)
/// </summary>
/// <param name="obj">The object to log</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -143,5 +144,28 @@ namespace GamecraftModdingAPI.Utility
{
Svelto.Console.SystemLog(obj.ToString());
}
/// <summary>
/// Write a descriptive message to Gamecraft's log only when the API is a Debug build
/// </summary>
/// <param name="obj">The object to log</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MetaDebugLog(object obj)
{
#if DEBUG
MetaLog($"[MetaDebug]{obj.ToString()}");
#endif
}
/// <summary>
/// Write a descriptive message to Gamecraft's log including the current time and the DLL's name
/// </summary>
/// <param name="obj">The object to log</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MetaLog(object obj)
{
var callAsm = Assembly.GetCallingAssembly();
Log($"[{DateTime.Now.ToString()}][{callAsm.GetName().Name}]{obj.ToString()}");
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Harmony;
using RobocraftX.FrontEnd;
namespace GamecraftModdingAPI.Utility
{
/// <summary>
/// Patch of bool RobocraftX.FrontEnd.MinimumSpecsCheck.CheckRequirementsMet()
/// </summary>
[HarmonyPatch(typeof(MinimumSpecsCheck), "CheckRequirementsMet")]
class MinimumSpecsCheckPatch
{
/// <summary>
/// Ignore result of the requirement check?
/// </summary>
public static bool ForcePassMinimumSpecCheck = false;
public static void Postfix(ref bool __result)
{
__result = __result || ForcePassMinimumSpecCheck;
}
}
}

View file

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Harmony;
using RobocraftX.Common;
namespace GamecraftModdingAPI.Utility
{
/// <summary>
/// Patch of bool RobocraftX.Common.SteamManager.VerifyOrInit()
/// This does not let you run Gamecraft without Steam.
/// DO NOT USE!
/// </summary>
[HarmonyPatch(typeof(SteamManager), "VerifyOrInit")]
class SteamInitPatch
{
/// <summary>
/// Ignore the result of steam initialization?
/// </summary>
public static bool ForcePassSteamCheck = false;
public static void Postfix(ref bool __result)
{
__result = __result || ForcePassSteamCheck;
}
}
}