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.

143 lines
4.3KB

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