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.

178 lines
5.4KB

  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.Common.Loading;
  11. using RobocraftX.ScreenshotTaker;
  12. using Techblox.Environment.Transition;
  13. using Techblox.GameSelection;
  14. using TechbloxModdingAPI.Blocks;
  15. using TechbloxModdingAPI.Engines;
  16. using TechbloxModdingAPI.Players;
  17. using TechbloxModdingAPI.Utility;
  18. namespace TechbloxModdingAPI.App
  19. {
  20. public class GameGameEngine : IApiEngine, IReactOnAddAndRemove<LoadingActionEntityStruct>
  21. {
  22. public WrappedHandler<GameEventArgs> EnterGame;
  23. public WrappedHandler<GameEventArgs> ExitGame;
  24. public string Name => "TechbloxModdingAPIGameInfoMenuEngine";
  25. public bool isRemovable => false;
  26. public EntitiesDB entitiesDB { set; private get; }
  27. private bool enteredGame;
  28. private bool loadingFinished;
  29. private bool playerJoined;
  30. public void Dispose()
  31. {
  32. ExitGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID });
  33. IsInGame = false;
  34. loadingFinished = false;
  35. playerJoined = false;
  36. enteredGame = false;
  37. }
  38. public void Ready()
  39. {
  40. enteredGame = true;
  41. Player.Joined += OnPlayerJoined;
  42. }
  43. private void OnPlayerJoined(object sender, PlayerEventArgs args)
  44. {
  45. Console.WriteLine("Player joined: " + args.PlayerId + " asd");
  46. if (args.Player.Type != PlayerType.Local) return;
  47. Console.WriteLine("Player joined is local asd");
  48. playerJoined = true;
  49. Player.Joined -= OnPlayerJoined;
  50. CheckJoinEvent();
  51. }
  52. // game functionality
  53. public bool IsInGame
  54. {
  55. get;
  56. private set;
  57. } = false;
  58. public void ExitCurrentGame(bool async = false)
  59. {
  60. if (async)
  61. {
  62. ExitCurrentGameAsync().RunOn(ClientLean.EveryFrameStepRunner_TimeRunningAndStopped);
  63. }
  64. else
  65. {
  66. entitiesDB.QueryEntity<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID).WantsToQuit = true;
  67. entitiesDB.PublishEntityChange<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  68. }
  69. }
  70. public IEnumerator<TaskContract> ExitCurrentGameAsync()
  71. {
  72. /*
  73. while (Lean.EveryFrameStepRunner_RUNS_IN_TIME_STOPPED_AND_RUNNING.isStopping) { yield return Yield.It; }
  74. AccessTools.Method(typeof(FullGameCompositionRoot), "SwitchToMenu").Invoke(FullGameFields.Instance, new object[0]);*/
  75. yield return Yield.It;
  76. entitiesDB.QueryEntity<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID).WantsToQuit = true;
  77. entitiesDB.PublishEntityChange<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  78. }
  79. public void SaveCurrentGame()
  80. {
  81. ref GameSceneEntityStruct gses = ref entitiesDB.QueryEntity<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  82. gses.LoadAfterSaving = false;
  83. gses.SaveNow = true;
  84. entitiesDB.PublishEntityChange<GameSceneEntityStruct>(CommonExclusiveGroups.GameSceneEGID);
  85. }
  86. public bool IsTimeRunningMode()
  87. {
  88. return TimeRunningModeUtil.IsTimeRunningMode(entitiesDB);
  89. }
  90. public bool IsTimeStoppedMode()
  91. {
  92. return TimeRunningModeUtil.IsTimeStoppedMode(entitiesDB);
  93. }
  94. public void ToggleTimeMode()
  95. {
  96. if (!entitiesDB.FoundInGroups<BlockTagEntityStruct>())
  97. throw new AppStateException("At least one block must exist in the world to enter simulation");
  98. SwitchAnimationUtil.Start(entitiesDB);
  99. }
  100. public EGID[] GetAllBlocksInGame(BlockIDs filter = BlockIDs.Invalid)
  101. {
  102. var allBlocks = entitiesDB.QueryEntities<BlockTagEntityStruct>();
  103. List<EGID> blockEGIDs = new List<EGID>();
  104. foreach (var (blocks, _) in allBlocks)
  105. {
  106. var (buffer, count) = blocks.ToBuffer();
  107. for (int i = 0; i < count; i++)
  108. {
  109. uint dbid;
  110. if (filter == BlockIDs.Invalid)
  111. dbid = (uint)filter;
  112. else
  113. dbid = entitiesDB.QueryEntity<DBEntityStruct>(buffer[i].ID).DBID;
  114. if (dbid == (ulong)filter)
  115. blockEGIDs.Add(buffer[i].ID);
  116. }
  117. }
  118. return blockEGIDs.ToArray();
  119. }
  120. public void EnableScreenshotTaker()
  121. {
  122. ref var local = ref entitiesDB.QueryEntity<ScreenshotModeEntityStruct>(ScreenshotTakerEgids.ScreenshotTaker);
  123. if (local.enabled)
  124. return;
  125. local.enabled = true;
  126. entitiesDB.PublishEntityChange<ScreenshotModeEntityStruct>(ScreenshotTakerEgids.ScreenshotTaker);
  127. }
  128. public GameSelectionComponent GetGameData()
  129. {
  130. return entitiesDB.QueryEntity<GameSelectionComponent>(GameSelectionConstants.GameSelectionEGID);
  131. }
  132. public void Add(ref LoadingActionEntityStruct entityComponent, EGID egid)
  133. {
  134. }
  135. public void Remove(ref LoadingActionEntityStruct entityComponent, EGID egid)
  136. { // Finished loading
  137. if (!enteredGame) return;
  138. enteredGame = false;
  139. loadingFinished = true;
  140. Console.WriteLine("Loading finished - asd");
  141. CheckJoinEvent();
  142. }
  143. private void CheckJoinEvent()
  144. {
  145. Console.WriteLine($"Check: {loadingFinished} {playerJoined}");
  146. if (!loadingFinished || !playerJoined) return;
  147. EnterGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID });
  148. IsInGame = true;
  149. }
  150. }
  151. }