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.

117 lines
4.3KB

  1. using System;
  2. using System.Reflection;
  3. using HarmonyLib;
  4. using RobocraftX;
  5. using RobocraftX.Services;
  6. using Svelto.Context;
  7. using TechbloxModdingAPI.App;
  8. using TechbloxModdingAPI.Blocks;
  9. using TechbloxModdingAPI.Tasks;
  10. using TechbloxModdingAPI.Utility;
  11. namespace TechbloxModdingAPI
  12. {
  13. /// <summary>
  14. /// The main class of the TechbloxModdingAPI.
  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 TechbloxModdingAPI.
  26. /// Call this as soon as possible after Techblox 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("TechbloxModdingAPI.Main.Init() called but API is already initialized!");
  36. return;
  37. }
  38. Logging.MetaDebugLog($"Patching Techblox");
  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 Techblox. 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. Utility.GameState.Init();
  57. // init block implementors
  58. Logging.MetaDebugLog($"Initializing Blocks");
  59. // init input
  60. Input.FakeInput.Init();
  61. // init object-oriented classes
  62. Player.Init();
  63. Block.Init();
  64. BlockGroup.Init();
  65. Wire.Init();
  66. // init client
  67. Logging.MetaDebugLog($"Initializing Client");
  68. Client.Init();
  69. Game.Init();
  70. // init UI
  71. Logging.MetaDebugLog($"Initializing UI");
  72. Interface.IMGUI.Constants.Init();
  73. Interface.IMGUI.IMGUIManager.Init();
  74. // init anti-anticheat
  75. Logging.MetaDebugLog("Initializing anti-anticheat");
  76. AntiAntiCheatPatch.Init(harmony);
  77. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  78. }
  79. /// <summary>
  80. /// Shuts down & cleans up the TechbloxModdingAPI.
  81. /// Call this as late as possible before Techblox quits.
  82. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  83. /// </summary>
  84. public static void Shutdown()
  85. {
  86. if (referenceCount > 0) { referenceCount--; }
  87. if (referenceCount == 0)
  88. {
  89. if (!IsInitialized)
  90. {
  91. Logging.LogWarning("TechbloxModdingAPI.Main.Shutdown() called but API is not initialized!");
  92. return;
  93. }
  94. Scheduler.Dispose();
  95. var currentAssembly = Assembly.GetExecutingAssembly();
  96. harmony.UnpatchAll(currentAssembly.GetName().Name);
  97. harmony = null;
  98. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  99. }
  100. }
  101. private static void OnPatchError()
  102. {
  103. ErrorBuilder.DisplayMustQuitError("Failed to patch Techblox!\n" +
  104. "Make sure you're using the latest version of TechbloxModdingAPI or disable mods if the API isn't released yet.");
  105. }
  106. }
  107. }