Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Reactor/Networking/Messages/ModdedHandshakeC2S.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
using System.Collections.Generic;
using System.Text;
using Hazel;

namespace Reactor.Networking.Messages;

internal static class ModdedHandshakeC2S
{
private const int MaxPackedSize = 5;

public static int GetMaxSerializedSize(IReadOnlyCollection<Mod> mods)
{
var size = MaxPackedSize;

foreach (var mod in mods)
{
size += GetMaxStringSize(mod.Id) + GetMaxStringSize(mod.Version) + sizeof(ushort);
if (mod.IsRequiredOnAllClients) size += GetMaxStringSize(mod.Name);
}

return size;
}

private static int GetMaxStringSize(string? value)
{
return MaxPackedSize + (value == null ? 0 : Encoding.UTF8.GetByteCount(value));
}

public static void Serialize(MessageWriter writer, IReadOnlyCollection<Mod> mods)
{
writer.WritePacked(mods.Count);
Expand Down
8 changes: 3 additions & 5 deletions Reactor/Networking/Patches/ClientPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,14 @@ public static void Prefix(ref bool useDtlsLayout)

public static void Postfix(ref Il2CppStructArray<byte> __result)
{
var handshake = new MessageWriter(1000);
var mods = ModList.Current;
var handshake = new MessageWriter(__result.Length + ReactorHeader.Size + ModdedHandshakeC2S.GetMaxSerializedSize(mods));

handshake.Write(__result);

ReactorHeader.Write(handshake);

ModdedHandshakeC2S.Serialize(
handshake,
ModList.Current
);
ModdedHandshakeC2S.Serialize(handshake, mods);

__result = handshake.ToByteArray(true);
handshake.Recycle();
Expand Down
Loading