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('_'); } 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; } 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; } 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 Regex userDataRegex = new Regex(@"'([0-9]+)':\[(.*?)\]", RegexOptions.Singleline); public static Dictionary> Analysis(string val)//正则表达式的字符串分割 { string s = ServerStringTrim(val); if (string.IsNullOrEmpty(s)) { return null; } s = s.Replace(" ", string.Empty); 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; } } public static Dictionary> ParseJsonDict(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; } 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; } 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; } 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; } }