A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using RobocraftX.StateSync;
  4. using Svelto.ECS;
  5. using TechbloxModdingAPI.Utility;
  6. namespace TechbloxModdingAPI.Common.Engines;
  7. public class EngineManager
  8. {
  9. private static Dictionary<ApiEngineType, List<IApiEngine>> _engines = new();
  10. /// <summary>
  11. /// Register an engine to a given game state and type. Or multiple.
  12. /// </summary>
  13. /// <param name="engine">The engine</param>
  14. /// <param name="types">The types to register to</param>
  15. public static void AddEngine(IApiEngine engine, params ApiEngineType[] types)
  16. {
  17. if (types.Length == 0)
  18. Logging.LogWarning($"Engine {engine.GetType().FullName} added without any types! This doesn't do anything.");
  19. foreach (var type in types)
  20. {
  21. if (!_engines.ContainsKey(type))
  22. _engines.Add(type, new List<IApiEngine>());
  23. _engines[type].Add(engine);
  24. }
  25. }
  26. public static void RegisterEngines(StateSyncRegistrationHelper helper, EnginesRoot enginesRoot, ApiEngineType type)
  27. {
  28. IEntityFactory factory = enginesRoot.GenerateEntityFactory();
  29. IEntityFunctions functions = enginesRoot.GenerateEntityFunctions();
  30. foreach (var engine in _engines[type])
  31. {
  32. string name = engine is INamedApiEngine namedEngine ? namedEngine.Name : engine.ToString();
  33. Logging.MetaDebugLog($"Registering {type} IApiEngine {name}");
  34. if (engine is IDeterministicEngine detEngine)
  35. if (helper is not null) helper.AddDeterministicEngine(detEngine);
  36. else throw new InvalidOperationException($"Attempting to add deterministic engine to non-deterministic state {type}");
  37. else
  38. enginesRoot.AddEngine(engine);
  39. switch (engine)
  40. {
  41. case IFactoryEngine factEngine:
  42. factEngine.Factory = factory;
  43. break;
  44. case IFunEngine funEngine:
  45. funEngine.Functions = functions;
  46. break;
  47. }
  48. }
  49. }
  50. }