diff --git a/src/Builtin/Worlds.cs b/src/Builtin/Worlds.cs index 49ebf7c..10bdcf6 100644 --- a/src/Builtin/Worlds.cs +++ b/src/Builtin/Worlds.cs @@ -2,10 +2,12 @@ { 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 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) { } } } diff --git a/src/Configs.meta b/src/Configs.meta deleted file mode 100644 index 8e150ad..0000000 --- a/src/Configs.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 180397e9b3d73d143a3239dd05b2a84b -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/src/Configs/EcsPipelineConfigContainer.cs b/src/Configs/EcsPipelineConfigContainer.cs deleted file mode 100644 index df08b24..0000000 --- a/src/Configs/EcsPipelineConfigContainer.cs +++ /dev/null @@ -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 - { - public static readonly IEcsPipelineConfigContainer Defaut; - static EcsPipelineConfigContainer() - { - var container = new EcsPipelineConfigContainer(); - Defaut = container; - } - public EcsPipelineConfigContainer Set(T value) - { - SetInternal(value); - return this; - } - public IEcsPipelineConfigContainer GetPipelineConfigs() - { - return this; - } - } -} diff --git a/src/Configs/EcsPipelineConfigContainer.cs.meta b/src/Configs/EcsPipelineConfigContainer.cs.meta deleted file mode 100644 index 12a7b60..0000000 --- a/src/Configs/EcsPipelineConfigContainer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: f3699ffd374c5074ebd5240c20ae5ba5 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/src/Configs/EcsWorldConfigContainer.cs b/src/Configs/EcsWorldConfigContainer.cs deleted file mode 100644 index 7a5b449..0000000 --- a/src/Configs/EcsWorldConfigContainer.cs +++ /dev/null @@ -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 - { - public static readonly IEcsWorldConfigContainer Defaut; - static EcsWorldConfigContainer() - { - var container = new EcsWorldConfigContainer(); - container.Set(new EcsWorldConfig()); - Defaut = container; - } - public EcsWorldConfigContainer Set(T value) - { - SetInternal(value); - return this; - } - public IEcsWorldConfigContainer GetWorldConfigs() - { - return this; - } - } - public static class EcsWorldConfigExtensions - { - public static T GetOrDefault(this IEcsWorldConfigContainer self, T defaultValue) - { - if (self.TryGet(out T value)) - { - return value; - } - return defaultValue; - } - } -} diff --git a/src/Configs/EcsWorldConfigContainer.cs.meta b/src/Configs/EcsWorldConfigContainer.cs.meta deleted file mode 100644 index 2c9a917..0000000 --- a/src/Configs/EcsWorldConfigContainer.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 0bd5947405343044b9e716c787ad4945 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index 7ffcf8d..9a3d5cb 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -14,7 +14,7 @@ namespace DCFApixels.DragonECS } public sealed class EcsPipeline { - private readonly IEcsPipelineConfigContainer _config; + private readonly IConfigContainer _configs; private Injector.Builder _injectorBuilder; private Injector _injector; @@ -31,9 +31,9 @@ namespace DCFApixels.DragonECS #endif #region Properties - public IEcsPipelineConfigContainer Config + public IConfigContainer Configs { - get { return _config; } + get { return _configs; } } public Injector Injector { @@ -58,9 +58,9 @@ namespace DCFApixels.DragonECS #endregion #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; _injectorBuilder = injectorBuilder; _injectorBuilder.Inject(this); @@ -186,7 +186,7 @@ namespace DCFApixels.DragonECS #endregion #region Builder - public static Builder New(IEcsPipelineConfigContainerWriter config = null) + public static Builder New(IConfigContainerWriter config = null) { return new Builder(config); } @@ -197,28 +197,28 @@ namespace DCFApixels.DragonECS private readonly Dictionary> _systems; private readonly string _basicLayer; public readonly LayerList Layers; - private readonly IEcsPipelineConfigContainerWriter _config; + private readonly IConfigContainerWriter _configs; private readonly Injector.Builder _injector; #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS private EcsProfilerMarker _buildBarker = new EcsProfilerMarker("EcsPipeline.Build"); #endif private List _initDeclaredRunners = new List(4); - public IEcsPipelineConfigContainerWriter Config + public IConfigContainerWriter Configs { - get { return _config; } + get { return _configs; } } public Injector.Builder Injector { get { return _injector; } } - public Builder(IEcsPipelineConfigContainerWriter config = null) + public Builder(IConfigContainerWriter config = null) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS _buildBarker.Begin(); #endif - if (config == null) { config = new EcsPipelineConfigContainer(); } - _config = config; + if (config == null) { config = ConfigContainer.Empty; } + _configs = config; _injector = new Injector.Builder(this); _injector.AddNode(); @@ -293,7 +293,7 @@ namespace DCFApixels.DragonECS if (_systems.TryGetValue(item, out var 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) { item.Declare(pipeline); diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index e7a1c99..622d2e3 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -6,10 +6,27 @@ using System.Runtime.InteropServices; 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 readonly short id; - private IEcsWorldConfigContainer _configs; + private IConfigContainer _configs; private bool _isDestroyed = false; @@ -38,7 +55,7 @@ namespace DCFApixels.DragonECS private List _entityListeners = new List(); #region Properties - public IEcsWorldConfigContainer Configs + public IConfigContainer Configs { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return _configs; } @@ -95,19 +112,17 @@ namespace DCFApixels.DragonECS #endregion #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) - { - configs = EcsWorldConfigContainer.Defaut; - } + if (configs == null) { configs = ConfigContainer.Empty; } bool nullWorld = this is NullWorld; if(nullWorld == false && worldID == NULL_WORLD_ID) { EcsDebug.PrintWarning($"The world identifier cannot be {NULL_WORLD_ID}"); } _configs = configs; - EcsWorldConfig config = configs.Get(); + EcsWorldConfig config = configs.GetWorldConfigOrDefault(); if (worldID < 0 || (worldID == NULL_WORLD_ID && nullWorld == false)) { @@ -554,7 +569,7 @@ namespace DCFApixels.DragonECS } internal EcsGroup GetFreeGroup() { - EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this, _configs.Get().GroupCapacity) : _groupsPool.Pop(); + EcsGroup result = _groupsPool.Count <= 0 ? new EcsGroup(this, _configs.GetWorldConfigOrDefault().GroupCapacity) : _groupsPool.Pop(); result._isReleased = false; return result; } diff --git a/src/EcsWorld.static.cs b/src/EcsWorld.static.cs index fa19f70..47d501c 100644 --- a/src/EcsWorld.static.cs +++ b/src/EcsWorld.static.cs @@ -131,7 +131,7 @@ namespace DCFApixels.DragonECS } private sealed class NullWorld : EcsWorld { - internal NullWorld() : base(EcsWorldConfigContainer.Defaut, 0) { } + internal NullWorld() : base(ConfigContainer.Empty, 0) { } } } } diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index a8fc7f1..e83a5f3 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -177,8 +177,8 @@ namespace DCFApixels.DragonECS _maskBit = EcsMaskChunck.FromID(componentTypeID); _mapping = new int[world.Capacity]; - _items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Configs.Get().PoolComponentsCapacity)]; - _recycledItems = new int[world.Configs.Get().PoolRecycledComponentsCapacity]; + _items = new T[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Configs.GetWorldConfigOrDefault().PoolComponentsCapacity)]; + _recycledItems = new int[world.Configs.GetWorldConfigOrDefault().PoolRecycledComponentsCapacity]; } void IEcsPoolImplementation.OnWorldResize(int newSize) { diff --git a/src/Configs/IConfigContainer.cs b/src/Utils/IConfigContainer.cs similarity index 76% rename from src/Configs/IConfigContainer.cs rename to src/Utils/IConfigContainer.cs index 4fce30e..01983b0 100644 --- a/src/Configs/IConfigContainer.cs +++ b/src/Utils/IConfigContainer.cs @@ -21,32 +21,23 @@ namespace DCFApixels.DragonECS bool TryGet(out T value); void Remove(); IEnumerable GetAllConfigs(); + IConfigContainer GetContainer(); } - public static class ConfigContainerExtensions + public sealed class ConfigContainer : IConfigContainer, IConfigContainerWriter, IEnumerable { - public static T GetOrDefault(this IConfigContainer self, T defaultValue) - { - if (self.TryGet(out T value)) - { - return value; - } - return defaultValue; - } - } + public static readonly ConfigContainer Empty = new ConfigContainer(); - public class DefaultConfigContainerBase : IConfigContainer, IConfigContainerWriter, IEnumerable - { private Dictionary _storage = new Dictionary(); - public DefaultConfigContainerBase() { } - public DefaultConfigContainerBase(IEnumerable range) + public ConfigContainer() { } + public ConfigContainer(IEnumerable range) { foreach (var item in range) { _storage.Add(item.GetType(), item); } } - public DefaultConfigContainerBase(params object[] range) + public ConfigContainer(params object[] range) { foreach (var item in range) { @@ -70,13 +61,14 @@ namespace DCFApixels.DragonECS { _storage.Remove(typeof(T)); } - protected void SetInternal(T value) + public ConfigContainer Set(T value) { _storage[typeof(T)] = value; + return this; } void IConfigContainerWriter.Set(T value) { - SetInternal(value); + Set(value); } public bool TryGet(out T value) { @@ -84,6 +76,10 @@ namespace DCFApixels.DragonECS value = rawValue == null ? default : (T)rawValue; return result; } + public IConfigContainer GetContainer() + { + return this; + } public IEnumerable GetAllConfigs() { return _storage.Values; @@ -97,14 +93,19 @@ namespace DCFApixels.DragonECS return GetAllConfigs().GetEnumerator(); } } - - public static class DefaultConfigContainerBaseExtensions + public static class ConfigContainerExtensions { - public static TContainer Set(this TContainer self, T value) - where TContainer : IConfigContainerWriter + public static T GetOrDefault(this IConfigContainer self, T defaultValue) { - self.Set(value); - return self; + if (self.TryGet(out T value)) + { + return value; + } + return defaultValue; + } + public static EcsWorldConfig GetWorldConfigOrDefault(this IConfigContainer self) + { + return self.GetOrDefault(EcsWorldConfig.Default); } } } diff --git a/src/Configs/IConfigContainer.cs.meta b/src/Utils/IConfigContainer.cs.meta similarity index 100% rename from src/Configs/IConfigContainer.cs.meta rename to src/Utils/IConfigContainer.cs.meta