From d954060a5ab268921fc1386fee0eaa8b2742a177 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 27 Dec 2020 21:13:49 +0100 Subject: [PATCH] Add ability to change properties of existing blocks And not storing custom block data for now --- GamecraftModdingAPI/Blocks/CustomBlock.cs | 21 +++++++++-- ...tityDescriptor.cs => CustomBlockEngine.cs} | 35 +++++++++++-------- .../Tests/GamecraftModdingAPIPluginTest.cs | 4 +++ 3 files changed, 43 insertions(+), 17 deletions(-) rename GamecraftModdingAPI/Blocks/{CustomBlockEntityDescriptor.cs => CustomBlockEngine.cs} (57%) diff --git a/GamecraftModdingAPI/Blocks/CustomBlock.cs b/GamecraftModdingAPI/Blocks/CustomBlock.cs index c72475e..bafccc2 100644 --- a/GamecraftModdingAPI/Blocks/CustomBlock.cs +++ b/GamecraftModdingAPI/Blocks/CustomBlock.cs @@ -30,7 +30,9 @@ namespace GamecraftModdingAPI.Blocks /// Key: Prefab path /// private static readonly Dictionary CustomBlocks = new Dictionary(); - private static readonly CustomBlockEngine Engine = new CustomBlockEngine(); + //private static readonly CustomBlockEngine Engine = new CustomBlockEngine(); + private static readonly List<(ushort id, Action action)> BlockChangeActions = + new List<(ushort, Action)>(); private static bool _canRegister = true; @@ -58,6 +60,16 @@ namespace GamecraftModdingAPI.Blocks Logging.MetaDebugLog("Registered custom block type " + typeName); } + /// + /// A low-level method for changing any property of an existing block. Use with caution. + /// + /// The block ID + /// An action that modifies a property of the block + public static void ChangeExistingBlock(ushort id, Action modifier) + { + BlockChangeActions.Add((id, modifier)); + } + public CustomBlock(EGID id) : base(id) { /*if (id.groupID != Group) @@ -129,9 +141,12 @@ namespace GamecraftModdingAPI.Blocks }; dataDB.GetValues().Add(cld.ID.ToString(), cld); //The registration needs to happen after the ID has been set dataDB.GetFasterValues().Add(cld.ID, cld); //So can't use the builtin method to create a CubeListData - Engine.RegisterBlock((ushort) cld.ID, key); + //Engine.RegisterBlock((ushort) cld.ID, key); - TODO } + foreach (var (id, action) in BlockChangeActions) + action(dataDB.GetValue(id)); + _canRegister = false; } @@ -173,7 +188,7 @@ namespace GamecraftModdingAPI.Blocks internal new static void Init() { Prepare().RunOn(ExtraLean.UIScheduler); - GameEngineManager.AddGameEngine(Engine); + //GameEngineManager.AddGameEngine(Engine); - TODO: Fix serialization and implement block ID update } /*internal static void OnBlockFactoryObtained(BlockEntityFactory factory) diff --git a/GamecraftModdingAPI/Blocks/CustomBlockEntityDescriptor.cs b/GamecraftModdingAPI/Blocks/CustomBlockEngine.cs similarity index 57% rename from GamecraftModdingAPI/Blocks/CustomBlockEntityDescriptor.cs rename to GamecraftModdingAPI/Blocks/CustomBlockEngine.cs index 7e8ea6e..bb1192b 100644 --- a/GamecraftModdingAPI/Blocks/CustomBlockEntityDescriptor.cs +++ b/GamecraftModdingAPI/Blocks/CustomBlockEngine.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using GamecraftModdingAPI.Engines; using GamecraftModdingAPI.Persistence; using GamecraftModdingAPI.Utility; @@ -9,10 +10,9 @@ using Svelto.ECS.Serialization; namespace GamecraftModdingAPI.Blocks { - public class CustomBlockEngine : IFactoryEngine + /*public class CustomBlockEngine : IFactoryEngine { - public class CustomBlockEntityDescriptor : SerializableEntityDescriptor< - CustomBlockEntityDescriptor._CustomBlockDescriptor> + public class CustomBlockEntityDescriptor : SerializableEntityDescriptor { [HashName("GamecraftModdingAPICustomBlockV0")] public class _CustomBlockDescriptor : IEntityDescriptor @@ -28,30 +28,37 @@ namespace GamecraftModdingAPI.Blocks public void Ready() { - SerializerManager.AddSerializer(new SimpleEntitySerializer(db => - { - var (coll, c) = db.QueryEntities(ApiExclusiveGroups.customBlockGroup); - var egids = new EGID[c]; - for (int i = 0; i < c; i++) - egids[i] = new EGID(coll[i].ID, ApiExclusiveGroups.customBlockGroup); + SerializerManager.AddSerializer( + new SimpleEntitySerializer(db => + { + var (coll, c) = db.QueryEntities(ApiExclusiveGroups.customBlockGroup); + var egids = new EGID[c]; + for (int i = 0; i < c; i++) + egids[i] = new EGID(coll[i].ID, ApiExclusiveGroups.customBlockGroup); - return egids; - })); + return egids; + })); + foreach (var (id, name) in _registeredBlocks) + { + Factory.BuildEntity(id, ApiExclusiveGroups.customBlockGroup) + .Init(new CustomBlock.DataStruct {Name = new ECSString(name), ID = id}); + } } public EntitiesDB entitiesDB { get; set; } + private List<(ushort id, string name)> _registeredBlocks = new List<(ushort, string)>(); + public void Dispose() { } public void RegisterBlock(ushort id, string name) { - Factory.BuildEntity(id, ApiExclusiveGroups.customBlockGroup) - .Init(new CustomBlock.DataStruct {Name = new ECSString(name), ID = id}); + _registeredBlocks.Add((id, name)); } public string Name { get; } = "GamecraftModdingAPICustomBlockEngine"; public bool isRemovable { get; } = false; public IEntityFactory Factory { get; set; } - } + }*/ } \ No newline at end of file diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index dd96226..acb8bc4 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -36,6 +36,7 @@ using UnityEngine.ResourceManagement.ResourceProviders; using Debug = FMOD.Debug; using EventType = GamecraftModdingAPI.Events.EventType; using Label = GamecraftModdingAPI.Interface.IMGUI.Label; +using ScalingPermission = DataLoader.ScalingPermission; namespace GamecraftModdingAPI.Tests { @@ -412,6 +413,9 @@ namespace GamecraftModdingAPI.Tests { Logging.MetaDebugLog("Test custom block catalog not found"); } + + CustomBlock.ChangeExistingBlock((ushort) BlockIDs.TyreS, + cld => cld.scalingPermission = ScalingPermission.NonUniform); #if TEST TestRoot.RunTests(); #endif