-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathByteWriter.cs
More file actions
128 lines (115 loc) · 3.67 KB
/
ByteWriter.cs
File metadata and controls
128 lines (115 loc) · 3.67 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using Definitions.ObjectModels;
using System.Diagnostics.CodeAnalysis;
namespace Dat.FileParsing;
public static class ByteWriter
{
[RequiresUnreferencedCode("WriteT uses reflection-based type handling and may call WriteLocoStruct which requires unreferenced code.")]
public static void WriteT(Span<byte> data, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, int offset, object val)
{
if (t == typeof(uint8_t))
{
ByteWriterT.Write(data, offset, (uint8_t)(dynamic)val);
}
else if (t == typeof(int8_t))
{
ByteWriterT.Write(data, offset, (int8_t)(dynamic)val);
}
else if (t == typeof(uint16_t))
{
ByteWriterT.Write(data, offset, (uint16_t)(dynamic)val);
}
else if (t == typeof(int16_t))
{
ByteWriterT.Write(data, offset, (int16_t)(dynamic)val);
}
else if (t == typeof(uint32_t))
{
ByteWriterT.Write(data, offset, (uint32_t)(dynamic)val);
}
else if (t == typeof(int32_t))
{
ByteWriterT.Write(data, offset, (int32_t)(dynamic)val);
}
else if (t == typeof(string_id))
{
// string ids should always be 0 in the dat file - they're only set when loaded into memory and never saved
val = 0;
ByteWriterT.Write(data, offset, (string_id)(dynamic)val);
}
else if (t == typeof(bool))
{
ByteWriterT.Write(data, offset, (bool)(dynamic)val);
}
else if (t.IsArray)
{
var elementType = t.GetElementType() ?? throw new ArgumentNullException(t.Name);
var size = ByteHelpers.GetObjectSize(elementType);
var arr = (Array)val;
for (var i = 0; i < arr.Length; i++)
{
var value = arr.GetValue(i) ?? throw new ArgumentNullException($"{t.Name}[{i}]");
WriteT(data, elementType, offset + (i * size), value);
}
}
else if (t.IsEnum)
{
var underlyingType = t.GetEnumUnderlyingType();
var underlyingValue = Convert.ChangeType(val, underlyingType);
WriteT(data, underlyingType, offset, underlyingValue);
}
else if (t.IsClass)
{
var objectSize = ByteHelpers.GetObjectSize(t);
var bytes = WriteLocoStruct((ILocoStruct)val);
if (bytes.Length != objectSize)
{
throw new InvalidOperationException();
}
bytes.CopyTo(data[offset..(offset + objectSize)]);
}
else
{
throw new InvalidOperationException("how");
}
}
[RequiresUnreferencedCode("WriteLocoStruct uses reflection to enumerate public properties of the object type, which may be trimmed.")]
public static ReadOnlySpan<byte> WriteLocoStruct(ILocoStruct obj)
{
ArgumentNullException.ThrowIfNull(obj);
var t = obj.GetType();
var objSize = ByteHelpers.GetObjectSize(t);
var buf = new byte[objSize];
foreach (var p in t.GetProperties())
{
// ignore non-loco properties on the records
var offsetAttr = AttributeHelper.Get<LocoStructOffsetAttribute>(p);
if (offsetAttr == null)
{
continue;
}
// write 0s to properties that need it
var skip = AttributeHelper.Get<LocoStructSkipReadAttribute>(p);
if (skip != null)
{
WriteT(buf, p.PropertyType, offsetAttr.Offset, 0);
continue;
}
// skip variable struct loading as they'll be set in special save
var variable = AttributeHelper.Get<LocoStructVariableLoadAttribute>(p);
if (variable != null)
{
continue;
}
// special array handling
var arrLength = 0;
if (p.PropertyType.IsArray)
{
var arrLengthAttr = AttributeHelper.Get<LocoArrayLengthAttribute>(p) ?? throw new ArgumentOutOfRangeException(nameof(obj), $"type {t} with property {p} didn't have LocoArrayLength attribute specified");
arrLength = arrLengthAttr.Length;
}
var propVal = p.GetValue(obj) ?? throw new ArgumentNullException($"{p.Name} for {obj} was null");
WriteT(buf, p.PropertyType, offsetAttr.Offset, propVal);
}
return buf;
}
}