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.

133 lines
3.6KB

  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 GamecraftModdingAPI.Blocks;
  12. using GamecraftModdingAPI.Engines;
  13. using GamecraftModdingAPI.Utility;
  14. using RobocraftX.Blocks;
  15. namespace GamecraftModdingAPI.App
  16. {
  17. public class GameGameEngine : IApiEngine
  18. {
  19. public event EventHandler<GameEventArgs> EnterGame;
  20. public event EventHandler<GameEventArgs> ExitGame;
  21. public string Name => "GamecraftModdingAPIGameInfoMenuEngine";
  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. TimeRunningModeUtil.ToggleTimeRunningState(entitiesDB);
  79. }
  80. public EGID[] GetAllBlocksInGame(BlockIDs filter = BlockIDs.Invalid)
  81. {
  82. var allBlocks = entitiesDB.QueryEntities<BlockTagEntityStruct>();
  83. List<EGID> blockEGIDs = new List<EGID>();
  84. if (filter == BlockIDs.Invalid)
  85. {
  86. foreach (var (blocks, _) in allBlocks)
  87. {
  88. var buffer = blocks.ToBuffer().buffer;
  89. for (int i = 0; i < buffer.capacity; i++)
  90. blockEGIDs.Add(buffer[i].ID);
  91. }
  92. return blockEGIDs.ToArray();
  93. }
  94. else
  95. {
  96. foreach (var (blocks, _) in allBlocks)
  97. {
  98. var array = blocks.ToBuffer().buffer;
  99. for (var index = 0; index < array.capacity; index++)
  100. {
  101. var block = array[index];
  102. uint dbid = entitiesDB.QueryEntity<DBEntityStruct>(block.ID).DBID;
  103. if (dbid == (ulong) filter)
  104. blockEGIDs.Add(block.ID);
  105. }
  106. }
  107. return blockEGIDs.ToArray();
  108. }
  109. }
  110. }
  111. }