FunGame-Core/Service/JsonManager.cs
milimoe 769d0e4281
添加免疫、驱散;顺序表、爆发技、助攻修改 (#129)
* 特效底层支持直接修改硬直时间;添加驱散类型

* 添加 debuff

* 明确了驱散定义;添加助攻窗口期;修改预释放爆发技为不可驱散;预释放爆发技一定是最先行动;修复复活时导致硬直时间变成负数的问题

* 调整驱散描述

* 实现驱散系统;修复角色百分比公式错误;添加非伤害类助攻;添加辅助数据统计;修改一些文本显示

* 添加免疫、吸血、护盾机制

* 继续完善免疫和驱散、护盾和特效钩子等

* 添加新特效类型
2025-04-26 03:07:10 +08:00

303 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using Milimoe.FunGame.Core.Library.Common.JsonConverter;
using Milimoe.FunGame.Core.Library.Common.Network;
namespace Milimoe.FunGame.Core.Service
{
internal class JsonManager
{
/// <summary>
/// 默认的序列化选项
/// </summary>
internal static JsonSerializerOptions GeneralOptions { get; } = new()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
ReferenceHandler = ReferenceHandler.IgnoreCycles,
Converters = { new DateTimeConverter(), new DataTableConverter(), new DataSetConverter(), new UserConverter(), new RoomConverter(),
new CharacterConverter(), new MagicResistanceConverter(), new EquipSlotConverter(), new SkillConverter(), new EffectConverter(), new ItemConverter(),
new InventoryConverter(), new NormalAttackConverter(), new ClubConverter(), new GoodsConverter(), new StoreConverter(),
new NovelOptionConverter(), new NovelNodeConverter(), new ShieldConverter()
}
};
/// <summary>
/// 获取Json字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
internal static string GetString<T>(T obj)
{
return JsonSerializer.Serialize(obj, GeneralOptions);
}
/// <summary>
/// 获取Json字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static string GetString<T>(T obj, JsonSerializerOptions options)
{
return JsonSerializer.Serialize(obj, options);
}
/// <summary>
/// 反序列化Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
internal static T? GetObject<T>(string json)
{
return JsonSerializer.Deserialize<T>(json, GeneralOptions);
}
/// <summary>
/// 反序列化Json对象使用 <paramref name="reader"/>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reader"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static T? GetObject<T>(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<T>(ref reader, options);
}
/// <summary>
/// 反序列化Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static T? GetObject<T>(string json, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<T>(json, options);
}
/// <summary>
/// 反序列化Json对象此方法可能无法返回正确的类型请注意辨别
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
internal static object? GetObject(string json)
{
return JsonSerializer.Deserialize<object>(json, GeneralOptions);
}
/// <summary>
/// 反序列化Json对象此方法可能无法返回正确的类型请注意辨别
/// </summary>
/// <param name="json"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static object? GetObject(string json, JsonSerializerOptions options)
{
return JsonSerializer.Deserialize<object>(json, options);
}
/// <summary>
/// 反序列化SocketObject中索引为index的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="index"></param>
/// <returns></returns>
/// <exception cref="IndexOutOfArrayLengthException"></exception>
internal static T? GetObject<T>(SocketObject obj, int index)
{
if (index >= obj.Parameters.Length) throw new IndexOutOfArrayLengthException();
JsonElement element = (JsonElement)obj.Parameters[index];
T? result = element.Deserialize<T>(GeneralOptions);
return result;
}
/// <summary>
/// 反序列化Hashtable中Key对应的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <param name="key"></param>
/// <returns></returns>
internal static T? GetObject<T>(Hashtable table, string key)
{
if (table.ContainsKey(key))
{
JsonElement? element = (JsonElement?)table[key];
if (element != null)
{
T? result = ((JsonElement)element).Deserialize<T>(GeneralOptions);
return result;
}
}
return default;
}
/// <summary>
/// 反序列化Dictionary中Key对应的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <returns></returns>
internal static T? GetObject<T>(Dictionary<string, object> dict, string key)
{
if (dict.TryGetValue(key, out object? value))
{
JsonElement? element = (JsonElement?)value;
if (element != null)
{
T? result = ((JsonElement)element).Deserialize<T>(GeneralOptions);
return result;
}
}
return default;
}
/// <summary>
/// 反序列化IEnumerable中的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="e"></param>
/// <param name="index"></param>
/// <returns></returns>
internal static T? GetObject<T>(IEnumerable<object> e, int index)
{
IEnumerable<JsonElement> elements = e.Cast<JsonElement>();
if (elements.Count() > index)
{
JsonElement? element = (JsonElement?)elements.ElementAt(index);
if (element != null)
{
T? result = ((JsonElement)element).Deserialize<T>(GeneralOptions);
return result;
}
}
return default;
}
/// <summary>
/// 反序列化IEnumerable中的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="e"></param>
/// <param name="index"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static T? GetObject<T>(IEnumerable<object> e, int index, JsonSerializerOptions options)
{
IEnumerable<JsonElement> elements = e.Cast<JsonElement>();
if (elements.Count() > index)
{
JsonElement? element = (JsonElement?)elements.ElementAt(index);
if (element != null)
{
T? result = ((JsonElement)element).Deserialize<T>(options);
return result;
}
}
return default;
}
/// <summary>
/// 反序列化Hashtable中Key对应的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <param name="key"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static T? GetObject<T>(Hashtable table, string key, JsonSerializerOptions options)
{
if (table.ContainsKey(key))
{
JsonElement? element = (JsonElement?)table[key];
if (element != null)
{
T? result = ((JsonElement)element).Deserialize<T>(options);
return result;
}
}
return default;
}
/// <summary>
/// 反序列化Dictionary中Key对应的Json对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static T? GetObject<T>(Dictionary<string, object> dict, string key, JsonSerializerOptions options)
{
if (dict.TryGetValue(key, out object? value))
{
JsonElement? element = (JsonElement?)value;
if (element != null)
{
T? result = ((JsonElement)element).Deserialize<T>(options);
return result;
}
}
return default;
}
/// <summary>
/// 反序列化多个Json对象
/// 注意必须是相同的Json对象才可以使用此方法解析
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
internal static List<T> GetObjects<T>(string json)
{
json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组
return JsonSerializer.Deserialize<List<T>>(json, GeneralOptions) ?? [];
}
/// <summary>
/// 反序列化多个Json对象
/// 注意必须是相同的Json对象才可以使用此方法解析
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <param name="options"></param>
/// <returns></returns>
internal static List<T> GetObjects<T>(string json, JsonSerializerOptions options)
{
json = "[" + json.Replace("}{", "},{") + "]"; // 将Json字符串转换为数组
return JsonSerializer.Deserialize<List<T>>(json, options) ?? [];
}
/// <summary>
/// 检查字符串是否为完整的JSON对象
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
internal static bool IsCompleteJson<T>(string json)
{
try
{
// 尝试解析JSON数据如果成功则表示接收到完整的JSON
GetObject<T>(json);
return true;
}
catch
{
// JSON解析失败表示接收到的数据不完整
return false;
}
}
}
}