75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace ET
|
|
{
|
|
public abstract class ACategory : ET.Object
|
|
{
|
|
public abstract Type ConfigType { get; }
|
|
public abstract IConfig GetOne();
|
|
public abstract void LoadAsync();
|
|
public abstract IEnumerable<K> GetAll<K>();
|
|
public abstract IConfig TryGet(long type) ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 管理该所有的配置
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public abstract class ACategory<T> : ACategory where T : IConfig
|
|
{
|
|
protected Dictionary<long, T> dict;
|
|
|
|
public override void BeginInit()
|
|
{
|
|
|
|
}
|
|
public override void LoadAsync()
|
|
{
|
|
try
|
|
{
|
|
string str = ConfigHelper.GetText(typeof(T).Name);
|
|
this.dict = ConfigHelper.ToObject<Dictionary<long, T>>(str);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new Exception($"parser json fail: {typeof(T).Name}", e);
|
|
}
|
|
}
|
|
|
|
public override Type ConfigType => typeof(T);
|
|
|
|
public override void EndInit()
|
|
{
|
|
}
|
|
|
|
public override IConfig TryGet(long type)
|
|
{
|
|
if (!this.dict.TryGetValue(type, out T t))
|
|
{
|
|
return null;
|
|
}
|
|
return t;
|
|
}
|
|
public T Get(long id,bool canError =true)
|
|
{
|
|
if (!this.dict.TryGetValue(id, out T t)&& canError)
|
|
{
|
|
Log.Error($"config == null which type = {GetType()} when id = {id}");
|
|
return default;
|
|
}
|
|
return t;
|
|
}
|
|
public override IEnumerable<K> GetAll<K>()
|
|
{
|
|
return this.dict.Values as IEnumerable<K>;
|
|
}
|
|
|
|
public override IConfig GetOne()
|
|
{
|
|
return this.dict.Values.First();
|
|
}
|
|
}
|
|
} |