rework configs

This commit is contained in:
Mikhail 2024-03-07 06:46:44 +08:00
parent 9561b7139b
commit 53911d4f25
15 changed files with 226 additions and 346 deletions

View File

@ -2,10 +2,10 @@
{
public sealed class EcsDefaultWorld : EcsWorld
{
public EcsDefaultWorld(IEcsWorldConfig config = null, short worldID = -1) : base(config, worldID) { }
public EcsDefaultWorld(IEcsWorldConfigContainer config = null, short worldID = -1) : base(config, worldID) { }
}
public sealed class EcsEventWorld : EcsWorld
{
public EcsEventWorld(IEcsWorldConfig config = null, short worldID = -1) : base(config, worldID) { }
public EcsEventWorld(IEcsWorldConfigContainer config = null, short worldID = -1) : base(config, worldID) { }
}
}

View File

@ -1,98 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
public interface IEcsPipelineConfig : IConfig { }
public interface IEcsPipelineConfigWriter : IConfigWriter
{
IEcsPipelineConfig GetPipelineConfig();
}
[Serializable]
public class EcsPipelineConfig : IEcsPipelineConfigWriter, IEcsPipelineConfig, IEnumerable<KeyValuePair<string, object>>
{
public static readonly IEcsWorldConfig Empty = new EmptyConfig();
private Dictionary<string, object> _storage = new Dictionary<string, object>();
public EcsPipelineConfig() { }
public EcsPipelineConfig(IEnumerable<KeyValuePair<string, object>> range)
{
_storage = new Dictionary<string, object>();
foreach (var item in range)
{
_storage.Add(item.Key, item.Value);
}
}
public EcsPipelineConfig(params KeyValuePair<string, object>[] range)
{
_storage = new Dictionary<string, object>();
foreach (var item in range)
{
_storage.Add(item.Key, item.Value);
}
}
public int Count
{
get { return _storage.Count; }
}
public T Get<T>(string valueName)
{
return (T)_storage[valueName];
}
public bool Has(string valueName)
{
return _storage.ContainsKey(valueName);
}
public void Remove(string valueName)
{
_storage.Remove(valueName);
}
public void Set<T>(string valueName, T value)
{
_storage[valueName] = value;
}
public void Add(string key, object value)
{
_storage.Add(key, value);
}
public void Add(KeyValuePair<string, object> pair)
{
_storage.Add(pair.Key, pair.Value);
}
public bool TryGet<T>(string valueName, out T value)
{
bool result = _storage.TryGetValue(valueName, out object rawValue);
value = rawValue == null ? default : (T)rawValue;
return result;
}
public IEnumerable<KeyValuePair<string, object>> GetAllConfigs()
{
return _storage;
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return GetAllConfigs().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetAllConfigs().GetEnumerator();
}
public IEcsPipelineConfig GetPipelineConfig()
{
return this;
}
private class EmptyConfig : IEcsWorldConfig
{
public int Count { get { return 0; } }
public T Get<T>(string valueName) { return default; }
public IEnumerable<KeyValuePair<string, object>> GetAllConfigs() { return Array.Empty<KeyValuePair<string, object>>(); }
public bool Has(string valueName) { return false; }
public bool TryGet<T>(string valueName, out T value) { value = default; return false; }
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
public interface IEcsPipelineConfigContainer : IConfigContainer { }
public interface IEcsPipelineConfigContainerWriter : IConfigContainerWriter
{
IEcsPipelineConfigContainer GetPipelineConfigs();
}
[Serializable]
public class EcsPipelineConfigContainer : DefaultConfigContainerBase, IEcsPipelineConfigContainerWriter, IEcsPipelineConfigContainer, IEnumerable<object>
{
public static readonly IEcsPipelineConfigContainer Defaut;
static EcsPipelineConfigContainer()
{
var container = new EcsPipelineConfigContainer();
Defaut = container;
}
public EcsPipelineConfigContainer Set<T>(T value)
{
SetInternal(value);
return this;
}
public IEcsPipelineConfigContainer GetPipelineConfigs()
{
return this;
}
}
}

View File

@ -1,184 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
public interface IEcsWorldConfig : IConfig { }
public interface IEcsWorldConfigWriter : IConfigWriter
{
IEcsWorldConfig GetWorldConfig();
}
[Serializable]
public class EcsWorldConfig : IEcsWorldConfigWriter, IEcsWorldConfig, IEnumerable<KeyValuePair<string, object>>
{
public static readonly IEcsWorldConfig Empty = new EmptyConfig();
private Dictionary<string, object> _storage = new Dictionary<string, object>();
public EcsWorldConfig() { }
public EcsWorldConfig(IEnumerable<KeyValuePair<string, object>> range)
{
_storage = new Dictionary<string, object>();
foreach (var item in range)
{
_storage.Add(item.Key, item.Value);
}
}
public EcsWorldConfig(params KeyValuePair<string, object>[] range)
{
_storage = new Dictionary<string, object>();
foreach (var item in range)
{
_storage.Add(item.Key, item.Value);
}
}
public int Count
{
get { return _storage.Count; }
}
public T Get<T>(string valueName)
{
return (T)_storage[valueName];
}
public bool Has(string valueName)
{
return _storage.ContainsKey(valueName);
}
public void Remove(string valueName)
{
_storage.Remove(valueName);
}
public void Set<T>(string valueName, T value)
{
_storage[valueName] = value;
}
public void Add(string key, object value)
{
_storage.Add(key, value);
}
public void Add(KeyValuePair<string, object> pair)
{
_storage.Add(pair.Key, pair.Value);
}
public bool TryGet<T>(string valueName, out T value)
{
bool result = _storage.TryGetValue(valueName, out object rawValue);
value = rawValue == null ? default : (T)rawValue;
return result;
}
public IEnumerable<KeyValuePair<string, object>> GetAllConfigs()
{
return _storage;
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return GetAllConfigs().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetAllConfigs().GetEnumerator();
}
public IEcsWorldConfig GetWorldConfig()
{
return this;
}
private class EmptyConfig : IEcsWorldConfig
{
public int Count { get { return 0; } }
public T Get<T>(string valueName) { return default; }
public IEnumerable<KeyValuePair<string, object>> GetAllConfigs() { return Array.Empty<KeyValuePair<string, object>>(); }
public bool Has(string valueName) { return false; }
public bool TryGet<T>(string valueName, out T value) { value = default; return false; }
}
}
public static class EcsWorldConfigExtensions
{
public static T GetOrDefault<T>(this IEcsWorldConfig self, string valueName, T defaultValue)
{
if (self.TryGet(valueName, out T value))
{
return value;
}
return defaultValue;
}
private const string ENTITIES_CAPACITY = nameof(ENTITIES_CAPACITY);
private const int ENTITIES_CAPACITY_DEFAULT = 512;
public static TConfig Set_EntitiesCapacity<TConfig>(this TConfig self, int value)
where TConfig : IEcsWorldConfigWriter
{
self.Set(ENTITIES_CAPACITY, value);
return self;
}
public static int Get_EntitiesCapacity(this IEcsWorldConfig self)
{
return self.GetOrDefault(ENTITIES_CAPACITY, ENTITIES_CAPACITY_DEFAULT);
}
//private const string RECYCLED_ENTITIES_CAPACITY = nameof(RECYCLED_ENTITIES_CAPACITY);
//public static void Set_RecycledEntitiesCapacity(this IEcsWorldConfig self, int value)
//{
// self.Set(RECYCLED_ENTITIES_CAPACITY, value);
//}
//public static int Get_RecycledEntitiesCapacity(this IEcsWorldConfig self)
//{
// return self.GetOrDefault(RECYCLED_ENTITIES_CAPACITY, self.Get_EntitiesCapacity() / 2);
//}
private const string GROUP_CAPACITY = nameof(GROUP_CAPACITY);
private const int GROUP_CAPACITY_DEFAULT = 512;
public static TConfig Set_GroupCapacity<TConfig>(this TConfig self, int value)
where TConfig : IEcsWorldConfigWriter
{
self.Set(GROUP_CAPACITY, value);
return self;
}
public static int Get_GroupCapacity(this IEcsWorldConfig self)
{
return self.GetOrDefault(GROUP_CAPACITY, GROUP_CAPACITY_DEFAULT);
}
private const string POOLS_CAPACITY = nameof(POOLS_CAPACITY);
private const int POOLS_CAPACITY_DEFAULT = 512;
public static TConfig Set_PoolsCapacity<TConfig>(this TConfig self, int value)
where TConfig : IEcsWorldConfigWriter
{
self.Set(POOLS_CAPACITY, value);
return self;
}
public static int Get_PoolsCapacity(this IEcsWorldConfig self)
{
return self.GetOrDefault(POOLS_CAPACITY, POOLS_CAPACITY_DEFAULT);
}
private const string COMPONENT_POOL_CAPACITY = nameof(COMPONENT_POOL_CAPACITY);
private const int COMPONENT_POOL_CAPACITY_DEFAULT = 512;
public static TConfig Set_PoolComponentsCapacity<TConfig>(this TConfig self, int value)
where TConfig : IEcsWorldConfigWriter
{
self.Set(COMPONENT_POOL_CAPACITY, value);
return self;
}
public static int Get_PoolComponentsCapacity(this IEcsWorldConfig self)
{
return self.GetOrDefault(COMPONENT_POOL_CAPACITY, COMPONENT_POOL_CAPACITY_DEFAULT);
}
private const string POOL_RECYCLED_COMPONENTS_CAPACITY = nameof(POOL_RECYCLED_COMPONENTS_CAPACITY);
public static TConfig Set_PoolRecycledComponentsCapacity<TConfig>(this TConfig self, int value)
where TConfig : IEcsWorldConfigWriter
{
self.Set(POOL_RECYCLED_COMPONENTS_CAPACITY, value);
return self;
}
public static int Get_PoolRecycledComponentsCapacity(this IEcsWorldConfig self)
{
return self.GetOrDefault(POOL_RECYCLED_COMPONENTS_CAPACITY, self.Get_PoolComponentsCapacity() / 2);
}
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
public class EcsWorldConfig
{
public readonly int EntitiesCapacity;
public readonly int GroupCapacity;
public readonly int PoolsCapacity;
public readonly int PoolComponentsCapacity;
public readonly int PoolRecycledComponentsCapacity;
public EcsWorldConfig(int entitiesCapacity = 512, int groupCapacity = 512, int poolsCapacity = 512, int poolComponentsCapacity = 512, int poolRecycledComponentsCapacity = 512 / 2)
{
EntitiesCapacity = entitiesCapacity;
GroupCapacity = groupCapacity;
PoolsCapacity = poolsCapacity;
PoolComponentsCapacity = poolComponentsCapacity;
PoolRecycledComponentsCapacity = poolRecycledComponentsCapacity;
}
}
public interface IEcsWorldConfigContainer : IConfigContainer { }
public interface IEcsWorldConfigContainerWriter : IConfigContainerWriter
{
IEcsWorldConfigContainer GetWorldConfigs();
}
[Serializable]
public class EcsWorldConfigContainer : DefaultConfigContainerBase, IEcsWorldConfigContainerWriter, IEcsWorldConfigContainer, IEnumerable<object>
{
public static readonly IEcsWorldConfigContainer Defaut;
static EcsWorldConfigContainer()
{
var container = new EcsWorldConfigContainer();
container.Set(new EcsWorldConfig());
Defaut = container;
}
public EcsWorldConfigContainer Set<T>(T value)
{
SetInternal(value);
return this;
}
public IEcsWorldConfigContainer GetWorldConfigs()
{
return this;
}
}
public static class EcsWorldConfigExtensions
{
public static T GetOrDefault<T>(this IEcsWorldConfigContainer self, T defaultValue)
{
if (self.TryGet(out T value))
{
return value;
}
return defaultValue;
}
}
}

View File

@ -1,34 +0,0 @@
using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
public interface IConfig
{
int Count { get; }
bool Has(string valueName);
T Get<T>(string valueName);
bool TryGet<T>(string valueName, out T value);
IEnumerable<KeyValuePair<string, object>> GetAllConfigs();
}
public interface IConfigWriter
{
int Count { get; }
void Set<T>(string valueName, T value);
bool Has(string valueName);
T Get<T>(string valueName);
bool TryGet<T>(string valueName, out T value);
void Remove(string valueName);
IEnumerable<KeyValuePair<string, object>> GetAllConfigs();
}
public static class ConfigExtensions
{
public static T GetOrDefault<T>(this IConfig self, string valueName, T defaultValue)
{
if (self.TryGet(valueName, out T value))
{
return value;
}
return defaultValue;
}
}
}

