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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Reflection;
  3. using GamecraftModdingAPI.Blocks;
  4. using HarmonyLib;
  5. using RobocraftX;
  6. using RobocraftX.Services;
  7. using Svelto.Context;
  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 Harmony 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 = new Harmony(currentAssembly.GetName().Name);
  41. try
  42. {
  43. harmony.PatchAll(currentAssembly);
  44. }
  45. catch (Exception e)
  46. { //Can't use ErrorBuilder or Logging.LogException (which eventually uses ErrorBuilder) yet
  47. Logging.Log(e.ToString());
  48. Logging.LogWarning("Failed to patch Gamecraft. Attempting to patch to display error...");
  49. harmony.Patch(AccessTools.Method(typeof(FullGameCompositionRoot), "OnContextInitialized")
  50. .MakeGenericMethod(typeof(UnityContext<FullGameCompositionRoot>)),
  51. new HarmonyMethod(((Action) OnPatchError).Method)); //Can't use lambdas here :(
  52. return;
  53. }
  54. // init utility
  55. Logging.MetaDebugLog($"Initializing Utility");
  56. #pragma warning disable 0612,0618
  57. Utility.GameState.Init();
  58. Utility.VersionTracking.Init();
  59. // create default event emitters
  60. Logging.MetaDebugLog($"Initializing Events");
  61. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.ApplicationInitialized, "GamecraftModdingAPIApplicationInitializedEventEmitter", false));
  62. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Menu, "GamecraftModdingAPIMenuActivatedEventEmitter", false));
  63. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.MenuSwitchedTo, "GamecraftModdingAPIMenuSwitchedToEventEmitter", false));
  64. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.Game, "GamecraftModdingAPIGameActivatedEventEmitter", false));
  65. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameReloaded, "GamecraftModdingAPIGameReloadedEventEmitter", false));
  66. EventManager.AddEventEmitter(new SimpleEventEmitterEngine(EventType.GameSwitchedTo, "GamecraftModdingAPIGameSwitchedToEventEmitter", false));
  67. EventManager.AddEventEmitter(GameHostTransitionDeterministicGroupEnginePatch.buildEngine);
  68. EventManager.AddEventEmitter(GameHostTransitionDeterministicGroupEnginePatch.simEngine);
  69. #pragma warning restore 0612,0618
  70. // init block implementors
  71. Logging.MetaDebugLog($"Initializing Blocks");
  72. // init inventory
  73. Inventory.Hotbar.Init();
  74. // init input
  75. Input.FakeInput.Init();
  76. // init object-oriented classes
  77. Player.Init();
  78. Block.Init();
  79. BlockGroup.Init();
  80. Wire.Init();
  81. GameClient.Init();
  82. AsyncUtils.Init();
  83. GamecraftModdingAPI.App.Client.Init();
  84. GamecraftModdingAPI.App.Game.Init();
  85. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  86. }
  87. /// <summary>
  88. /// Shuts down & cleans up the GamecraftModdingAPI.
  89. /// Call this as late as possible before Gamecraft quits.
  90. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  91. /// </summary>
  92. public static void Shutdown()
  93. {
  94. if (referenceCount > 0) { referenceCount--; }
  95. if (referenceCount == 0)
  96. {
  97. if (!IsInitialized)
  98. {
  99. Logging.LogWarning("GamecraftModdingAPI.Main.Shutdown() called but API is not initialized!");
  100. return;
  101. }
  102. Scheduler.Dispose();
  103. var currentAssembly = Assembly.GetExecutingAssembly();
  104. harmony.UnpatchAll(currentAssembly.GetName().Name);
  105. harmony = null;
  106. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  107. }
  108. }
  109. private static void OnPatchError()
  110. {
  111. ErrorBuilder.DisplayMustQuitError("Failed to patch Gamecraft!\n" +
  112. "Make sure you're using the latest version of GamecraftModdingAPI or disable mods if the API isn't released yet.");
  113. }
  114. }
  115. }