Skip to content

Commit

Permalink
Fixed a crash on chat parsing.
Browse files Browse the repository at this point in the history
Returned the commended try catch block.
  • Loading branch information
milutinke committed Feb 21, 2024
1 parent b2ef5cb commit 13de67b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
98 changes: 51 additions & 47 deletions MinecraftClient/Protocol/Handlers/Protocol18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ internal Tuple<int, Queue<byte>> ReadNextPacket()
/// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns>
internal bool HandlePacket(int packetId, Queue<byte> packetData)
{
//try
//{
try
{
switch (currentState)
{
// https://wiki.vg/Protocol#Login
Expand Down Expand Up @@ -403,16 +403,16 @@ internal bool HandlePacket(int packetId, Queue<byte> packetData)
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick,
dataTypes.ReadNextChat(packetData));
return false;

case ConfigurationPacketTypesIn.FinishConfiguration:
currentState = CurrentState.Play;
SendPacket(ConfigurationPacketTypesOut.FinishConfiguration, new List<byte>());
break;

case ConfigurationPacketTypesIn.KeepAlive:
SendPacket(ConfigurationPacketTypesOut.KeepAlive, packetData);
break;

case ConfigurationPacketTypesIn.Ping:
SendPacket(ConfigurationPacketTypesOut.Pong, packetData);
break;
Expand All @@ -425,12 +425,12 @@ internal bool HandlePacket(int packetId, Queue<byte> packetData)
World.StoreDimensionList(registryCodec);

break;

case ConfigurationPacketTypesIn.RemoveResourcePack:
if (dataTypes.ReadNextBool(packetData)) // Has UUID
dataTypes.ReadNextUUID(packetData); // UUID
break;

case ConfigurationPacketTypesIn.ResourcePack:
HandleResourcePackPacket(packetData);
break;
Expand All @@ -449,34 +449,34 @@ internal bool HandlePacket(int packetId, Queue<byte> packetData)
default:
return true;
}
//}
//catch (Exception innerException)
//{
// //throw;
// if (innerException is ThreadAbortException || innerException is SocketException ||
// innerException.InnerException is SocketException)
// throw; //Thread abort or Connection lost rather than invalid data

// throw new System.IO.InvalidDataException(
// string.Format(Translations.exception_packet_process,
// packetPalette.GetIncomingTypeById(packetId),
// packetId,
// protocolVersion,
// currentState == CurrentState.Login,
// innerException.GetType()),
// innerException);
//}
}
catch (Exception innerException)
{
//throw;
if (innerException is ThreadAbortException || innerException is SocketException ||
innerException.InnerException is SocketException)
throw; //Thread abort or Connection lost rather than invalid data

throw new System.IO.InvalidDataException(
string.Format(Translations.exception_packet_process,
packetPalette.GetIncomingTypeById(packetId),
packetId,
protocolVersion,
currentState == CurrentState.Login,
innerException.GetType()),
innerException);
}

return true;
}

public void HandleResourcePackPacket(Queue<byte> packetData)
{
var uuid = Guid.Empty;

if (protocolVersion >= MC_1_20_4_Version)
uuid = dataTypes.ReadNextUUID(packetData);

var url = dataTypes.ReadNextString(packetData);
var hash = dataTypes.ReadNextString(packetData);

Expand All @@ -493,29 +493,31 @@ public void HandleResourcePackPacket(Queue<byte> packetData)
return;

//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
? dataTypes.ConcatBytes(DataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash))
: Array.Empty<byte>();

var responseHeader =
protocolVersion < MC_1_10_Version // After 1.10, the MC does not include resource pack hash in responses
? dataTypes.ConcatBytes(DataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash))
: Array.Empty<byte>();

var basePacketData = protocolVersion >= MC_1_20_4_Version && uuid != Guid.Empty
? dataTypes.ConcatBytes(responseHeader, DataTypes.GetUUID(uuid))
: responseHeader;

var acceptedResourcePackData = dataTypes.ConcatBytes(basePacketData, DataTypes.GetVarInt(3));
var loadedResourcePackData = dataTypes.ConcatBytes(basePacketData, DataTypes.GetVarInt(0));

