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.

101 lines
4.3KB

  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 HarmonyLib;
  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 Harmony 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 = new Harmony(currentAssembly.GetName().Name);
  41. harmony.PatchAll(currentAssembly);
  42. // init utility
  43. Logging.MetaDebugLog($"Initializing Utility");
  44. Utility.GameState.Init();
  45. Utility.VersionTracking.Init();
  46. // create default event emitters
  47. Logging.MetaDebugLog($"Initializing Events");
  48. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false));
  49. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false));
  50. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false));
  51. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false));
  52. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false));
  53. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false));
  54. EventManager.AddEventEmitter(GameHostTransitionDeterministicGroupEnginePatch.buildEngine);
  55. EventManager.AddEventEmitter(GameHostTransitionDeterministicGroupEnginePatch.simEngine);
  56. // init block implementors
  57. Logging.MetaDebugLog($"Initializing Blocks");
  58. Blocks.Movement.Init();
  59. Blocks.Rotation.Init();
  60. Blocks.Signals.Init();
  61. Blocks.Placement.Init();
  62. Blocks.Tweakable.Init();
  63. Blocks.Removal.Init();
  64. // init inventory
  65. Inventory.Hotbar.Init();
  66. // init input
  67. Input.FakeInput.Init();
  68. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  69. }
  70. /// <summary>
  71. /// Shuts down & cleans up the GamecraftModdingAPI.
  72. /// Call this as late as possible before Gamecraft quits.
  73. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  74. /// </summary>
  75. public static void Shutdown()
  76. {
  77. if (referenceCount > 0) { referenceCount--; }
  78. if (referenceCount == 0)
  79. {
  80. if (!IsInitialized)
  81. {
  82. Logging.LogWarning("GamecraftModdingAPI.Main.Shutdown() called but API is not initialized!");
  83. return;
  84. }
  85. Scheduler.Dispose();
  86. var currentAssembly = Assembly.GetExecutingAssembly();
  87. harmony.UnpatchAll(currentAssembly.GetName().Name);
  88. harmony = null;
  89. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  90. }
  91. }
  92. }
  93. }