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