-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathByteHelpers.cs
More file actions
37 lines (33 loc) · 931 Bytes
/
ByteHelpers.cs
File metadata and controls
37 lines (33 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
namespace Dat.FileParsing;
public static class ByteHelpers
{
public static int GetObjectSize([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type)
{
var size = 0;
if (type == typeof(byte) || type == typeof(sbyte))
{
size = 1;
}
else if (type == typeof(uint16_t) || type == typeof(int16_t))
{
size = 2;
}
else if (type == typeof(uint32_t) || type == typeof(int32_t))
{
size = 4;
}
else if (type.IsEnum)
{
size = Marshal.SizeOf(type.GetEnumUnderlyingType());
}
else
{
var sizeAttr = AttributeHelper.Get<LocoStructSizeAttribute>(type) ?? throw new ArgumentOutOfRangeException(nameof(LocoStructSizeAttribute), $"type {type} didn't have LocoStructSizeAttribute");
size = sizeAttr.Size;
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(size, nameof(size));
return size;
}
}