This commit is contained in:
2026-05-13 23:02:02 +07:00
parent 5025383676
commit 93da00c206
885 changed files with 980996 additions and 1987 deletions

View File

@@ -0,0 +1,154 @@
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using DA_Assets.Extensions;
#if JSONNET_EXISTS
using Newtonsoft.Json;
#endif
namespace DA_Assets.FCU
{
public class DAFormatter
{
static List<string> GetJsonPropertyNames<T>()
{
List<string> propertyNames = new List<string>();
FieldInfo[] fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var field in fields)
{
#if JSONNET_EXISTS
JsonPropertyAttribute attribute = field.GetCustomAttribute<JsonPropertyAttribute>();
if (attribute != null)
{
propertyNames.Add(attribute.PropertyName ?? field.Name);
}
#endif
}
foreach (var property in properties)
{
#if JSONNET_EXISTS
JsonPropertyAttribute attribute = property.GetCustomAttribute<JsonPropertyAttribute>();
if (attribute != null)
{
propertyNames.Add(attribute.PropertyName ?? property.Name);
}
#endif
}
return propertyNames;
}
private const int indentLength = 4;
private static string Repeat(int n) => new string(' ', n * indentLength);
public static JFResult Format<T>(string str)
{
JFResult jsonFormatResult = new JFResult();
if (str.IsEmpty())
{
return new JFResult
{
Json = string.Empty,
IsValid = false,
MatchTargetType = false
};
}
List<string> typeNames = GetJsonPropertyNames<T>();
foreach (string typeName in typeNames)
{
if (str.Contains($"\"{typeName}\""))
{
jsonFormatResult.MatchTargetType = true;
break;
}
}
bool hasOpenBrace = false;
bool hasCloseBrace = false;
int indent = 0;
bool quoted = false;
StringBuilder sb = new StringBuilder();
for (var i = 0; i < str.Length; i++)
{
var ch = str[i];
switch (ch)
{
case '{':
case '[':
if (ch == '{')
hasOpenBrace = true;
sb.Append(ch);
if (quoted == false)
{
sb.AppendLine();
sb.Append(Repeat(++indent));
}
break;
case '}':
case ']':
if (ch == '}')
hasCloseBrace = true;
if (quoted == false)
{
sb.AppendLine();
sb.Append(Repeat(--indent));
}
sb.Append(ch);
break;
case '"':
sb.Append(ch);
bool escaped = false;
var index = i;
while (index > 0 && str[--index] == '\\')
escaped = !escaped;
if (escaped == false)
quoted = !quoted;
break;
case ',':
sb.Append(ch);
if (quoted == false)
{
sb.AppendLine();
sb.Append(Repeat(indent));
}
break;
case ':':
sb.Append(ch);
if (quoted == false)
sb.Append(" ");
break;
default:
sb.Append(ch);
break;
}
}
jsonFormatResult.Json = sb.ToString();
jsonFormatResult.IsValid = hasOpenBrace && hasCloseBrace;
return jsonFormatResult;
}
}
public struct JFResult
{
public bool IsValid { get; set; }
public string Json { get; set; }
public bool MatchTargetType { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 06a73c612ca81644c9da29771f70be48
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,98 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
#if JSONNET_EXISTS
using Newtonsoft.Json;
#endif
namespace DA_Assets.FCU
{
public class DAJson
{
#if JSONNET_EXISTS
private static JsonSerializerSettings settings = new JsonSerializerSettings()
{
Error = (sender, error) => error.ErrorContext.Handled = true,
Formatting = Formatting.Indented
};
#endif
public static string ToJson(object obj)
{
#if JSONNET_EXISTS
return JsonConvert.SerializeObject(obj, settings);
#else
return "";
#endif
}
public static T FromJson<T>(string json)
{
#if JSONNET_EXISTS
return JsonConvert.DeserializeObject<T>(json, settings);
#else
return default(T);
#endif
}
public static async Task<DAResult<T>> FromJsonAsync<T>(string json)
{
DAResult<T> @return = new DAResult<T>();
try
{
#if JSONNET_EXISTS == false
throw new MissingComponentException("Json.NET packaghe is not installed.");
#endif
JFResult jfr = default;
await Task.Run(() =>
{
jfr = DAFormatter.Format<T>(json);
});
if (jfr.IsValid == false)
{
throw new Exception("Not valid json.");
}
if (jfr.MatchTargetType == false)
{
throw new InvalidCastException("The input json does not match the target type.");
}
await Task.Run(() =>
{
#if JSONNET_EXISTS
@return.Object = JsonConvert.DeserializeObject<T>(json, settings);
#endif
});
@return.Success = true;
}
catch (InvalidCastException ex)
{
@return.Success = false;
@return.Error = new WebError(29, ex.Message, ex);
}
catch (MissingComponentException ex)
{
@return.Success = false;
@return.Error = new WebError(455, ex.Message, ex);
}
catch (ThreadAbortException ex)
{
@return.Success = false;
@return.Error = new WebError(-1, ex.Message, ex);
}
catch (Exception ex)
{
@return.Success = false;
@return.Error = new WebError(422, ex.Message, ex);
}
return @return;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 13a6488720117b440923d3f85416ede7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: