Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
hatulaile committed Sep 22, 2024
1 parent e14a033 commit 41c46f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace HeyBoxChatBotCs.Api.ServerMessageHandler.DataConverters;

public interface IDataConverter
{
public Task<object?> ConverterAsync(string message);
}
Original file line number Diff line number Diff line change
@@ -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<object?> ConverterAsync(string message)
{
return Task.FromResult<object?>(JsonSerializer.Deserialize<ServerMessage<UserSendCommandData>>(message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -11,12 +12,12 @@ internal static class ServerMessageHandler
{
private static JsonSerializerOptions JsonSerializerOptions { get; } = JsonSerializerOptions.Default;

public static FrozenDictionary<string, KeyValuePair<Type, IDataHandler>> HandlerMapping { get; } =
new Dictionary<string, KeyValuePair<Type, IDataHandler>>()
public static FrozenDictionary<string, KeyValuePair<IDataConverter, IDataHandler>> HandlerMapping { get; } =
new Dictionary<string, KeyValuePair<IDataConverter, IDataHandler>>()
{
{
"50",
new KeyValuePair<Type, IDataHandler>(typeof(ServerMessage<UserSendCommandData>),
new KeyValuePair<IDataConverter, IDataHandler>(new UserSendCommandConverter(),
new UserSendCommandHandler())
}
}.ToFrozenDictionary();
Expand All @@ -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, IDataHandler> type))
if (!HandlerMapping.TryGetValue(typeStr, out KeyValuePair<IDataConverter, IDataHandler> 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解析的结果不是期望结果!");
Expand Down

0 comments on commit 41c46f3

Please sign in to comment.