A stable modding interface between Techblox and mods https://mod.exmods.org/
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

118 satır
4.3KB

  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 inventory
  63. Inventory.Hotbar.Init();
  64. // init input
  65. Input.FakeInput.Init();
  66. // init object-oriented classes
  67. Player.Init();
  68. FlyCam.Init();
  69. Block.Init();
  70. BlockGroup.Init();
  71. Wire.Init();
  72. Logging.MetaDebugLog($"Initializing Client");
  73. GameClient.Init();
  74. Client.Init();
  75. Game.Init();
  76. // init UI
  77. Interface.IMGUI.Constants.Init();
  78. Interface.IMGUI.IMGUIManager.Init();
  79. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized");
  80. }
  81. /// <summary>
  82. /// Shuts down & cleans up the TechbloxModdingAPI.
  83. /// Call this as late as possible before Techblox quits.
  84. /// Ideally, this should be called from your main Plugin class's OnApplicationQuit() method.
  85. /// </summary>
  86. public static void Shutdown()
  87. {
  88. if (referenceCount > 0) { referenceCount--; }
  89. if (referenceCount == 0)
  90. {
  91. if (!IsInitialized)
  92. {
  93. Logging.LogWarning("TechbloxModdingAPI.Main.Shutdown() called but API is not initialized!");
  94. return;
  95. }
  96. Scheduler.Dispose();
  97. var currentAssembly = Assembly.GetExecutingAssembly();
  98. harmony.UnpatchAll(currentAssembly.GetName().Name);
  99. harmony = null;
  100. Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} shutdown");
  101. }
  102. }
  103. private static void OnPatchError()
  104. {
  105. ErrorBuilder.DisplayMustQuitError("Failed to patch Techblox!\n" +
  106. "Make sure you're using the latest version of TechbloxModdingAPI or disable mods if the API isn't released yet.");
  107. }
  108. }
  109. }