update configs

This commit is contained in:
Mikhail 2024-03-07 07:48:18 +08:00
parent 53911d4f25
commit ef0f13850f
12 changed files with 68 additions and 168 deletions

View File

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

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 180397e9b3d73d143a3239dd05b2a84b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,30 +0,0 @@
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,11 +0,0 @@
fileFormatVersion: 2
guid: f3699ffd374c5074ebd5240c20ae5ba5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,58 +0,0 @@
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,11 +0,0 @@
fileFormatVersion: 2
guid: 0bd5947405343044b9e716c787ad4945
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

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

View File

@ -6,10 +6,27 @@ using System.Runtime.InteropServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public class EcsWorldConfig
{
public static readonly EcsWorldConfig Default = new 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 partial class EcsWorld : IEntityStorage public partial class EcsWorld : IEntityStorage
{ {
public readonly short id; public readonly short id;
private IEcsWorldConfigContainer _configs; private IConfigContainer _configs;
private bool _isDestroyed = false; private bool _isDestroyed = false;
@ -38,7 +55,7 @@ namespace DCFApixels.DragonECS
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>(); private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
#region Properties #region Properties
public IEcsWorldConfigContainer Configs public IConfigContainer Configs
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _configs; } get { return _configs; }
@ -95,19 +112,17 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Constructors/Destroy #region Constructors/Destroy
public EcsWorld(IEcsWorldConfigContainer configs = null, short worldID = -1) public EcsWorld(EcsWorldConfig config, short worldID = -1) : this(config == null ? ConfigContainer.Empty : new ConfigContainer().Set(config), worldID) { }
public EcsWorld(IConfigContainer configs = null, short worldID = -1)
{ {
if (configs == null) if (configs == null) { configs = ConfigContainer.Empty; }
{
configs = EcsWorldConfigContainer.Defaut;
}
bool nullWorld = this is NullWorld; bool nullWorld = this is NullWorld;
if(nullWorld == false && worldID == NULL_WORLD_ID) if(nullWorld == false && worldID == NULL_WORLD_ID)
{ {
EcsDebug.PrintWarning($"The world identifier cannot be {NULL_WORLD_ID}"); EcsDebug.PrintWarning($"The world identifier cannot be {NULL_WORLD_ID}");
} }
_configs = configs; _configs = configs;
EcsWorldConfig config = configs.Get<EcsWorldConfig>(); EcsWorldConfig config = configs.GetWorldConfigOrDefault();
if (worldID < 0 || (worldID == NULL_WORLD_ID && nullWorld == false)) if (worldID < 0 || (worldID == NULL_WORLD_ID && nullWorld == false))
{ {
@ -554,7 +569,7 @@ namespace DCFApixels.DragonECS
} }
internal EcsGroup GetFreeGroup() internal EcsGroup GetFreeGroup()
{ {
EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this, _configs.Get<EcsWorldConfig>().GroupCapacity) : _groupsPool.Pop(); EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this, _configs.GetWorldConfigOrDefault().GroupCapacity) : _groupsPool.Pop();
result._isReleased = false; result._isReleased = false;
return result; return result;
} }

View File

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

View File

@ -177,8 +177,8 @@ namespace DCFApixels.DragonECS
_maskBit = EcsMaskChunck.FromID(componentTypeID); _maskBit = EcsMaskChunck.FromID(componentTypeID);
_mapping = new int[world.Capacity]; _mapping = new int[world.Capacity];
_items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Configs.Get<EcsWorldConfig>().PoolComponentsCapacity)]; _items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Configs.GetWorldConfigOrDefault().PoolComponentsCapacity)];
_recycledItems = new int[world.Configs.Get<EcsWorldConfig>().PoolRecycledComponentsCapacity]; _recycledItems = new int[world.Configs.GetWorldConfigOrDefault().PoolRecycledComponentsCapacity];
} }
void IEcsPoolImplementation.OnWorldResize(int newSize) void IEcsPoolImplementation.OnWorldResize(int newSize)
{ {

View File

@ -21,32 +21,23 @@ namespace DCFApixels.DragonECS
bool TryGet<T>(out T value); bool TryGet<T>(out T value);
void Remove<T>(); void Remove<T>();
IEnumerable<object> GetAllConfigs(); IEnumerable<object> GetAllConfigs();
IConfigContainer GetContainer();
} }
public static class ConfigContainerExtensions public sealed class ConfigContainer : IConfigContainer, IConfigContainerWriter, IEnumerable<object>
{ {
public static T GetOrDefault<T>(this IConfigContainer self, T defaultValue) public static readonly ConfigContainer Empty = new ConfigContainer();
{
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>(); private Dictionary<Type, object> _storage = new Dictionary<Type, object>();
public DefaultConfigContainerBase() { } public ConfigContainer() { }
public DefaultConfigContainerBase(IEnumerable<object> range) public ConfigContainer(IEnumerable<object> range)
{ {
foreach (var item in range) foreach (var item in range)
{ {
_storage.Add(item.GetType(), item); _storage.Add(item.GetType(), item);
} }
} }
public DefaultConfigContainerBase(params object[] range) public ConfigContainer(params object[] range)
{ {
foreach (var item in range) foreach (var item in range)
{ {
@ -70,13 +61,14 @@ namespace DCFApixels.DragonECS
{ {
_storage.Remove(typeof(T)); _storage.Remove(typeof(T));
} }
protected void SetInternal<T>(T value) public ConfigContainer Set<T>(T value)
{ {
_storage[typeof(T)] = value; _storage[typeof(T)] = value;
return this;
} }
void IConfigContainerWriter.Set<T>(T value) void IConfigContainerWriter.Set<T>(T value)
{ {
SetInternal(value); Set(value);
} }
public bool TryGet<T>(out T value) public bool TryGet<T>(out T value)
{ {
@ -84,6 +76,10 @@ namespace DCFApixels.DragonECS
value = rawValue == null ? default : (T)rawValue; value = rawValue == null ? default : (T)rawValue;
return result; return result;
} }
public IConfigContainer GetContainer()
{
return this;
}
public IEnumerable<object> GetAllConfigs() public IEnumerable<object> GetAllConfigs()
{ {
return _storage.Values; return _storage.Values;
@ -97,14 +93,19 @@ namespace DCFApixels.DragonECS
return GetAllConfigs().GetEnumerator(); return GetAllConfigs().GetEnumerator();
} }
} }
public static class ConfigContainerExtensions
public static class DefaultConfigContainerBaseExtensions
{ {
public static TContainer Set<TContainer, T>(this TContainer self, T value) public static T GetOrDefault<T>(this IConfigContainer self, T defaultValue)
where TContainer : IConfigContainerWriter
{ {
self.Set(value); if (self.TryGet(out T value))
return self; {
return value;
}
return defaultValue;
}
public static EcsWorldConfig GetWorldConfigOrDefault(this IConfigContainer self)
{
return self.GetOrDefault(EcsWorldConfig.Default);
} }
} }
} }