using LitJson;  
 | 
using System;  
 | 
using System.Collections;  
 | 
using System.Collections.Generic;  
 | 
using System.Text.RegularExpressions;  
 | 
using UnityEngine;  
 | 
  
 | 
public class ConfigParse  
 | 
{  
 | 
    public enum SegStrType  
 | 
    {  
 | 
        Multiple,  
 | 
        KeyValue,  
 | 
    }  
 | 
  
 | 
    public static T? GetSegValue<T>(string msg, string key, SegStrType type) where T : struct  
 | 
    {  
 | 
        T? result = null;  
 | 
        switch (type)  
 | 
        {  
 | 
            case SegStrType.Multiple:  
 | 
                {  
 | 
                    uint index = 0;  
 | 
                    uint.TryParse(key, out index);  
 | 
                    result = (T)Convert.ChangeType(GetMultipleStr(msg, index), typeof(T));  
 | 
                }  
 | 
                break;  
 | 
            case SegStrType.KeyValue:  
 | 
                {  
 | 
                    result = (T)Convert.ChangeType(GetKeyValue(msg, key), typeof(T));  
 | 
                }  
 | 
                break;  
 | 
        }  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    public static string GetSegValue(string msg, string key, SegStrType type)  
 | 
    {  
 | 
        switch (type)  
 | 
        {  
 | 
            case SegStrType.Multiple:  
 | 
                {  
 | 
                    uint index = 0;  
 | 
                    uint.TryParse(key, out index);  
 | 
                    return GetMultipleStr(msg, index);  
 | 
                }  
 | 
            case SegStrType.KeyValue:  
 | 
                {  
 | 
                    return GetKeyValue(msg, key);  
 | 
                }  
 | 
        }  
 | 
        return string.Empty;  
 | 
    }  
 | 
  
 | 
    private static string GetMultipleStr(string msg, uint index)  
 | 
    {  
 | 
        string[] segs = GetMultipleStr(msg);  
 | 
        if (segs != null && index < segs.Length)  
 | 
        {  
 | 
            return segs[index];  
 | 
        }  
 | 
        return string.Empty;  
 | 
    }  
 | 
  
 | 
    public static T[] GetMultipleStr<T>(string msg) where T : struct  
 | 
    {  
 | 
        string[] segs = GetMultipleStr(msg);  
 | 
        if (segs != null && segs.Length > 0)  
 | 
        {  
 | 
            T[] array = new T[segs.Length];  
 | 
            for (int i = 0; i < segs.Length; i++)  
 | 
            {  
 | 
                array[i] = (T)Convert.ChangeType(segs[i], typeof(T));  
 | 
            }  
 | 
            return array;  
 | 
        }  
 | 
        return null;  
 | 
    }  
 | 
  
 | 
    public static string[] GetMultipleStr(string msg)  
 | 
    {  
 | 
        string[] segs = msg.Split('|');  
 | 
        if (segs.Length == 1 && segs[0].Equals(string.Empty)) return null;  
 | 
        return segs;  
 | 
    }  
 | 
  
 | 
    private static string GetKeyValue(string msg, string key)  
 | 
    {  
 | 
        string[] segs = GetMultipleStr(msg);  
 | 
        for (int i = 0; i < segs.Length; i++)  
 | 
        {  
 | 
            string[] pair = GetKeyValue(segs[i]);  
 | 
            if (pair.Length > 1)  
 | 
            {  
 | 
                if (!pair[0].Equals(key)) continue;  
 | 
                else return pair[1];  
 | 
            }  
 | 
            else continue;  
 | 
        }  
 | 
        return string.Empty;  
 | 
    }  
 | 
  
 | 
    private static string[] GetKeyValue(string msg)  
 | 
    {  
 | 
        return msg.Split('_');  
 | 
    }  
 | 
  
 | 
    public static T[] GetKeyValueKeys<T>(string msg) where T : struct  
 | 
    {  
 | 
        string[] segs = GetMultipleStr(msg);  
 | 
        if (segs != null && segs.Length > 0)  
 | 
        {  
 | 
            T[] array = new T[segs.Length];  
 | 
            for (int i = 0; i < segs.Length; i++)  
 | 
            {  
 | 
                string[] pair = GetKeyValue(segs[i]);  
 | 
                if (pair.Length > 1)  
 | 
                {  
 | 
                    array[i] = (T)Convert.ChangeType(pair[0], typeof(T));  
 | 
                }  
 | 
            }  
 | 
            return array;  
 | 
        }  
 | 
        return null;  
 | 
    }  
 | 
  
 | 
    public static T[] GetKeyValueValues<T>(string msg) where T : struct  
 | 
    {  
 | 
        string[] segs = GetMultipleStr(msg);  
 | 
        if (segs != null && segs.Length > 0)  
 | 
        {  
 | 
            T[] array = new T[segs.Length];  
 | 
            for (int i = 0; i < segs.Length; i++)  
 | 
            {  
 | 
                string[] pair = GetKeyValue(segs[i]);  
 | 
                if (pair.Length > 1)  
 | 
                {  
 | 
                    array[i] = (T)Convert.ChangeType(pair[1], typeof(T));  
 | 
                }  
 | 
            }  
 | 
            return array;  
 | 
        }  
 | 
        return null;  
 | 
    }  
 | 
  
 | 
    public static Dictionary<T, P> GetDic<T, P>(string msg)  
 | 
    {  
 | 
        Dictionary<T, P> dic = null;  
 | 
        string[] segs = GetMultipleStr(msg);  
 | 
        if (segs != null && segs.Length > 0)  
 | 
        {  
 | 
            dic = new Dictionary<T, P>();  
 | 
            for (int i = 0; i < segs.Length; i++)  
 | 
            {  
 | 
                string[] pair = GetKeyValue(segs[i]);  
 | 
                if (pair.Length > 1)  
 | 
                {  
 | 
                    dic.Add((T)Convert.ChangeType(pair[0], typeof(T)), (P)Convert.ChangeType(pair[1], typeof(P)));  
 | 
                }  
 | 
            }  
 | 
        }  
 | 
        return dic;  
 | 
    }  
 | 
  
 | 
    public static string ServerStringTrim(string str)  
 | 
    {  
 | 
        if (!string.IsNullOrEmpty(str))  
 | 
        {  
 | 
            str = str.Replace("\0", "");  
 | 
            str = str.Replace("\x00", "");  
 | 
            return str;  
 | 
        }  
 | 
        else  
 | 
        {  
 | 
            return string.Empty;  
 | 
        }  
 | 
    }  
 | 
  
 | 
    //{'17':['63','6','27'],'65':['800'],'55':['139'],'19':['1000','2600','130']}  
 | 
    public static Regex userDataRegex = new Regex(@"'([0-9]+)':\[(.*?)\]", RegexOptions.Singleline);  
 | 
    public static Dictionary<int, List<int>> Analysis(string val)//正则表达式的字符串分割  
 | 
    {  
 | 
        string s = ServerStringTrim(val);  
 | 
        if (string.IsNullOrEmpty(s))  
 | 
        {  
 | 
            return null;  
 | 
        }  
 | 
  
 | 
        s = s.Replace(" ", string.Empty);  
 | 
  
 | 
  
 | 
        var dict = JsonMapper.ToObject<Dictionary<string, string[]>>(s);  
 | 
  
 | 
        if (dict == null || dict.Count == 0)  
 | 
            return null;  
 | 
  
 | 
        Dictionary<int, List<int>> result = new Dictionary<int, List<int>>();  
 | 
  
 | 
        foreach (var item in dict)  
 | 
        {  
 | 
            List<int> list = new List<int>();  
 | 
            for (int i = 0; i < item.Value.Length; i++)  
 | 
            {  
 | 
                list.Add(int.Parse(item.Value[i]));  
 | 
            }  
 | 
            if (list.Count != 0)  
 | 
                result[int.Parse(item.Key)] = list;  
 | 
        }  
 | 
  
 | 
        return result;  
 | 
  
 | 
        //if (!userDataRegex.IsMatch(s))  
 | 
        //{  
 | 
        //    return null;  
 | 
        //}  
 | 
        //else  
 | 
        //{  
 | 
        //    Dictionary<int, List<int>> dics = new Dictionary<int, List<int>>();  
 | 
        //    foreach (Match match in userDataRegex.Matches(s))  
 | 
        //    {  
 | 
        //        int id = int.Parse(match.Groups[1].Value);  
 | 
        //        string str = match.Groups[2].Value;  
 | 
        //        string[] vals = str.Split(',');  
 | 
        //        List<int> list = new List<int>();  
 | 
        //        for (int i = 0; i < vals.Length; i++)  
 | 
        //        {  
 | 
        //            int intval = int.Parse(vals[i].Replace('\'', ' '));  
 | 
        //            list.Add(intval);  
 | 
        //        }  
 | 
        //        if (!dics.ContainsKey(id))  
 | 
        //        {  
 | 
        //            dics.Add(id, list);  
 | 
        //        }  
 | 
        //    }  
 | 
        //    return dics;  
 | 
        //}  
 | 
    }  
 | 
  
 | 
    public static Dictionary<int, List<int>> ParseJsonDict(string jsonStr)  
 | 
    {  
 | 
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))  
 | 
        {  
 | 
            return new Dictionary<int, List<int>>();  
 | 
        }  
 | 
        var dict = JsonMapper.ToObject<Dictionary<string, List<int>>>(jsonStr);  
 | 
        Dictionary<int, List<int>> result = new Dictionary<int, List<int>>();  
 | 
  
 | 
        foreach (var item in dict)  
 | 
        {  
 | 
            result[int.Parse(item.Key)] = item.Value;  
 | 
        }  
 | 
  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    public static Dictionary<int, int> ParseIntDict(string jsonStr)  
 | 
    {  
 | 
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))  
 | 
        {  
 | 
            return new Dictionary<int, int>();  
 | 
        }  
 | 
        var dict = JsonMapper.ToObject<Dictionary<string, int>>(jsonStr);  
 | 
        Dictionary<int, int> result = new Dictionary<int, int>();  
 | 
  
 | 
        foreach (var item in dict)  
 | 
        {  
 | 
            result[int.Parse(item.Key)] = item.Value;  
 | 
        }  
 | 
  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    public static Dictionary<int, int[]> ParseIntArrayDict(string jsonStr)  
 | 
    {  
 | 
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))  
 | 
        {  
 | 
            return new Dictionary<int, int[]>();  
 | 
        }  
 | 
        var dict = JsonMapper.ToObject<Dictionary<string, int[]>>(jsonStr);  
 | 
        Dictionary<int, int[]> result = new Dictionary<int, int[]>();  
 | 
  
 | 
        foreach (var item in dict)  
 | 
        {  
 | 
            result[int.Parse(item.Key)] = item.Value;  
 | 
        }  
 | 
  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
    public static Dictionary<int, int[][]> ParseIntArray2Dict(string jsonStr)  
 | 
    {  
 | 
        if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr))  
 | 
        {  
 | 
            return new Dictionary<int, int[][]>();  
 | 
        }  
 | 
        var dict = JsonMapper.ToObject<Dictionary<string, int[][]>>(jsonStr);  
 | 
        Dictionary<int, int[][]> result = new Dictionary<int, int[][]>();  
 | 
  
 | 
        foreach (var item in dict)  
 | 
        {  
 | 
            result[int.Parse(item.Key)] = item.Value;  
 | 
        }  
 | 
  
 | 
        return result;  
 | 
    }  
 | 
  
 | 
  
 | 
    //万分率转为每个id对应的概率 [[万分概率,id1],[万分概率,id2]]  
 | 
    public static Dictionary<int, int> GetRateDict(int[][] rateArray)  
 | 
    {  
 | 
        Dictionary<int, int> dic = new Dictionary<int, int>();  
 | 
        //概率为 减去上一个概率的值即为当前ID概率  
 | 
        for (int i = 0;i< rateArray.Length; i++)  
 | 
        {  
 | 
            if (i > 0)  
 | 
            {  
 | 
                dic[rateArray[i][1]] = rateArray[i][0] - rateArray[i - 1][0];  
 | 
            }  
 | 
            else  
 | 
            {  
 | 
                dic[rateArray[i][1]] = rateArray[i][0];  
 | 
            }  
 | 
        }  
 | 
  
 | 
        return dic;  
 | 
    }  
 | 
}  
 |