hch
2025-09-17 f306d23e461ab236ed88173f7b4a5bc35a3e9bc9
Main/Config/ConfigBase.cs
@@ -7,49 +7,67 @@
public class ConfigBase<U, T> where T : ConfigBase<U, T>, new()
{
    public static Dictionary<U, T> dic = new Dictionary<U, T>();
    /// <summary>
    /// 是否访问过静态构造函数
    /// </summary>
    public static bool visit = false;
    static ConfigBase()
    {
        if (isInit)
        {
            return;
        }
        LazyInit();
    }
    private static Dictionary<U, T> m_dic = new Dictionary<U, T>();
    public static Dictionary<U, T> dic
    {
        get
        {
            return m_dic;
        }
    }
    public static bool isInit = false;
    public static T Get(U id)
    {
        LazyInit();
        if (dic.ContainsKey(id))
        if (m_dic.ContainsKey(id))
        {
            return dic[id];
            return m_dic[id];
        }
        return null;
    }
    public static void ForceInit()
    {
        ConfigManager.Instance.LoadConfigByType(typeof(T));
    }
    // public static void ForceRelease()
    // {
    //     m_dic.Clear();
    //     isInit = false;
    // }
    public static List<U> GetKeys()
    {
        LazyInit();
        List<U> result = new List<U>();
        result.AddRange(dic.Keys);
        result.AddRange(m_dic.Keys);
        return result;
    }
    public static List<T> GetValues()
    {
        LazyInit();
        List<T> result = new List<T>();
        result.AddRange(dic.Values);
        result.AddRange(m_dic.Values);
        return result;
    }
    public static bool HasKey(U key)
    {
        LazyInit();
        return dic.ContainsKey(key);
        return m_dic.ContainsKey(key);
    }
    public static void LazyInit()
@@ -63,37 +81,40 @@
    public static void Init(string[] lines)
    {
        dic.Clear();
        for (int i = 3; i < lines.Length; i++)
        if (isInit)
        {
            string line = lines[i];
            var index = line.IndexOf("\t");
            if (index == -1)
            {
                continue;
            }
            string strKey = line.Substring(0, index);
            T config = new T();
            U key = config.LoadKey(strKey);
            config.LoadConfig(line);
            config.OnConfigParseCompleted();
#if UNITY_EDITOR
            try
            {
#endif
                dic.Add(key, config);
#if UNITY_EDITOR
            }
            catch (ArgumentException exception)
            {
                Debug.LogError(typeof(T).Name + " 重复的key " + key + " " + exception.Message);
            }
#endif
            return;
        }
        foreach (var cfg in dic.Values)
        for (int i = 3; i < lines.Length; i++)
            {
                string line = lines[i];
                var index = line.IndexOf("\t");
                if (index == -1)
                {
                    continue;
                }
                string strKey = line.Substring(0, index);
                T config = new T();
                U key = config.LoadKey(strKey);
                config.LoadConfig(line);
                config.OnConfigParseCompleted();
#if UNITY_EDITOR
                try
                {
#endif
                    m_dic.Add(key, config);
#if UNITY_EDITOR
                }
                catch (ArgumentException exception)
                {
                    Debug.LogError(typeof(T).Name + " 重复的key " + key + " " + exception.Message);
                }
#endif
            }
        foreach (var cfg in m_dic.Values)
        {
            cfg.AllConfigLoadFinish();
        }