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.

95 lines
4.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. using Harmony;
  8. using GamecraftModdingAPI.Utility;
  9. using GamecraftModdingAPI.Events;
  10. using GamecraftModdingAPI.Tasks;
  11. namespace GamecraftModdingAPI
  12. {
  13. /// <summary>
  14. /// The main class of the GamecraftModdingAPI.
  15. /// Use this to initialize the API before calling it.
  16. /// </summary>
  17. public static class Main
  18. {
  19. private static HarmonyInstance harmony;
  20. public static bool IsInitialized {
  21. get { return harmony != null; }
  22. }
  23. private static int referenceCount = 0;
  24. /// <summary>
  25. /// Initializes the GamecraftModdingAPI.
  26. /// Call this as soon as possible after Gamecraft starts up.
  27. /// Ideally, this should be called from your main Plugin class's OnApplicationStart() method.
  28. /// </summary>
  29. public static void Init()
  30. {
  31. referenceCount++;
  32. if (referenceCount > 1) { return; }
  33. if (IsInitialized)
  34. {
  35. Logging.LogWarning("GamecraftModdingAPI.Main.Init() called but API is already initialized!");
  36. return;
  37. }
  38. Logging.MetaDebugLog($"Patching Gamecraft");
  39. var currentAssembly = Assembly.GetExecutingAssembly();
  40. harmony = HarmonyInstance.Create(currentAssembly.GetName().Name);
  41. harmony.PatchAll(currentAssembly);
  42. // init utility
  43. Logging.MetaDebugLog($"Initializing Utility");
  44. Utility.GameState.Init();
  45. // create default event emitters
  46. Logging.MetaDebugLog($"Initializing Events");
  47. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false));
  48. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false));
  49. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false));
  50. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false));
  51. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false));
  52. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false));
  53. EventManager.AddEventEmitter(DeterministicStepComposeEngineGroupsPatch.buildEngine);
  54. EventManager.AddEventEmitter(DeterministicStepComposeEngineGroupsPatch.simEngine);
  55. // init block implementors
  56. Logging.MetaDebugLog($"Initializing Blocks");
  57. Blocks.Movement.Init();
  58. Blocks.Rotation.Init();
  59. Blocks.Signals.Init();
  60. Blocks.Placement.Init();
  61. Blocks.Tweakable.Init();
  62. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  63. }
  64. /// <summary>
  65. /// Shuts down & cleans up the GamecraftModdingAPI.
  66. /// Call this as late as possible before Gamecraft quits.
  67. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  68. /// </summary>
  69. public static void Shutdown()
  70. {
  71. if (referenceCount > 0) { referenceCount--; }
  72. if (referenceCount == 0)
  73. {
  74. if (!IsInitialized)
  75. {
  76. Logging.LogWarning("GamecraftModdingAPI.Main.Shutdown() called but API is not initialized!");
  77. return;
  78. }
  79. Scheduler.Dispose();
  80. var currentAssembly = Assembly.GetExecutingAssembly();
  81. harmony.UnpatchAll(currentAssembly.GetName().Name);
  82. harmony = null;
  83. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  84. }
  85. }
  86. }
  87. }