if (currentState == CurrentState.Configuration)
{
SendPacket(ConfigurationPacketTypesOut.ResourcePackResponse, acceptedResourcePackData); // Accepted
SendPacket(ConfigurationPacketTypesOut.ResourcePackResponse, loadedResourcePackData); // Successfully loaded
SendPacket(ConfigurationPacketTypesOut.ResourcePackResponse,
loadedResourcePackData); // Successfully loaded
}
else
{
SendPacket(PacketTypesOut.ResourcePackStatus, acceptedResourcePackData); // Accepted
SendPacket(PacketTypesOut.ResourcePackStatus, loadedResourcePackData); // Successfully loaded
}
}

private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
{
switch (packetPalette.GetIncomingTypeById(packetId))
Expand Down Expand Up @@ -1677,7 +1679,7 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
var hasIcon = dataTypes.ReadNextBool(packetData);
if (hasIcon)
{
if(protocolVersion < MC_1_20_2_Version)
if (protocolVersion < MC_1_20_2_Version)
iconBase64 = dataTypes.ReadNextString(packetData);
else
{
Expand Down Expand Up @@ -2164,9 +2166,9 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
break;
case PacketTypesIn.ResetScore:
dataTypes.ReadNextString(packetData); // Entity Name
if(dataTypes.ReadNextBool(packetData)) // Has Objective Name
if (dataTypes.ReadNextBool(packetData)) // Has Objective Name
dataTypes.ReadNextString(packetData); // Objective Name

break;
case PacketTypesIn.SpawnEntity:
if (handler.GetEntityHandlingEnabled())
Expand Down Expand Up @@ -2517,7 +2519,7 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
dataTypes.ReadNextVarInt(packetData); // Block Interaction
dataTypes.ReadParticleData(packetData, itemPalette); // Small Explosion Particles
dataTypes.ReadParticleData(packetData, itemPalette); // Large Explosion Particles

// Explosion Sound
dataTypes.ReadNextString(packetData); // Sound Name
var hasFixedRange = dataTypes.ReadNextBool(packetData);
Expand All @@ -2533,11 +2535,11 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
case PacketTypesIn.ScoreboardObjective:
var objectiveName = dataTypes.ReadNextString(packetData);
var mode = dataTypes.ReadNextByte(packetData);

var objectiveValue = string.Empty;
var objectiveType = -1;
var numberFormat = 0;

if (mode is 0 or 2)
{
objectiveValue = dataTypes.ReadNextChat(packetData);
Expand All @@ -2560,15 +2562,16 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
var objectiveValue2 = -1;
var objectiveDisplayName3 = string.Empty;
var numberFormat2 = 0;

if (protocolVersion >= MC_1_20_4_Version)
{
objectiveName3 = dataTypes.ReadNextString(packetData); // Objective Name
objectiveValue2 = dataTypes.ReadNextVarInt(packetData); // Value

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
numberFormat2 = dataTypes.ReadNextVarInt(packetData); // Number Format
}
Expand All @@ -2580,12 +2583,13 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)

if (action3 != 1 || protocolVersion >= MC_1_8_Version)
objectiveName3 = dataTypes.ReadNextString(packetData);

if (action3 != 1)
objectiveValue2 = dataTypes.ReadNextVarInt(packetData);
}

handler.OnUpdateScore(entityName, action3, objectiveName3, objectiveDisplayName3, objectiveValue2, numberFormat2);
handler.OnUpdateScore(entityName, action3, objectiveName3, objectiveDisplayName3, objectiveValue2,
numberFormat2);
break;
case PacketTypesIn.BlockChangedAck:
handler.OnBlockChangeAck(dataTypes.ReadNextVarInt(packetData));
Expand Down Expand Up @@ -2635,7 +2639,7 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
dataTypes.ReadNextFloat(packetData);
dataTypes.ReadNextBool(packetData);
break;

default:
return false; //Ignored packet
}
Expand Down Expand Up @@ -2704,7 +2708,7 @@ private void SendPacket(PacketTypesOut packet, IEnumerable<byte> packetData)
{
SendPacket(packetPalette.GetOutgoingIdByType(packet), packetData);
}

/// <summary>
/// Send a configuration packet to the server. Packet ID, compression, and encryption will be handled automatically.
/// </summary>
Expand Down Expand Up @@ -4493,7 +4497,7 @@ public bool SendPlayerSession(PlayerKeyPair? playerKeyPair)
return false;
}
}

return false;
}

Expand Down
5 changes: 4 additions & 1 deletion MinecraftClient/Protocol/Message/ChatParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,10 @@ private static string NbtToString(Dictionary<string, object> nbt)
var withs = (object[])withComponent;
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));
}
}
Expand Down

0 comments on commit 13de67b

Please sign in to comment.