mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 18:14:37 +08:00
update world configs
This commit is contained in:
parent
2d5271b410
commit
16c62e69a6
@ -95,7 +95,7 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
if (config == null)
|
if (config == null)
|
||||||
{
|
{
|
||||||
config = EmptyConfig.Instance;
|
config = EcsWorldConfig.Empty;
|
||||||
}
|
}
|
||||||
_config = config;
|
_config = config;
|
||||||
|
|
||||||
@ -570,21 +570,6 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region EmptyConfig
|
|
||||||
private class EmptyConfig : IEcsWorldConfig
|
|
||||||
{
|
|
||||||
public static readonly EmptyConfig Instance = new EmptyConfig();
|
|
||||||
private EmptyConfig() { }
|
|
||||||
public bool IsLocked => true;
|
|
||||||
public T Get<T>(string valueName) { return default; }
|
|
||||||
public bool Has(string valueName) { return false; }
|
|
||||||
public void Lock() { }
|
|
||||||
public void Remove(string valueName) { }
|
|
||||||
public void Set<T>(string valueName, T value) { }
|
|
||||||
public bool TryGet<T>(string valueName, out T value) { value = default; return false; }
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Callbacks Interface
|
#region Callbacks Interface
|
||||||
|
@ -1,25 +1,48 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace DCFApixels.DragonECS
|
namespace DCFApixels.DragonECS
|
||||||
{
|
{
|
||||||
public interface IEcsWorldConfig
|
public interface IEcsWorldConfig
|
||||||
{
|
{
|
||||||
|
int Count { get; }
|
||||||
bool Has(string valueName);
|
bool Has(string valueName);
|
||||||
T Get<T>(string valueName);
|
T Get<T>(string valueName);
|
||||||
bool TryGet<T>(string valueName, out T value);
|
bool TryGet<T>(string valueName, out T value);
|
||||||
|
IEnumerable<KeyValuePair<string, object>> GetAllConfigs();
|
||||||
}
|
}
|
||||||
public interface IEcsWorldConfigWriter
|
public interface IEcsWorldConfigWriter
|
||||||
{
|
{
|
||||||
|
int Count { get; }
|
||||||
void Set<T>(string valueName, T value);
|
void Set<T>(string valueName, T value);
|
||||||
bool Has(string valueName);
|
bool Has(string valueName);
|
||||||
T Get<T>(string valueName);
|
T Get<T>(string valueName);
|
||||||
bool TryGet<T>(string valueName, out T value);
|
bool TryGet<T>(string valueName, out T value);
|
||||||
void Remove(string valueName);
|
void Remove(string valueName);
|
||||||
|
IEnumerable<KeyValuePair<string, object>> GetAllConfigs();
|
||||||
}
|
}
|
||||||
public class EcsWorldConfig : IEcsWorldConfigWriter, IEcsWorldConfig
|
[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>();
|
private Dictionary<string, object> _storage = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
public EcsWorldConfig() { }
|
||||||
|
public EcsWorldConfig(IEnumerable<KeyValuePair<string, object>> range)
|
||||||
|
{
|
||||||
|
_storage = new Dictionary<string, object>(range);
|
||||||
|
}
|
||||||
|
public EcsWorldConfig(params KeyValuePair<string, object>[] range)
|
||||||
|
{
|
||||||
|
_storage = new Dictionary<string, object>(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get { return _storage.Count; }
|
||||||
|
}
|
||||||
public T Get<T>(string valueName)
|
public T Get<T>(string valueName)
|
||||||
{
|
{
|
||||||
return (T)_storage[valueName];
|
return (T)_storage[valueName];
|
||||||
@ -36,12 +59,41 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
_storage[valueName] = 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)
|
public bool TryGet<T>(string valueName, out T value)
|
||||||
{
|
{
|
||||||
bool result = _storage.TryGetValue(valueName, out object rawValue);
|
bool result = _storage.TryGetValue(valueName, out object rawValue);
|
||||||
value = rawValue == null ? default : (T)rawValue;
|
value = rawValue == null ? default : (T)rawValue;
|
||||||
return result;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 class EcsWorldConfigExtensions
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user