View File

@ -0,0 +1,110 @@
using System.Collections;
using System;
using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
public interface IConfigContainer
{
int Count { get; }
bool Has<T>();
T Get<T>();
bool TryGet<T>(out T value);
IEnumerable<object> GetAllConfigs();
}
public interface IConfigContainerWriter
{
int Count { get; }
void Set<T>(T value);
bool Has<T>();
T Get<T>();
bool TryGet<T>(out T value);
void Remove<T>();
IEnumerable<object> GetAllConfigs();
}
public static class ConfigContainerExtensions
{
public static T GetOrDefault<T>(this IConfigContainer self, T defaultValue)
{
if (self.TryGet(out T value))
{
return value;
}
return defaultValue;
}
}
public class DefaultConfigContainerBase : IConfigContainer, IConfigContainerWriter, IEnumerable<object>
{
private Dictionary<Type, object> _storage = new Dictionary<Type, object>();
public DefaultConfigContainerBase() { }
public DefaultConfigContainerBase(IEnumerable<object> range)
{
foreach (var item in range)
{
_storage.Add(item.GetType(), item);
}
}
public DefaultConfigContainerBase(params object[] range)
{
foreach (var item in range)
{
_storage.Add(item.GetType(), item);
}
}
public int Count
{
get { return _storage.Count; }
}
public T Get<T>()
{
return (T)_storage[typeof(T)];
}
public bool Has<T>()
{
return _storage.ContainsKey(typeof(T));
}
public void Remove<T>()
{
_storage.Remove(typeof(T));
}
protected void SetInternal<T>(T value)
{
_storage[typeof(T)] = value;
}
void IConfigContainerWriter.Set<T>(T value)
{
SetInternal(value);
}
public bool TryGet<T>(out T value)
{
bool result = _storage.TryGetValue(typeof(T), out object rawValue);
value = rawValue == null ? default : (T)rawValue;
return result;
}
public IEnumerable<object> GetAllConfigs()
{
return _storage.Values;
}
public IEnumerator<object> GetEnumerator()
{
return _storage.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetAllConfigs().GetEnumerator();
}
}
public static class DefaultConfigContainerBaseExtensions
{
public static TContainer Set<TContainer, T>(this TContainer self, T value)
where TContainer : IConfigContainerWriter
{
self.Set(value);
return self;
}
}
}

