Add Simulation and Build switch to events

This commit is contained in:
NGnius 2020-02-25 22:19:22 -05:00
parent 7519bc37ae
commit 7ac5120ef5
6 changed files with 137 additions and 1 deletions

View file

@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Harmony;
using Svelto.ECS;
using RobocraftX.StateSync;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Patch of RobocraftX.StateSync.DeterministicStepCompositionRoot.ComposeEnginesGroups(...)
/// </summary>
[HarmonyPatch(typeof(DeterministicStepCompositionRoot), "ComposeEnginesGroups")]
//[HarmonyPatch]
class DeterministicStepComposeEngineGroupsPatch
{
public static readonly GameStateBuildEmitterEngine buildEngine = new GameStateBuildEmitterEngine();
public static readonly GameStateSimulationEmitterEngine simEngine = new GameStateSimulationEmitterEngine();
public static void Prefix(ref StateSyncRegistrationHelper stateSyncReg)
{
stateSyncReg.buildModeInitializationEngines.Add(buildEngine);
stateSyncReg.simulationModeInitializationEngines.Add(simEngine);
}
public static MethodBase NopeTargetMethod()
{
return typeof(DeterministicStepCompositionRoot).GetMethods().First(m => m.Name == "ComposeEnginesGroups")
.MakeGenericMethod(typeof(object));
}
}
}

View file

@ -17,6 +17,8 @@ namespace GamecraftModdingAPI.Events
MenuSwitchedTo, MenuSwitchedTo,
Game, Game,
GameReloaded, GameReloaded,
GameSwitchedTo GameSwitchedTo,
SimulationSwitchedTo,
BuildSwitchedTo
} }
} }

View file

@ -0,0 +1,44 @@
using System;
using Unity.Jobs;
using RobocraftX.StateSync;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Event emitter engine for switching to to build mode.
/// </summary>
public class GameStateBuildEmitterEngine : IEventEmitterEngine, IInitializeOnBuildStart
{
public string Name { get; } = "GamecraftModdingAPIGameStateBuildEventEmitter" ;
public IEntitiesDB entitiesDB { set; private get; }
public object type { get; } = EventType.BuildSwitchedTo;
public bool isRemovable { get; } = false;
public IEntityFactory Factory { set; private get; }
public void Dispose() { }
public void Emit()
{
Logging.Log("Dispatching Build Switched To event");
if (Factory == null) { return; }
Factory.BuildEntity<ModEventEntityDescriptor>(ApiExclusiveGroups.eventID++, ApiExclusiveGroups.eventsExclusiveGroup)
.Init(new ModEventEntityStruct { type = type });
}
public JobHandle OnInitializeBuildMode()
{
Emit();
return default(JobHandle);
}
public void Ready() { }
}
}

View file

@ -0,0 +1,44 @@
using System;
using Unity.Jobs;
using RobocraftX.StateSync;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Events
{
/// <summary>
/// Event emitter engine for switching to simulation mode.
/// </summary>
public class GameStateSimulationEmitterEngine : IEventEmitterEngine, IInitializeOnSimulationStart
{
public string Name { get; } = "GamecraftModdingAPIGameStateSimulationEventEmitter" ;
public IEntitiesDB entitiesDB { set; private get; }
public object type { get; } = EventType.SimulationSwitchedTo;
public bool isRemovable { get; } = false;
public IEntityFactory Factory { set; private get; }
public void Dispose() { }
public void Emit()
{
Logging.Log("Dispatching Simulation Switched To event");
if (Factory == null) { return; }
Factory.BuildEntity<ModEventEntityDescriptor>(ApiExclusiveGroups.eventID++, ApiExclusiveGroups.eventsExclusiveGroup)
.Init(new ModEventEntityStruct { type = type });
}
public JobHandle OnInitializeSimulationMode()
{
Emit();
return default(JobHandle);
}
public void Ready() { }
}
}

View file

@ -56,6 +56,8 @@ namespace GamecraftModdingAPI
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false)); EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false));
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false)); EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false));
EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false)); EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false));
EventManager.AddEventEmitter(DeterministicStepComposeEngineGroupsPatch.buildEngine);
EventManager.AddEventEmitter(DeterministicStepComposeEngineGroupsPatch.simEngine);
// init block implementors // init block implementors
Logging.MetaDebugLog($"Initializing Blocks"); Logging.MetaDebugLog($"Initializing Blocks");
Blocks.Movement.Init(); Blocks.Movement.Init();

View file

@ -68,6 +68,10 @@ namespace GamecraftModdingAPI.Tests
EventType.GameReloaded, "gamerel API debug")); EventType.GameReloaded, "gamerel API debug"));
EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Switched To event!"); }, () => { }, EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Switched To event!"); }, () => { },
EventType.GameSwitchedTo, "gameswitch API debug")); 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 commands // debug/test commands
if (Dependency.Hell("ExtraCommands")) if (Dependency.Hell("ExtraCommands"))