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.

126 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. 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_RUNS_IN_TIME_STOPPED_AND_RUNNING);
  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. EntityCollection<DBEntityStruct> blocks = entitiesDB.QueryEntities<DBEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  82. if (filter == BlockIDs.Invalid)
  83. {
  84. EGID[] blockEGIDs = new EGID[blocks.count];
  85. for (uint b = 0; b < blocks.count; b++)
  86. {
  87. blockEGIDs[b] = blocks[b].ID;
  88. }
  89. return blockEGIDs;
  90. }
  91. else
  92. {
  93. uint dbidFilter = (uint)filter;
  94. List<EGID> blockEGIDs = new List<EGID>();
  95. for (uint b = 0; b < blocks.count; b++)
  96. {
  97. if (blocks[b].DBID == dbidFilter)
  98. {
  99. blockEGIDs.Add(blocks[b].ID);
  100. }
  101. }
  102. return blockEGIDs.ToArray();
  103. }
  104. }
  105. }
  106. }