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 3.9KB

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