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> _engines = new(); /// /// Register an engine to a given game state and type. Or multiple. /// /// The engine /// The types to register to 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()); _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; } } } }