Add ability to change properties of existing blocks

And not storing custom block data for now
This commit is contained in:
Norbi Peti 2020-12-27 21:13:49 +01:00
parent fdc47832f4
commit d954060a5a
3 changed files with 43 additions and 17 deletions

View file

@ -30,7 +30,9 @@ namespace GamecraftModdingAPI.Blocks
/// Key: Prefab path /// Key: Prefab path
/// </summary> /// </summary>
private static readonly Dictionary<string, Type> CustomBlocks = new Dictionary<string, Type>(); private static readonly Dictionary<string, Type> CustomBlocks = new Dictionary<string, Type>();
private static readonly CustomBlockEngine Engine = new CustomBlockEngine(); //private static readonly CustomBlockEngine Engine = new CustomBlockEngine();
private static readonly List<(ushort id, Action<CubeListData> action)> BlockChangeActions =
new List<(ushort, Action<CubeListData>)>();
private static bool _canRegister = true; private static bool _canRegister = true;
@ -58,6 +60,16 @@ namespace GamecraftModdingAPI.Blocks
Logging.MetaDebugLog("Registered custom block type " + typeName); Logging.MetaDebugLog("Registered custom block type " + typeName);
} }
/// <summary>
/// A low-level method for changing any property of an existing block. Use with caution.
/// </summary>
/// <param name="id">The block ID</param>
/// <param name="modifier">An action that modifies a property of the block</param>
public static void ChangeExistingBlock(ushort id, Action<CubeListData> modifier)
{
BlockChangeActions.Add((id, modifier));
}
public CustomBlock(EGID id) : base(id) public CustomBlock(EGID id) : base(id)
{ {
/*if (id.groupID != Group) /*if (id.groupID != Group)
@ -129,9 +141,12 @@ namespace GamecraftModdingAPI.Blocks
}; };
dataDB.GetValues<CubeListData>().Add(cld.ID.ToString(), cld); //The registration needs to happen after the ID has been set dataDB.GetValues<CubeListData>().Add(cld.ID.ToString(), cld); //The registration needs to happen after the ID has been set
dataDB.GetFasterValues<CubeListData>().Add(cld.ID, cld); //So can't use the builtin method to create a CubeListData dataDB.GetFasterValues<CubeListData>().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<CubeListData>(id));
_canRegister = false; _canRegister = false;
} }
@ -173,7 +188,7 @@ namespace GamecraftModdingAPI.Blocks
internal new static void Init() internal new static void Init()
{ {
Prepare().RunOn(ExtraLean.UIScheduler); Prepare().RunOn(ExtraLean.UIScheduler);
GameEngineManager.AddGameEngine(Engine); //GameEngineManager.AddGameEngine(Engine); - TODO: Fix serialization and implement block ID update
} }
/*internal static void OnBlockFactoryObtained(BlockEntityFactory factory) /*internal static void OnBlockFactoryObtained(BlockEntityFactory factory)

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using GamecraftModdingAPI.Engines; using GamecraftModdingAPI.Engines;
using GamecraftModdingAPI.Persistence; using GamecraftModdingAPI.Persistence;
using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Utility;
@ -9,10 +10,9 @@ using Svelto.ECS.Serialization;
namespace GamecraftModdingAPI.Blocks namespace GamecraftModdingAPI.Blocks
{ {
public class CustomBlockEngine : IFactoryEngine /*public class CustomBlockEngine : IFactoryEngine
{ {
public class CustomBlockEntityDescriptor : SerializableEntityDescriptor< public class CustomBlockEntityDescriptor : SerializableEntityDescriptor<CustomBlockEntityDescriptor._CustomBlockDescriptor>
CustomBlockEntityDescriptor._CustomBlockDescriptor>
{ {
[HashName("GamecraftModdingAPICustomBlockV0")] [HashName("GamecraftModdingAPICustomBlockV0")]
public class _CustomBlockDescriptor : IEntityDescriptor public class _CustomBlockDescriptor : IEntityDescriptor
@ -28,7 +28,8 @@ namespace GamecraftModdingAPI.Blocks
public void Ready() public void Ready()
{ {
SerializerManager.AddSerializer<CustomBlockEntityDescriptor>(new SimpleEntitySerializer<CustomBlockEntityDescriptor>(db => SerializerManager.AddSerializer<CustomBlockEntityDescriptor>(
new SimpleEntitySerializer<CustomBlockEntityDescriptor>(db =>
{ {
var (coll, c) = db.QueryEntities<CustomBlock.DataStruct>(ApiExclusiveGroups.customBlockGroup); var (coll, c) = db.QueryEntities<CustomBlock.DataStruct>(ApiExclusiveGroups.customBlockGroup);
var egids = new EGID[c]; var egids = new EGID[c];
@ -37,21 +38,27 @@ namespace GamecraftModdingAPI.Blocks
return egids; return egids;
})); }));
foreach (var (id, name) in _registeredBlocks)
{
Factory.BuildEntity<CustomBlockEntityDescriptor>(id, ApiExclusiveGroups.customBlockGroup)
.Init(new CustomBlock.DataStruct {Name = new ECSString(name), ID = id});
}
} }
public EntitiesDB entitiesDB { get; set; } public EntitiesDB entitiesDB { get; set; }
private List<(ushort id, string name)> _registeredBlocks = new List<(ushort, string)>();
public void Dispose() public void Dispose()
{ {
} }
public void RegisterBlock(ushort id, string name) public void RegisterBlock(ushort id, string name)
{ {
Factory.BuildEntity<CustomBlockEntityDescriptor>(id, ApiExclusiveGroups.customBlockGroup) _registeredBlocks.Add((id, name));
.Init(new CustomBlock.DataStruct {Name = new ECSString(name), ID = id});
} }
public string Name { get; } = "GamecraftModdingAPICustomBlockEngine"; public string Name { get; } = "GamecraftModdingAPICustomBlockEngine";
public bool isRemovable { get; } = false; public bool isRemovable { get; } = false;
public IEntityFactory Factory { get; set; } public IEntityFactory Factory { get; set; }
} }*/
} }

View file

@ -36,6 +36,7 @@ using UnityEngine.ResourceManagement.ResourceProviders;
using Debug = FMOD.Debug; using Debug = FMOD.Debug;
using EventType = GamecraftModdingAPI.Events.EventType; using EventType = GamecraftModdingAPI.Events.EventType;
using Label = GamecraftModdingAPI.Interface.IMGUI.Label; using Label = GamecraftModdingAPI.Interface.IMGUI.Label;
using ScalingPermission = DataLoader.ScalingPermission;
namespace GamecraftModdingAPI.Tests namespace GamecraftModdingAPI.Tests
{ {
@ -412,6 +413,9 @@ namespace GamecraftModdingAPI.Tests
{ {
Logging.MetaDebugLog("Test custom block catalog not found"); Logging.MetaDebugLog("Test custom block catalog not found");
} }
CustomBlock.ChangeExistingBlock((ushort) BlockIDs.TyreS,
cld => cld.scalingPermission = ScalingPermission.NonUniform);
#if TEST #if TEST
TestRoot.RunTests(); TestRoot.RunTests();
#endif #endif