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.

80 lines
2.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Svelto.ECS;
  7. using GamecraftModdingAPI.Engines;
  8. namespace GamecraftModdingAPI.Utility
  9. {
  10. /// <summary>
  11. /// Keeps track of custom game-modifying engines
  12. /// </summary>
  13. public static class GameEngineManager
  14. {
  15. private static Dictionary<string, IApiEngine> _gameEngines = new Dictionary<string, IApiEngine>();
  16. private static EnginesRoot _lastEngineRoot;
  17. public static void AddGameEngine(IApiEngine engine)
  18. {
  19. _gameEngines[engine.Name] = engine;
  20. if (_lastEngineRoot != null)
  21. {
  22. Logging.MetaDebugLog($"Registering Game IApiEngine {engine.Name}");
  23. _lastEngineRoot.AddEngine(engine);
  24. if (typeof(IFactoryEngine).IsAssignableFrom(engine.GetType()))
  25. {
  26. ((IFactoryEngine)engine).Factory = _lastEngineRoot.GenerateEntityFactory();
  27. }
  28. }
  29. }
  30. public static bool ExistsGameEngine(string name)
  31. {
  32. return _gameEngines.ContainsKey(name);
  33. }
  34. public static bool ExistsGameEngine(IApiEngine engine)
  35. {
  36. return ExistsGameEngine(engine.Name);
  37. }
  38. public static IApiEngine GetGameEngine(string name)
  39. {
  40. return _gameEngines[name];
  41. }
  42. public static string[] GetGameEngineNames()
  43. {
  44. return _gameEngines.Keys.ToArray();
  45. }
  46. public static void RemoveGameEngine(string name)
  47. {
  48. if (_gameEngines[name].isRemovable)
  49. {
  50. _gameEngines.Remove(name);
  51. }
  52. }
  53. public static void RegisterEngines(EnginesRoot enginesRoot)
  54. {
  55. _lastEngineRoot = enginesRoot;
  56. IEntityFactory factory = enginesRoot.GenerateEntityFactory();
  57. foreach (var key in _gameEngines.Keys)
  58. {
  59. Logging.MetaDebugLog($"Registering Game IApiEngine {_gameEngines[key].Name}");
  60. enginesRoot.AddEngine(_gameEngines[key]);
  61. if (typeof(IFactoryEngine).IsAssignableFrom(_gameEngines[key].GetType()))
  62. {
  63. ((IFactoryEngine)_gameEngines[key]).Factory = factory;
  64. }
  65. }
  66. }
  67. }
  68. }