154 lines
5.3 KiB
C#
154 lines
5.3 KiB
C#
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<string> OnSet = Set;
|
||
[BITCommand]
|
||
public static void Get(string key)
|
||
{
|
||
BIT4Log.Log(Data.Get<string>(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<string, object> 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<JArray>());
|
||
break;
|
||
case JTokenType.Boolean:
|
||
Data.Set(path, jToken.ToObject<bool>());
|
||
goto case JTokenType.String;
|
||
case JTokenType.Bytes:
|
||
Data.Set(path, jToken.ToObject<byte[]>());
|
||
break;
|
||
case JTokenType.Date:
|
||
Data.Set(path, jToken.ToObject<DateTime>());
|
||
goto case JTokenType.String;
|
||
case JTokenType.Float:
|
||
Data.Set(path, jToken.ToObject<float>());
|
||
goto case JTokenType.String;
|
||
case JTokenType.Guid:
|
||
Data.Set(path, jToken.ToObject<Guid>());
|
||
goto case JTokenType.String;
|
||
case JTokenType.Integer:
|
||
Data.Set(path, jToken.ToObject<int>());
|
||
goto case JTokenType.String;
|
||
case JTokenType.String:
|
||
Data.Set(path, jToken.ToObject<string>());
|
||
break;
|
||
case JTokenType.TimeSpan:
|
||
Data.Set(path, jToken.ToObject<TimeSpan>());
|
||
goto case JTokenType.String;
|
||
case JTokenType.Property:
|
||
Data.Set(path, jToken.ToObject<JProperty>());
|
||
/*
|
||
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($"<22>ѻ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:{genericType}");
|
||
stringBuilder.AppendLine($"<22>ѻ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:{type}");
|
||
BIT4Log.LogException(e);
|
||
}
|
||
}
|
||
Data.Set(path, jToken.ToObject<JProperty>());
|
||
*/
|
||
break;
|
||
case JTokenType.Object:
|
||
Data.Set(path, jToken.ToObject<JObject>());
|
||
break;
|
||
}
|
||
foreach (var j in jToken)
|
||
{
|
||
ForEach(j);
|
||
}
|
||
}
|
||
}
|
||
} |