DragonECS/src/Utils/IConfigContainer.cs

119 lines
3.3 KiB
C#
Raw Normal View History

2024-03-07 21:24:16 +08:00
using System;
using System.Collections;
2024-03-07 06:46:44 +08:00
using System.Collections.Generic;
namespace DCFApixels.DragonECS
{
public interface IConfigContainer
{
int Count { get; }
bool Has<T>();
T Get<T>();
bool TryGet<T>(out T value);
IEnumerable<KeyValuePair<Type, object>> GetAllConfigs();
2024-03-07 06:46:44 +08:00
}
public interface IConfigContainerWriter
{
int Count { get; }
void Set<T>(T value);
void Set(Type type, object value);
2024-03-07 06:46:44 +08:00
bool Has<T>();
T Get<T>();
bool TryGet<T>(out T value);
void Remove<T>();
IEnumerable<KeyValuePair<Type, object>> GetAllConfigs();
2024-03-07 07:48:18 +08:00
IConfigContainer GetContainer();
2024-03-07 06:46:44 +08:00
}
public sealed class ConfigContainer : IConfigContainer, IConfigContainerWriter, IEnumerable<KeyValuePair<Type, object>>
2024-03-07 06:46:44 +08:00
{
2024-03-07 07:48:18 +08:00
public static readonly ConfigContainer Empty = new ConfigContainer();
2024-03-07 06:46:44 +08:00
private Dictionary<Type, object> _storage = new Dictionary<Type, object>();
2024-03-07 07:48:18 +08:00
public ConfigContainer() { }
public ConfigContainer(IEnumerable<object> range)
2024-03-07 06:46:44 +08:00
{
foreach (var item in range)
{
_storage.Add(item.GetType(), item);
}
}
2024-03-07 07:48:18 +08:00
public ConfigContainer(params object[] range)
2024-03-07 06:46:44 +08:00
{
foreach (var item in range)
{
_storage.Add(item.GetType(), item);
}
}
public int Count
{
get { return _storage.Count; }
}
public T Get<T>()
{
return (T)_storage[typeof(T)];
}
public bool Has<T>()
{
return _storage.ContainsKey(typeof(T));
}
public void Remove<T>()
{
_storage.Remove(typeof(T));
}
2024-03-07 07:48:18 +08:00
public ConfigContainer Set<T>(T value)
2024-03-07 06:46:44 +08:00
{
_storage[typeof(T)] = value;
2024-03-07 07:48:18 +08:00
return this;
2024-03-07 06:46:44 +08:00
}
public ConfigContainer Set(Type type, object value)
{
_storage[type] = value;
return this;
}
void IConfigContainerWriter.Set(Type type, object value)
{
Set(type, value);
}
2024-03-07 06:46:44 +08:00
void IConfigContainerWriter.Set<T>(T value)
{
2024-03-07 07:48:18 +08:00
Set(value);
2024-03-07 06:46:44 +08:00
}
public bool TryGet<T>(out T value)
{
bool result = _storage.TryGetValue(typeof(T), out object rawValue);
value = rawValue == null ? default : (T)rawValue;
return result;
}
public IConfigContainer GetContainer() { return this; }
public IEnumerable<KeyValuePair<Type, object>> GetAllConfigs()
2024-03-07 06:46:44 +08:00
{
return _storage;
2024-03-07 06:46:44 +08:00
}
public IEnumerator<KeyValuePair<Type, object>> GetEnumerator()
2024-03-07 06:46:44 +08:00
{
return _storage.GetEnumerator();
2024-03-07 06:46:44 +08:00
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetAllConfigs().GetEnumerator();
}
}
2024-03-07 07:48:18 +08:00
public static class ConfigContainerExtensions
2024-03-07 06:46:44 +08:00
{
2024-03-07 07:48:18 +08:00
public static T GetOrDefault<T>(this IConfigContainer self, T defaultValue)
{
if (self.TryGet(out T value))
{
return value;
}
return defaultValue;
}
public static EcsWorldConfig GetWorldConfigOrDefault(this IConfigContainer self)
2024-03-07 06:46:44 +08:00
{
2024-03-07 07:48:18 +08:00
return self.GetOrDefault(EcsWorldConfig.Default);
2024-03-07 06:46:44 +08:00
}
}
}