Skip to content
0.2.0-dev Download ZIP
266 lines ·8.4 KB Raw
1using System.Buffers.Binary;
2using System.Text;
3
4namespace EvilEngine.Adapters.Tests.Support;
5
6public sealed class SerializedFileWriter(string unity = "2019.4.29f1", int platform = 24)
7{
8 private readonly List<string> _externals = [];
9
10 private readonly List<(int ClassId, byte[] Data)> _objects = [];
11
12 public SerializedFileWriter Object(int classId, byte[] data)
13 {
14 _objects.Add((classId, data));
15 return this;
16 }
17
18 public SerializedFileWriter External(string path)
19 {
20 _externals.Add(path);
21 return this;
22 }
23
24 public static byte[] PlayerSettings(string company, string product, string version, int beforeCategory = 260)
25 {
26 var bytes = new MemoryStream();
27 bytes.Write(new byte[36]);
28 AlignedString(bytes, company);
29 AlignedString(bytes, product);
30 bytes.Write(new byte[beforeCategory]);
31 AlignedString(bytes, "public.app-category.games");
32 bytes.Write(new byte[120]);
33 AlignedString(bytes, version);
34 bytes.Write(new byte[64]);
35 return bytes.ToArray();
36 }
37
38 public static byte[] PlayerSettings2022(string company, string product, string version, int logos = 1,
39 int mipmapLimitGroups = 0, int stackTraceTypes = 6)
40 {
41 var bytes = new MemoryStream();
42 bytes.Write(new byte[36]);
43 AlignedString(bytes, company);
44 AlignedString(bytes, product);
45 bytes.Write(new byte[104]);
46 Int32(bytes, logos);
47 bytes.Write(new byte[logos * 16]);
48 bytes.Write(new byte[124]);
49 Int32(bytes, mipmapLimitGroups);
50 for (var group = 0; group < mipmapLimitGroups; group++)
51 {
52 AlignedString(bytes, $"Group {group}");
53 Int32(bytes, group);
54 }
55
56 Int32(bytes, stackTraceTypes);
57 bytes.Write(new byte[stackTraceTypes * 4]);
58 bytes.Write(new byte[80]);
59 AlignedString(bytes, "public.app-category.games");
60 bytes.Write(new byte[144]);
61 AlignedString(bytes, "1.0");
62 AlignedString(bytes, "1.0");
63 AlignedString(bytes, version);
64 bytes.Write(new byte[64]);
65 return bytes.ToArray();
66 }
67
68 public static byte[] BuildSettings(params string[] scenes)
69 {
70 var bytes = new MemoryStream();
71 Int32(bytes, scenes.Length);
72 foreach (var scene in scenes)
73 AlignedString(bytes, scene);
74 bytes.Write(new byte[32]);
75 return bytes.ToArray();
76 }
77
78 public static byte[] MonoScript(string name, string className, string ns = "", string assembly = "Assembly-CSharp")
79 {
80 var bytes = new MemoryStream();
81 AlignedString(bytes, name);
82 bytes.Write(new byte[4 + 16]);
83 AlignedString(bytes, className);
84 AlignedString(bytes, ns);
85 AlignedString(bytes, assembly);
86 return bytes.ToArray();
87 }
88
89 public static byte[] MonoBehaviour(int scriptFile, long scriptPathId, string name, params int[] fields)
90 {
91 var bytes = new MemoryStream();
92 Int32(bytes, 0);
93 bytes.Write(BitConverter.GetBytes(7L));
94 bytes.Write([1, 0, 0, 0]);
95 Int32(bytes, scriptFile);
96 bytes.Write(BitConverter.GetBytes(scriptPathId));
97 AlignedString(bytes, name);
98 foreach (var field in fields)
99 Int32(bytes, field);
100 return bytes.ToArray();
101 }
102
103 public byte[] Build(int format = 21, bool bigEndian = false, bool typeTrees = false)
104 {
105 var classes = _objects.Select(item => item.ClassId).Distinct().ToList();
106 var metadata = new Metadata(bigEndian);
107 metadata.CString(unity);
108 metadata.Int32(platform);
109 metadata.Byte(typeTrees ? (byte)1 : (byte)0);
110 metadata.Int32(classes.Count);
111 foreach (var classId in classes)
112 {
113 metadata.Int32(classId);
114 metadata.Byte(0);
115 metadata.Int16(-1);
116 if (classId == 114)
117 metadata.Bytes(new byte[16]);
118 metadata.Bytes(new byte[16]);
119 if (!typeTrees)
120 continue;
121 metadata.Int32(1);
122 metadata.Int32(4);
123 metadata.Bytes(new byte[32]);
124 metadata.Bytes("Base"u8.ToArray());
125 metadata.Int32(0);
126 }
127
128 var headerSize = format >= 22 ? 48 : 20;
129 var placed = new List<long>();
130 long data = 0;
131 foreach (var (_, bytes) in _objects)
132 {
133 placed.Add(data);
134 data = (data + bytes.Length + 7) & ~7L;
135 }
136
137 metadata.Int32(_objects.Count);
138 for (var index = 0; index < _objects.Count; index++)
139 {
140 metadata.Align();
141 metadata.Int64(index + 1);
142 if (format >= 22)
143 metadata.Int64(placed[index]);
144 else
145 metadata.Int32((int)placed[index]);
146 metadata.Int32(_objects[index].Data.Length);
147 metadata.Int32(classes.IndexOf(_objects[index].ClassId));
148 }
149
150 metadata.Int32(0);
151 metadata.Int32(_externals.Count);
152 foreach (var external in _externals)
153 {
154 metadata.CString("");
155 metadata.Bytes(new byte[16]);
156 metadata.Int32(0);
157 metadata.CString(external);
158 }
159
160 metadata.Int32(0);
161 metadata.CString("");
162 var body = metadata.ToArray();
163 var dataOffset = (headerSize + body.Length + 15) & ~15L;
164 var fileSize = dataOffset + data;
165 var file = new byte[fileSize];
166 var header = file.AsSpan();
167 BinaryPrimitives.WriteInt32BigEndian(header[8..], format);
168 header[16] = bigEndian ? (byte)1 : (byte)0;
169 if (format >= 22)
170 {
171 BinaryPrimitives.WriteUInt32BigEndian(header[20..], (uint)body.Length);
172 BinaryPrimitives.WriteInt64BigEndian(header[24..], fileSize);
173 BinaryPrimitives.WriteInt64BigEndian(header[32..], dataOffset);
174 }
175 else
176 {
177 BinaryPrimitives.WriteUInt32BigEndian(header, (uint)body.Length);
178 BinaryPrimitives.WriteUInt32BigEndian(header[4..], (uint)fileSize);
179 BinaryPrimitives.WriteUInt32BigEndian(header[12..], (uint)dataOffset);
180 }
181
182 body.CopyTo(file, headerSize);
183 for (var index = 0; index < _objects.Count; index++)
184 _objects[index].Data.CopyTo(file, dataOffset + placed[index]);
185 return file;
186 }
187
188 private static void Int32(MemoryStream bytes, int value)
189 {
190 Span<byte> span = stackalloc byte[4];
191 BinaryPrimitives.WriteInt32LittleEndian(span, value);
192 bytes.Write(span);
193 }
194
195 private static void AlignedString(MemoryStream bytes, string text)
196 {
197 var encoded = Encoding.UTF8.GetBytes(text);
198 Span<byte> length = stackalloc byte[4];
199 BinaryPrimitives.WriteInt32LittleEndian(length, encoded.Length);
200 bytes.Write(length);
201 bytes.Write(encoded);
202 while (bytes.Length % 4 != 0)
203 bytes.WriteByte(0);
204 }
205
206 private sealed class Metadata(bool bigEndian)
207 {
208 private readonly MemoryStream _bytes = new();
209
210 public void Byte(byte value)
211 {
212 _bytes.WriteByte(value);
213 }
214
215 public void Bytes(byte[] value)
216 {
217 _bytes.Write(value);
218 }
219
220 public void Int16(short value)
221 {
222 Span<byte> span = stackalloc byte[2];
223 if (bigEndian)
224 BinaryPrimitives.WriteInt16BigEndian(span, value);
225 else
226 BinaryPrimitives.WriteInt16LittleEndian(span, value);
227 _bytes.Write(span);
228 }
229
230 public void Int32(int value)
231 {
232 Span<byte> span = stackalloc byte[4];
233 if (bigEndian)
234 BinaryPrimitives.WriteInt32BigEndian(span, value);
235 else
236 BinaryPrimitives.WriteInt32LittleEndian(span, value);
237 _bytes.Write(span);
238 }
239
240 public void Int64(long value)
241 {
242 Span<byte> span = stackalloc byte[8];
243 if (bigEndian)
244 BinaryPrimitives.WriteInt64BigEndian(span, value);
245 else
246 BinaryPrimitives.WriteInt64LittleEndian(span, value);
247 _bytes.Write(span);
248 }
249
250 public void CString(string value)
251 {
252 _bytes.Write(Encoding.UTF8.GetBytes(value + "\0"));
253 }
254
255 public void Align()
256 {
257 while (_bytes.Length % 4 != 0)
258 _bytes.WriteByte(0);
259 }
260
261 public byte[] ToArray()
262 {
263 return _bytes.ToArray();
264 }
265 }
266}