Remove reflection based SwapStructureMembersEndian.

This commit is contained in:
2025-10-21 12:13:25 +01:00
parent 309adcbe03
commit 003b390830

View File

@@ -234,103 +234,6 @@ public static class Marshal
return (T)SwapStructureMembersEndianPdp(str);
}
/// <summary>Swaps all members of a structure</summary>
/// <param name="str"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public static object SwapStructureMembersEndian(object str)
{
Type t = str.GetType();
FieldInfo[] fieldInfo = t.GetFields();
foreach(FieldInfo fi in fieldInfo)
{
if(fi.FieldType == typeof(short) ||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(short))
{
var x = (short)(fi.GetValue(str) ?? default(short));
fi.SetValue(str, (short)(x << 8 | x >> 8 & 0xFF));
}
else if(fi.FieldType == typeof(int) ||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int))
{
var x = (int)(fi.GetValue(str) ?? default(int));
x = (int)(x << 8 & 0xFF00FF00 | (uint)x >> 8 & 0xFF00FF);
fi.SetValue(str, (int)((uint)x << 16 | (uint)x >> 16 & 0xFFFF));
}
else if(fi.FieldType == typeof(long) ||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(long))
{
var x = (long)(fi.GetValue(str) ?? default(long));
x = (x & 0x00000000FFFFFFFF) << 32 | (long)(((ulong)x & 0xFFFFFFFF00000000) >> 32);
x = (x & 0x0000FFFF0000FFFF) << 16 | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16);
x = (x & 0x00FF00FF00FF00FF) << 8 | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8);
fi.SetValue(str, x);
}
else if(fi.FieldType == typeof(ushort) ||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ushort))
{
var x = (ushort)(fi.GetValue(str) ?? default(ushort));
fi.SetValue(str, (ushort)(x << 8 | x >> 8));
}
else if(fi.FieldType == typeof(uint) ||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint))
{
var x = (uint)(fi.GetValue(str) ?? default(uint));
x = x << 8 & 0xFF00FF00 | x >> 8 & 0xFF00FF;
fi.SetValue(str, x << 16 | x >> 16);
}
else if(fi.FieldType == typeof(ulong) ||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ulong))
{
var x = (ulong)(fi.GetValue(str) ?? default(ulong));
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
fi.SetValue(str, x);
}
else if(fi.FieldType == typeof(float))
{
var flt = (float)(fi.GetValue(str) ?? default(float));
byte[] flt_b = BitConverter.GetBytes(flt);
fi.SetValue(str, BitConverter.ToSingle([flt_b[3], flt_b[2], flt_b[1], flt_b[0]], 0));
}
else if(fi.FieldType == typeof(double))
{
var dbl = (double)(fi.GetValue(str) ?? default(double));
byte[] dbl_b = BitConverter.GetBytes(dbl);
fi.SetValue(str,
BitConverter.ToDouble([
dbl_b[7], dbl_b[6], dbl_b[5], dbl_b[4], dbl_b[3], dbl_b[2],
dbl_b[1], dbl_b[0]
],
0));
}
else if(fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte))
{
// Do nothing, can't byteswap them!
}
else if(fi.FieldType == typeof(Guid))
{
// TODO: Swap GUID
}
// TODO: Swap arrays
else if(fi.FieldType.IsValueType && fi.FieldType is { IsEnum: false, IsArray: false })
{
object obj = fi.GetValue(str);
object strc = SwapStructureMembersEndian(obj);
fi.SetValue(str, strc);
}
}
return str;
}
/// <summary>Swaps all fields in an structure considering them to follow PDP endian conventions</summary>
/// <param name="str">Source structure</param>
/// <returns>Resulting structure</returns>