using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Base
{
///
/// 管理该所有的配置
///
///
public abstract class ACategory: ICategory where T : AConfig
{
protected Dictionary dict;
public virtual void BeginInit()
{
this.dict = new Dictionary();
string path = $@"Config/{typeof (T).Name}";
string configStr;
try
{
configStr = ((GameObject)Resources.Load("Config")).Get(typeof(T).Name).text;
}
catch (Exception)
{
throw new GameException($"load config file fail, path: {path}");
}
foreach (string str in configStr.Split(new[] { "\r\n" }, StringSplitOptions.None))
{
try
{
string str2 = str.Trim();
if (str2 == "")
{
continue;
}
T t = MongoHelper.FromJson(str2);
this.dict.Add(t.Id, t);
}
catch (Exception e)
{
throw new GameException($"parser json fail: {str}", e);
}
}
}
public Type ConfigType
{
get
{
return typeof (T);
}
}
public virtual void EndInit()
{
}
public T this[long type]
{
get
{
T t;
if (!this.dict.TryGetValue(type, out t))
{
throw new ConfigException($"{typeof (T)} 没有找到配置, key: {type}");
}
return t;
}
}
public T TryGet(int type)
{
T t;
if (!this.dict.TryGetValue(type, out t))
{
return null;
}
return t;
}
public T[] GetAll()
{
return this.dict.Values.ToArray();
}
public T GetOne()
{
return this.dict.Values.First();
}
}
}