forked from iiDk-the-actual/convert.iidk.online
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrbxXmlParser.js
More file actions
604 lines (553 loc) · 25 KB
/
rbxXmlParser.js
File metadata and controls
604 lines (553 loc) · 25 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
"use strict";
// ─── rbxXmlParser.js ─────────────────────────────────────────────────────────
// Decodes Roblox XML model files (.rbxmx / .rbxlx) into the same instance-tree
// format produced by rbxBinaryParser.js so callers need no special-casing.
//
// Output shape per instance:
// {
// ClassName : "Part",
// Children : [ ...child instances... ],
// _id : "<uuid>", // stable external identifier
// _rbxId : "RBX0" | null, // referent string from the XML (debug)
// <PropName>: <value>, // same shapes as binary decoder
// }
// ─────────────────────────────────────────────────────────────────────────────
const { XMLParser } = require("fast-xml-parser");
const { v4: uuidv4 } = require("uuid");
// ─── All XML property type tag names ─────────────────────────────────────────
const PROP_TAGS = [
"string", "ProtectedString", "BinaryString", "SharedString",
"bool",
"int", "int64",
"float", "double",
"token",
"CoordinateFrame",
"Vector3", "Vector2", "Vector3int16", "Vector2int16",
"UDim", "UDim2",
"Ray",
"Faces", "Axes",
"BrickColor",
"Color3", "Color3uint8",
"NumberSequence", "ColorSequence",
"NumberRange",
"Rect",
"PhysicalProperties",
"Content",
"Ref",
"UniqueId",
];
const m = {
1: ["White", [242, 243, 243]],
2: ["Grey", [161, 165, 162]],
3: ["Light yellow", [249, 233, 153]],
5: ["Brick yellow", [215, 197, 154]],
6: ["Light green (Mint)", [194, 218, 184]],
9: ["Light reddish violet", [232, 186, 200]],
11: ["Pastel Blue", [128, 187, 219]],
12: ["Light orange brown", [203, 132, 66]],
18: ["Nougat", [204, 142, 105]],
21: ["Bright red", [196, 40, 28]],
22: ["Med. reddish violet", [196, 112, 160]],
23: ["Bright blue", [13, 105, 172]],
24: ["Bright yellow", [245, 205, 48]],
25: ["Earth orange", [98, 71, 50]],
26: ["Black", [27, 42, 53]],
27: ["Dark grey", [109, 110, 108]],
28: ["Dark green", [40, 127, 71]],
29: ["Medium green", [161, 196, 140]],
36: ["Lig. Yellowich orange", [243, 207, 155]],
37: ["Bright green", [75, 151, 75]],
38: ["Dark orange", [160, 95, 53]],
39: ["Light bluish violet", [193, 202, 222]],
40: ["Transparent", [236, 236, 236]],
41: ["Tr. Red", [205, 84, 75]],
42: ["Tr. Lg blue", [193, 223, 240]],
43: ["Tr. Blue", [123, 182, 232]],
44: ["Tr. Yellow", [247, 241, 141]],
45: ["Light blue", [180, 210, 228]],
47: ["Tr. Flu. Reddish orange", [217, 133, 108]],
48: ["Tr. Green", [132, 182, 141]],
49: ["Tr. Flu. Green", [248, 241, 132]],
50: ["Phosph. White", [236, 232, 222]],
100: ["Light red", [238, 196, 182]],
101: ["Medium red", [218, 134, 122]],
102: ["Medium blue", [110, 153, 202]],
103: ["Light grey", [199, 193, 183]],
104: ["Bright violet", [107, 50, 124]],
105: ["Br. yellowish orange", [226, 155, 64]],
106: ["Bright orange", [218, 133, 65]],
107: ["Bright bluish green", [0, 143, 156]],
108: ["Earth yellow", [104, 92, 67]],
110: ["Bright bluish violet", [67, 84, 147]],
111: ["Tr. Brown", [191, 183, 177]],
112: ["Medium bluish violet", [104, 116, 172]],
113: ["Tr. Medi. reddish violet", [229, 173, 200]],
115: ["Med. yellowish green", [199, 210, 60]],
116: ["Med. bluish green", [85, 165, 175]],
118: ["Light bluish green", [183, 215, 213]],
119: ["Br. yellowish green", [164, 189, 71]],
120: ["Lig. yellowish green", [217, 228, 167]],
121: ["Med. yellowish orange", [231, 172, 88]],
123: ["Br. reddish orange", [211, 111, 76]],
124: ["Bright reddish violet", [146, 57, 120]],
125: ["Light orange", [234, 184, 146]],
126: ["Tr. Bright bluish violet", [165, 165, 203]],
127: ["Gold", [220, 188, 129]],
128: ["Dark nougat", [174, 122, 89]],
131: ["Silver", [156, 163, 168]],
133: ["Neon orange", [213, 115, 61]],
134: ["Neon green", [216, 221, 86]],
135: ["Sand blue", [116, 134, 157]],
136: ["Sand violet", [135, 124, 144]],
137: ["Medium orange", [224, 152, 100]],
138: ["Sand yellow", [149, 138, 115]],
140: ["Earth blue", [32, 58, 86]],
141: ["Earth green", [39, 70, 45]],
143: ["Tr. Flu. Blue", [207, 226, 247]],
145: ["Sand blue metallic", [121, 136, 161]],
146: ["Sand violet metallic", [149, 142, 163]],
147: ["Sand yellow metallic", [147, 135, 103]],
148: ["Dark grey metallic", [87, 88, 87]],
149: ["Black metallic", [22, 29, 50]],
150: ["Light grey metallic", [171, 173, 172]],
151: ["Sand green", [120, 144, 130]],
153: ["Sand red", [149, 121, 119]],
154: ["Dark red", [123, 46, 47]],
157: ["Tr. Flu. Yellow", [255, 246, 123]],
158: ["Tr. Flu. Red", [225, 164, 194]],
168: ["Gun metallic", [117, 108, 98]],
176: ["Red flip/flop", [151, 105, 91]],
178: ["Yellow flip/flop", [180, 132, 85]],
179: ["Silver flip/flop", [137, 135, 136]],
180: ["Curry", [215, 169, 75]],
190: ["Fire Yellow", [249, 214, 46]],
191: ["Flame yellowish orange", [232, 171, 45]],
192: ["Reddish brown", [105, 64, 40]],
193: ["Flame reddish orange", [207, 96, 36]],
194: ["Medium stone grey", [163, 162, 165]],
195: ["Royal blue", [70, 103, 164]],
196: ["Dark Royal blue", [35, 71, 139]],
198: ["Bright reddish lilac", [142, 66, 133]],
199: ["Dark stone grey", [99, 95, 98]],
200: ["Lemon metalic", [130, 138, 93]],
208: ["Light stone grey", [229, 228, 223]],
209: ["Dark Curry", [176, 142, 68]],
210: ["Faded green", [112, 149, 120]],
211: ["Turquoise", [121, 181, 181]],
212: ["Light Royal blue", [159, 195, 233]],
213: ["Medium Royal blue", [108, 129, 183]],
216: ["Rust", [144, 76, 42]],
217: ["Brown", [124, 92, 70]],
218: ["Reddish lilac", [150, 112, 159]],
219: ["Lilac", [107, 98, 155]],
220: ["Light lilac", [167, 169, 206]],
221: ["Bright purple", [205, 98, 152]],
222: ["Light purple", [228, 173, 200]],
223: ["Light pink", [220, 144, 149]],
224: ["Light brick yellow", [240, 213, 160]],
225: ["Warm yellowish orange", [235, 184, 127]],
226: ["Cool yellow", [253, 234, 141]],
232: ["Dove blue", [125, 187, 221]],
268: ["Medium lilac", [52, 43, 117]],
301: ["Slime green", [80, 109, 84]],
302: ["Smoky grey", [91, 93, 105]],
303: ["Dark blue", [0, 16, 176]],
304: ["Parsley green", [44, 101, 29]],
305: ["Steel blue", [82, 124, 174]],
306: ["Storm blue", [51, 88, 130]],
307: ["Lapis", [16, 42, 220]],
308: ["Dark indigo", [61, 21, 133]],
309: ["Sea green", [52, 142, 64]],
310: ["Shamrock", [91, 154, 76]],
311: ["Fossil", [159, 161, 172]],
312: ["Mulberry", [89, 34, 89]],
313: ["Forest green", [31, 128, 29]],
314: ["Cadet blue", [159, 173, 192]],
315: ["Electric blue", [9, 137, 207]],
316: ["Eggplant", [123, 0, 123]],
317: ["Moss", [124, 156, 107]],
318: ["Artichoke", [138, 171, 133]],
319: ["Sage green", [185, 196, 177]],
320: ["Ghost grey", [202, 203, 209]],
321: ["Lilac", [167, 94, 155]],
322: ["Plum", [123, 47, 123]],
323: ["Olivine", [148, 190, 129]],
324: ["Laurel green", [168, 189, 153]],
325: ["Quill grey", [223, 223, 222]],
327: ["Crimson", [151, 0, 0]],
328: ["Mint", [177, 229, 166]],
329: ["Baby blue", [152, 194, 219]],
330: ["Carnation pink", [255, 152, 220]],
331: ["Persimmon", [255, 89, 89]],
332: ["Maroon", [117, 0, 0]],
333: ["Gold", [239, 184, 56]],
334: ["Daisy orange", [248, 217, 109]],
335: ["Pearl", [231, 231, 236]],
336: ["Fog", [199, 212, 228]],
337: ["Salmon", [255, 148, 148]],
338: ["Terra Cotta", [190, 104, 98]],
339: ["Cocoa", [86, 36, 36]],
340: ["Wheat", [241, 231, 199]],
341: ["Buttermilk", [254, 243, 187]],
342: ["Mauve", [224, 178, 208]],
343: ["Sunrise", [212, 144, 189]],
344: ["Tawny", [150, 85, 85]],
345: ["Rust", [143, 76, 42]],
346: ["Cashmere", [211, 190, 150]],
347: ["Khaki", [226, 220, 188]],
348: ["Lily white", [237, 234, 234]],
349: ["Seashell", [233, 218, 218]],
350: ["Burgundy", [136, 62, 62]],
351: ["Cork", [188, 155, 93]],
352: ["Burlap", [199, 172, 120]],
353: ["Beige", [202, 191, 163]],
354: ["Oyster", [187, 179, 178]],
355: ["Pine Cone", [108, 88, 75]],
356: ["Fawn brown", [160, 132, 79]],
357: ["Hurricane grey", [149, 137, 136]],
358: ["Cloudy grey", [171, 168, 158]],
359: ["Linen", [175, 148, 131]],
360: ["Copper", [150, 103, 102]],
361: ["Medium brown", [86, 66, 54]],
362: ["Bronze", [126, 104, 63]],
363: ["Flint", [105, 102, 92]],
364: ["Dark taupe", [90, 76, 66]],
365: ["Burnt Sienna", [106, 57, 9]],
1001: ["Institutional white", [248, 248, 248]],
1002: ["Mid gray", [205, 205, 205]],
1003: ["Really black", [17, 17, 17]],
1004: ["Really red", [255, 0, 0]],
1005: ["Deep orange", [255, 176, 0]],
1006: ["Alder", [180, 128, 255]],
1007: ["Dusty Rose", [163, 75, 75]],
1008: ["Olive", [193, 190, 66]],
1009: ["New Yeller", [255, 255, 0]],
1010: ["Really blue", [0, 0, 255]],
1011: ["Navy blue", [0, 32, 96]],
1012: ["Deep blue", [33, 84, 185]],
1013: ["Cyan", [4, 175, 236]],
1014: ["CGA brown", [170, 85, 0]],
1015: ["Magenta", [170, 0, 170]],
1016: ["Pink", [255, 102, 204]],
1017: ["Deep orange", [255, 175, 0]],
1018: ["Teal", [18, 238, 212]],
1019: ["Toothpaste", [0, 255, 255]],
1020: ["Lime green", [0, 255, 0]],
1021: ["Camo", [58, 125, 21]],
1022: ["Grime", [127, 142, 100]],
1023: ["Lavender", [140, 91, 159]],
1024: ["Pastel light blue", [175, 221, 255]],
1025: ["Pastel orange", [255, 201, 201]],
1026: ["Pastel violet", [177, 167, 255]],
1027: ["Pastel blue-green", [159, 243, 233]],
1028: ["Pastel green", [204, 255, 204]],
1029: ["Pastel yellow", [255, 255, 204]],
1030: ["Pastel brown", [255, 204, 153]],
1031: ["Royal purple", [98, 37, 209]],
1032: ["Hot pink", [255, 0, 191]]
};
// ─── Rotation matrix → Euler angles (YXZ intrinsic, matches BasePart.Orientation) ──
function matrixToEuler(m) {
// m = [R00, R01, R02, R10, R11, R12, R20, R21, R22]
// YXZ decomposition: R = Ry(Y) * Rx(X) * Rz(Z)
const a = Math.atan2(-m[5], Math.sqrt(m[3] * m[3] + m[4] * m[4]));
const b = Math.atan2(m[2], m[8]);
const c = Math.atan2(m[3], m[4]);
return [180 * a / Math.PI, 180 * b / Math.PI, 180 * c / Math.PI];
}
var e = {
d: (a, r) => {
for (var t in r) e.o(r, t) && !e.o(a, t) && Object.defineProperty(a, t, {
enumerable: !0,
get: r[t]
})
},
o: (e, a) => Object.prototype.hasOwnProperty.call(e, a),
r: e => {
"undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, {
value: "Module"
}), Object.defineProperty(e, "__esModule", {
value: !0
})
}
},
a = {};
e.r(a), e.d(a, {
decode: () => c
});
// ─── Parse a single property entry ───────────────────────────────────────────
// `entry` is the object fast-xml-parser built for one property element.
// Simple types store their value in entry["#text"]; complex types store child
// elements as keys directly on the entry object.
function parsePropValue(type, entry, propName) {
// For simple (leaf) types the text content lives under #text.
const text = (entry != null && typeof entry === "object")
? entry["#text"]
: entry;
if (propName === "BrickColor") {
const num = parseInt(text, 10);
const entryColor = m[num];
return {
Number: num,
Name: entryColor ? entryColor[0] : "Really black",
Color: entryColor ? { R: entryColor[1][0], G: entryColor[1][1], B: entryColor[1][2] } : { R: 0, G: 0, B: 0 }
};
}
switch (type) {
// ── Strings ──────────────────────────────────────────────────────────────
case "string":
case "ProtectedString":
case "BinaryString":
case "SharedString":
return text == null ? "" : String(text);
// ── Primitives ───────────────────────────────────────────────────────────
case "bool":
return text === "true" || text === true;
case "int":
case "token":
case "int64":
return parseInt(text, 10);
case "float":
case "double":
return parseFloat(text);
// ── Vectors ──────────────────────────────────────────────────────────────
case "Vector3":
case "Vector3int16":
return {
X: parseFloat(entry.X ?? 0),
Y: parseFloat(entry.Y ?? 0),
Z: parseFloat(entry.Z ?? 0),
};
case "Vector2":
case "Vector2int16":
return {
X: parseFloat(entry.X ?? 0),
Y: parseFloat(entry.Y ?? 0),
};
// ── CFrame ───────────────────────────────────────────────────────────────
case "CoordinateFrame": {
const x = parseFloat(entry.X ?? 0);
const y = parseFloat(entry.Y ?? 0);
const z = parseFloat(entry.Z ?? 0);
const matrix = [
parseFloat(entry.R00 ?? 1), parseFloat(entry.R01 ?? 0), parseFloat(entry.R02 ?? 0),
parseFloat(entry.R10 ?? 0), parseFloat(entry.R11 ?? 1), parseFloat(entry.R12 ?? 0),
parseFloat(entry.R20 ?? 0), parseFloat(entry.R21 ?? 0), parseFloat(entry.R22 ?? 1),
];
const [ox, oy, oz] = matrixToEuler(matrix);
return {
Position: { X: x, Y: y, Z: z },
Orientation: { X: ox, Y: oy, Z: oz },
Components: [x, y, z, ...matrix],
};
}
// ── UDim / UDim2 ─────────────────────────────────────────────────────────
case "UDim":
return {
Scale: parseFloat(entry.S ?? entry.Scale ?? 0),
Offset: parseFloat(entry.O ?? entry.Offset ?? 0),
};
case "UDim2":
return {
X: {
Scale: parseFloat(entry.XS ?? 0),
Offset: parseFloat(entry.XO ?? 0),
},
Y: {
Scale: parseFloat(entry.YS ?? 0),
Offset: parseFloat(entry.YO ?? 0),
},
};
// ── Ray ──────────────────────────────────────────────────────────────────
case "Ray": {
const orig = entry.origin ?? entry.Origin ?? {};
const dir = entry.direction ?? entry.Direction ?? {};
return {
Origin: { X: parseFloat(orig.X ?? 0), Y: parseFloat(orig.Y ?? 0), Z: parseFloat(orig.Z ?? 0) },
Direction: { X: parseFloat(dir.X ?? 0), Y: parseFloat(dir.Y ?? 0), Z: parseFloat(dir.Z ?? 0) },
};
}
// ── Faces / Axes ─────────────────────────────────────────────────────────
case "Faces":
return {
Right: entry.Right === "true",
Top: entry.Top === "true",
Back: entry.Back === "true",
Left: entry.Left === "true",
Bottom: entry.Bottom === "true",
Front: entry.Front === "true",
};
case "Axes":
return {
X: entry.X === "true",
Y: entry.Y === "true",
Z: entry.Z === "true",
};
// ── Colour ───────────────────────────────────────────────────────────────
case "BrickColor": {
const num = parseInt(text, 10);
const entry = m[num];
return {
Number: num,
Name: entry ? entry[0] : "Really black",
Color: entry ? { R: entry[1][0], G: entry[1][1], B: entry[1][2] } : { R: 0, G: 0, B: 0 }
};
}
case "Color3":
case "Color3uint8":
return {
R: parseFloat(entry.R ?? 0),
G: parseFloat(entry.G ?? 0),
B: parseFloat(entry.B ?? 0),
};
// ── Sequences / Ranges ───────────────────────────────────────────────────
case "NumberSequence": {
const raw = text ? String(text).trim() : "";
const parts = raw.split(/\s+/).map(Number).filter(n => !isNaN(n));
const out = [];
// Format: (count? or just triplets: time value envelope ...)
for (let i = 0; i + 2 < parts.length; i += 3)
out.push({ Time: parts[i], Value: parts[i+1], Envelope: parts[i+2] });
return out;
}
case "NumberRange":
return {
Min: parseFloat(entry.min ?? entry.Min ?? 0),
Max: parseFloat(entry.max ?? entry.Max ?? 0),
};
// ── Rect ─────────────────────────────────────────────────────────────────
case "Rect": {
const min = entry.min ?? entry.Min ?? {};
const max = entry.max ?? entry.Max ?? {};
return {
Min: { X: parseFloat(min.X ?? 0), Y: parseFloat(min.Y ?? 0) },
Max: { X: parseFloat(max.X ?? 0), Y: parseFloat(max.Y ?? 0) },
};
}
// ── PhysicalProperties ───────────────────────────────────────────────────
case "PhysicalProperties": {
// Custom physics: <PhysicalProperties><CustomPhysics>true</CustomPhysics>...</>
if (entry.CustomPhysics === "true" || entry.CustomPhysics === true) {
return {
Density: parseFloat(entry.Density ?? 0),
Friction: parseFloat(entry.Friction ?? 0),
Elasticity: parseFloat(entry.Elasticity ?? 0),
FrictionWeight: parseFloat(entry.FrictionWeight ?? 1),
ElasticityWeight: parseFloat(entry.ElasticityWeight ?? 1),
};
}
return null; // default physics → binary parity
}
// ── Content (asset URLs) ─────────────────────────────────────────────────
case "Content": {
if (entry != null && typeof entry === "object") {
if (entry.url != null) return entry.url ? String(entry.url) : null;
if (entry.null != null) return null;
if (entry.hash != null) return String(entry.hash);
}
return text != null ? String(text) : null;
}
// ── References (resolved to { __ref: uuid } in a second pass) ────────────
case "Ref": {
const s = text != null ? String(text).trim() : "";
if (!s || s === "null" || s === "nil") return null;
return { __ref: s }; // __ref holds the XML referent string for now
}
// ── UniqueId ─────────────────────────────────────────────────────────────
case "UniqueId":
return text != null ? String(text) : null;
default:
return entry;
}
}
// ─── Public API ──────────────────────────────────────────────────────────────
/**
* Detect whether a buffer contains an rbxmx XML file.
* Checks for `<roblox xmlns` in the first 512 bytes (skips UTF-8 BOM if present).
*/
function isXmlBuffer(buffer) {
const head = Buffer.from(buffer).slice(0, 512).toString("utf-8").trimStart();
return head.startsWith("<roblox xmlns") || head.startsWith("<roblox\n") || head.startsWith("<roblox ");
}
/**
* Decode an rbxmx XML buffer into an instance tree.
* Returns an array of root instances (same shape as rbxBinaryParser.decode).
*/
function decodeXml(buffer) {
const text = Buffer.from(buffer).toString("utf-8");
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "@_",
// Force arrays for Item lists and every property type tag so that
// duplicate tags (e.g. multiple <float> entries) always become arrays.
isArray: (name) => name === "Item" || PROP_TAGS.includes(name),
parseTagValue: false, // keep all text as strings; we parse manually
trimValues: true,
parseAttributeValue: false,
});
const doc = parser.parse(text);
if (!doc || !doc.roblox) throw new Error("Not a valid Roblox XML file");
const roblox = doc.roblox;
// referent string → instance object (for Ref resolution)
const referentMap = new Map();
// ── Recursively build instance tree ──────────────────────────────────────
function parseItem(itemNode) {
const className = itemNode["@_class"] ?? "Unknown";
const referent = itemNode["@_referent"] ?? null;
const inst = {
ClassName: className,
Children: [],
_id: uuidv4(),
_rbxId: referent,
};
if (referent) referentMap.set(referent, inst);
// ── Properties ───────────────────────────────────────────────────────
const props = itemNode.Properties;
if (props && typeof props === "object") {
for (const [type, entries] of Object.entries(props)) {
if (type.startsWith("@_")) continue;
const arr = Array.isArray(entries) ? entries : [entries];
for (const entry of arr) {
if (entry == null) continue;
const rawName = (typeof entry === "object" ? entry["@_name"] : null);
if (!rawName) continue;
// Mirror binary parser: capitalise the first letter of property names
const propName = rawName.charAt(0).toUpperCase() + rawName.slice(1);
// Color3uint8 is stored separately but exposed as Color3 (matches binary)
const exposedName = propName === "Color3uint8" ? "Color3" : propName;
inst[exposedName] = parsePropValue(type, entry, propName);
}
}
}
// ── Child Items ──────────────────────────────────────────────────────
if (Array.isArray(itemNode.Item)) {
for (const child of itemNode.Item) {
inst.Children.push(parseItem(child));
}
}
return inst;
}
const roots = [];
if (Array.isArray(roblox.Item)) {
for (const item of roblox.Item) {
roots.push(parseItem(item));
}
}
// ── Second pass: resolve Ref values ──────────────────────────────────────
// Replace { __ref: "RBX0" } (XML referent) with { __ref: "<uuid>" } so the
// output is identical in shape to the binary parser's Reference values.
function resolveRefs(inst) {
for (const key of Object.keys(inst)) {
if (key === "Children" || key === "_id" || key === "_rbxId" || key === "ClassName") continue;
const val = inst[key];
if (val && typeof val === "object" && typeof val.__ref === "string") {
const target = referentMap.get(val.__ref);
inst[key] = target ? { __ref: target._id } : null;
}
}
for (const child of inst.Children) resolveRefs(child);
}
for (const root of roots) resolveRefs(root);
return roots;
}
module.exports = { decodeXml, isXmlBuffer };