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.

134 lines
3.8KB

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