mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-23 04:29:36 +08:00
加载项控制器修改 (#107)
* 明确 JsonTool 类的意义 * 添加了一个 JsonTool 对象 * 明确 SQLHelper 的使用规则;添加取消令牌在 Delay 方法上
This commit is contained in:
parent
05d1c7204a
commit
f0b3d0a549
@ -1,32 +1,54 @@
|
||||
using System.Collections;
|
||||
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.Architecture;
|
||||
using Milimoe.FunGame.Core.Library.Common.JsonConverter;
|
||||
using Milimoe.FunGame.Core.Service;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Api.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个Json工具类<para/>
|
||||
/// Json工具类<para/>
|
||||
/// 此工具类拥有单独的序列化选项,支持添加自定义转换器 <see cref="BaseEntityConverter{T}"/><para/>
|
||||
/// <see cref="BaseEntityConverter{T}"/> 继承自 <see cref="JsonConverter"/>
|
||||
/// </summary>
|
||||
public class JsonTool
|
||||
{
|
||||
/// <summary>
|
||||
/// 序列化选项
|
||||
/// 默认的序列化选项
|
||||
/// </summary>
|
||||
public static JsonSerializerOptions JsonSerializerOptions => JsonManager.GeneralOptions;
|
||||
|
||||
/// <summary>
|
||||
/// 序列化选项
|
||||
/// </summary>
|
||||
public JsonSerializerOptions Options { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个Json工具类<para/>
|
||||
/// 此工具类拥有单独的序列化选项,支持添加自定义转换器 <see cref="BaseEntityConverter{T}"/><para/>
|
||||
/// <see cref="BaseEntityConverter{T}"/> 继承自 <see cref="JsonConverter"/>
|
||||
/// </summary>
|
||||
public JsonTool()
|
||||
{
|
||||
Options.WriteIndented = JsonSerializerOptions.WriteIndented;
|
||||
Options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
|
||||
Options.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
||||
foreach (JsonConverter converter in JsonSerializerOptions.Converters)
|
||||
{
|
||||
Options.Converters.Add(converter);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册一个自定义转换器,支持 <see cref="BaseEntityConverter{T}"/>
|
||||
/// </summary>
|
||||
/// <param name="converter"></param>
|
||||
public static void AddConverter(JsonConverter converter)
|
||||
public void AddConverter(JsonConverter converter)
|
||||
{
|
||||
if (!JsonSerializerOptions.Converters.Contains(converter))
|
||||
JsonSerializerOptions.Converters.Add(converter);
|
||||
if (!Options.Converters.Contains(converter))
|
||||
Options.Converters.Add(converter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -47,7 +69,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public string GetString<T>(T obj) => JsonManager.GetString(obj, JsonSerializerOptions);
|
||||
public string GetString<T>(T obj) => JsonManager.GetString(obj, Options);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化Json对象
|
||||
@ -55,14 +77,14 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="json"></param>
|
||||
/// <returns></returns>
|
||||
public T? GetObject<T>(string json) => JsonManager.GetObject<T>(json, JsonSerializerOptions);
|
||||
public T? GetObject<T>(string json) => JsonManager.GetObject<T>(json, Options);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化Json对象,此方法可能无法返回正确的类型,请注意辨别
|
||||
/// </summary>
|
||||
/// <param name="json"></param>
|
||||
/// <returns></returns>
|
||||
public object? GetObject(string json) => JsonManager.GetObject(json, JsonSerializerOptions);
|
||||
public object? GetObject(string json) => JsonManager.GetObject(json, Options);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化Hashtable中Key对应的Json对象
|
||||
@ -71,7 +93,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
/// <param name="table"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public T? GetObject<T>(Hashtable table, string key) => JsonManager.GetObject<T>(table, key, JsonSerializerOptions);
|
||||
public T? GetObject<T>(Hashtable table, string key) => JsonManager.GetObject<T>(table, key, Options);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化Dictionary中Key对应的Json对象
|
||||
@ -80,7 +102,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
/// <param name="dict"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public T? GetObject<T>(Dictionary<string, object> dict, string key) => JsonManager.GetObject<T>(dict, key, JsonSerializerOptions);
|
||||
public T? GetObject<T>(Dictionary<string, object> dict, string key) => JsonManager.GetObject<T>(dict, key, Options);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化IEnumerable中的Json对象 可指定反序列化选项
|
||||
@ -89,7 +111,7 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
/// <param name="e"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public T? JsonDeserializeFromIEnumerable<T>(IEnumerable<object> e, int index) => JsonManager.GetObject<T>(e, index, JsonSerializerOptions);
|
||||
public T? JsonDeserializeFromIEnumerable<T>(IEnumerable<object> e, int index) => JsonManager.GetObject<T>(e, index, Options);
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化多个Json对象
|
||||
@ -98,6 +120,6 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="json"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> GetObjects<T>(string json) => JsonManager.GetObjects<T>(json, JsonSerializerOptions);
|
||||
public List<T> GetObjects<T>(string json) => JsonManager.GetObjects<T>(json, Options);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Milimoe.FunGame.Core.Interface.Addons;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Interface.Addons;
|
||||
using Milimoe.FunGame.Core.Library.Common.Addon;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
|
||||
@ -41,6 +42,11 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
/// <returns></returns>
|
||||
public void Error(Exception e) => MaskMethod_Error(e);
|
||||
|
||||
/// <summary>
|
||||
/// JSON 工具类对象
|
||||
/// </summary>
|
||||
public JsonTool JSON { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 新建一个BaseAddonController
|
||||
/// </summary>
|
||||
|
@ -1,4 +1,4 @@
|
||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||
using Milimoe.FunGame.Core.Api.Utility;
|
||||
using Milimoe.FunGame.Core.Interface.Addons;
|
||||
using Milimoe.FunGame.Core.Library.SQLScript.Common;
|
||||
@ -17,7 +17,7 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
public class ServerAddonController<T>(IAddon addon, Dictionary<string, object> delegates) : BaseAddonController<T>(addon, delegates), IServerAddon where T : IAddon
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库连接器
|
||||
/// 数据库连接器 [ 后台长连接仅查询专用,请勿用于事务、更新和新增,对应需求请使用:<see cref="GetSQLHelper"/> ]
|
||||
/// </summary>
|
||||
public SQLHelper? SQLHelper => _sqlHelper;
|
||||
|
||||
@ -32,7 +32,16 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
private CancellationTokenSource? _cts = null;
|
||||
|
||||
/// <summary>
|
||||
/// 新建 SQLHelper
|
||||
/// 获取一个可以用来进行事务操作、更新/新增数据的数据库连接器 [ 请使用 using Controller.GetSQLHelper() 来让它能够自动释放 ]
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public SQLHelper? GetSQLHelper()
|
||||
{
|
||||
return Factory.OpenFactory.GetSQLHelper();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新建 SQLHelper [ 后台长连接仅查询专用,请勿用于事务、更新和新增,对应需求请使用:<see cref="GetSQLHelper"/> ]
|
||||
/// </summary>
|
||||
public void NewSQLHelper()
|
||||
{
|
||||
@ -55,7 +64,7 @@ namespace Milimoe.FunGame.Core.Controller
|
||||
// 每两小时触发一次SQL服务器的心跳查询,防止SQL服务器掉线
|
||||
try
|
||||
{
|
||||
await Task.Delay(2 * 1000 * 3600);
|
||||
await Task.Delay(2 * 1000 * 3600, _cts.Token);
|
||||
_sqlHelper?.ExecuteDataSet(ServerLoginLogs.Select_GetLastLoginTime());
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
|
Loading…
x
Reference in New Issue
Block a user