using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ET
{
///
/// Config组件会扫描所有的有ConfigAttribute标签的配置,加载进来
///
public class ConfigComponent: Singleton
{
public struct GetAllConfigBytes
{
}
public struct GetOneConfigBytes
{
public string ConfigName;
}
private readonly Dictionary allConfig = new Dictionary();
public override void Dispose()
{
foreach (var kv in this.allConfig)
{
kv.Value.Destroy();
}
}
public object LoadOneConfig(Type configType)
{
this.allConfig.TryGetValue(configType, out ISingleton oneConfig);
if (oneConfig != null)
{
oneConfig.Destroy();
}
byte[] oneConfigBytes = EventSystem.Instance.Invoke(0, new GetOneConfigBytes() {ConfigName = configType.FullName});
object category = SerializeHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
ISingleton singleton = category as ISingleton;
singleton.Register();
this.allConfig[configType] = singleton;
return category;
}
public void Load()
{
this.allConfig.Clear();
Dictionary configBytes = EventSystem.Instance.Invoke>(0, new GetAllConfigBytes());
foreach (Type type in configBytes.Keys)
{
byte[] oneConfigBytes = configBytes[type];
this.LoadOneInThread(type, oneConfigBytes);
}
}
public async ETTask LoadAsync()
{
this.allConfig.Clear();
Dictionary configBytes = EventSystem.Instance.Invoke>(0, new GetAllConfigBytes());
using ListComponent listTasks = ListComponent.Create();
foreach (Type type in configBytes.Keys)
{
byte[] oneConfigBytes = configBytes[type];
Task task = Task.Run(() => LoadOneInThread(type, oneConfigBytes));
listTasks.Add(task);
}
await Task.WhenAll(listTasks.ToArray());
foreach (ISingleton category in this.allConfig.Values)
{
category.Register();
}
Log.Debug("register all config ok");
}
private void LoadOneInThread(Type configType, byte[] oneConfigBytes)
{
object category = SerializeHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
Log.Debug($"Deserialize: {configType} ok");
lock (this)
{
this.allConfig[configType] = category as ISingleton;
}
}
}
}