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.

92 lines
3.9KB

  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. // init block implementors
  54. Logging.MetaDebugLog($"Initializing Blocks");
  55. Blocks.Movement.Init();
  56. Blocks.Rotation.Init();
  57. Blocks.Signals.Init();
  58. Blocks.Placement.Init();
  59. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  60. }
  61. /// <summary>
  62. /// Shuts down & cleans up the GamecraftModdingAPI.
  63. /// Call this as late as possible before Gamecraft quits.
  64. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  65. /// </summary>
  66. public static void Shutdown()
  67. {
  68. if (referenceCount > 0) { referenceCount--; }
  69. if (referenceCount == 0)
  70. {
  71. if (!IsInitialized)
  72. {
  73. Logging.LogWarning("GamecraftModdingAPI.Main.Shutdown() called but API is not initialized!");
  74. return;
  75. }
  76. Scheduler.Dispose();
  77. var currentAssembly = Assembly.GetExecutingAssembly();
  78. harmony.UnpatchAll(currentAssembly.GetName().Name);
  79. harmony = null;
  80. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  81. }
  82. }
  83. }
  84. }