zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Module/Config/ACategory.cs

75 lines
1.9 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
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
{
2021-04-11 19:50:39 +08:00
string str = ConfigHelper.GetText(typeof(T).Name);
2021-04-08 20:09:59 +08:00
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();
}
}
}