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.

Main.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // init inventory
  61. Inventory.Hotbar.Init();
  62. // init input
  63. Input.FakeInput.Init();
  64. // init object-oriented classes
  65. Player.Init();
  66. Block.Init();
  67. GameClient.Init();
  68. AsyncUtils.Init();
  69. GamecraftModdingAPI.App.Client.Init();
  70. GamecraftModdingAPI.App.Game.Init();
  71. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  72. }
  73. /// <summary>
  74. /// Shuts down & cleans up the GamecraftModdingAPI.
  75. /// Call this as late as possible before Gamecraft quits.
  76. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  77. /// </summary>
  78. public static void Shutdown()
  79. {
  80. if (referenceCount > 0) { referenceCount--; }
  81. if (referenceCount == 0)
  82. {
  83. if (!IsInitialized)
  84. {
  85. Logging.LogWarning("GamecraftModdingAPI.Main.Shutdown() called but API is not initialized!");
  86. return;
  87. }
  88. Scheduler.Dispose();
  89. var currentAssembly = Assembly.GetExecutingAssembly();
  90. harmony.UnpatchAll(currentAssembly.GetName().Name);
  91. harmony = null;
  92. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  93. }
  94. }
  95. }
  96. }