View File

@ -14,7 +14,7 @@ namespace DCFApixels.DragonECS
}
public sealed class EcsPipeline
{
private readonly IEcsPipelineConfig _config;
private readonly IEcsPipelineConfigContainer _config;
private Injector.Builder _injectorBuilder;
private Injector _injector;
@ -31,7 +31,7 @@ namespace DCFApixels.DragonECS
#endif
#region Properties
public IEcsPipelineConfig Config
public IEcsPipelineConfigContainer Config
{
get { return _config; }
}
@ -58,7 +58,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Constructors
private EcsPipeline(IEcsPipelineConfig config, Injector.Builder injectorBuilder, IEcsProcess[] systems)
private EcsPipeline(IEcsPipelineConfigContainer config, Injector.Builder injectorBuilder, IEcsProcess[] systems)
{
_config = config;
_allSystems = systems;
@ -186,7 +186,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Builder
public static Builder New(IEcsPipelineConfigWriter config = null)
public static Builder New(IEcsPipelineConfigContainerWriter config = null)
{
return new Builder(config);
}
@ -197,14 +197,14 @@ namespace DCFApixels.DragonECS
private readonly Dictionary<string, List<IEcsProcess>> _systems;
private readonly string _basicLayer;
public readonly LayerList Layers;
private readonly IEcsPipelineConfigWriter _config;
private readonly IEcsPipelineConfigContainerWriter _config;
private readonly Injector.Builder _injector;
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
private EcsProfilerMarker _buildBarker = new EcsProfilerMarker("EcsPipeline.Build");
#endif
private List<InitDeclaredRunner> _initDeclaredRunners = new List<InitDeclaredRunner>(4);
public IEcsPipelineConfigWriter Config
public IEcsPipelineConfigContainerWriter Config
{
get { return _config; }
}
@ -212,12 +212,12 @@ namespace DCFApixels.DragonECS
{
get { return _injector; }
}
public Builder(IEcsPipelineConfigWriter config = null)
public Builder(IEcsPipelineConfigContainerWriter config = null)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_buildBarker.Begin();
#endif
if (config == null) { config = new EcsPipelineConfig(); }
if (config == null) { config = new EcsPipelineConfigContainer(); }
_config = config;
_injector = new Injector.Builder(this);
@ -293,7 +293,7 @@ namespace DCFApixels.DragonECS
if (_systems.TryGetValue(item, out var list))
result.AddRange(list);
}
EcsPipeline pipeline = new EcsPipeline(_config.GetPipelineConfig(), _injector, result.ToArray());
EcsPipeline pipeline = new EcsPipeline(_config.GetPipelineConfigs(), _injector, result.ToArray());
foreach (var item in _initDeclaredRunners)
{
item.Declare(pipeline);

View File

@ -9,7 +9,7 @@ namespace DCFApixels.DragonECS
public partial class EcsWorld : IEntityStorage
{
public readonly short id;
private IEcsWorldConfig _config;
private IEcsWorldConfigContainer _configs;
private bool _isDestroyed = false;
@ -38,10 +38,10 @@ namespace DCFApixels.DragonECS
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
#region Properties
public IEcsWorldConfig Config
public IEcsWorldConfigContainer Configs
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _config; }
get { return _configs; }
}
public long Version
{
@ -95,18 +95,19 @@ namespace DCFApixels.DragonECS
#endregion
#region Constructors/Destroy
public EcsWorld(IEcsWorldConfig config = null, short worldID = -1)
public EcsWorld(IEcsWorldConfigContainer configs = null, short worldID = -1)
{
if (config == null)
if (configs == null)
{
config = EcsWorldConfig.Empty;
configs = EcsWorldConfigContainer.Defaut;
}
bool nullWorld = this is NullWorld;
if(nullWorld == false && worldID == NULL_WORLD_ID)
{
EcsDebug.PrintWarning($"The world identifier cannot be {NULL_WORLD_ID}");
}
_config = config;
_configs = configs;
EcsWorldConfig config = configs.Get<EcsWorldConfig>();
if (worldID < 0 || (worldID == NULL_WORLD_ID && nullWorld == false))
{
@ -129,12 +130,12 @@ namespace DCFApixels.DragonECS
_poolsMediator = new PoolsMediator(this);
int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_PoolsCapacity());
int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.PoolsCapacity);
_pools = new IEcsPoolImplementation[poolsCapacity];
_poolComponentCounts = new int[poolsCapacity];
ArrayUtility.Fill(_pools, _nullPool);
int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.Get_EntitiesCapacity());
int entitiesCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.EntitiesCapacity);
_entityDispenser = new IdDispenser(entitiesCapacity, 0, OnEntityDispenserResized);
}
public void Destroy()
@ -553,7 +554,7 @@ namespace DCFApixels.DragonECS
}
internal EcsGroup GetFreeGroup()
{
EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this, _config.Get_GroupCapacity()) : _groupsPool.Pop();
EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this, _configs.Get<EcsWorldConfig>().GroupCapacity) : _groupsPool.Pop();
result._isReleased = false;
return result;
}

