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.

225 lines
10KB

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