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