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.

103 lines
4.4KB

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