Skip to content

Commit

Permalink
Update Protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Jan 26, 2024
1 parent 1a20b60 commit 86824eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
12 changes: 6 additions & 6 deletions OpenGSQ/Exceptions/InvalidPacketException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public static void ThrowIfNotEqual<T>(T received, T expected)
{
if (typeof(T) == typeof(byte[]))
{
if (!(received as byte[]).SequenceEqual(expected as byte[]))
if (!(received as byte[])!.SequenceEqual((expected as byte[])!))
{
throw new InvalidPacketException(GetMessage(received, expected));
}
}
else if (!received.Equals(expected))
else if (!received!.Equals(expected))
{
throw new InvalidPacketException(GetMessage(received, expected));
}
Expand All @@ -48,13 +48,13 @@ private static string GetMessage<T>(T received, T expected)

if (typeof(T) == typeof(byte[]))
{
receivedStr = BitConverter.ToString(received as byte[]);
expectedStr = BitConverter.ToString(expected as byte[]);
receivedStr = BitConverter.ToString((received as byte[])!);
expectedStr = BitConverter.ToString((expected as byte[])!);
}
else
{
receivedStr = received.ToString();
expectedStr = expected.ToString();
receivedStr = received!.ToString()!;
expectedStr = expected!.ToString()!;
}

return $"Packet header mismatch. Received: {receivedStr}. Expected: {expectedStr}.";
Expand Down
5 changes: 5 additions & 0 deletions OpenGSQ/Protocols/Minecraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public async Task<Dictionary<string, object>> GetStatus(int version = 47)
// The packet may respond with two json objects, so we need to get the json length exactly
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(Encoding.UTF8.GetString(br.ReadBytes(count)));

if (data == null)
{
throw new InvalidOperationException("Failed to deserialize the response into a dictionary. The response might be empty or not in the expected format.");
}

return data;
}
}
Expand Down
5 changes: 5 additions & 0 deletions OpenGSQ/RconProtocols/SourceRcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public async Task Authenticate(string password)
/// <exception cref="TimeoutException">Thrown when the operation times out.</exception>
public async Task<string> SendCommand(string command)
{
if (_tcpClient == null)
{
throw new InvalidOperationException("The client is not authenticated. Please ensure that the Authenticate() method has been successfully called before attempting to send a command.");
}

// Send the command and a empty command packet
int id = new Random().Next(4096), dummyId = id + 1;
await _tcpClient.SendAsync(new Packet(id, PacketType.SERVERDATA_EXECCOMMAND, command).GetBytes());
Expand Down

0 comments on commit 86824eb

Please sign in to comment.