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.

131 lines
3.5KB

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