12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace ET
- {
-
-
-
- public class ConfigComponent: Singleton<ConfigComponent>
- {
- public struct GetAllConfigBytes
- {
- }
-
- public struct GetOneConfigBytes
- {
- public string ConfigName;
- }
-
- private readonly Dictionary<Type, ISingleton> allConfig = new Dictionary<Type, ISingleton>();
- 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<GetOneConfigBytes, byte[]>(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<Type, byte[]> configBytes = EventSystem.Instance.Invoke<GetAllConfigBytes, Dictionary<Type, byte[]>>(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<Type, byte[]> configBytes = EventSystem.Instance.Invoke<GetAllConfigBytes, Dictionary<Type, byte[]>>(0, new GetAllConfigBytes());
- using ListComponent<Task> listTasks = ListComponent<Task>.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;
- }
- }
- }
- }
|