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.

114 lines
4.2KB

  1. using System;
  2. using System.Reflection;
  3. using HarmonyLib;
  4. using RobocraftX;
  5. using RobocraftX.Schedulers;
  6. using RobocraftX.Services;
  7. using Svelto.Context;
  8. using Svelto.Tasks.ExtraLean;
  9. using TechbloxModdingAPI.App;
  10. using TechbloxModdingAPI.Blocks;
  11. using TechbloxModdingAPI.Events;
  12. using TechbloxModdingAPI.Tasks;
  13. using TechbloxModdingAPI.Utility;
  14. namespace TechbloxModdingAPI
  15. {
  16. /// <summary>
  17. /// The main class of the TechbloxModdingAPI.
  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 TechbloxModdingAPI.
  29. /// Call this as soon as possible after Techblox 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("TechbloxModdingAPI.Main.Init() called but API is already initialized!");
  39. return;
  40. }
  41. Logging.MetaDebugLog($"Patching Techblox");
  42. var currentAssembly = Assembly.GetExecutingAssembly();
  43. harmony = new Harmony(currentAssembly.GetName().Name);
  44. try
  45. {
  46. harmony.PatchAll(currentAssembly);
  47. }
  48. catch (Exception e)
  49. { //Can't use ErrorBuilder or Logging.LogException (which eventually uses ErrorBuilder) yet
  50. Logging.Log(e.ToString());
  51. Logging.LogWarning("Failed to patch Techblox. Attempting to patch to display error...");
  52. harmony.Patch(AccessTools.Method(typeof(FullGameCompositionRoot), "OnContextInitialized")
  53. .MakeGenericMethod(typeof(UnityContext<FullGameCompositionRoot>)),
  54. new HarmonyMethod(((Action) OnPatchError).Method)); //Can't use lambdas here :(
  55. return;
  56. }
  57. // init utility
  58. Logging.MetaDebugLog($"Initializing Utility");
  59. Utility.GameState.Init();
  60. // init block implementors
  61. Logging.MetaDebugLog($"Initializing Blocks");
  62. // init input
  63. Input.FakeInput.Init();
  64. // init object-oriented classes
  65. Player.Init();
  66. Block.Init();
  67. BlockGroup.Init();
  68. Wire.Init();
  69. Logging.MetaDebugLog($"Initializing Client");
  70. Client.Init();
  71. Game.Init();
  72. // init UI
  73. Interface.IMGUI.Constants.Init();
  74. Interface.IMGUI.IMGUIManager.Init();
  75. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  76. }
  77. /// <summary>
  78. /// Shuts down & cleans up the TechbloxModdingAPI.
  79. /// Call this as late as possible before Techblox 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("TechbloxModdingAPI.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. private static void OnPatchError()
  100. {
  101. ErrorBuilder.DisplayMustQuitError("Failed to patch Techblox!\n" +
  102. "Make sure you're using the latest version of TechbloxModdingAPI or disable mods if the API isn't released yet.");
  103. }
  104. }
  105. }