diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index eef892a..ac98f84 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -95,7 +95,7 @@ namespace DCFApixels.DragonECS { if (config == null) { - config = EmptyConfig.Instance; + config = EcsWorldConfig.Empty; } _config = config; @@ -570,21 +570,6 @@ namespace DCFApixels.DragonECS } } #endregion - - #region EmptyConfig - private class EmptyConfig : IEcsWorldConfig - { - public static readonly EmptyConfig Instance = new EmptyConfig(); - private EmptyConfig() { } - public bool IsLocked => true; - public T Get(string valueName) { return default; } - public bool Has(string valueName) { return false; } - public void Lock() { } - public void Remove(string valueName) { } - public void Set(string valueName, T value) { } - public bool TryGet(string valueName, out T value) { value = default; return false; } - } - #endregion } #region Callbacks Interface diff --git a/src/EcsWorldConfig.cs b/src/EcsWorldConfig.cs index 17c619d..8cfaca7 100644 --- a/src/EcsWorldConfig.cs +++ b/src/EcsWorldConfig.cs @@ -1,25 +1,48 @@ -using System.Collections.Generic; +using System; +using System.Collections; +using System.Collections.Generic; namespace DCFApixels.DragonECS { public interface IEcsWorldConfig { + int Count { get; } bool Has(string valueName); T Get(string valueName); bool TryGet(string valueName, out T value); + IEnumerable> GetAllConfigs(); } public interface IEcsWorldConfigWriter { + int Count { get; } void Set(string valueName, T value); bool Has(string valueName); T Get(string valueName); bool TryGet(string valueName, out T value); void Remove(string valueName); + IEnumerable> GetAllConfigs(); } - public class EcsWorldConfig : IEcsWorldConfigWriter, IEcsWorldConfig + [Serializable] + public class EcsWorldConfig : IEcsWorldConfigWriter, IEcsWorldConfig, IEnumerable> { + public static readonly IEcsWorldConfig Empty = new EmptyConfig(); + private Dictionary _storage = new Dictionary(); + public EcsWorldConfig() { } + public EcsWorldConfig(IEnumerable> range) + { + _storage = new Dictionary(range); + } + public EcsWorldConfig(params KeyValuePair[] range) + { + _storage = new Dictionary(range); + } + + public int Count + { + get { return _storage.Count; } + } public T Get(string valueName) { return (T)_storage[valueName]; @@ -36,12 +59,41 @@ namespace DCFApixels.DragonECS { _storage[valueName] = value; } + public void Add(string key, object value) + { + _storage.Add(key, value); + } + public void Add(KeyValuePair pair) + { + _storage.Add(pair.Key, pair.Value); + } public bool TryGet(string valueName, out T value) { bool result = _storage.TryGetValue(valueName, out object rawValue); value = rawValue == null ? default : (T)rawValue; return result; } + public IEnumerable> GetAllConfigs() + { + return _storage; + } + public IEnumerator> GetEnumerator() + { + return GetAllConfigs().GetEnumerator(); + } + IEnumerator IEnumerable.GetEnumerator() + { + return GetAllConfigs().GetEnumerator(); + } + + private class EmptyConfig : IEcsWorldConfig + { + public int Count { get { return 0; } } + public T Get(string valueName) { return default; } + public IEnumerable> GetAllConfigs() { return Array.Empty>(); } + public bool Has(string valueName) { return false; } + public bool TryGet(string valueName, out T value) { value = default; return false; } + } } public static class EcsWorldConfigExtensions {