From 61184145a914ef360350f4df7263709cee19c5c9 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 10 May 2021 22:45:07 +0200 Subject: [PATCH] Start using new extension methods, code cleanup Removed all of the different block property getter methods --- TechbloxModdingAPI/Block.cs | 8 +- TechbloxModdingAPI/Blocks/BlockEngine.cs | 83 +-------------------- TechbloxModdingAPI/Blocks/MovementEngine.cs | 44 ++++------- TechbloxModdingAPI/Blocks/RotationEngine.cs | 61 ++++++--------- TechbloxModdingAPI/Blocks/SignalEngine.cs | 38 +++------- 5 files changed, 55 insertions(+), 179 deletions(-) diff --git a/TechbloxModdingAPI/Block.cs b/TechbloxModdingAPI/Block.cs index f99ca28..0e97a03 100644 --- a/TechbloxModdingAPI/Block.cs +++ b/TechbloxModdingAPI/Block.cs @@ -254,10 +254,10 @@ namespace TechbloxModdingAPI /// public float3 Position { - get => MovementEngine.GetPosition(Id, InitData); + get => MovementEngine.GetPosition(this); set { - MovementEngine.MoveBlock(Id, InitData, value); + MovementEngine.MoveBlock(this, value); if (blockGroup != null) blockGroup.PosAndRotCalculated = false; BlockEngine.UpdateDisplayedBlock(Id); @@ -269,10 +269,10 @@ namespace TechbloxModdingAPI /// public float3 Rotation { - get => RotationEngine.GetRotation(Id, InitData); + get => RotationEngine.GetRotation(this); set { - RotationEngine.RotateBlock(Id, InitData, value); + RotationEngine.RotateBlock(this, value); if (blockGroup != null) blockGroup.PosAndRotCalculated = false; BlockEngine.UpdateDisplayedBlock(Id); diff --git a/TechbloxModdingAPI/Blocks/BlockEngine.cs b/TechbloxModdingAPI/Blocks/BlockEngine.cs index 7bade49..935e3a1 100644 --- a/TechbloxModdingAPI/Blocks/BlockEngine.cs +++ b/TechbloxModdingAPI/Blocks/BlockEngine.cs @@ -70,75 +70,9 @@ namespace TechbloxModdingAPI.Blocks : entitiesDB.QueryEntity(index, CommonExclusiveGroups.COLOUR_PALETTE_GROUP).Colour; - public OptionalRef GetBlockInfo(EGID blockID) where T : unmanaged, IEntityComponent + public OptionalRef GetBlockInfo(Block block) where T : unmanaged, IEntityComponent { - return entitiesDB.TryQueryEntitiesAndIndex(blockID, out uint index, out var array) - ? new OptionalRef(array, index) - : new OptionalRef(); - } - - public ref T GetBlockInfoViewStruct(EGID blockID) where T : struct, INeedEGID, IEntityViewComponent - { - if (entitiesDB.Exists(blockID)) - return ref entitiesDB.QueryEntity(blockID); - T[] structHolder = new T[1]; //Create something that can be referenced - return ref structHolder[0]; //Gets a default value automatically - } - - public U GetBlockInfo(Block block, Func getter, - U def = default) where T : unmanaged, IEntityComponent - { - if (entitiesDB.Exists(block.Id)) - return getter(entitiesDB.QueryEntity(block.Id)); - return GetBlockInitInfo(block, getter, def); - } - - public U GetBlockInfoViewStruct(Block block, Func getter, - U def = default) where T : struct, IEntityViewComponent - { - if (entitiesDB.Exists(block.Id)) - return getter(entitiesDB.QueryEntity(block.Id)); - return GetBlockInitInfo(block, getter, def); - } - - private U GetBlockInitInfo(Block block, Func getter, U def) where T : struct, IEntityComponent - { - if (block.InitData.Group == null) return def; - var initializer = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference); - if (initializer.Has()) - return getter(initializer.Get()); - return def; - } - - public delegate void Setter(ref T component, U value) where T : struct, IEntityComponent; - - public void SetBlockInfoViewStruct(Block block, Setter setter, U value) where T : struct, IEntityViewComponent - { - if (entitiesDB.Exists(block.Id)) - setter(ref entitiesDB.QueryEntity(block.Id), value); - else - SetBlockInitInfo(block, setter, value); - } - - public void SetBlockInfo(Block block, Setter setter, U value) where T : unmanaged, IEntityComponent - { - if (entitiesDB.Exists(block.Id)) - setter(ref entitiesDB.QueryEntity(block.Id), value); - else - SetBlockInitInfo(block, setter, value); - } - - private void SetBlockInitInfo(Block block, Setter setter, U value) - where T : struct, IEntityComponent - { - if (block.InitData.Group != null) - { - var initializer = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference); - T component = initializer.Has() ? initializer.Get() : default; - ref T structRef = ref component; - setter(ref structRef, value); - initializer.Init(structRef); - } + return entitiesDB.QueryEntityOptional(block); } public void UpdateDisplayedBlock(EGID id) @@ -153,7 +87,8 @@ namespace TechbloxModdingAPI.Blocks internal void UpdatePrefab(Block block, ushort type, byte material, bool flipped) { uint pid = PrefabsID.GetOrCreatePrefabID(type, material, 0, flipped); - entitiesDB.QueryEntityOrDefault() + entitiesDB.QueryEntityOrDefault(block).prefabID = pid; + entitiesDB.QueryEntityOrDefault(block) = new PhysicsPrefabEntityStruct(pid); } public bool BlockExists(EGID blockID) @@ -161,16 +96,6 @@ namespace TechbloxModdingAPI.Blocks return entitiesDB.Exists(blockID); } - public bool GetBlockInfoExists(Block block) where T : struct, IEntityComponent - { - if (entitiesDB.Exists(block.Id)) - return true; - if (block.InitData.Group == null) - return false; - var init = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference); - return init.Has(); - } - public SimBody[] GetSimBodiesFromID(byte id) { var ret = new FasterList(4); diff --git a/TechbloxModdingAPI/Blocks/MovementEngine.cs b/TechbloxModdingAPI/Blocks/MovementEngine.cs index aeeac20..7ecf934 100644 --- a/TechbloxModdingAPI/Blocks/MovementEngine.cs +++ b/TechbloxModdingAPI/Blocks/MovementEngine.cs @@ -34,21 +34,12 @@ namespace TechbloxModdingAPI.Blocks // implementations for Movement static class - internal float3 MoveBlock(EGID blockID, BlockEngine.BlockInitData data, float3 vector) + internal float3 MoveBlock(Block block, float3 vector) { - if (!entitiesDB.Exists(blockID)) - { - if (data.Group == null) return float3.zero; - var init = new EntityInitializer(blockID, data.Group, data.Reference); - init.GetOrCreate().position = vector; - init.GetOrCreate().position = vector; - init.GetOrCreate().position = vector; - return vector; - } - ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity(blockID); - ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity(blockID); - ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity(blockID); - ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity(blockID); + ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntityOrDefault(block); + ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntityOrDefault(block); + ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntityOrDefault(block); + ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntityOrDefault(block); // main (persistent) position posStruct.position = vector; // placement grid position @@ -56,24 +47,21 @@ namespace TechbloxModdingAPI.Blocks // rendered position transStruct.position = vector; // collision position - FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation - { - Value = posStruct.position - }); - entitiesDB.QueryEntity(blockID).isProcessed = false; + if (phyStruct.ID != EGID.Empty) + { //It exists + FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation + { + Value = posStruct.position + }); + } + + entitiesDB.QueryEntityOrDefault(block).isProcessed = false; return posStruct.position; } - internal float3 GetPosition(EGID blockID, BlockEngine.BlockInitData data) + internal float3 GetPosition(Block block) { - if (!entitiesDB.Exists(blockID)) - { - if (data.Group == null) return float3.zero; - var init = new EntityInitializer(blockID, data.Group, data.Reference); - return init.Has() ? init.Get().position : float3.zero; - } - ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity(blockID); - return posStruct.position; + return entitiesDB.QueryEntityOrDefault(block).position; } } } diff --git a/TechbloxModdingAPI/Blocks/RotationEngine.cs b/TechbloxModdingAPI/Blocks/RotationEngine.cs index cef4691..b1e54e5 100644 --- a/TechbloxModdingAPI/Blocks/RotationEngine.cs +++ b/TechbloxModdingAPI/Blocks/RotationEngine.cs @@ -34,55 +34,38 @@ namespace TechbloxModdingAPI.Blocks // implementations for Rotation static class - internal float3 RotateBlock(EGID blockID, BlockEngine.BlockInitData data, Vector3 vector) + internal float3 RotateBlock(Block block, Vector3 vector) { - if (!entitiesDB.Exists(blockID)) - { - if (data.Group == null) return float3.zero; - var init = new EntityInitializer(blockID, data.Group, data.Reference); - init.GetOrCreate().rotation = Quaternion.Euler(vector); - init.GetOrCreate().rotation = Quaternion.Euler(vector); - init.GetOrCreate().rotation = Quaternion.Euler(vector); - return vector; - } - ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity(blockID); - ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity(blockID); - ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity(blockID); - ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity(blockID); - // main (persistent) position + ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntityOrDefault(block); + ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntityOrDefault(block); + ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntityOrDefault(block); + ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntityOrDefault(block); + // main (persistent) rotation Quaternion newRotation = rotStruct.rotation; newRotation.eulerAngles = vector; rotStruct.rotation = newRotation; // placement grid rotation - Quaternion newGridRotation = gridStruct.rotation; - newGridRotation.eulerAngles = vector; - gridStruct.rotation = newGridRotation; - // rendered position - Quaternion newTransRotation = rotStruct.rotation; - newTransRotation.eulerAngles = vector; - transStruct.rotation = newTransRotation; - // collision position - FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Unity.Transforms.Rotation - { - Value = rotStruct.rotation - }); - entitiesDB.QueryEntity(blockID).isProcessed = false; + gridStruct.rotation = newRotation; + // rendered rotation + transStruct.rotation = newRotation; + // collision rotation + if (phyStruct.ID != EGID.Empty) + { //It exists + FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, + new Unity.Transforms.Rotation + { + Value = rotStruct.rotation + }); + } + + entitiesDB.QueryEntityOrDefault(block).isProcessed = false; return ((Quaternion)rotStruct.rotation).eulerAngles; } - internal float3 GetRotation(EGID blockID, BlockEngine.BlockInitData data) + internal float3 GetRotation(Block block) { - if (!entitiesDB.Exists(blockID)) - { - if (data.Group == null) return float3.zero; - var init = new EntityInitializer(blockID, data.Group, data.Reference); - return init.Has() - ? (float3) ((Quaternion) init.Get().rotation).eulerAngles - : float3.zero; - } - - ref RotationEntityStruct rotStruct = ref entitiesDB.QueryEntity(blockID); + ref RotationEntityStruct rotStruct = ref entitiesDB.QueryEntityOrDefault(block); return ((Quaternion) rotStruct.rotation).eulerAngles; } } diff --git a/TechbloxModdingAPI/Blocks/SignalEngine.cs b/TechbloxModdingAPI/Blocks/SignalEngine.cs index 6e40af1..f486463 100644 --- a/TechbloxModdingAPI/Blocks/SignalEngine.cs +++ b/TechbloxModdingAPI/Blocks/SignalEngine.cs @@ -3,6 +3,7 @@ using Svelto.ECS; using Svelto.DataStructures; using Gamecraft.Wires; using TechbloxModdingAPI.Engines; +using TechbloxModdingAPI.Utility; namespace TechbloxModdingAPI.Blocks { @@ -87,8 +88,8 @@ namespace TechbloxModdingAPI.Blocks public ref PortEntityStruct GetPortByOffset(Block block, byte portNumber, bool input) { - BlockPortsStruct bps = GetFromDbOrInitData(block, block.Id, out bool exists); - if (!exists) + var bps = entitiesDB.QueryEntityOptional(block); + if (!bps) { throw new BlockException("Block does not exist"); } @@ -208,8 +209,9 @@ namespace TechbloxModdingAPI.Blocks public EGID MatchBlockInputToPort(Block block, byte portUsage, out bool exists) { - BlockPortsStruct ports = GetFromDbOrInitData(block, block.Id, out exists); - return new EGID(ports.firstInputID + portUsage, NamedExclusiveGroup.Group); + var ports = entitiesDB.QueryEntityOptional(block); + exists = ports; + return new EGID(ports.Get().firstInputID + portUsage, NamedExclusiveGroup.Group); } public EGID MatchBlockInputToPort(EGID block, byte portUsage, out bool exists) @@ -226,8 +228,9 @@ namespace TechbloxModdingAPI.Blocks public EGID MatchBlockOutputToPort(Block block, byte portUsage, out bool exists) { - BlockPortsStruct ports = GetFromDbOrInitData(block, block.Id, out exists); - return new EGID(ports.firstOutputID + portUsage, NamedExclusiveGroup.Group); + var ports = entitiesDB.QueryEntityOptional(block); + exists = ports; + return new EGID(ports.Get().firstOutputID + portUsage, NamedExclusiveGroup.Group); } public EGID MatchBlockOutputToPort(EGID block, byte portUsage, out bool exists) @@ -385,29 +388,6 @@ namespace TechbloxModdingAPI.Blocks return results.ToArray(); } - private ref T GetFromDbOrInitData(Block block, EGID id, out bool exists) where T : unmanaged, IEntityComponent - { - T[] defRef = new T[1]; - if (entitiesDB.Exists(id)) - { - exists = true; - return ref entitiesDB.QueryEntity(id); - } - if (block == null || block.InitData.Group == null) - { - exists = false; - return ref defRef[0]; - } - EntityInitializer initializer = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference); - if (initializer.Has()) - { - exists = true; - return ref initializer.Get(); - } - exists = false; - return ref defRef[0]; - } - private EntityCollection GetSignalStruct(uint signalID, out uint index, bool input = true) { ExclusiveGroup group = input