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(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(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(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(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(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 GetDic(string msg) { Dictionary dic = null; string[] segs = GetMultipleStr(msg); if (segs != null && segs.Length > 0) { dic = new Dictionary(); 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> Analysis(string val)//正则表达式的字符串分割 { string s = ServerStringTrim(val); if (string.IsNullOrEmpty(s)) { return null; } s = s.Replace(" ", string.Empty); var dict = JsonMapper.ToObject>(s); if (dict == null || dict.Count == 0) return null; Dictionary> result = new Dictionary>(); foreach (var item in dict) { List list = new List(); 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> dics = new Dictionary>(); // 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 list = new List(); // 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 ParseIntDict(string jsonStr) { if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr)) { return new Dictionary(); } var dict = JsonMapper.ToObject>(jsonStr); Dictionary result = new Dictionary(); foreach (var item in dict) { result[int.Parse(item.Key)] = item.Value; } return result; } //json格式: {"1":[1,2],"2":[3,4]} public static Dictionary ParseIntArrayDict(string jsonStr) { if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr)) { return new Dictionary(); } var dict = JsonMapper.ToObject>(jsonStr); Dictionary result = new Dictionary(); foreach (var item in dict) { result[int.Parse(item.Key)] = item.Value; } return result; } //json格式: {"1":[[1,2],[3,4]]} public static Dictionary ParseIntArray2Dict(string jsonStr) { if (jsonStr == "{}" || string.IsNullOrEmpty(jsonStr)) { return new Dictionary(); } var dict = JsonMapper.ToObject>(jsonStr); Dictionary result = new Dictionary(); foreach (var item in dict) { result[int.Parse(item.Key)] = item.Value; } return result; } //万分率转为每个id对应的概率 [[万分概率,id1],[万分概率,id2]] public static Dictionary GetRateDict(int[][] rateArray) { Dictionary dic = new Dictionary(); //概率为 减去上一个概率的值即为当前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; } }