using System; using System.Collections.Generic; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Cysharp.Threading.Tasks; namespace BITKit { public static class DataParser { public static readonly Action OnSet = Set; [BITCommand] public static void Get(string key) { BIT4Log.Log(Data.Get(key)); } [BITCommand] public static void Set(string key, string value) { if (Guid.TryParse(value, out var guidResult)) { Data.Set(key, guidResult); } else if (bool.TryParse(value, out var boolResult)) { Data.Set(key, boolResult); Data.Set(key, boolResult ? 1 : 0); } else if (int.TryParse(value, out var intResult)) { Data.Set(key, intResult); Data.Set(key, intResult is 1); } else if (float.TryParse(value, out var floatResult)) { Data.Set(key, floatResult); } else { Data.Set(key, value); } } [BITCommand] public static void SetContainer(string key, string typeName, string json) { try { var type = Type.GetType(typeName, true); var value = JsonConvert.DeserializeObject(json, type); } catch (System.Exception) { throw; //Debug.LogException(e); } } [BITCommand] public static async void ShowAllData() { await UniTask.SwitchToThreadPool(); StringBuilder stringBuilder = new(); lock (Data.Objects) { Dictionary copy = new(Data.Objects); foreach (var item in copy) { stringBuilder.AppendLine($"{item.Key}:{item.Value}"); } } } public static void Set(string json) { if (string.IsNullOrEmpty(json) is false) { try { ForEach(JObject.Parse(json)); } catch (Exception) { throw; } } } static void ForEach(JToken jToken) { var path = jToken.Path; switch (jToken.Type) { case JTokenType.Array: Data.Set(path, jToken.ToObject()); break; case JTokenType.Boolean: Data.Set(path, jToken.ToObject()); goto case JTokenType.String; case JTokenType.Bytes: Data.Set(path, jToken.ToObject()); break; case JTokenType.Date: Data.Set(path, jToken.ToObject()); goto case JTokenType.String; case JTokenType.Float: Data.Set(path, jToken.ToObject()); goto case JTokenType.String; case JTokenType.Guid: Data.Set(path, jToken.ToObject()); goto case JTokenType.String; case JTokenType.Integer: Data.Set(path, jToken.ToObject()); goto case JTokenType.String; case JTokenType.String: Data.Set(path, jToken.ToObject()); break; case JTokenType.TimeSpan: Data.Set(path, jToken.ToObject()); goto case JTokenType.String; case JTokenType.Property: Data.Set(path, jToken.ToObject()); /* var list = Data.rawEvents.GetOrAdd(path,x=> new()); foreach (var item in list) { var type = item.GetType(); var genericType = item.GetType().GetGenericArguments().First(); try { var value = jToken.ToObject(genericType); var method = type.GetMethod(nameof(Action.Invoke)); method.Invoke(value, new object[] { value }); } catch (Exception e) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"�ѻ�ȡ���ĺ�������:{genericType}"); stringBuilder.AppendLine($"�ѻ�ȡ������������:{type}"); BIT4Log.LogException(e); } } Data.Set(path, jToken.ToObject()); */ break; case JTokenType.Object: Data.Set(path, jToken.ToObject()); break; } foreach (var j in jToken) { ForEach(j); } } } }