Fixed a crash on chat parsing. + Returned the commended try catch block

Fixed a crash on chat parsing. + Returned the commended try catch block
This commit is contained in:
Anon 2024-02-21 17:41:37 +01:00 committed by GitHub
commit 798a24e8be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 53 additions and 48 deletions

View file

@ -686,7 +686,6 @@ namespace MinecraftClient.Protocol.Handlers
? key >> 5 // 1.8 ? key >> 5 // 1.8
: ReadNextVarInt(cache); // 1.9+ : ReadNextVarInt(cache); // 1.9+
EntityMetaDataType type; EntityMetaDataType type;
try try
{ {

View file

@ -365,8 +365,8 @@ namespace MinecraftClient.Protocol.Handlers
/// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns> /// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns>
internal bool HandlePacket(int packetId, Queue<byte> packetData) internal bool HandlePacket(int packetId, Queue<byte> packetData)
{ {
//try try
//{ {
switch (currentState) switch (currentState)
{ {
// https://wiki.vg/Protocol#Login // https://wiki.vg/Protocol#Login
@ -403,16 +403,16 @@ namespace MinecraftClient.Protocol.Handlers
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick,
dataTypes.ReadNextChat(packetData)); dataTypes.ReadNextChat(packetData));
return false; return false;
case ConfigurationPacketTypesIn.FinishConfiguration: case ConfigurationPacketTypesIn.FinishConfiguration:
currentState = CurrentState.Play; currentState = CurrentState.Play;
SendPacket(ConfigurationPacketTypesOut.FinishConfiguration, new List<byte>()); SendPacket(ConfigurationPacketTypesOut.FinishConfiguration, new List<byte>());
break; break;
case ConfigurationPacketTypesIn.KeepAlive: case ConfigurationPacketTypesIn.KeepAlive:
SendPacket(ConfigurationPacketTypesOut.KeepAlive, packetData); SendPacket(ConfigurationPacketTypesOut.KeepAlive, packetData);
break; break;
case ConfigurationPacketTypesIn.Ping: case ConfigurationPacketTypesIn.Ping:
SendPacket(ConfigurationPacketTypesOut.Pong, packetData); SendPacket(ConfigurationPacketTypesOut.Pong, packetData);
break; break;
@ -425,12 +425,12 @@ namespace MinecraftClient.Protocol.Handlers
World.StoreDimensionList(registryCodec); World.StoreDimensionList(registryCodec);
break; break;
case ConfigurationPacketTypesIn.RemoveResourcePack: case ConfigurationPacketTypesIn.RemoveResourcePack:
if (dataTypes.ReadNextBool(packetData)) // Has UUID if (dataTypes.ReadNextBool(packetData)) // Has UUID
dataTypes.ReadNextUUID(packetData); // UUID dataTypes.ReadNextUUID(packetData); // UUID
break; break;
case ConfigurationPacketTypesIn.ResourcePack: case ConfigurationPacketTypesIn.ResourcePack:
HandleResourcePackPacket(packetData); HandleResourcePackPacket(packetData);
break; break;
@ -449,23 +449,22 @@ namespace MinecraftClient.Protocol.Handlers
default: default:
return true; return true;
} }
//} }
//catch (Exception innerException) catch (Exception innerException)
//{ {
// //throw; if (innerException is ThreadAbortException || innerException is SocketException ||
// if (innerException is ThreadAbortException || innerException is SocketException || innerException.InnerException is SocketException)
// innerException.InnerException is SocketException) throw; //Thread abort or Connection lost rather than invalid data
// throw; //Thread abort or Connection lost rather than invalid data
// throw new System.IO.InvalidDataException( throw new System.IO.InvalidDataException(
// string.Format(Translations.exception_packet_process, string.Format(Translations.exception_packet_process,
// packetPalette.GetIncomingTypeById(packetId), packetPalette.GetIncomingTypeById(packetId),
// packetId, packetId,
// protocolVersion, protocolVersion,
// currentState == CurrentState.Login, currentState == CurrentState.Login,
// innerException.GetType()), innerException.GetType()),
// innerException); innerException);
//} }
return true; return true;
} }
@ -473,10 +472,10 @@ namespace MinecraftClient.Protocol.Handlers
public void HandleResourcePackPacket(Queue<byte> packetData) public void HandleResourcePackPacket(Queue<byte> packetData)
{ {
var uuid = Guid.Empty; var uuid = Guid.Empty;
if (protocolVersion >= MC_1_20_4_Version) if (protocolVersion >= MC_1_20_4_Version)
uuid = dataTypes.ReadNextUUID(packetData); uuid = dataTypes.ReadNextUUID(packetData);
var url = dataTypes.ReadNextString(packetData); var url = dataTypes.ReadNextString(packetData);
var hash = dataTypes.ReadNextString(packetData); var hash = dataTypes.ReadNextString(packetData);
@ -493,21 +492,23 @@ namespace MinecraftClient.Protocol.Handlers
return; return;
//Send back "accepted" and "successfully loaded" responses for plugins or server config making use of resource pack mandatory //Send back "accepted" and "successfully loaded" responses for plugins or server config making use of resource pack mandatory
var responseHeader = protocolVersion < MC_1_10_Version // After 1.10, the MC does not include resource pack hash in responses var responseHeader =
? dataTypes.ConcatBytes(DataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash)) protocolVersion < MC_1_10_Version // After 1.10, the MC does not include resource pack hash in responses
: Array.Empty<byte>(); ? dataTypes.ConcatBytes(DataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash))
: Array.Empty<byte>();
var basePacketData = protocolVersion >= MC_1_20_4_Version && uuid != Guid.Empty var basePacketData = protocolVersion >= MC_1_20_4_Version && uuid != Guid.Empty
? dataTypes.ConcatBytes(responseHeader, DataTypes.GetUUID(uuid)) ? dataTypes.ConcatBytes(responseHeader, DataTypes.GetUUID(uuid))
: responseHeader; : responseHeader;
var acceptedResourcePackData = dataTypes.ConcatBytes(basePacketData, DataTypes.GetVarInt(3)); var acceptedResourcePackData = dataTypes.ConcatBytes(basePacketData, DataTypes.GetVarInt(3));
var loadedResourcePackData = dataTypes.ConcatBytes(basePacketData, DataTypes.GetVarInt(0)); var loadedResourcePackData = dataTypes.ConcatBytes(basePacketData, DataTypes.GetVarInt(0));
if (currentState == CurrentState.Configuration) if (currentState == CurrentState.Configuration)
{ {
SendPacket(ConfigurationPacketTypesOut.ResourcePackResponse, acceptedResourcePackData); // Accepted SendPacket(ConfigurationPacketTypesOut.ResourcePackResponse, acceptedResourcePackData); // Accepted
SendPacket(ConfigurationPacketTypesOut.ResourcePackResponse, loadedResourcePackData); // Successfully loaded SendPacket(ConfigurationPacketTypesOut.ResourcePackResponse,
loadedResourcePackData); // Successfully loaded
} }
else else
{ {
@ -515,7 +516,7 @@ namespace MinecraftClient.Protocol.Handlers
SendPacket(PacketTypesOut.ResourcePackStatus, loadedResourcePackData); // Successfully loaded SendPacket(PacketTypesOut.ResourcePackStatus, loadedResourcePackData); // Successfully loaded
} }
} }
private bool HandlePlayPackets(int packetId, Queue<byte> packetData) private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
{ {
switch (packetPalette.GetIncomingTypeById(packetId)) switch (packetPalette.GetIncomingTypeById(packetId))
@ -1677,7 +1678,7 @@ namespace MinecraftClient.Protocol.Handlers
var hasIcon = dataTypes.ReadNextBool(packetData); var hasIcon = dataTypes.ReadNextBool(packetData);
if (hasIcon) if (hasIcon)
{ {
if(protocolVersion < MC_1_20_2_Version) if (protocolVersion < MC_1_20_2_Version)
iconBase64 = dataTypes.ReadNextString(packetData); iconBase64 = dataTypes.ReadNextString(packetData);
else else
{ {
@ -2164,9 +2165,9 @@ namespace MinecraftClient.Protocol.Handlers
break; break;
case PacketTypesIn.ResetScore: case PacketTypesIn.ResetScore:
dataTypes.ReadNextString(packetData); // Entity Name dataTypes.ReadNextString(packetData); // Entity Name
if(dataTypes.ReadNextBool(packetData)) // Has Objective Name if (dataTypes.ReadNextBool(packetData)) // Has Objective Name
dataTypes.ReadNextString(packetData); // Objective Name dataTypes.ReadNextString(packetData); // Objective Name
break; break;
case PacketTypesIn.SpawnEntity: case PacketTypesIn.SpawnEntity:
if (handler.GetEntityHandlingEnabled()) if (handler.GetEntityHandlingEnabled())
@ -2517,7 +2518,7 @@ namespace MinecraftClient.Protocol.Handlers
dataTypes.ReadNextVarInt(packetData); // Block Interaction dataTypes.ReadNextVarInt(packetData); // Block Interaction
dataTypes.ReadParticleData(packetData, itemPalette); // Small Explosion Particles dataTypes.ReadParticleData(packetData, itemPalette); // Small Explosion Particles
dataTypes.ReadParticleData(packetData, itemPalette); // Large Explosion Particles dataTypes.ReadParticleData(packetData, itemPalette); // Large Explosion Particles
// Explosion Sound // Explosion Sound
dataTypes.ReadNextString(packetData); // Sound Name dataTypes.ReadNextString(packetData); // Sound Name
var hasFixedRange = dataTypes.ReadNextBool(packetData); var hasFixedRange = dataTypes.ReadNextBool(packetData);
@ -2533,11 +2534,11 @@ namespace MinecraftClient.Protocol.Handlers
case PacketTypesIn.ScoreboardObjective: case PacketTypesIn.ScoreboardObjective:
var objectiveName = dataTypes.ReadNextString(packetData); var objectiveName = dataTypes.ReadNextString(packetData);
var mode = dataTypes.ReadNextByte(packetData); var mode = dataTypes.ReadNextByte(packetData);
var objectiveValue = string.Empty; var objectiveValue = string.Empty;
var objectiveType = -1; var objectiveType = -1;
var numberFormat = 0; var numberFormat = 0;
if (mode is 0 or 2) if (mode is 0 or 2)
{ {
objectiveValue = dataTypes.ReadNextChat(packetData); objectiveValue = dataTypes.ReadNextChat(packetData);
@ -2560,15 +2561,16 @@ namespace MinecraftClient.Protocol.Handlers
var objectiveValue2 = -1; var objectiveValue2 = -1;
var objectiveDisplayName3 = string.Empty; var objectiveDisplayName3 = string.Empty;
var numberFormat2 = 0; var numberFormat2 = 0;
if (protocolVersion >= MC_1_20_4_Version) if (protocolVersion >= MC_1_20_4_Version)
{ {
objectiveName3 = dataTypes.ReadNextString(packetData); // Objective Name objectiveName3 = dataTypes.ReadNextString(packetData); // Objective Name
objectiveValue2 = dataTypes.ReadNextVarInt(packetData); // Value objectiveValue2 = dataTypes.ReadNextVarInt(packetData); // Value
if (dataTypes.ReadNextBool(packetData)) // Has Display Name if (dataTypes.ReadNextBool(packetData)) // Has Display Name
objectiveDisplayName3 = ChatParser.ParseText(dataTypes.ReadNextString(packetData)); // Has Display Name objectiveDisplayName3 =
ChatParser.ParseText(dataTypes.ReadNextString(packetData)); // Has Display Name
if (dataTypes.ReadNextBool(packetData)) // Has Number Format if (dataTypes.ReadNextBool(packetData)) // Has Number Format
numberFormat2 = dataTypes.ReadNextVarInt(packetData); // Number Format numberFormat2 = dataTypes.ReadNextVarInt(packetData); // Number Format
} }
@ -2580,12 +2582,13 @@ namespace MinecraftClient.Protocol.Handlers
if (action3 != 1 || protocolVersion >= MC_1_8_Version) if (action3 != 1 || protocolVersion >= MC_1_8_Version)
objectiveName3 = dataTypes.ReadNextString(packetData); objectiveName3 = dataTypes.ReadNextString(packetData);
if (action3 != 1) if (action3 != 1)
objectiveValue2 = dataTypes.ReadNextVarInt(packetData); objectiveValue2 = dataTypes.ReadNextVarInt(packetData);
} }
handler.OnUpdateScore(entityName, action3, objectiveName3, objectiveDisplayName3, objectiveValue2, numberFormat2); handler.OnUpdateScore(entityName, action3, objectiveName3, objectiveDisplayName3, objectiveValue2,
numberFormat2);
break; break;
case PacketTypesIn.BlockChangedAck: case PacketTypesIn.BlockChangedAck:
handler.OnBlockChangeAck(dataTypes.ReadNextVarInt(packetData)); handler.OnBlockChangeAck(dataTypes.ReadNextVarInt(packetData));
@ -2635,7 +2638,7 @@ namespace MinecraftClient.Protocol.Handlers
dataTypes.ReadNextFloat(packetData); dataTypes.ReadNextFloat(packetData);
dataTypes.ReadNextBool(packetData); dataTypes.ReadNextBool(packetData);
break; break;
default: default:
return false; //Ignored packet return false; //Ignored packet
} }
@ -2704,7 +2707,7 @@ namespace MinecraftClient.Protocol.Handlers
{ {
SendPacket(packetPalette.GetOutgoingIdByType(packet), packetData); SendPacket(packetPalette.GetOutgoingIdByType(packet), packetData);
} }
/// <summary> /// <summary>
/// Send a configuration packet to the server. Packet ID, compression, and encryption will be handled automatically. /// Send a configuration packet to the server. Packet ID, compression, and encryption will be handled automatically.
/// </summary> /// </summary>
@ -4493,7 +4496,7 @@ namespace MinecraftClient.Protocol.Handlers
return false; return false;
} }
} }
return false; return false;
} }

View file

@ -471,7 +471,10 @@ namespace MinecraftClient.Protocol.Message
var withs = (object[])withComponent; var withs = (object[])withComponent;
for (int i = 0; i < withs.Length; i++) for (int i = 0; i < withs.Length; i++)
{ {
var withDict = (Dictionary<string, object>)withs[i]; var withDict = withs[i] is string
? new Dictionary<string, object>() { { "text", (string)withs[i] } }
: (Dictionary<string, object>)withs[i];
translateString.Add(NbtToString(withDict)); translateString.Add(NbtToString(withDict));
} }
} }