Browse Source

Add Simulation and Build switch to events

tags/v0.1.4
NGnius 4 years ago
parent
commit
7ac5120ef5
6 changed files with 137 additions and 1 deletions
  1. +40
    -0
      GamecraftModdingAPI/Events/DeterministicStepComposeEngineGroupsPatch.cs
  2. +3
    -1
      GamecraftModdingAPI/Events/EventType.cs
  3. +44
    -0
      GamecraftModdingAPI/Events/GameStateBuildEmitterEngine.cs
  4. +44
    -0
      GamecraftModdingAPI/Events/GameStateSimulationEmitterEngine.cs
  5. +2
    -0
      GamecraftModdingAPI/Main.cs
  6. +4
    -0
      GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs

+ 40
- 0
GamecraftModdingAPI/Events/DeterministicStepComposeEngineGroupsPatch.cs 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));
}
}
}

+ 3
- 1
GamecraftModdingAPI/Events/EventType.cs View File

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

+ 44
- 0
GamecraftModdingAPI/Events/GameStateBuildEmitterEngine.cs 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() { }
}
}

+ 44
- 0
GamecraftModdingAPI/Events/GameStateSimulationEmitterEngine.cs 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() { }
}
}

+ 2
- 0
GamecraftModdingAPI/Main.cs View File

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


+ 4
- 0
GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs View File

@@ -68,6 +68,10 @@ namespace GamecraftModdingAPI.Tests
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 commands
if (Dependency.Hell("ExtraCommands"))


Loading…
Cancel
Save