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.

142 lines
4.1KB

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