using System; using System.Collections.Generic; using RobocraftX.Common; using RobocraftX.Schedulers; using RobocraftX.SimulationModeState; using Svelto.ECS; using Svelto.Tasks; using Svelto.Tasks.Lean; using RobocraftX.Blocks; using RobocraftX.ScreenshotTaker; using TechbloxModdingAPI.Blocks; using TechbloxModdingAPI.Engines; using TechbloxModdingAPI.Utility; namespace TechbloxModdingAPI.App { public class GameGameEngine : IApiEngine { public WrappedHandler EnterGame; public WrappedHandler ExitGame; public string Name => "TechbloxModdingAPIGameInfoMenuEngine"; public bool isRemovable => false; public EntitiesDB entitiesDB { set; private get; } public void Dispose() { ExitGame.Invoke(this, new GameEventArgs { GameName = GameMode.SaveGameDetails.Name, GamePath = GameMode.SaveGameDetails.Folder }); IsInGame = false; } public void Ready() { EnterGame.Invoke(this, new GameEventArgs { GameName = GameMode.SaveGameDetails.Name, GamePath = GameMode.SaveGameDetails.Folder }); IsInGame = true; } // game functionality public bool IsInGame { get; private set; } = false; public void ExitCurrentGame(bool async = false) { if (async) { ExitCurrentGameAsync().RunOn(Lean.EveryFrameStepRunner_TimeRunningAndStopped); } else { entitiesDB.QueryEntity(CommonExclusiveGroups.GameSceneEGID).WantsToQuit = true; entitiesDB.PublishEntityChange(CommonExclusiveGroups.GameSceneEGID); } } public IEnumerator ExitCurrentGameAsync() { /* while (Lean.EveryFrameStepRunner_RUNS_IN_TIME_STOPPED_AND_RUNNING.isStopping) { yield return Yield.It; } AccessTools.Method(typeof(FullGameCompositionRoot), "SwitchToMenu").Invoke(FullGameFields.Instance, new object[0]);*/ yield return Yield.It; entitiesDB.QueryEntity(CommonExclusiveGroups.GameSceneEGID).WantsToQuit = true; entitiesDB.PublishEntityChange(CommonExclusiveGroups.GameSceneEGID); } public void SaveCurrentGame() { ref GameSceneEntityStruct gses = ref entitiesDB.QueryEntity(CommonExclusiveGroups.GameSceneEGID); gses.LoadAfterSaving = false; gses.SaveNow = true; entitiesDB.PublishEntityChange(CommonExclusiveGroups.GameSceneEGID); } public bool IsTimeRunningMode() { return TimeRunningModeUtil.IsTimeRunningMode(entitiesDB); } public bool IsTimeStoppedMode() { return TimeRunningModeUtil.IsTimeStoppedMode(entitiesDB); } public void ToggleTimeMode() { if (!entitiesDB.FoundInGroups()) throw new AppStateException("At least one block must exist in the world to enter simulation"); TimeRunningModeUtil.ToggleTimeRunningState(entitiesDB); } public EGID[] GetAllBlocksInGame(BlockIDs filter = BlockIDs.Invalid) { var allBlocks = entitiesDB.QueryEntities(); List blockEGIDs = new List(); if (filter == BlockIDs.Invalid) { foreach (var (blocks, _) in allBlocks) { var buffer = blocks.ToBuffer().buffer; for (int i = 0; i < buffer.capacity; i++) blockEGIDs.Add(buffer[i].ID); } return blockEGIDs.ToArray(); } else { foreach (var (blocks, _) in allBlocks) { var array = blocks.ToBuffer().buffer; for (var index = 0; index < array.capacity; index++) { var block = array[index]; uint dbid = entitiesDB.QueryEntity(block.ID).DBID; if (dbid == (ulong) filter) blockEGIDs.Add(block.ID); } } return blockEGIDs.ToArray(); } } public void EnableScreenshotTaker() { ref var local = ref entitiesDB.QueryEntity(ScreenshotTakerEgids.ScreenshotTaker); if (local.enabled) return; local.enabled = true; entitiesDB.PublishEntityChange(ScreenshotTakerEgids.ScreenshotTaker); } } }