Rework engine inheritance structure

This commit is contained in:
NGnius (Graham) 2020-05-11 20:28:26 -04:00
parent a17e5a6449
commit 42b21bc16d
25 changed files with 140 additions and 136 deletions

View file

@ -18,9 +18,10 @@ using Svelto.ECS.EntityStructs;
using Unity.Transforms; using Unity.Transforms;
using Unity.Mathematics; using Unity.Mathematics;
using UnityEngine; using UnityEngine;
using Svelto.DataStructures;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using Svelto.DataStructures; using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -33,7 +34,9 @@ namespace GamecraftModdingAPI.Blocks
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public bool IsInGame = false; public bool isRemovable => false;
public bool IsInGame = false;
public void Dispose() public void Dispose()
{ {

View file

@ -21,6 +21,7 @@ using UnityEngine;
using uREPL; using uREPL;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -128,7 +129,9 @@ namespace GamecraftModdingAPI.Blocks
public string Name { get; } = "GamecraftModdingAPIPlacementGameEngine"; public string Name { get; } = "GamecraftModdingAPIPlacementGameEngine";
[HarmonyPatch] public bool isRemovable => false;
[HarmonyPatch]
public class FactoryObtainerPatch public class FactoryObtainerPatch
{ {
static void Postfix(BlockEntityFactory blockEntityFactory) static void Postfix(BlockEntityFactory blockEntityFactory)

View file

@ -12,6 +12,7 @@ using uREPL;
using GamecraftModdingAPI.Commands; using GamecraftModdingAPI.Commands;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -54,7 +55,9 @@ namespace GamecraftModdingAPI.Blocks
public string Name { get; } = "GamecraftModdingAPIRemovalGameEngine"; public string Name { get; } = "GamecraftModdingAPIRemovalGameEngine";
[HarmonyPatch] public bool isRemovable => false;
[HarmonyPatch]
public class FactoryObtainerPatch public class FactoryObtainerPatch
{ {
static void Postfix(IEntityFunctions entityFunctions, static void Postfix(IEntityFunctions entityFunctions,

View file

@ -20,6 +20,7 @@ using Unity.Mathematics;
using UnityEngine; using UnityEngine;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -32,7 +33,9 @@ namespace GamecraftModdingAPI.Blocks
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public bool IsInGame = false; public bool isRemovable => false;
public bool IsInGame = false;
public void Dispose() public void Dispose()
{ {

View file

@ -22,6 +22,7 @@ using UnityEngine;
using Gamecraft.Wires; using Gamecraft.Wires;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -33,8 +34,10 @@ namespace GamecraftModdingAPI.Blocks
public string Name { get; } = "GamecraftModdingAPISignalGameEngine"; public string Name { get; } = "GamecraftModdingAPISignalGameEngine";
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
public bool IsInGame = false; public bool IsInGame = false;
public void Dispose() public void Dispose()
{ {

View file

@ -3,9 +3,11 @@ using System.Reflection;
using RobocraftX.Blocks; using RobocraftX.Blocks;
using Gamecraft.Wires; using Gamecraft.Wires;
using Svelto.ECS;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using Svelto.ECS; using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
@ -15,6 +17,8 @@ namespace GamecraftModdingAPI.Blocks
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
public bool IsInGame = false; public bool IsInGame = false;
public void Dispose() public void Dispose()

View file

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Svelto.ECS; using Svelto.ECS;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Commands namespace GamecraftModdingAPI.Commands
{ {

View file

@ -31,7 +31,9 @@ namespace GamecraftModdingAPI.Commands
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public void Dispose() public bool isRemovable => true;
public void Dispose()
{ {
GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}"); GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
CommandRegistrationHelper.Unregister(this.Name); CommandRegistrationHelper.Unregister(this.Name);

View file

@ -22,7 +22,9 @@ namespace GamecraftModdingAPI.Commands
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public void Dispose() public bool isRemovable => true;
public void Dispose()
{ {
GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}"); GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
CommandRegistrationHelper.Unregister(this.Name); CommandRegistrationHelper.Unregister(this.Name);

View file

@ -22,7 +22,9 @@ namespace GamecraftModdingAPI.Commands
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public void Dispose() public bool isRemovable => true;
public void Dispose()
{ {
GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}"); GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
CommandRegistrationHelper.Unregister(this.Name); CommandRegistrationHelper.Unregister(this.Name);

View file

@ -22,7 +22,9 @@ namespace GamecraftModdingAPI.Commands
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public void Dispose() public bool isRemovable => true;
public void Dispose()
{ {
GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}"); GamecraftModdingAPI.Utility.Logging.MetaDebugLog($"Unregistering SimpleCustomCommandEngine {this.Name}");
CommandRegistrationHelper.Unregister(this.Name); CommandRegistrationHelper.Unregister(this.Name);

View file

@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Svelto.ECS; using Svelto.ECS;
namespace GamecraftModdingAPI.Utility namespace GamecraftModdingAPI.Engines
{ {
/// <summary> /// <summary>
/// Base engine interface used by all GamecraftModdingAPI engines /// Base engine interface used by all GamecraftModdingAPI engines
@ -17,5 +17,10 @@ namespace GamecraftModdingAPI.Utility
/// The name of the engine /// The name of the engine
/// </summary> /// </summary>
string Name { get; } string Name { get; }
/// <summary>
/// Whether the emitter can be removed with Manager.RemoveEventEmitter(name)
/// </summary>
bool isRemovable { get; }
} }
} }

View file

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Svelto.ECS;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Engines
{
/// <summary>
/// Engine interface to create a ModEventEntityStruct in entitiesDB when Emit() is called.
/// </summary>
public interface IFactoryEngine : IApiEngine
{
/// <summary>
/// The EntityFactory for the entitiesDB.
/// Use this to create a ModEventEntityStruct when Emit() is called.
/// </summary>
IEntityFactory Factory { set; }
}
}

View file

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Svelto.ECS;
using Svelto.ECS.Internal;
using GamecraftModdingAPI.Events;
namespace GamecraftModdingAPI.Engines
{
/// <summary>
/// Engine interface to handle ModEventEntityStruct events emitted by IEventEmitterEngines.
/// </summary>
public interface IReactionaryEngine<T> : IApiEngine, IReactOnAddAndRemove<T>, IReactOnAddAndRemove where T : unmanaged, IEntityComponent
{
}
}

View file

@ -6,35 +6,18 @@ using System.Threading.Tasks;
using Svelto.ECS; using Svelto.ECS;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Events namespace GamecraftModdingAPI.Events
{ {
/// <summary> /// <summary>
/// Engine interface to create a ModEventEntityStruct in entitiesDB when Emit() is called. /// Engine interface to create a ModEventEntityStruct in entitiesDB when a specific event occurs.
/// </summary> /// </summary>
public interface IEventEmitterEngine : IApiEngine public interface IEventEmitterEngine : IFactoryEngine
{ {
/// <summary> /// <summary>
/// The type of event emitted /// Emit the event. (Optional)
/// </summary> /// </summary>
int type { get; } void Emit();
/// <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

@ -7,14 +7,14 @@ using System.Threading.Tasks;
using Svelto.ECS; using Svelto.ECS;
using Svelto.ECS.Internal; using Svelto.ECS.Internal;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Events namespace GamecraftModdingAPI.Events
{ {
/// <summary> /// <summary>
/// Engine interface to handle ModEventEntityStruct events emitted by IEventEmitterEngines. /// Engine interface to handle ModEventEntityStruct events emitted by IEventEmitterEngines.
/// </summary> /// </summary>
public interface IEventHandlerEngine : IApiEngine, IReactOnAddAndRemove<ModEventEntityStruct>, IReactOnAddAndRemove public interface IEventHandlerEngine : IReactionaryEngine<ModEventEntityStruct>
{ {
} }
} }

View file

@ -24,7 +24,9 @@ namespace GamecraftModdingAPI.Events
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public void Add(ref ModEventEntityStruct entityView, EGID egid) public bool isRemovable => true;
public void Add(ref ModEventEntityStruct entityView, EGID egid)
{ {
if (entityView.type.Equals(this.type)) if (entityView.type.Equals(this.type))
{ {

View file

@ -5,6 +5,7 @@ using RobocraftX.Players;
using Svelto.ECS; using Svelto.ECS;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Input namespace GamecraftModdingAPI.Input
{ {
@ -14,6 +15,8 @@ namespace GamecraftModdingAPI.Input
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
public bool IsReady = false; public bool IsReady = false;
public void Dispose() public void Dispose()

View file

@ -10,6 +10,7 @@ using Svelto.ECS;
using GamecraftModdingAPI.Blocks; using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Inventory namespace GamecraftModdingAPI.Inventory
{ {
@ -19,6 +20,8 @@ namespace GamecraftModdingAPI.Inventory
public EntitiesDB entitiesDB { set; private get; } public EntitiesDB entitiesDB { set; private get; }
public bool isRemovable => false;
public bool IsInGame = false; public bool IsInGame = false;
public void Dispose() public void Dispose()

View file

@ -1,38 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Analytics;
using HarmonyLib;
using RobocraftX.Common;
using Svelto.ECS;
namespace GamecraftModdingAPI.Utility
{
/// <summary>
/// Patch of Analytics.AnalyticsCompositionRoot.Compose<T>(...)
/// This stops some analytics collection built into Gamecraft.
/// DO NOT USE! (This will likely crash your game on shutdown)
/// </summary>
[HarmonyPatch]
class AnalyticsDisablerPatch
{
/// <summary>
/// Don't activate gameplay analytics?
/// </summary>
public static bool DisableAnalytics = false;
public static bool Prefix(object contextHolder, EnginesRoot enginesRoot, RCXMode rcxMode)
{
return !DisableAnalytics;
}
public static MethodBase TargetMethod(Harmony instance)
{
return typeof(Analytics.AnalyticsCompositionRoot).GetMethod("Compose").MakeGenericMethod(typeof(object));
}
}
}

View file

@ -6,6 +6,8 @@ using System.Threading.Tasks;
using Svelto.ECS; using Svelto.ECS;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Utility namespace GamecraftModdingAPI.Utility
{ {
/// <summary> /// <summary>
@ -24,6 +26,10 @@ namespace GamecraftModdingAPI.Utility
{ {
Logging.MetaDebugLog($"Registering Game IApiEngine {engine.Name}"); Logging.MetaDebugLog($"Registering Game IApiEngine {engine.Name}");
_lastEngineRoot.AddEngine(engine); _lastEngineRoot.AddEngine(engine);
if (typeof(IFactoryEngine).IsAssignableFrom(engine.GetType()))
{
((IFactoryEngine)engine).Factory = _lastEngineRoot.GenerateEntityFactory();
}
} }
} }
@ -49,16 +55,24 @@ namespace GamecraftModdingAPI.Utility
public static void RemoveGameEngine(string name) public static void RemoveGameEngine(string name)
{ {
_gameEngines.Remove(name); if (_gameEngines[name].isRemovable)
{
_gameEngines.Remove(name);
}
} }
public static void RegisterEngines(EnginesRoot enginesRoot) public static void RegisterEngines(EnginesRoot enginesRoot)
{ {
_lastEngineRoot = enginesRoot; _lastEngineRoot = enginesRoot;
IEntityFactory factory = enginesRoot.GenerateEntityFactory();
foreach (var key in _gameEngines.Keys) foreach (var key in _gameEngines.Keys)
{ {
Logging.MetaDebugLog($"Registering Game IApiEngine {_gameEngines[key].Name}"); Logging.MetaDebugLog($"Registering Game IApiEngine {_gameEngines[key].Name}");
enginesRoot.AddEngine(_gameEngines[key]); enginesRoot.AddEngine(_gameEngines[key]);
if (typeof(IFactoryEngine).IsAssignableFrom(_gameEngines[key].GetType()))
{
((IFactoryEngine)_gameEngines[key]).Factory = factory;
}
} }
} }
} }

View file

@ -7,6 +7,8 @@ using System.Threading.Tasks;
using RobocraftX.SimulationModeState; using RobocraftX.SimulationModeState;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Utility namespace GamecraftModdingAPI.Utility
{ {
class GameStateEngine : IApiEngine class GameStateEngine : IApiEngine
@ -19,7 +21,9 @@ namespace GamecraftModdingAPI.Utility
public bool IsInGame { get { return _isInGame; } } public bool IsInGame { get { return _isInGame; } }
public void Dispose() public bool isRemovable => false;
public void Dispose()
{ {
_isInGame = false; _isInGame = false;
} }

View file

@ -6,6 +6,8 @@ using System.Threading.Tasks;
using Svelto.ECS; using Svelto.ECS;
using GamecraftModdingAPI.Engines;
namespace GamecraftModdingAPI.Utility namespace GamecraftModdingAPI.Utility
{ {
/// <summary> /// <summary>
@ -25,6 +27,10 @@ namespace GamecraftModdingAPI.Utility
{ {
Logging.MetaDebugLog($"Registering Menu IApiEngine {engine.Name}"); Logging.MetaDebugLog($"Registering Menu IApiEngine {engine.Name}");
_lastEngineRoot.AddEngine(engine); _lastEngineRoot.AddEngine(engine);
if (typeof(IFactoryEngine).IsAssignableFrom(engine.GetType()))
{
((IFactoryEngine)engine).Factory = _lastEngineRoot.GenerateEntityFactory();
}
} }
} }
@ -50,16 +56,24 @@ namespace GamecraftModdingAPI.Utility
public static void RemoveMenuEngine(string name) public static void RemoveMenuEngine(string name)
{ {
_menuEngines.Remove(name); if (_menuEngines[name].isRemovable)
{
_menuEngines.Remove(name);
}
} }
public static void RegisterEngines(EnginesRoot enginesRoot) public static void RegisterEngines(EnginesRoot enginesRoot)
{ {
_lastEngineRoot = enginesRoot; _lastEngineRoot = enginesRoot;
IEntityFactory factory = enginesRoot.GenerateEntityFactory();
foreach (var key in _menuEngines.Keys) foreach (var key in _menuEngines.Keys)
{ {
Logging.MetaDebugLog($"Registering Menu IApiEngine {_menuEngines[key].Name}"); Logging.MetaDebugLog($"Registering Menu IApiEngine {_menuEngines[key].Name}");
enginesRoot.AddEngine(_menuEngines[key]); enginesRoot.AddEngine(_menuEngines[key]);
if (typeof(IFactoryEngine).IsAssignableFrom(_menuEngines[key].GetType()))
{
((IFactoryEngine)_menuEngines[key]).Factory = factory;
}
} }
} }
} }

View file

@ -1,28 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
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

@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HarmonyLib;
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;
}
}
}