| 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;  | 
|     }  | 
|   | 
|     //一维数组:来源格式如 1|2|3|4  | 
|     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('_');  | 
|     }  | 
|   | 
|     //一维数组:来源格式如 1_2|2_3|3_1|4_3;  | 
|     //返回一维数组结构如 1|2|3|4  | 
|     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;  | 
|     }  | 
|   | 
|     //一维数组:来源格式如 1_2|2_3|3_1|4_3;  | 
|     //返回一维数组结构如 2|3|1|3  | 
|     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;  | 
|     }  | 
|   | 
|     //二维数组:来源格式如 1_2_1|2_3_4|3_1_2|4_3_4  | 
|     public static T[][] GetArray2<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] = new T[pair.Length];  | 
|                     for (int j = 0; j < pair.Length; j++)  | 
|                     {  | 
|                         array[i][j] = (T)Convert.ChangeType(pair[j], typeof(T));  | 
|                     }  | 
|                 }  | 
|             }  | 
|             return array;  | 
|         }  | 
|         return null;  | 
|     }  | 
|   | 
|     //字典:来源格式如 1_2|2_3|3_1|4_3;  | 
|     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 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;  | 
|         //}  | 
|     }  | 
|   | 
|   | 
|     //json格式: {"1":1}  | 
|     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;  | 
|     }  | 
|   | 
|     //json格式: {"1":[1,2],"2":[3,4]}  | 
|     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;  | 
|     }  | 
|   | 
|     //json格式: {"1":[[1,2],[3,4]]}  | 
|     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;  | 
|     }  | 
| }  |