73 lines
2.1 KiB
C#
73 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace ET
|
|
{
|
|
|
|
public class ConfigComponentAwakeSystem : AwakeSystem<ConfigComponent>
|
|
{
|
|
public override void Awake(ConfigComponent self)
|
|
{
|
|
ConfigComponent.Instance = self;
|
|
self.Load();
|
|
}
|
|
}
|
|
|
|
|
|
public class ConfigComponentLoadSystem : LoadSystem<ConfigComponent>
|
|
{
|
|
public override void Load(ConfigComponent self)
|
|
{
|
|
self.Load();
|
|
}
|
|
}
|
|
public static class ConfigComponentSystem
|
|
{
|
|
public static void Load(this ConfigComponent self)
|
|
{
|
|
self.allConfig.Clear();
|
|
HashSet<Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute));
|
|
|
|
foreach (Type type in types)
|
|
{
|
|
object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
|
|
if (attrs.Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
ConfigAttribute configAttribute = attrs[0] as ConfigAttribute;
|
|
|
|
object obj = Activator.CreateInstance(type);
|
|
|
|
ACategory iCategory = obj as ACategory;
|
|
if (iCategory == null)
|
|
{
|
|
throw new Exception($"class: {type.Name} not inherit from ACategory");
|
|
}
|
|
iCategory.BeginInit();
|
|
iCategory.LoadAsync();
|
|
iCategory.EndInit();
|
|
|
|
self.allConfig[iCategory.ConfigType] = iCategory;
|
|
}
|
|
}
|
|
public static T Get<T>(this ConfigComponent self,long id)where T:class
|
|
{
|
|
if(self.allConfig.TryGetValue(typeof(T),out ACategory aCategory))
|
|
{
|
|
return aCategory.TryGet(id) as T;
|
|
}
|
|
return null;
|
|
}
|
|
public static IEnumerable<T> GetAll<T>(this ConfigComponent self) where T : class
|
|
{
|
|
if (self.allConfig.TryGetValue(typeof(T), out ACategory aCategory))
|
|
{
|
|
return aCategory.GetAll<T>();
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|