Skip to content
Open
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
163 changes: 105 additions & 58 deletions LibraryCore/Globals.cs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions LibraryCore/LibraryCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MemoryPack.Core" Version="1.21.4" />
<PackageReference Include="MemoryPack.Generator" Version="1.21.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions LibraryCore/MemoryPackFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Drawing;
using Library;
using MemoryPack;

namespace LibraryCore;

public sealed class StatsFormatter : MemoryPackFormatter<Stats>
{
public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref Stats value)
{
writer.WriteCollectionHeader(value.Values.Count);
foreach (var pair in value.Values)
{
writer.WriteVarInt((int)pair.Key);
writer.WriteVarInt(pair.Value);
}
}

public override void Deserialize(ref MemoryPackReader reader, scoped ref Stats value)
{
reader.TryReadCollectionHeader(out var length);

var dict = new SortedDictionary<Stat, int>();
for (int i = 0; i < length; i++)
{
dict[(Stat)reader.ReadVarIntInt32()] = reader.ReadVarIntInt32();
}
value = new Stats() { Values = dict };
}
}

public sealed class ColorFormatter : MemoryPackFormatter<Color>
{
public override void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref Color value)
{
var argb = value.ToArgb();
var rgba = argb << 8 | argb >> 24;
writer.WriteVarInt(rgba);
}

public override void Deserialize(ref MemoryPackReader reader, scoped ref Color value)
{
var rgba = reader.ReadVarIntInt32();
var argb = rgba >> 8 | rgba << 24;
value = Color.FromArgb(argb);
}
}
13 changes: 7 additions & 6 deletions LibraryCore/Network/BaseConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,22 +315,23 @@ private void ProcessPacket(Packet p)

DateTime start = Time.Now;

var packetType = p.GetType();
MethodInfo info;
if (!PacketMethods.TryGetValue(p.PacketType, out info))
PacketMethods[p.PacketType] = info = GetType().GetMethod("Process", new[] { p.PacketType });
if (!PacketMethods.TryGetValue(packetType, out info))
PacketMethods[packetType] = info = GetType().GetMethod("Process", new[] { packetType });

if (info == null)
throw new NotImplementedException($"Not Implemented Exception: Method Process({p.PacketType}).");
throw new NotImplementedException($"Not Implemented Exception: Method Process({packetType}).");

info.Invoke(this, new object[] { p });
info.Invoke(this, [p]);

if (!Monitor) return;

TimeSpan execution = Time.Now - start;
DiagnosticValue value;

if (!Diagnostics.TryGetValue(p.PacketType.FullName, out value))
Diagnostics[p.PacketType.FullName] = value = new DiagnosticValue { Name = p.PacketType.FullName };
if (!Diagnostics.TryGetValue(packetType.FullName, out value))
Diagnostics[packetType.FullName] = value = new DiagnosticValue { Name = packetType.FullName };

value.Count++;
value.TotalTime += execution;
Expand Down
Loading