using System.Collections.Generic;  
 | 
using UnityEngine;  
 | 
using Cysharp.Threading.Tasks;  
 | 
using System.IO;  
 | 
using System;  
 | 
  
 | 
  
 | 
public class ConfigBase<U, T> where T : ConfigBase<U, T>, new()  
 | 
{  
 | 
    /// <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)  
 | 
    {  
 | 
        if (m_dic.ContainsKey(id))  
 | 
        {  
 | 
            return m_dic[id];  
 | 
        }  
 | 
  
 | 
        return null;  
 | 
    }  
 | 
  
 | 
    // public static void ForceRelease()  
 | 
    // {  
 | 
    //     m_dic.Clear();  
 | 
    //     isInit = false;  
 | 
    // }  
 | 
  
 | 
    public static List<U> GetKeys()  
 | 
    {  
 | 
        List<U> result = new List<U>();  
 | 
        result.AddRange(m_dic.Keys);  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
  
 | 
    public static List<T> GetValues()  
 | 
    {  
 | 
        List<T> result = new List<T>();  
 | 
        result.AddRange(m_dic.Values);  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    public static bool HasKey(U key)  
 | 
    {  
 | 
        return m_dic.ContainsKey(key);  
 | 
    }  
 | 
  
 | 
    public static void LazyInit()  
 | 
    {  
 | 
        if (!isInit)  
 | 
        {  
 | 
            //  实际上是同步的  
 | 
            ConfigManager.Instance.LoadConfigByType(typeof(T));  
 | 
        }  
 | 
    }  
 | 
  
 | 
    public static void Init(string[] lines)  
 | 
    {  
 | 
        if (isInit)  
 | 
        {  
 | 
            return;  
 | 
        }  
 | 
  
 | 
        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();  
 | 
        // }  
 | 
  
 | 
        isInit = true;  
 | 
    }  
 | 
  
 | 
    public virtual U LoadKey(string line)  
 | 
    {  
 | 
        return default(U);  
 | 
    }  
 | 
  
 | 
    protected virtual void AllConfigLoadFinish()  
 | 
    {  
 | 
          
 | 
    }  
 | 
  
 | 
    public virtual void LoadConfig(string line)  
 | 
    {  
 | 
          
 | 
    }  
 | 
  
 | 
    protected virtual void OnConfigParseCompleted()  
 | 
    {  
 | 
          
 | 
    }  
 | 
      
 | 
    protected int ParseInt(string str)  
 | 
    {  
 | 
        int result = 0;  
 | 
        int.TryParse(str, out result);  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    protected float ParseFloat(string str)  
 | 
    {  
 | 
        float result = 0f;  
 | 
        float.TryParse(str, out result);  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    protected string[] Split(string str, char split)  
 | 
    {  
 | 
        return str.Split(split);  
 | 
    }  
 | 
  
 | 
    protected List<string> ParseStrList(string str, char split)  
 | 
    {  
 | 
        List<string> result = new List<string>();  
 | 
        string[] strs = Split(str, split);  
 | 
        for (int i = 0; i < strs.Length; i++)  
 | 
        {  
 | 
            result.Add(strs[i]);  
 | 
        }  
 | 
  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    protected List<int> ParseIntList(string str, char split)  
 | 
    {  
 | 
        List<int> result = new List<int>();  
 | 
        string[] strs = Split(str, split);  
 | 
        for (int i = 0; i < strs.Length; i++)  
 | 
        {  
 | 
            result.Add(ParseInt(strs[i]));  
 | 
        }  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    protected U GetKey(string key)  
 | 
    {  
 | 
        if (typeof(U) == typeof(string))  
 | 
        {  
 | 
            return (U)(object)key;  
 | 
        }  
 | 
        else if (typeof(U) == typeof(int))  
 | 
        {  
 | 
            return (U)(object)ParseInt(key);  
 | 
        }  
 | 
        else  
 | 
        {  
 | 
            Debug.LogError("GetKey 意外的key类型 类型错误 " + typeof(U).Name);  
 | 
            return default(U);  
 | 
        }  
 | 
    }  
 | 
} 
 |