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.

210 lines
10KB

  1. using System;
  2. using System.Reflection;
  3. using Harmony;
  4. // test
  5. using Svelto.ECS;
  6. using RobocraftX.Blocks;
  7. using RobocraftX.Common;
  8. using RobocraftX.SimulationModeState;
  9. using GamecraftModdingAPI.Commands;
  10. using GamecraftModdingAPI.Events;
  11. using GamecraftModdingAPI.Utility;
  12. namespace GamecraftModdingAPI.Tests
  13. {
  14. // unused by design
  15. /// <summary>
  16. /// Modding API implemented as a standalone IPA Plugin.
  17. /// Ideally, GamecraftModdingAPI should be loaded by another mod; not itself
  18. /// </summary>
  19. public class GamecraftModdingAPIPluginTest
  20. #if DEBUG
  21. : IllusionPlugin.IEnhancedPlugin
  22. #endif
  23. {
  24. private static HarmonyInstance harmony { get; set; }
  25. public string[] Filter { get; } = new string[] { "Gamecraft", "GamecraftPreview" };
  26. public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
  27. public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  28. public string HarmonyID { get; } = "org.git.exmods.modtainers.gamecraftmoddingapi";
  29. public void OnApplicationQuit()
  30. {
  31. GamecraftModdingAPI.Main.Shutdown();
  32. }
  33. public void OnApplicationStart()
  34. {
  35. FileLog.Reset();
  36. HarmonyInstance.DEBUG = true;
  37. GamecraftModdingAPI.Main.Init();
  38. Logging.MetaDebugLog($"Version group id {(uint)ApiExclusiveGroups.versionGroup}");
  39. // in case Steam is not installed/running
  40. // this will crash the game slightly later during startup
  41. //SteamInitPatch.ForcePassSteamCheck = true;
  42. // in case running in a VM
  43. //MinimumSpecsCheckPatch.ForcePassMinimumSpecCheck = true;
  44. // disable some Gamecraft analytics
  45. //AnalyticsDisablerPatch.DisableAnalytics = true;
  46. // disable background music
  47. Logging.MetaDebugLog("Audio Mixers: "+string.Join(",", AudioTools.GetMixers()));
  48. //AudioTools.SetVolume(0.0f, "Music"); // The game now sets this from settings again after this is called :(
  49. Utility.VersionTracking.Enable();
  50. // debug/test handlers
  51. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("App Inited event!"); }, () => { },
  52. EventType.ApplicationInitialized, "appinit API debug"));
  53. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Activated event!"); },
  54. () => { Logging.Log("Menu Destroyed event!"); },
  55. EventType.Menu, "menuact API debug"));
  56. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Menu Switched To event!"); }, () => { },
  57. EventType.MenuSwitchedTo, "menuswitch API debug"));
  58. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Activated event!"); },
  59. () => { Logging.Log("Game Destroyed event!"); },
  60. EventType.Game, "gameact API debug"));
  61. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Reloaded event!"); }, () => { },
  62. EventType.GameReloaded, "gamerel API debug"));
  63. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Switched To event!"); }, () => { },
  64. EventType.GameSwitchedTo, "gameswitch API debug"));
  65. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Mode Simulation Switched To event!"); }, () => { },
  66. EventType.SimulationSwitchedTo, "simulationswitch API debug"));
  67. EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("Game Mode Build Switched To event!"); }, () => { },
  68. EventType.BuildSwitchedTo, "buildswitch API debug"));
  69. // debug/test commands
  70. if (Dependency.Hell("ExtraCommands"))
  71. {
  72. CommandBuilder.Builder()
  73. .Name("Exit")
  74. .Description("Close Gamecraft immediately, without any prompts")
  75. .Action(() => { UnityEngine.Application.Quit(); })
  76. .Build();
  77. CommandBuilder.Builder()
  78. .Name("SetFOV")
  79. .Description("Set the player camera's field of view")
  80. .Action((float d) => { UnityEngine.Camera.main.fieldOfView = d; })
  81. .Build();
  82. CommandBuilder.Builder()
  83. .Name("MoveLastBlock")
  84. .Description("Move the most-recently-placed block, and any connected blocks by the given offset")
  85. .Action((float x, float y, float z) => {
  86. bool success = GamecraftModdingAPI.Blocks.Movement.MoveConnectedBlocks(
  87. GamecraftModdingAPI.Blocks.BlockIdentifiers.LatestBlockID,
  88. new Unity.Mathematics.float3(x, y, z));
  89. if (!success)
  90. {
  91. GamecraftModdingAPI.Utility.Logging.CommandLogError("Blocks can only be moved in Build mode!");
  92. }
  93. }).Build();
  94. CommandBuilder.Builder()
  95. .Name("PlaceAluminium")
  96. .Description("Place a block of aluminium at the given coordinates")
  97. .Action((float x, float y, float z) => { Blocks.Placement.PlaceBlock(Blocks.BlockIDs.AluminiumCube, new Unity.Mathematics.float3(x, y, z)); })
  98. .Build();
  99. System.Random random = new System.Random(); // for command below
  100. CommandBuilder.Builder()
  101. .Name("RandomizeSignalsInputs")
  102. .Description("Do the thing")
  103. .Action(() => {
  104. if (!GameState.IsSimulationMode())
  105. {
  106. Logging.CommandLogError("You must be in simulation mode for this to work!");
  107. return;
  108. }
  109. Tasks.Repeatable task = new Tasks.Repeatable(
  110. () => {
  111. uint count = 0;
  112. EGID[] eBlocks = Blocks.Signals.GetElectricBlocks();
  113. for (uint i = 0u; i < eBlocks.Length; i++)
  114. {
  115. uint[] ids = Blocks.Signals.GetSignalIDs(eBlocks[i]);
  116. for (uint j = 0u; j < ids.Length; j++)
  117. {
  118. Blocks.Signals.SetSignalByID(ids[j], (float)random.NextDouble());
  119. count++;
  120. }
  121. }
  122. Logging.MetaDebugLog($"Did the thing on {count} inputs");
  123. },
  124. () => { return GameState.IsSimulationMode(); });
  125. Tasks.Scheduler.Schedule(task);
  126. }).Build();
  127. /*
  128. CommandManager.AddCommand(new SimpleCustomCommandEngine<float>((float d) => { UnityEngine.Camera.main.fieldOfView = d; },
  129. "SetFOV", "Set the player camera's field of view"));
  130. CommandManager.AddCommand(new SimpleCustomCommandEngine<float, float, float>(
  131. (x, y, z) => {
  132. bool success = GamecraftModdingAPI.Blocks.Movement.MoveConnectedBlocks(
  133. GamecraftModdingAPI.Blocks.BlockIdentifiers.LatestBlockID,
  134. new Unity.Mathematics.float3(x, y, z));
  135. if (!success)
  136. {
  137. GamecraftModdingAPI.Utility.Logging.CommandLogError("Blocks can only be moved in Build mode!");
  138. }
  139. }, "MoveLastBlock", "Move the most-recently-placed block, and any connected blocks by the given offset"));
  140. CommandManager.AddCommand(new SimpleCustomCommandEngine<float, float, float>(
  141. (x, y, z) => { Blocks.Placement.PlaceBlock(Blocks.BlockIDs.AluminiumCube, new Unity.Mathematics.float3(x, y, z)); },
  142. "PlaceAluminium", "Place a block of aluminium at the given coordinates"));
  143. System.Random random = new System.Random(); // for command below
  144. CommandManager.AddCommand(new SimpleCustomCommandEngine(
  145. () => {
  146. if (!GameState.IsSimulationMode())
  147. {
  148. Logging.CommandLogError("You must be in simulation mode for this to work!");
  149. return;
  150. }
  151. Tasks.Repeatable task = new Tasks.Repeatable(() => {
  152. uint count = 0;
  153. EGID[] eBlocks = Blocks.Signals.GetElectricBlocks();
  154. for (uint i = 0u; i < eBlocks.Length; i++)
  155. {
  156. uint[] ids = Blocks.Signals.GetSignalIDs(eBlocks[i]);
  157. for (uint j = 0u; j < ids.Length; j++)
  158. {
  159. Blocks.Signals.SetSignalByID(ids[j], (float)random.NextDouble());
  160. count++;
  161. }
  162. }
  163. Logging.MetaDebugLog($"Did the thing on {count} inputs");
  164. },
  165. () => { return GameState.IsSimulationMode(); });
  166. Tasks.Scheduler.Schedule(task);
  167. }, "RandomizeSignalsInputs", "Do the thing"));
  168. */
  169. }
  170. // dependency test
  171. if (Dependency.Hell("GamecraftScripting", new Version("0.0.1.0")))
  172. {
  173. Logging.LogWarning("You're in GamecraftScripting dependency hell");
  174. }
  175. else
  176. {
  177. Logging.Log("Compatible GamecraftScripting detected");
  178. }
  179. }
  180. public void OnFixedUpdate() { }
  181. public void OnLateUpdate() { }
  182. public void OnLevelWasInitialized(int level) { }
  183. public void OnLevelWasLoaded(int level) { }
  184. public void OnUpdate() { }
  185. }
  186. }