0.2.0-dev Download ZIP
415 lines ·11.8 KB Raw
| 1 | using System.Text; |
| 2 | |
| 3 | namespace EvilEngine.Adapters.Tests.Support; |
| 4 | |
| 5 | public sealed class StudioWriter |
| 6 | { |
| 7 | public int Version { get; init; } = 48; |
| 8 | |
| 9 | public int Checksum { get; init; } = 1234; |
| 10 | |
| 11 | public int VvdChecksum { get; init; } = 1234; |
| 12 | |
| 13 | public int TipParent { get; init; } |
| 14 | |
| 15 | public float? FixedPointScale { get; init; } |
| 16 | |
| 17 | public int StripFlags { get; init; } = 1; |
| 18 | |
| 19 | public ushort[] StripIndices { get; init; } = [0, 2, 1]; |
| 20 | |
| 21 | public string? Sequence { get; init; } |
| 22 | |
| 23 | public string? Include { get; init; } |
| 24 | |
| 25 | public int BlendCells { get; init; } = 1; |
| 26 | |
| 27 | public short[] TipTurns { get; init; } = [0, 100, 200]; |
| 28 | |
| 29 | public byte[] Mdl() |
| 30 | { |
| 31 | var w = new Writer(); |
| 32 | var strings = new List<(int At, int Base, string Text)>(); |
| 33 | |
| 34 | void Name(int at, int @base, string text) |
| 35 | { |
| 36 | strings.Add((at, @base, text)); |
| 37 | } |
| 38 | |
| 39 | w.Pad(408); |
| 40 | w.I32(0, 0x54534449); |
| 41 | w.I32(4, Version); |
| 42 | w.I32(8, Checksum); |
| 43 | w.Bytes(12, Encoding.ASCII.GetBytes("test.mdl")); |
| 44 | w.Vec(80, 0, 0, 64); |
| 45 | w.Vec(104, -8, -8, 0); |
| 46 | w.Vec(116, 8, 8, 16); |
| 47 | w.I32(152, FixedPointScale is null ? 0 : 0x00200000); |
| 48 | w.F32(328, 25); |
| 49 | w.I32(332, 1); |
| 50 | w.F32(392, FixedPointScale ?? 0); |
| 51 | |
| 52 | const int bones = 408, |
| 53 | hitboxSet = 840, |
| 54 | hitbox = 852, |
| 55 | textures = 920, |
| 56 | directories = 1048, |
| 57 | skins = 1052, |
| 58 | part = 1056, |
| 59 | models = 1072, |
| 60 | mesh = 1368, |
| 61 | flex = 1484, |
| 62 | vertices = 1544, |
| 63 | attachment = 1592, |
| 64 | descs = 1684, |
| 65 | controller = 1692, |
| 66 | rule = 1712, |
| 67 | op = 1724, |
| 68 | end = 1732; |
| 69 | w.Pad(end); |
| 70 | |
| 71 | w.I32(156, 2); |
| 72 | w.I32(160, bones); |
| 73 | for (var i = 0; i < 2; i++) |
| 74 | { |
| 75 | var at = bones + i * 216; |
| 76 | Name(at, at, i == 0 ? "root" : "tip"); |
| 77 | w.I32(at + 4, i == 0 ? -1 : TipParent); |
| 78 | w.Vec(at + 32, 0, 0, i == 0 ? 0 : 10); |
| 79 | w.F32(at + 56, 1); |
| 80 | float[] pose = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, i == 0 ? 0 : -10]; |
| 81 | for (var k = 0; k < 12; k++) |
| 82 | w.F32(at + 96 + k * 4, pose[k]); |
| 83 | Name(at + 176, at, "metal"); |
| 84 | w.I32(at + 180, 1); |
| 85 | } |
| 86 | |
| 87 | w.I32(172, 1); |
| 88 | w.I32(176, hitboxSet); |
| 89 | Name(hitboxSet, hitboxSet, "default"); |
| 90 | w.I32(hitboxSet + 4, 1); |
| 91 | w.I32(hitboxSet + 8, hitbox - hitboxSet); |
| 92 | w.I32(hitbox, 1); |
| 93 | w.I32(hitbox + 4, 2); |
| 94 | w.Vec(hitbox + 8, -1, -2, -3); |
| 95 | w.Vec(hitbox + 20, 1, 2, 3); |
| 96 | Name(hitbox + 32, hitbox, "head"); |
| 97 | |
| 98 | w.I32(204, 2); |
| 99 | w.I32(208, textures); |
| 100 | Name(textures, textures, "skin_a"); |
| 101 | Name(textures + 64, textures + 64, "skin_b"); |
| 102 | w.I32(212, 1); |
| 103 | w.I32(216, directories); |
| 104 | Name(directories, 0, "models\\test\\"); |
| 105 | w.I32(220, 1); |
| 106 | w.I32(224, 2); |
| 107 | w.I32(228, skins); |
| 108 | w.I16(skins, 0); |
| 109 | w.I16(skins + 2, 1); |
| 110 | |
| 111 | w.I32(232, 1); |
| 112 | w.I32(236, part); |
| 113 | Name(part, part, "body"); |
| 114 | w.I32(part + 4, 2); |
| 115 | w.I32(part + 8, 1); |
| 116 | w.I32(part + 12, models - part); |
| 117 | w.Bytes(models, Encoding.ASCII.GetBytes("tri_reference.smd")); |
| 118 | w.F32(models + 68, 1); |
| 119 | w.I32(models + 72, 1); |
| 120 | w.I32(models + 76, mesh - models); |
| 121 | w.I32(models + 80, 3); |
| 122 | w.Bytes(models + 148, Encoding.ASCII.GetBytes("blank")); |
| 123 | w.I32(models + 148 + 84, 3 * 48); |
| 124 | |
| 125 | w.I32(mesh + 4, models - mesh); |
| 126 | w.I32(mesh + 8, 3); |
| 127 | w.I32(mesh + 16, 1); |
| 128 | w.I32(mesh + 20, flex - mesh); |
| 129 | w.I32(mesh + 52, 3); |
| 130 | |
| 131 | w.I32(flex, 0); |
| 132 | w.F32(flex + 8, 1); |
| 133 | w.F32(flex + 12, 10); |
| 134 | w.F32(flex + 16, 11); |
| 135 | w.I32(flex + 20, 3); |
| 136 | w.I32(flex + 24, vertices - flex); |
| 137 | w.I32(flex + 28, 1); |
| 138 | byte[] sides = [0, 51, 255]; |
| 139 | for (var v = 0; v < 3; v++) |
| 140 | { |
| 141 | var at = vertices + v * 16; |
| 142 | w.U16(at, (ushort)v); |
| 143 | w.U8(at + 2, 255); |
| 144 | w.U8(at + 3, sides[v]); |
| 145 | if (FixedPointScale is { } scale) |
| 146 | w.I16(at + 4, (short)(1 / scale)); |
| 147 | else |
| 148 | w.Half(at + 4, 1); |
| 149 | } |
| 150 | |
| 151 | w.I32(240, 1); |
| 152 | w.I32(244, attachment); |
| 153 | Name(attachment, attachment, "muzzle"); |
| 154 | w.I32(attachment + 8, 1); |
| 155 | float[] local = [1, 0, 0, 2, 0, 1, 0, 0, 0, 0, 1, 0]; |
| 156 | for (var k = 0; k < 12; k++) |
| 157 | w.F32(attachment + 12 + k * 4, local[k]); |
| 158 | |
| 159 | w.I32(260, 2); |
| 160 | w.I32(264, descs); |
| 161 | Name(descs, descs, "smileR"); |
| 162 | Name(descs + 4, descs + 4, "smileL"); |
| 163 | w.I32(268, 1); |
| 164 | w.I32(272, controller); |
| 165 | Name(controller, controller, "phoneme"); |
| 166 | Name(controller + 4, controller, "smile"); |
| 167 | w.I32(controller + 8, -1); |
| 168 | w.F32(controller + 16, 1); |
| 169 | w.I32(276, 1); |
| 170 | w.I32(280, rule); |
| 171 | w.I32(rule + 4, 1); |
| 172 | w.I32(rule + 8, op - rule); |
| 173 | w.I32(op, 1); |
| 174 | w.F32(op + 4, 0.5f); |
| 175 | |
| 176 | if (Sequence is { } label) |
| 177 | { |
| 178 | const int anim = end, seq = anim + 100, cells = seq + 212; |
| 179 | var pose = cells + (BlendCells * 2 + 3) / 4 * 4; |
| 180 | var data = BlendCells > 1 ? pose + 20 : pose; |
| 181 | w.Pad(data + 48); |
| 182 | w.F32(408 + 216 + 84 + 8, 0.001f); |
| 183 | w.I32(180, 1); |
| 184 | w.I32(184, anim); |
| 185 | Name(anim + 4, anim, "@" + label); |
| 186 | w.F32(anim + 8, 10); |
| 187 | w.I32(anim + 12, 1); |
| 188 | w.I32(anim + 16, TipTurns.Length); |
| 189 | w.I32(anim + 56, data - anim); |
| 190 | w.I32(188, 1); |
| 191 | w.I32(192, seq); |
| 192 | Name(seq + 4, seq, label); |
| 193 | w.I32(seq + 12, 1); |
| 194 | w.I32(seq + 60, cells - seq); |
| 195 | w.I32(seq + 68, BlendCells); |
| 196 | w.I32(seq + 72, 1); |
| 197 | w.I32(seq + 76, BlendCells > 1 ? 0 : -1); |
| 198 | w.I32(seq + 80, -1); |
| 199 | if (BlendCells > 1) |
| 200 | { |
| 201 | w.F32(seq + 84, -1); |
| 202 | w.F32(seq + 92, 1); |
| 203 | w.I32(300, 1); |
| 204 | w.I32(304, pose); |
| 205 | Name(pose, pose, "move_x"); |
| 206 | w.F32(pose + 8, -1); |
| 207 | w.F32(pose + 12, 1); |
| 208 | } |
| 209 | |
| 210 | w.U8(data, 0); |
| 211 | w.U8(data + 1, 0x03); |
| 212 | w.I16(data + 2, 16); |
| 213 | w.U16(data + 4, 32768); |
| 214 | w.U16(data + 6, 32768); |
| 215 | w.U16(data + 8, 0x8000 | (16384 + 16384 / 2)); |
| 216 | w.Half(data + 10, 1); |
| 217 | w.Half(data + 12, 2); |
| 218 | w.Half(data + 14, 3); |
| 219 | w.U8(data + 16, 1); |
| 220 | w.U8(data + 17, 0x08); |
| 221 | w.I16(data + 18, 0); |
| 222 | w.I16(data + 24, 6); |
| 223 | w.U8(data + 26, (byte)TipTurns.Length); |
| 224 | w.U8(data + 27, (byte)TipTurns.Length); |
| 225 | for (var k = 0; k < TipTurns.Length; k++) |
| 226 | w.I16(data + 28 + k * 2, TipTurns[k]); |
| 227 | } |
| 228 | |
| 229 | if (Include is { } include) |
| 230 | { |
| 231 | var at = w.Length; |
| 232 | w.Pad(at + 8); |
| 233 | w.I32(336, 1); |
| 234 | w.I32(340, at); |
| 235 | Name(at + 4, at, include); |
| 236 | } |
| 237 | |
| 238 | var keyValues = Encoding.ASCII.GetBytes("mdlkeyvalue { prop_data { \"base\" \"Metal.Small\" } }\0"); |
| 239 | w.I32(312, w.Length); |
| 240 | w.I32(316, keyValues.Length); |
| 241 | w.Append(keyValues); |
| 242 | Name(308, 0, "metal_box"); |
| 243 | foreach (var (at, @base, text) in strings) |
| 244 | { |
| 245 | w.I32(at, w.Length - @base); |
| 246 | w.Append(Encoding.ASCII.GetBytes(text + "\0")); |
| 247 | } |
| 248 | |
| 249 | w.I32(76, w.Length); |
| 250 | return w.ToArray(); |
| 251 | } |
| 252 | |
| 253 | public byte[] Vvd() |
| 254 | { |
| 255 | var w = new Writer(); |
| 256 | w.Pad(64 + 3 * 48 + 3 * 16); |
| 257 | w.I32(0, 0x56534449); |
| 258 | w.I32(4, 4); |
| 259 | w.I32(8, VvdChecksum); |
| 260 | w.I32(12, 1); |
| 261 | w.I32(16, 3); |
| 262 | w.I32(52, 64); |
| 263 | w.I32(56, 64); |
| 264 | w.I32(60, 64 + 3 * 48); |
| 265 | float[][] positions = [[0, 0, 0], [10, 0, 0], [0, 10, 0]]; |
| 266 | float[][] uvs = [[0, 0], [1, 0], [0, 1]]; |
| 267 | for (var v = 0; v < 3; v++) |
| 268 | { |
| 269 | var at = 64 + v * 48; |
| 270 | w.F32(at, 1); |
| 271 | w.U8(at + 12, (byte)(v == 2 ? 1 : 0)); |
| 272 | w.U8(at + 15, 1); |
| 273 | w.Vec(at + 16, positions[v][0], positions[v][1], positions[v][2]); |
| 274 | w.Vec(at + 28, 0, 0, 1); |
| 275 | w.F32(at + 40, uvs[v][0]); |
| 276 | w.F32(at + 44, uvs[v][1]); |
| 277 | var tangent = 64 + 3 * 48 + v * 16; |
| 278 | w.Vec(tangent, 1, 0, 0); |
| 279 | w.F32(tangent + 12, 1); |
| 280 | } |
| 281 | |
| 282 | return w.ToArray(); |
| 283 | } |
| 284 | |
| 285 | public byte[] Vtx() |
| 286 | { |
| 287 | var w = new Writer(); |
| 288 | const int part = 36, |
| 289 | models = 44, |
| 290 | lod0 = 60, |
| 291 | lod1 = 72, |
| 292 | mesh = 84, |
| 293 | group = 93, |
| 294 | vertices = 118, |
| 295 | indices = 145, |
| 296 | strip = 151, |
| 297 | end = 178; |
| 298 | w.Pad(end); |
| 299 | w.I32(0, 7); |
| 300 | w.I32(4, 24); |
| 301 | w.I32(12, 3); |
| 302 | w.I32(16, Checksum); |
| 303 | w.I32(20, 1); |
| 304 | w.I32(28, 1); |
| 305 | w.I32(32, part); |
| 306 | w.I32(part, 2); |
| 307 | w.I32(part + 4, models - part); |
| 308 | w.I32(models, 1); |
| 309 | w.I32(models + 4, lod0 - models); |
| 310 | w.I32(models + 8, 1); |
| 311 | w.I32(models + 12, lod1 - (models + 8)); |
| 312 | w.I32(lod0, 1); |
| 313 | w.I32(lod0 + 4, mesh - lod0); |
| 314 | w.I32(mesh, 1); |
| 315 | w.I32(mesh + 4, group - mesh); |
| 316 | w.I32(group, 3); |
| 317 | w.I32(group + 4, vertices - group); |
| 318 | w.I32(group + 8, StripIndices.Length); |
| 319 | w.I32(group + 12, indices - group); |
| 320 | w.I32(group + 16, 1); |
| 321 | w.I32(group + 20, strip - group); |
| 322 | for (var v = 0; v < 3; v++) |
| 323 | { |
| 324 | w.U8(vertices + v * 9 + 3, 1); |
| 325 | w.U16(vertices + v * 9 + 4, (ushort)v); |
| 326 | } |
| 327 | |
| 328 | var stripIndices = new Writer(); |
| 329 | stripIndices.Pad(StripIndices.Length * 2); |
| 330 | for (var i = 0; i < StripIndices.Length; i++) |
| 331 | stripIndices.U16(i * 2, StripIndices[i]); |
| 332 | w.I32(strip, StripIndices.Length); |
| 333 | w.I32(strip + 8, 3); |
| 334 | w.I16(strip + 16, 1); |
| 335 | w.U8(strip + 18, (byte)StripFlags); |
| 336 | if (StripIndices.Length != 3) |
| 337 | { |
| 338 | w.I32(group + 12, w.Length - group); |
| 339 | w.Append(stripIndices.ToArray()); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | w.Bytes(indices, stripIndices.ToArray()); |
| 344 | } |
| 345 | |
| 346 | return w.ToArray(); |
| 347 | } |
| 348 | |
| 349 | private sealed class Writer |
| 350 | { |
| 351 | private readonly MemoryStream _stream = new(); |
| 352 | |
| 353 | public int Length => (int)_stream.Length; |
| 354 | |
| 355 | public void Pad(int length) |
| 356 | { |
| 357 | if (_stream.Length < length) |
| 358 | _stream.SetLength(length); |
| 359 | } |
| 360 | |
| 361 | public void Bytes(int at, byte[] bytes) |
| 362 | { |
| 363 | _stream.Position = at; |
| 364 | _stream.Write(bytes); |
| 365 | } |
| 366 | |
| 367 | public void Append(byte[] bytes) |
| 368 | { |
| 369 | _stream.Position = _stream.Length; |
| 370 | _stream.Write(bytes); |
| 371 | } |
| 372 | |
| 373 | public void I32(int at, int value) |
| 374 | { |
| 375 | Bytes(at, BitConverter.GetBytes(value)); |
| 376 | } |
| 377 | |
| 378 | public void I16(int at, short value) |
| 379 | { |
| 380 | Bytes(at, BitConverter.GetBytes(value)); |
| 381 | } |
| 382 | |
| 383 | public void U16(int at, ushort value) |
| 384 | { |
| 385 | Bytes(at, BitConverter.GetBytes(value)); |
| 386 | } |
| 387 | |
| 388 | public void U8(int at, byte value) |
| 389 | { |
| 390 | Bytes(at, [value]); |
| 391 | } |
| 392 | |
| 393 | public void F32(int at, float value) |
| 394 | { |
| 395 | Bytes(at, BitConverter.GetBytes(value)); |
| 396 | } |
| 397 | |
| 398 | public void Half(int at, float value) |
| 399 | { |
| 400 | Bytes(at, BitConverter.GetBytes((Half)value)); |
| 401 | } |
| 402 | |
| 403 | public void Vec(int at, float x, float y, float z) |
| 404 | { |
| 405 | F32(at, x); |
| 406 | F32(at + 4, y); |
| 407 | F32(at + 8, z); |
| 408 | } |
| 409 | |
| 410 | public byte[] ToArray() |
| 411 | { |
| 412 | return _stream.ToArray(); |
| 413 | } |
| 414 | } |
| 415 | } |