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> { public static readonly IEcsWorldConfig Empty = new EmptyConfig(); private Dictionary _storage = new Dictionary(); public EcsWorldConfig() { } public EcsWorldConfig(IEnumerable> range) { _storage = new Dictionary(); foreach (var item in range) { _storage.Add(item.Key, item.Value); } } public EcsWorldConfig(params KeyValuePair[] range) { _storage = new Dictionary(); foreach (var item in range) { _storage.Add(item.Key, item.Value); } } public int Count { get { return _storage.Count; } } public T Get(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(string valueName, T value) { _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(); } public IEcsWorldConfig GetWorldConfig() { return this; } 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 { public static T GetOrDefault(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(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(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(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(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(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); } } }