diff --git a/TechbloxModdingAPI/App/GameGameEngine.cs b/TechbloxModdingAPI/App/GameGameEngine.cs index 2c6e177..2bed201 100644 --- a/TechbloxModdingAPI/App/GameGameEngine.cs +++ b/TechbloxModdingAPI/App/GameGameEngine.cs @@ -7,17 +7,17 @@ using Svelto.ECS; using Svelto.Tasks; using Svelto.Tasks.Lean; using RobocraftX.Blocks; +using RobocraftX.Common.Loading; using RobocraftX.ScreenshotTaker; using Techblox.Environment.Transition; using Techblox.GameSelection; using TechbloxModdingAPI.Blocks; using TechbloxModdingAPI.Engines; -using TechbloxModdingAPI.Tasks; using TechbloxModdingAPI.Utility; namespace TechbloxModdingAPI.App { - public class GameGameEngine : IApiEngine + public class GameGameEngine : IApiEngine, IReactOnAddAndRemove { public WrappedHandler EnterGame; @@ -26,9 +26,11 @@ namespace TechbloxModdingAPI.App public string Name => "TechbloxModdingAPIGameInfoMenuEngine"; public bool isRemovable => false; - + public EntitiesDB entitiesDB { set; private get; } + private bool enteredGame; + public void Dispose() { ExitGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID }); @@ -37,14 +39,7 @@ namespace TechbloxModdingAPI.App public void Ready() { - EnteringGame().RunOn(Scheduler.leanRunner); - } - - private IEnumerator EnteringGame() - { - yield return new WaitForSubmissionEnumerator(GameLoadedEnginePatch.Scheduler).Continue(); - EnterGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID }); - IsInGame = true; + enteredGame = true; } // game functionality @@ -139,5 +134,17 @@ namespace TechbloxModdingAPI.App { return entitiesDB.QueryEntity(GameSelectionConstants.GameSelectionEGID); } - } + + public void Add(ref LoadingActionEntityStruct entityComponent, EGID egid) + { + } + + public void Remove(ref LoadingActionEntityStruct entityComponent, EGID egid) + { // Finished loading + if (!enteredGame) return; + EnterGame.Invoke(this, new GameEventArgs { GameName = GetGameData().saveName, GamePath = GetGameData().gameID }); + IsInGame = true; + enteredGame = false; + } + } } diff --git a/TechbloxModdingAPI/Block.cs b/TechbloxModdingAPI/Block.cs index 0d730c5..ec1147a 100644 --- a/TechbloxModdingAPI/Block.cs +++ b/TechbloxModdingAPI/Block.cs @@ -327,7 +327,10 @@ namespace TechbloxModdingAPI : throw new BlockTypeException("Unknown block type! Could not set default material."); if (!FullGameFields._dataDb.ContainsKey(val)) throw new BlockException($"Block material {value} does not exist!"); - BlockEngine.GetBlockInfo(this).materialId = val; + ref var comp = ref BlockEngine.GetBlockInfo(this); + if (comp.materialId == val) + return; + comp.materialId = val; BlockEngine.UpdatePrefab(this, val, Flipped); //The default causes the screen to go black } } diff --git a/TechbloxModdingAPI/Blocks/BlockTests.cs b/TechbloxModdingAPI/Blocks/BlockTests.cs index 198011c..4bc29db 100644 --- a/TechbloxModdingAPI/Blocks/BlockTests.cs +++ b/TechbloxModdingAPI/Blocks/BlockTests.cs @@ -5,7 +5,9 @@ using System.Reflection; using DataLoader; using Svelto.Tasks; +using Svelto.Tasks.Enumerators; using Unity.Mathematics; +using UnityEngine; using TechbloxModdingAPI.App; using TechbloxModdingAPI.Tests; @@ -86,6 +88,12 @@ namespace TechbloxModdingAPI.Blocks foreach (var property in block.GetType().GetProperties()) { if (property.Name == "Material" || property.Name == "Flipped") continue; // TODO: Crashes in game + if (property.Name == "Material" || property.Name == "Flipped") + { + Console.WriteLine("Block type: "+block.Type); + Console.WriteLine("Will set " + property.Name); + yield return new WaitForSecondsEnumerator(1).Continue(); + } //Includes specialised block properties if (property.SetMethod == null) continue; var testValues = new (Type, object, Predicate)[] diff --git a/TechbloxModdingAPI/Blocks/Engines/BlockEngine.cs b/TechbloxModdingAPI/Blocks/Engines/BlockEngine.cs index 26be582..c7c84cf 100644 --- a/TechbloxModdingAPI/Blocks/Engines/BlockEngine.cs +++ b/TechbloxModdingAPI/Blocks/Engines/BlockEngine.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; using System.Linq; +using System.Reflection; +using HarmonyLib; using Gamecraft.ColourPalette; using Gamecraft.TimeRunning; @@ -11,8 +13,8 @@ using RobocraftX.Rendering; using RobocraftX.Rendering.GPUI; using Svelto.DataStructures; using Svelto.ECS; -using Svelto.ECS.EntityStructs; using Svelto.ECS.Hybrid; +using Techblox.BuildingDrone; using Unity.Mathematics; using TechbloxModdingAPI.Engines; @@ -92,10 +94,7 @@ namespace TechbloxModdingAPI.Blocks.Engines public void UpdateDisplayedBlock(EGID id) { if (!BlockExists(id)) return; - var pos = entitiesDB.QueryEntity(id); - var rot = entitiesDB.QueryEntity(id); - var scale = entitiesDB.QueryEntity(id); - entitiesDB.QueryEntity(id).matrix = float4x4.TRS(pos.position, rot.rotation, scale.scale); + RenderingPatch.UpdateBlocks(); } internal void UpdatePrefab(Block block, byte material, bool flipped) @@ -115,7 +114,17 @@ namespace TechbloxModdingAPI.Blocks.Engines PrefabsID.GetOrCreatePrefabID((ushort) prefabAssetID, material, 1, flipped); entitiesDB.QueryEntityOrDefault(block).prefabID = prefabId; if (block.Exists) + { + entitiesDB.PublishEntityChange(block.Id); entitiesDB.PublishEntityChange(block.Id); + + ref BuildingActionComponent local = + ref entitiesDB.QueryEntity(BuildingDroneUtility + .GetLocalBuildingDrone(entitiesDB).ToEGID(entitiesDB)); + local.buildAction = BuildAction.ChangeMaterial; + local.targetPosition = block.Position; + this.entitiesDB.PublishEntityChange(local.ID); + } //Phyiscs prefab: prefabAssetID, set on block creation from the CubeListData } @@ -239,5 +248,27 @@ namespace TechbloxModdingAPI.Blocks.Engines return entitiesDB; } #endif + + [HarmonyPatch] + public static class RenderingPatch + { + private static ComputeRenderingEntitiesMatricesEngine Engine; + + public static void Postfix(ComputeRenderingEntitiesMatricesEngine __instance) + { + Engine = __instance; + } + + public static MethodBase TargetMethod() + { + return typeof(ComputeRenderingEntitiesMatricesEngine).GetConstructors()[0]; + } + + public static void UpdateBlocks() + { + var data = new RenderingDataStruct(); + Engine.Add(ref data, new EGID(0, CommonExclusiveGroups.BUTTON_BLOCK_GROUP)); + } + } } } \ No newline at end of file diff --git a/TechbloxModdingAPI/Player.cs b/TechbloxModdingAPI/Player.cs index b3b0536..2ce88f1 100644 --- a/TechbloxModdingAPI/Player.cs +++ b/TechbloxModdingAPI/Player.cs @@ -360,8 +360,8 @@ namespace TechbloxModdingAPI /// /// The player's mode in time stopped mode, determining what they place. /// - public PlayerBuildingMode BuildingMode => (PlayerBuildingMode) playerEngine - .GetCharacterStruct(Id).Get().timeStoppedContext; + public PlayerBuildingMode BuildingMode => (PlayerBuildingMode)Math.Log((double)playerEngine + .GetCharacterStruct(Id).Get().timeStoppedContext, 2); // It's a bit field in game now /// /// Whether the player is sprinting. @@ -522,6 +522,11 @@ namespace TechbloxModdingAPI return (int) Id; } + public override string ToString() + { + return $"{nameof(Type)}: {Type}, {nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}, {nameof(Mass)}: {Mass}"; + } + // internal methods internal static void Init() diff --git a/TechbloxModdingAPI/Players/PlayerBuildingMode.cs b/TechbloxModdingAPI/Players/PlayerBuildingMode.cs index 5e75555..7bc25a8 100644 --- a/TechbloxModdingAPI/Players/PlayerBuildingMode.cs +++ b/TechbloxModdingAPI/Players/PlayerBuildingMode.cs @@ -6,6 +6,7 @@ ColourMode, ConfigMode, BlueprintMode, - MaterialMode + MaterialMode, + LandscapeMode } } \ No newline at end of file