Store custom block IDs in save files

This commit is contained in:
Norbi Peti 2020-12-26 01:59:06 +01:00
parent 95574a50f8
commit fdc47832f4
4 changed files with 97 additions and 9 deletions

View file

@ -6,10 +6,12 @@ using System.Reflection;
using HarmonyLib;
using DataLoader;
using RobocraftX.Blocks;
using RobocraftX.Rendering;
using RobocraftX.Schedulers;
using Svelto.ECS;
using Svelto.ECS.Experimental;
using Svelto.Tasks;
using Svelto.Tasks.ExtraLean;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Material = UnityEngine.Material;
@ -18,13 +20,17 @@ using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Blocks
{
/// <summary>
/// Experimental support for adding custom blocks to the game.
/// </summary>
public class CustomBlock : Block
{
private static ushort nextID = 500;
/// <summary>
/// Key: Prefab path
/// </summary>
private static 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 bool _canRegister = true;
@ -48,7 +54,7 @@ namespace GamecraftModdingAPI.Blocks
throw new ArgumentException("The given block type doesn't have a concrete full name.");
if (!File.Exists(attr.Catalog))
throw new FileNotFoundException("The specified catalog cannot be found for " + typeName);
_customBlocks.Add(attr.AssetPath, type);
CustomBlocks.Add(attr.AssetPath, type);
Logging.MetaDebugLog("Registered custom block type " + typeName);
}
@ -79,7 +85,7 @@ namespace GamecraftModdingAPI.Blocks
for (var index = 0; index < prefabs.Count; index++)
{
if (_customBlocks.ContainsKey(prefabData[index].prefabName)) //This is a custom block
if (CustomBlocks.ContainsKey(prefabData[index].prefabName)) //This is a custom block
prefabs[index].GetComponentsInChildren<MeshRenderer>()[0].sharedMaterials = materials;
}
}
@ -96,7 +102,7 @@ namespace GamecraftModdingAPI.Blocks
public static void Prefix(IDataDB dataDB)
{
//var abd = dataDB.GetValue<CubeListData>((int) BlockIDs.AluminiumCube);
foreach (var (key, type) in _customBlocks)
foreach (var (key, type) in CustomBlocks)
{
var attr = type.GetCustomAttribute<CustomBlockAttribute>();
var cld = new CubeListData
@ -123,6 +129,7 @@ namespace GamecraftModdingAPI.Blocks
};
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
Engine.RegisterBlock((ushort) cld.ID, key);
}
_canRegister = false;
@ -150,9 +157,9 @@ namespace GamecraftModdingAPI.Blocks
}
}*/
internal static IEnumerator Prepare()
private static IEnumerator Prepare()
{ //Should be pretty quick
foreach (var type in _customBlocks.Values)
foreach (var type in CustomBlocks.Values)
{
var attr = type.GetCustomAttribute<CustomBlockAttribute>();
Logging.Log("Loading custom block catalog " + attr.Catalog);
@ -163,11 +170,23 @@ namespace GamecraftModdingAPI.Blocks
}
}
internal new static void Init()
{
Prepare().RunOn(ExtraLean.UIScheduler);
GameEngineManager.AddGameEngine(Engine);
}
/*internal static void OnBlockFactoryObtained(BlockEntityFactory factory)
{
var builders = (Dictionary<CubeCategory, IBlockBuilder>)
AccessTools.Field(factory.GetType(), "_blockBuilders").GetValue(factory);
builders.Add((CubeCategory) 1000, new BlockBuilder<StandardBlockEntityDescriptor>(Group));
}*/
internal struct DataStruct : IEntityComponent
{
public ECSString Name;
public ushort ID;
}
}
}

View file

@ -0,0 +1,57 @@
using System;
using GamecraftModdingAPI.Engines;
using GamecraftModdingAPI.Persistence;
using GamecraftModdingAPI.Utility;
using RobocraftX.Common;
using Svelto.ECS;
using Svelto.ECS.Experimental;
using Svelto.ECS.Serialization;
namespace GamecraftModdingAPI.Blocks
{
public class CustomBlockEngine : IFactoryEngine
{
public class CustomBlockEntityDescriptor : SerializableEntityDescriptor<
CustomBlockEntityDescriptor._CustomBlockDescriptor>
{
[HashName("GamecraftModdingAPICustomBlockV0")]
public class _CustomBlockDescriptor : IEntityDescriptor
{
public IComponentBuilder[] componentsToBuild { get; } =
{
new SerializableComponentBuilder<SerializationType, CustomBlock.DataStruct>(
((int) SerializationType.Network, new DefaultSerializer<CustomBlock.DataStruct>()),
((int) SerializationType.Storage, new DefaultSerializer<CustomBlock.DataStruct>()))
};
}
}
public void Ready()
{
SerializerManager.AddSerializer<CustomBlockEntityDescriptor>(new SimpleEntitySerializer<CustomBlockEntityDescriptor>(db =>
{
var (coll, c) = db.QueryEntities<CustomBlock.DataStruct>(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;
}));
}
public EntitiesDB entitiesDB { get; set; }
public void Dispose()
{
}
public void RegisterBlock(ushort id, string name)
{
Factory.BuildEntity<CustomBlockEntityDescriptor>(id, ApiExclusiveGroups.customBlockGroup)
.Init(new CustomBlock.DataStruct {Name = new ECSString(name), ID = id});
}
public string Name { get; } = "GamecraftModdingAPICustomBlockEngine";
public bool isRemovable { get; } = false;
public IEntityFactory Factory { get; set; }
}
}

View file

@ -91,7 +91,7 @@ namespace GamecraftModdingAPI
AsyncUtils.Init();
GamecraftModdingAPI.App.Client.Init();
GamecraftModdingAPI.App.Game.Init();
CustomBlock.Prepare().RunOn(ExtraLean.UIScheduler);
CustomBlock.Init();
// init UI
Interface.IMGUI.Constants.Init();
Interface.IMGUI.IMGUIManager.Init();

View file

@ -38,6 +38,18 @@ namespace GamecraftModdingAPI.Utility
}
return _versionGroup;
}
}
}
private static ExclusiveGroup _customBlockGroup;
public static ExclusiveGroup customBlockGroup
{
get
{
if (_customBlockGroup == null)
_customBlockGroup = new ExclusiveGroup("GamecraftModdingAPI CustomBlockGroup");
return _customBlockGroup;
}
}
}
}