View File

@ -131,7 +131,7 @@ namespace DCFApixels.DragonECS
}
private sealed class NullWorld : EcsWorld
{
internal NullWorld() : base(EcsWorldConfig.Empty, 0) { }
internal NullWorld() : base(EcsWorldConfigContainer.Defaut, 0) { }
}
}
}

View File

@ -16,9 +16,9 @@ namespace DCFApixels.DragonECS
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
private T[] _items; //dense
private int _itemsCount;
private int _itemsCount = 0;
private int[] _recycledItems;
private int _recycledItemsCount;
private int _recycledItemsCount = 0;
private IEcsComponentReset<T> _componentResetHandler = EcsComponentResetHandler<T>.instance;
private bool _isHasComponentResetHandler = EcsComponentResetHandler<T>.isHasHandler;
@ -177,10 +177,8 @@ namespace DCFApixels.DragonECS
_maskBit = EcsMaskChunck.FromID(componentTypeID);
_mapping = new int[world.Capacity];
_recycledItems = new int[world.Config.Get_PoolRecycledComponentsCapacity()];
_recycledItemsCount = 0;
_items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Config.Get_PoolComponentsCapacity())];
_itemsCount = 0;
_items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Configs.Get<EcsWorldConfig>().PoolComponentsCapacity)];
_recycledItems = new int[world.Configs.Get<EcsWorldConfig>().PoolRecycledComponentsCapacity];
}
void IEcsPoolImplementation.OnWorldResize(int newSize)
{

View File

@ -14,7 +14,7 @@ namespace DCFApixels.DragonECS
private EcsMaskChunck _maskBit;
private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
private int _count;
private int _count = 0;
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
@ -156,7 +156,6 @@ namespace DCFApixels.DragonECS
_maskBit = EcsMaskChunck.FromID(componentTypeID);
_mapping = new bool[world.Capacity];
_count = 0;
}
void IEcsPoolImplementation.OnWorldResize(int newSize)
{