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.5KB

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