1.19.3 Chat command signing support & Update chat paser

This commit is contained in:
BruceChen 2023-01-14 00:53:36 +08:00
parent fe0b268878
commit 0ce9690778
6 changed files with 164 additions and 103 deletions

View file

@ -5,31 +5,31 @@ namespace MinecraftClient.Protocol.Message
public class ChatMessage
{
// in 1.19 and above, isSignedChat = true
public readonly bool isSignedChat;
public bool isSignedChat;
public readonly string content;
public string content;
public readonly bool isJson;
public bool isJson, isSenderJson;
// 0: chat (chat box), 1: system message (chat box), 2: game info (above hotbar), 3: say command,
// 4: msg command, 5: team msg command, 6: emote command, 7: tellraw command
public readonly int chatTypeId;
public int chatTypeId;
public readonly Guid senderUUID;
public Guid senderUUID;
public readonly bool isSystemChat;
public bool isSystemChat;
public readonly string? unsignedContent;
public string? unsignedContent;
public readonly string? displayName;
public string? displayName;
public readonly string? teamName;
public string? teamName;
public readonly DateTime? timestamp;
public DateTime? timestamp;
public readonly byte[]? signature;
public byte[]? signature;
public readonly bool? isSignatureLegal;
public bool? isSignatureLegal;
public ChatMessage(string content, bool isJson, int chatType, Guid senderUUID, string? unsignedContent, string displayName, string? teamName, long timestamp, byte[] signature, bool isSignatureLegal)
{
@ -47,11 +47,12 @@ namespace MinecraftClient.Protocol.Message
this.isSignatureLegal = isSignatureLegal;
}
public ChatMessage(string content, bool isJson, int chatType, Guid senderUUID, bool isSystemChat = false)
public ChatMessage(string content, string? displayName, bool isJson, int chatType, Guid senderUUID, bool isSystemChat = false)
{
isSignedChat = isSystemChat;
this.isSystemChat = isSystemChat;
this.content = content;
this.displayName = displayName;
this.isJson = isJson;
chatTypeId = chatType;
this.senderUUID = senderUUID;

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
@ -28,6 +29,30 @@ namespace MinecraftClient.Protocol
public static Dictionary<int, MessageType>? ChatId2Type;
public static void ReadChatType(Dictionary<string, object> registryCodec)
{
Dictionary<int, MessageType> chatTypeDictionary = ChatId2Type ?? new();
var chatTypeListNbt = (object[])(((Dictionary<string, object>)registryCodec["minecraft:chat_type"])["value"]);
foreach (var (chatName, chatId) in from Dictionary<string, object> chatTypeNbt in chatTypeListNbt
let chatName = (string)chatTypeNbt["name"]
let chatId = (int)chatTypeNbt["id"]
select (chatName, chatId))
{
chatTypeDictionary[chatId] = chatName switch
{
"minecraft:chat" => MessageType.CHAT,
"minecraft:emote_command" => MessageType.EMOTE_COMMAND,
"minecraft:msg_command_incoming" => MessageType.MSG_COMMAND_INCOMING,
"minecraft:msg_command_outgoing" => MessageType.MSG_COMMAND_OUTGOING,
"minecraft:say_command" => MessageType.SAY_COMMAND,
"minecraft:team_msg_command_incoming" => MessageType.TEAM_MSG_COMMAND_INCOMING,
"minecraft:team_msg_command_outgoing" => MessageType.TEAM_MSG_COMMAND_OUTGOING,
_ => MessageType.CHAT,
};
}
ChatId2Type = chatTypeDictionary;
}
/// <summary>
/// The main function to convert text from MC 1.6+ JSON to MC 1.5.2 formatted text
/// </summary>
@ -47,7 +72,7 @@ namespace MinecraftClient.Protocol
/// <returns>Returns the translated text</returns>
public static string ParseSignedChat(ChatMessage message, List<string>? links = null)
{
string sender = message.displayName!;
string sender = message.isSenderJson ? ParseText(message.displayName!) : message.displayName!;
string content;
if (Config.Signature.ShowModifiedChat && message.unsignedContent != null)
{
@ -66,7 +91,7 @@ namespace MinecraftClient.Protocol
List<string> usingData = new();
MessageType chatType;
if (message.isSystemChat)
if (message.chatTypeId == -1)
chatType = MessageType.RAW_MSG;
else if (!ChatId2Type!.TryGetValue(message.chatTypeId, out chatType))
chatType = MessageType.CHAT;
@ -119,7 +144,7 @@ namespace MinecraftClient.Protocol
if (message.isSystemChat)
{
if (Config.Signature.MarkSystemMessage)
color = "§z §r "; // Custom color code §z : Background Gray
color = "§z▍§r"; // Custom color code §z : Background Gray
}
else
{
@ -128,18 +153,18 @@ namespace MinecraftClient.Protocol
if (Config.Signature.ShowModifiedChat && message.unsignedContent != null)
{
if (Config.Signature.MarkModifiedMsg)
color = "§x §r "; // Custom color code §x : Background Yellow
color = "§x▍§r"; // Custom color code §x : Background Yellow
}
else
{
if (Config.Signature.MarkLegallySignedMsg)
color = "§y §r "; // Custom color code §y : Background Green
color = "§y▍§r"; // Custom color code §y : Background Green
}
}
else
{
if (Config.Signature.MarkIllegallySignedMsg)
color = "§w §r "; // Custom color code §w : Background Red
color = "§w▍§r"; // Custom color code §w : Background Red
}
}
return color + text;

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using static MinecraftClient.Protocol.Message.LastSeenMessageList;
@ -120,7 +121,7 @@ namespace MinecraftClient.Protocol.Message
{
// net.minecraft.network.message.LastSeenMessagesCollector#add(net.minecraft.network.message.MessageSignatureData, boolean)
// net.minecraft.network.message.LastSeenMessagesCollector#add(net.minecraft.network.message.AcknowledgedMessage)
if (entry == lastEntry)
if (lastEntry != null && entry.signature.SequenceEqual(lastEntry.signature))
return false;
lastEntry = entry;