Skip to content
Closed
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
46 changes: 38 additions & 8 deletions Source/Common/ByteReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Text;
using Multiplayer.Common.Util;

namespace Multiplayer.Common
{
Expand Down Expand Up @@ -140,14 +139,45 @@ public virtual ulong[] ReadPrefixedULongs()

public virtual T ReadEnum<T>() where T : Enum
{
var values = EnumCache<T>.Values;
ushort enumIndex = values.Length switch
Type type = Enum.GetUnderlyingType(typeof(T));

if (type == typeof(byte))
{
return (T)Enum.ToObject(typeof(T), ReadByte());
}
else if (type == typeof(sbyte))
{
return (T)Enum.ToObject(typeof(T), ReadSByte());
}
else if (type == typeof(short))
{
return (T)Enum.ToObject(typeof(T), ReadShort());
}
else if (type == typeof(ushort))
{
return (T)Enum.ToObject(typeof(T), ReadUShort());
}
else if (type == typeof(int))
{
<= byte.MaxValue => ReadByte(),
<= ushort.MaxValue => ReadUShort(),
_ => throw new Exception($"Enum {typeof(T).FullName} has more than {ushort.MaxValue} values!")
};
return (T)values.GetValue(enumIndex);
return (T)Enum.ToObject(typeof(T), ReadInt32());
}
else if (type == typeof(uint))
{
return (T)Enum.ToObject(typeof(T), ReadUInt32());
}
else if (type == typeof(long) || type == typeof(IntPtr))
{
return (T)Enum.ToObject(typeof(T), ReadLong());
}
else if (type == typeof(ulong) || type == typeof(UIntPtr))
{
return (T)Enum.ToObject(typeof(T), ReadULong());
}
else
{
// This should never happen
throw new ReaderException($"Enum type unknown ({type})");
}
}

private int IncrementIndex(int size)
Expand Down
51 changes: 37 additions & 14 deletions Source/Common/ByteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Multiplayer.Common.Util;
using Verse;

namespace Multiplayer.Common
{
Expand Down Expand Up @@ -87,18 +85,43 @@ public virtual ByteWriter WriteString(string? s)

public virtual void WriteEnum<T>(T value) where T : Enum
{
var values = typeof(T) == typeof(Enum) ? EnumCache.Values(value.GetType()) : EnumCache<T>.Values;
var index = Array.IndexOf(values, value);
switch (values.Length)
{
case <= byte.MaxValue:
WriteByte((byte)index);
break;
case <= ushort.MaxValue:
WriteUShort((ushort)index);
break;
default:
throw new Exception($"Enum {value.GetType().FullName} has more than {ushort.MaxValue} values!");
Type type = Enum.GetUnderlyingType(typeof(T) == typeof(Enum) ? value.GetType() : typeof(T));

if (type == typeof(byte))
{
WriteByte(Convert.ToByte(value));
}
else if (type == typeof(sbyte))
{
WriteSByte(Convert.ToSByte(value));
}
else if (type == typeof(short))
{
WriteShort(Convert.ToInt16(value));
}
else if (type == typeof(ushort))
{
WriteUShort(Convert.ToUInt16(value));
}
else if (type == typeof(int))
{
WriteInt32(Convert.ToInt32(value));
}
else if (type == typeof(uint))
{
WriteUInt32(Convert.ToUInt32(value));
}
else if (type == typeof(long) || type == typeof(IntPtr))
{
WriteLong(Convert.ToInt64(value));
}
else if (type == typeof(ulong) || type == typeof(UIntPtr))
{
WriteULong(Convert.ToUInt64(value));
}
else
{
ServerLog.Error($"MP ByteWriter.WriteEnum: Unknown type {type}");
}
}
private void Write(object obj)
Expand Down
6 changes: 2 additions & 4 deletions Source/Common/ScheduledCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public ScheduledCommand(CommandType type, int ticks, int factionId, int mapId, i
public static byte[] Serialize(ScheduledCommand cmd)
{
ByteWriter writer = new ByteWriter();
// Use WriteByte directly instead of WriteEnum for better performance, as this is frequently used.
writer.WriteByte((byte)cmd.type);
writer.WriteEnum(cmd.type);
writer.WriteInt32(cmd.ticks);
writer.WriteInt32(cmd.factionId);
writer.WriteInt32(cmd.mapId);
Expand All @@ -44,8 +43,7 @@ public static byte[] Serialize(ScheduledCommand cmd)

public static ScheduledCommand Deserialize(ByteReader data)
{
// Use ReadByte directly instead of ReadEnum for better performance, as this is frequently used.
CommandType cmd = (CommandType)data.ReadByte();
CommandType cmd = data.ReadEnum<CommandType>();
int ticks = data.ReadInt32();
int factionId = data.ReadInt32();
int mapId = data.ReadInt32();
Expand Down
17 changes: 0 additions & 17 deletions Source/Common/Util/EnumCache.cs

This file was deleted.

Loading