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.

137 lines
4.1KB

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