diff --git a/HeyBoxChatBotCs.Api/ServerMessageHandler/DataConverters/IDataConverter.cs b/HeyBoxChatBotCs.Api/ServerMessageHandler/DataConverters/IDataConverter.cs new file mode 100644 index 0000000..a592641 --- /dev/null +++ b/HeyBoxChatBotCs.Api/ServerMessageHandler/DataConverters/IDataConverter.cs @@ -0,0 +1,6 @@ +namespace HeyBoxChatBotCs.Api.ServerMessageHandler.DataConverters; + +public interface IDataConverter +{ + public Task ConverterAsync(string message); +} \ No newline at end of file diff --git a/HeyBoxChatBotCs.Api/ServerMessageHandler/DataConverters/UserSendCommandConverter.cs b/HeyBoxChatBotCs.Api/ServerMessageHandler/DataConverters/UserSendCommandConverter.cs new file mode 100644 index 0000000..c880dcd --- /dev/null +++ b/HeyBoxChatBotCs.Api/ServerMessageHandler/DataConverters/UserSendCommandConverter.cs @@ -0,0 +1,13 @@ +using System.Text.Json; +using HeyBoxChatBotCs.Api.ServerMessageHandler.ServerMessageData; +using HeyBoxChatBotCs.Api.ServerMessageHandler.System; + +namespace HeyBoxChatBotCs.Api.ServerMessageHandler.DataConverters; + +public class UserSendCommandConverter : IDataConverter +{ + public Task ConverterAsync(string message) + { + return Task.FromResult(JsonSerializer.Deserialize>(message)); + } +} \ No newline at end of file diff --git a/HeyBoxChatBotCs.Api/ServerMessageHandler/System/ServerMessageHandler.cs b/HeyBoxChatBotCs.Api/ServerMessageHandler/System/ServerMessageHandler.cs index ce43b01..58eb19d 100644 --- a/HeyBoxChatBotCs.Api/ServerMessageHandler/System/ServerMessageHandler.cs +++ b/HeyBoxChatBotCs.Api/ServerMessageHandler/System/ServerMessageHandler.cs @@ -2,6 +2,7 @@ using System.Text.Json; using System.Text.Json.Nodes; using HeyBoxChatBotCs.Api.Features; +using HeyBoxChatBotCs.Api.ServerMessageHandler.DataConverters; using HeyBoxChatBotCs.Api.ServerMessageHandler.DataHandlers; using HeyBoxChatBotCs.Api.ServerMessageHandler.ServerMessageData; @@ -11,12 +12,12 @@ internal static class ServerMessageHandler { private static JsonSerializerOptions JsonSerializerOptions { get; } = JsonSerializerOptions.Default; - public static FrozenDictionary> HandlerMapping { get; } = - new Dictionary>() + public static FrozenDictionary> HandlerMapping { get; } = + new Dictionary>() { { "50", - new KeyValuePair(typeof(ServerMessage), + new KeyValuePair(new UserSendCommandConverter(), new UserSendCommandHandler()) } }.ToFrozenDictionary(); @@ -43,27 +44,14 @@ internal static async Task ProcessMessageAsync(string json) internal static async Task ProcessMessageAsync(string typeStr, string json) { - if (!HandlerMapping.TryGetValue(typeStr, out KeyValuePair type)) + if (!HandlerMapping.TryGetValue(typeStr, out KeyValuePair type)) { Log.Error($"解析服务器发送信息时失败:发现未知服务器信息类型 {typeStr}"); return; } - - if (!Misc.Misc.IsDerivedFromClass(type.Key, typeof(ServerMessage<>), true)) - { - Log.Error($"解析服务器发送信息时失败: {typeStr} 对应的类型是错误的!"); - return; - } - - if (type.Key.IsGenericTypeDefinition) - { - Log.Error($"解析服务器发送信息时失败: {typeStr} 对应的类型是没有泛型构造!"); - return; - } - try { - object? message = JsonSerializer.Deserialize(json, type.Key, JsonSerializerOptions); + object? message = await type.Key.ConverterAsync(json); if (message is null) { Log.Error("解析服务器发送信息时失败:Json解析的结果不是期望结果!");