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.

144 lines
4.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using HarmonyLib;
  4. using RobocraftX;
  5. using RobocraftX.Common;
  6. using RobocraftX.Schedulers;
  7. using RobocraftX.SimulationModeState;
  8. using Svelto.ECS;
  9. using Svelto.Tasks;
  10. using Svelto.Tasks.Lean;
  11. using RobocraftX.Blocks;
  12. using RobocraftX.ScreenshotTaker;
  13. using TechbloxModdingAPI.Blocks;
  14. using TechbloxModdingAPI.Engines;
  15. using TechbloxModdingAPI.Utility;
  16. namespace TechbloxModdingAPI.App
  17. {
  18. public class GameGameEngine : IApiEngine
  19. {
  20. public event EventHandler<GameEventArgs> EnterGame;
  21. public event EventHandler<GameEventArgs> ExitGame;
  22. public string Name => "TechbloxModdingAPIGameInfoMenuEngine";
  23. public bool isRemovable => false;
  24. public EntitiesDB entitiesDB { set; private get; }
  25. public void Dispose()
  26. {
  27. ExceptionUtil.InvokeEvent(ExitGame, this, new GameEventArgs { GameName = GameMode.SaveGameDetails.Name, GamePath = GameMode.SaveGameDetails.Folder });
  28. IsInGame = false;
  29. }
  30. public void Ready()
  31. {
  32. ExceptionUtil.InvokeEvent(EnterGame, this, new GameEventArgs { GameName = GameMode.SaveGameDetails.Name, GamePath = GameMode.SaveGameDetails.Folder });
  33. IsInGame = true;
  34. }
  35. // game functionality
  36. public bool IsInGame
  37. {
  38. get;
  39. private set;
  40. } = false;
  41. public void ExitCurrentGame(bool async = false)
  42. {
  43. if (async)
  44. {
  45. ExitCurrentGameAsync().RunOn(Lean.EveryFrameStepRunner_TimeRunningAndStopped);
  46. }
  47. else
  48. {
  49. entitiesDB.QueryEntity<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID).WantsToQuit = true;
  50. entitiesDB.PublishEntityChange<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  51. }
  52. }
  53. public IEnumerator<TaskContract> ExitCurrentGameAsync()
  54. {
  55. /*
  56. while (Lean.EveryFrameStepRunner_RUNS_IN_TIME_STOPPED_AND_RUNNING.isStopping) { yield return Yield.It; }
  57. AccessTools.Method(typeof(FullGameCompositionRoot), "SwitchToMenu").Invoke(FullGameFields.Instance, new object[0]);*/
  58. yield return Yield.It;
  59. entitiesDB.QueryEntity<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID).WantsToQuit = true;
  60. entitiesDB.PublishEntityChange<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  61. }
  62. public void SaveCurrentGame()
  63. {
  64. ref GameSceneEntityStruct gses = ref entitiesDB.QueryEntity<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  65. gses.LoadAfterSaving = false;
  66. gses.SaveNow = true;
  67. entitiesDB.PublishEntityChange<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  68. }
  69. public bool IsTimeRunningMode()
  70. {
  71. return TimeRunningModeUtil.IsTimeRunningMode(entitiesDB);
  72. }
  73. public bool IsTimeStoppedMode()
  74. {
  75. return TimeRunningModeUtil.IsTimeStoppedMode(entitiesDB);
  76. }
  77. public void ToggleTimeMode()
  78. {
  79. if (!entitiesDB.FoundInGroups<BlockTagEntityStruct>())
  80. throw new AppStateException("At least one block must exist in the world to enter simulation");
  81. TimeRunningModeUtil.ToggleTimeRunningState(entitiesDB);
  82. }
  83. public EGID[] GetAllBlocksInGame(BlockIDs filter = BlockIDs.Invalid)
  84. {
  85. var allBlocks = entitiesDB.QueryEntities<BlockTagEntityStruct>();
  86. List<EGID> blockEGIDs = new List<EGID>();
  87. if (filter == BlockIDs.Invalid)
  88. {
  89. foreach (var (blocks, _) in allBlocks)
  90. {
  91. var buffer = blocks.ToBuffer().buffer;
  92. for (int i = 0; i < buffer.capacity; i++)
  93. blockEGIDs.Add(buffer[i].ID);
  94. }
  95. return blockEGIDs.ToArray();
  96. }
  97. else
  98. {
  99. foreach (var (blocks, _) in allBlocks)
  100. {
  101. var array = blocks.ToBuffer().buffer;
  102. for (var index = 0; index < array.capacity; index++)
  103. {
  104. var block = array[index];
  105. uint dbid = entitiesDB.QueryEntity<DBEntityStruct>(block.ID).DBID;
  106. if (dbid == (ulong) filter)
  107. blockEGIDs.Add(block.ID);
  108. }
  109. }
  110. return blockEGIDs.ToArray();
  111. }
  112. }
  113. public void EnableScreenshotTaker()
  114. {
  115. ref var local = ref entitiesDB.QueryEntity<ScreenshotModeEntityStruct>(ScreenshotTakerEgids.ScreenshotTaker);
  116. if (local.enabled)
  117. return;
  118. local.enabled = true;
  119. entitiesDB.PublishEntityChange<ScreenshotModeEntityStruct>(ScreenshotTakerEgids.ScreenshotTaker);
  120. }
  121. }
  122. }