|
- using System;
- using System.Collections.Generic;
- using RobocraftX.StateSync;
- using Svelto.ECS;
- using TechbloxModdingAPI.Utility;
-
- namespace TechbloxModdingAPI.Common.Engines;
-
- public class EngineManager
- {
- private static Dictionary<ApiEngineType, List<IApiEngine>> _engines = new();
-
- /// <summary>
- /// Register an engine to a given game state and type. Or multiple.
- /// </summary>
- /// <param name="engine">The engine</param>
- /// <param name="types">The types to register to</param>
- public static void AddEngine(IApiEngine engine, params ApiEngineType[] types)
- {
- if (types.Length == 0)
- Logging.LogWarning($"Engine {engine.GetType().FullName} added without any types! This doesn't do anything.");
- foreach (var type in types)
- {
- if (!_engines.ContainsKey(type))
- _engines.Add(type, new List<IApiEngine>());
- _engines[type].Add(engine);
- }
- }
-
- public static void RegisterEngines(StateSyncRegistrationHelper helper, EnginesRoot enginesRoot, ApiEngineType type)
- {
- IEntityFactory factory = enginesRoot.GenerateEntityFactory();
- IEntityFunctions functions = enginesRoot.GenerateEntityFunctions();
- foreach (var engine in _engines[type])
- {
- string name = engine is INamedApiEngine namedEngine ? namedEngine.Name : engine.ToString();
- Logging.MetaDebugLog($"Registering {type} IApiEngine {name}");
- if (engine is IDeterministicEngine detEngine)
- if (helper is not null) helper.AddDeterministicEngine(detEngine);
- else throw new InvalidOperationException($"Attempting to add deterministic engine to non-deterministic state {type}");
- else
- enginesRoot.AddEngine(engine);
- switch (engine)
- {
- case IFactoryEngine factEngine:
- factEngine.Factory = factory;
- break;
- case IFunEngine funEngine:
- funEngine.Functions = functions;
- break;
- }
- }
- }
- }
|