@carbonenginejs/runtime-resource 0.8.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +31 -29
  3. package/README.md +67 -548
  4. package/dist/CjsMotherLode.js +1271 -1271
  5. package/dist/CjsResMan.js +3005 -3005
  6. package/dist/CjsResManQueue.js +214 -214
  7. package/dist/CjsResource.js +566 -566
  8. package/dist/CjsResourceSource.js +68 -59
  9. package/dist/CjsResourceSource.js.map +1 -1
  10. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  11. package/dist/format/CjsBlueReader.js +269 -269
  12. package/dist/format/CjsFormat.js +197 -192
  13. package/dist/format/CjsFormat.js.map +1 -1
  14. package/dist/format/CjsReader.js +18 -18
  15. package/dist/format/CjsResourceProbe.js +277 -277
  16. package/dist/format/payloadContract.js +173 -173
  17. package/dist/formats/black/CjsBlackFormat.js +278 -278
  18. package/dist/formats/black/core/CjsBlackBinaryReader.js +157 -153
  19. package/dist/formats/black/core/CjsBlackBinaryReader.js.map +1 -1
  20. package/dist/formats/black/core/CjsBlackPropertyReaders.js +387 -382
  21. package/dist/formats/black/core/CjsBlackPropertyReaders.js.map +1 -1
  22. package/dist/formats/black/core/CjsBlackReader.js +639 -639
  23. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +437 -433
  24. package/dist/formats/black/core/CjsBlackSchemaRegistry.js.map +1 -1
  25. package/dist/formats/black/core/black-schema-v1-2026-07-23.json.js +4 -0
  26. package/dist/formats/black/core/black-schema-v1-2026-07-23.json.js.map +1 -0
  27. package/dist/formats/black/core/blackConstants.js +7 -7
  28. package/dist/formats/black/core/blackDefinitions.js +10 -10
  29. package/dist/formats/black/core/blackDefinitions.js.map +1 -1
  30. package/dist/formats/black/core/blackEnums.js +6 -6
  31. package/dist/formats/black/core/blackSchema.js +3 -3
  32. package/dist/formats/black/core/blackVersion.js +22 -22
  33. package/dist/formats/black/core/helpers.js +182 -182
  34. package/dist/formats/black/core/schema.js +4 -4
  35. package/dist/formats/black/index.js +2 -2
  36. package/dist/formats/bnk/CjsBnkFormat.js +125 -125
  37. package/dist/formats/bnk/core/graph.js +140 -140
  38. package/dist/formats/bnk/core/helpers.js +409 -409
  39. package/dist/formats/bnk/core/musicNodes.js +493 -489
  40. package/dist/formats/bnk/core/musicNodes.js.map +1 -1
  41. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  42. package/dist/formats/bnk/index.js +2 -2
  43. package/dist/formats/cmf/CjsCmfFormat.js +497 -497
  44. package/dist/formats/cmf/core/binary.js +123 -118
  45. package/dist/formats/cmf/core/binary.js.map +1 -1
  46. package/dist/formats/cmf/core/buffers.js +233 -233
  47. package/dist/formats/cmf/core/constants.js +47 -47
  48. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  49. package/dist/formats/cmf/core/helpers.js +287 -287
  50. package/dist/formats/cmf/core/pack.js +276 -276
  51. package/dist/formats/cmf/core/schema.js +364 -364
  52. package/dist/formats/cmf/core/shared.js +268 -268
  53. package/dist/formats/cmf/core/writer.js +517 -517
  54. package/dist/formats/cmf/index.js +2 -2
  55. package/dist/formats/dds/CjsDdsFormat.js +200 -193
  56. package/dist/formats/dds/CjsDdsFormat.js.map +1 -1
  57. package/dist/formats/dds/core/bc6h.js +288 -288
  58. package/dist/formats/dds/core/bc7.js +256 -251
  59. package/dist/formats/dds/core/bc7.js.map +1 -1
  60. package/dist/formats/dds/core/helpers.js +815 -815
  61. package/dist/formats/dds/index.js +2 -2
  62. package/dist/formats/fbx/CjsFbxFormat.js +266 -266
  63. package/dist/formats/fbx/core/helpers.js +3911 -3901
  64. package/dist/formats/fbx/core/helpers.js.map +1 -1
  65. package/dist/formats/fbx/index.js +2 -2
  66. package/dist/formats/flac/CjsFlacFormat.js +93 -87
  67. package/dist/formats/flac/CjsFlacFormat.js.map +1 -1
  68. package/dist/formats/flac/core/helpers.js +295 -295
  69. package/dist/formats/flac/index.js +2 -2
  70. package/dist/formats/gif/CjsGifFormat.js +92 -87
  71. package/dist/formats/gif/CjsGifFormat.js.map +1 -1
  72. package/dist/formats/gif/core/helpers.js +360 -360
  73. package/dist/formats/gif/index.js +2 -2
  74. package/dist/formats/gltf/CjsGltfFormat.js +290 -290
  75. package/dist/formats/gltf/core/helpers.js +292 -292
  76. package/dist/formats/gltf/core/json.js +79 -79
  77. package/dist/formats/gltf/core/parser.js +666 -666
  78. package/dist/formats/gltf/core/targets.js +163 -163
  79. package/dist/formats/gltf/index.js +2 -2
  80. package/dist/formats/gr2/CjsGr2Format.js +46 -0
  81. package/dist/formats/gr2/CjsGr2Format.js.map +1 -0
  82. package/dist/formats/gr2/core/CjsFormatGr2.js +273 -0
  83. package/dist/formats/gr2/core/CjsFormatGr2.js.map +1 -0
  84. package/dist/formats/gr2/core/bitknit2.js +280 -0
  85. package/dist/formats/gr2/core/bitknit2.js.map +1 -0
  86. package/dist/formats/gr2/core/curves.js +1047 -0
  87. package/dist/formats/gr2/core/curves.js.map +1 -0
  88. package/dist/formats/gr2/core/gsf.js +72 -0
  89. package/dist/formats/gr2/core/gsf.js.map +1 -0
  90. package/dist/formats/gr2/core/helpers.js +332 -0
  91. package/dist/formats/gr2/core/helpers.js.map +1 -0
  92. package/dist/formats/gr2/core/json.js +622 -0
  93. package/dist/formats/gr2/core/json.js.map +1 -0
  94. package/dist/formats/gr2/core/oodle1.js +388 -0
  95. package/dist/formats/gr2/core/oodle1.js.map +1 -0
  96. package/dist/formats/gr2/core/reader.js +617 -0
  97. package/dist/formats/gr2/core/reader.js.map +1 -0
  98. package/dist/formats/gr2/core/tangents.js +48 -0
  99. package/dist/formats/gr2/core/tangents.js.map +1 -0
  100. package/dist/formats/gr2/core/targets.js +351 -0
  101. package/dist/formats/gr2/core/targets.js.map +1 -0
  102. package/dist/formats/gr2/index.js +3 -0
  103. package/dist/formats/gr2/index.js.map +1 -0
  104. package/dist/formats/index.js +30 -23
  105. package/dist/formats/index.js.map +1 -1
  106. package/dist/formats/jpeg/CjsJpegFormat.js +212 -206
  107. package/dist/formats/jpeg/CjsJpegFormat.js.map +1 -1
  108. package/dist/formats/jpeg/core/helpers.js +350 -350
  109. package/dist/formats/jpeg/core/jpeg.js +387 -377
  110. package/dist/formats/jpeg/core/jpeg.js.map +1 -1
  111. package/dist/formats/jpeg/index.js +2 -2
  112. package/dist/formats/mp3/CjsMp3Format.js +197 -192
  113. package/dist/formats/mp3/CjsMp3Format.js.map +1 -1
  114. package/dist/formats/mp3/core/helpers.js +338 -338
  115. package/dist/formats/mp3/index.js +2 -2
  116. package/dist/formats/mp4/CjsMp4Format.js +197 -191
  117. package/dist/formats/mp4/CjsMp4Format.js.map +1 -1
  118. package/dist/formats/mp4/core/helpers.js +449 -449
  119. package/dist/formats/mp4/index.js +2 -2
  120. package/dist/formats/obj/CjsObjFormat.js +253 -253
  121. package/dist/formats/obj/core/helpers.js +573 -573
  122. package/dist/formats/obj/core/json.js +64 -64
  123. package/dist/formats/obj/core/parser.js +321 -321
  124. package/dist/formats/obj/index.js +2 -2
  125. package/dist/formats/ogg/CjsOggFormat.js +94 -88
  126. package/dist/formats/ogg/CjsOggFormat.js.map +1 -1
  127. package/dist/formats/ogg/core/helpers.js +387 -387
  128. package/dist/formats/ogg/core/imdct.js +178 -178
  129. package/dist/formats/ogg/core/vorbis.js +1004 -999
  130. package/dist/formats/ogg/core/vorbis.js.map +1 -1
  131. package/dist/formats/ogg/index.js +2 -2
  132. package/dist/formats/png/CjsPngFormat.js +201 -195
  133. package/dist/formats/png/CjsPngFormat.js.map +1 -1
  134. package/dist/formats/png/core/helpers.js +583 -583
  135. package/dist/formats/png/index.js +2 -2
  136. package/dist/formats/red/CjsRedFormat.js +261 -261
  137. package/dist/formats/red/core/CjsRedReader.js +194 -194
  138. package/dist/formats/red/core/blackDefinitions.js +3 -3
  139. package/dist/formats/red/core/helpers.js +139 -139
  140. package/dist/formats/red/core/redGraph.js +68 -68
  141. package/dist/formats/red/core/schema.js +4 -4
  142. package/dist/formats/red/index.js +2 -2
  143. package/dist/formats/stl/CjsStlFormat.js +365 -365
  144. package/dist/formats/stl/core/helpers.js +261 -261
  145. package/dist/formats/stl/core/json.js +51 -51
  146. package/dist/formats/stl/core/stl.js +634 -629
  147. package/dist/formats/stl/core/stl.js.map +1 -1
  148. package/dist/formats/stl/core/targets.js +163 -163
  149. package/dist/formats/stl/index.js +2 -2
  150. package/dist/formats/tga/CjsTgaFormat.js +197 -192
  151. package/dist/formats/tga/CjsTgaFormat.js.map +1 -1
  152. package/dist/formats/tga/core/helpers.js +446 -446
  153. package/dist/formats/tga/index.js +2 -2
  154. package/dist/formats/wav/CjsWavFormat.js +198 -192
  155. package/dist/formats/wav/CjsWavFormat.js.map +1 -1
  156. package/dist/formats/wav/core/helpers.js +328 -328
  157. package/dist/formats/wav/index.js +2 -2
  158. package/dist/formats/webm/CjsWebmFormat.js +197 -191
  159. package/dist/formats/webm/CjsWebmFormat.js.map +1 -1
  160. package/dist/formats/webm/core/helpers.js +537 -537
  161. package/dist/formats/webm/index.js +2 -2
  162. package/dist/formats/webp/CjsWebpFormat.js +91 -86
  163. package/dist/formats/webp/CjsWebpFormat.js.map +1 -1
  164. package/dist/formats/webp/core/helpers.js +214 -214
  165. package/dist/formats/webp/index.js +2 -2
  166. package/dist/formats/wem/CjsWemFormat.js +242 -241
  167. package/dist/formats/wem/CjsWemFormat.js.map +1 -1
  168. package/dist/formats/wem/core/bitStream.js +259 -259
  169. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  170. package/dist/formats/wem/core/helpers.js +417 -417
  171. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  172. package/dist/formats/wem/core/ptadpcm.js +77 -77
  173. package/dist/formats/wem/core/resolve.js +121 -121
  174. package/dist/formats/wem/core/wemToOgg.js +485 -485
  175. package/dist/formats/wem/index.js +2 -2
  176. package/dist/formats/yaml/CjsYamlFormat.js +89 -83
  177. package/dist/formats/yaml/CjsYamlFormat.js.map +1 -1
  178. package/dist/formats/yaml/core/CjsYamlReader.js +311 -305
  179. package/dist/formats/yaml/core/CjsYamlReader.js.map +1 -1
  180. package/dist/formats/yaml/core/helpers.js +160 -160
  181. package/dist/formats/yaml/index.js +2 -2
  182. package/dist/index.js +49 -48
  183. package/dist/index.js.map +1 -1
  184. package/dist/resourcePath.js +18 -18
  185. package/dist/resourceStates.js +9 -9
  186. package/dist/resources/AudioGeometryResData.js +47 -47
  187. package/dist/resources/GStateBindingCallbackData.js +31 -31
  188. package/dist/resources/MeshDecalData.js +37 -37
  189. package/dist/resources/MeshDecalLodData.js +34 -34
  190. package/dist/resources/Tr2EffectRes.js +71 -71
  191. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -60
  192. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  193. package/dist/resources/Tr2ImageRes.js +114 -114
  194. package/dist/resources/Tr2LightProfileRes.js +40 -40
  195. package/dist/resources/Tr2MaterialArea.js +34 -34
  196. package/dist/resources/Tr2MaterialMesh.js +31 -31
  197. package/dist/resources/Tr2MaterialRes.js +34 -34
  198. package/dist/resources/Tr2ShaderPermutation.js +43 -43
  199. package/dist/resources/Tr2TextureLodManager.js +80 -80
  200. package/dist/resources/Tr2TextureLodUpdateRequest.js +37 -37
  201. package/dist/resources/Tr2TexturePackChannel.js +37 -37
  202. package/dist/resources/Tr2TexturePipeline.js +52 -52
  203. package/dist/resources/Tr2TexturePipelineParams.js +34 -34
  204. package/dist/resources/Tr2TexturePipelineStepCompress.js +40 -40
  205. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js +22 -0
  206. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js.map +1 -0
  207. package/dist/resources/Tr2TexturePipelineStepLimitSize.js +34 -34
  208. package/dist/resources/Tr2TexturePipelineStepLoad.js +31 -31
  209. package/dist/resources/Tr2TexturePipelineStepPack.js +43 -43
  210. package/dist/resources/TriGeometryRes.js +239 -239
  211. package/dist/resources/TriGeometryResAreaData.js +59 -59
  212. package/dist/resources/TriGeometryResJointData.js +38 -38
  213. package/dist/resources/TriGeometryResLodData.js +88 -88
  214. package/dist/resources/TriGeometryResMeshData.js +63 -63
  215. package/dist/resources/TriGeometryResSkeletonData.js +34 -34
  216. package/dist/resources/TriGrannyRes.js +43 -43
  217. package/dist/resources/TriJointBinding.js +38 -38
  218. package/dist/resources/TriMorphTargetGeometryConstants.js +46 -46
  219. package/dist/resources/TriRtGeometryConstants.js +88 -88
  220. package/dist/resources/TriTextureRes.js +304 -304
  221. package/dist/resources/enums.js +18 -18
  222. package/dist/resources/resourceBoundary.js +47 -47
  223. package/dist/resources/texturePipelineBehavior.js +308 -308
  224. package/dist/texture/CjsTextureArrayRes.js +406 -406
  225. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  226. package/docs/README.md +73 -0
  227. package/docs/architecture.md +86 -0
  228. package/docs/concepts/resource-lifecycle.md +217 -0
  229. package/docs/formats/README.md +105 -0
  230. package/docs/formats/gr2.md +161 -0
  231. package/{FORMAT-PROVENANCE.md → docs/formats/provenance.md} +173 -155
  232. package/docs/formats/stl.md +37 -0
  233. package/docs/formats/wwise.md +44 -0
  234. package/docs/reference/classes/README.md +33 -0
  235. package/docs/reference/classes/core.md +106 -0
  236. package/docs/reference/classes/dropped.md +46 -0
  237. package/docs/reference/classes/formats.md +522 -0
  238. package/docs/reference/classes/resources.md +346 -0
  239. package/docs/reference/classes/texture.md +26 -0
  240. package/docs/reference/events.md +92 -0
  241. package/docs/reference/motherlode-cache.md +244 -0
  242. package/docs/reference/queues.md +102 -0
  243. package/docs/reference/reload.md +107 -0
  244. package/docs/reference/texture-arrays.md +113 -0
  245. package/docs/reference/texture-pipeline.md +53 -0
  246. package/docs/roadmap.md +104 -0
  247. package/format-notices/black/LICENSE +21 -21
  248. package/format-notices/black/NOTICE +47 -47
  249. package/format-notices/bnk/LICENSE +21 -21
  250. package/format-notices/bnk/NOTICE +20 -20
  251. package/format-notices/cmf/LICENSE +21 -21
  252. package/format-notices/cmf/NOTICE +36 -36
  253. package/format-notices/dds/LICENSE +21 -21
  254. package/format-notices/dds/NOTICE +14 -14
  255. package/format-notices/fbx/LICENSE +21 -21
  256. package/format-notices/fbx/NOTICE +14 -14
  257. package/format-notices/flac/LICENSE +21 -21
  258. package/format-notices/flac/NOTICE +14 -14
  259. package/format-notices/gif/LICENSE +21 -21
  260. package/format-notices/gif/NOTICE +14 -14
  261. package/format-notices/gltf/LICENSE +21 -21
  262. package/format-notices/gltf/NOTICE +27 -27
  263. package/format-notices/gr2/LICENSE +21 -0
  264. package/format-notices/gr2/NOTICE +60 -0
  265. package/format-notices/gr2/THIRD-PARTY-NOTICES.md +93 -0
  266. package/format-notices/jpeg/LICENSE +21 -21
  267. package/format-notices/jpeg/NOTICE +14 -14
  268. package/format-notices/mp3/LICENSE +21 -21
  269. package/format-notices/mp3/NOTICE +14 -14
  270. package/format-notices/mp4/LICENSE +21 -21
  271. package/format-notices/mp4/NOTICE +14 -14
  272. package/format-notices/obj/LICENSE +21 -21
  273. package/format-notices/obj/NOTICE +26 -26
  274. package/format-notices/ogg/LICENSE +21 -21
  275. package/format-notices/ogg/NOTICE +28 -28
  276. package/format-notices/png/LICENSE +21 -21
  277. package/format-notices/png/NOTICE +14 -14
  278. package/format-notices/red/LICENSE +21 -21
  279. package/format-notices/red/NOTICE +31 -31
  280. package/format-notices/stl/LICENSE +21 -21
  281. package/format-notices/stl/NOTICE +21 -21
  282. package/format-notices/tga/LICENSE +21 -21
  283. package/format-notices/tga/NOTICE +14 -14
  284. package/format-notices/wav/LICENSE +21 -21
  285. package/format-notices/wav/NOTICE +14 -14
  286. package/format-notices/webm/LICENSE +21 -21
  287. package/format-notices/webm/NOTICE +14 -14
  288. package/format-notices/webp/LICENSE +21 -21
  289. package/format-notices/webp/NOTICE +14 -14
  290. package/format-notices/wem/LICENSE +57 -57
  291. package/format-notices/wem/NOTICE +33 -33
  292. package/format-notices/yaml/LICENSE +21 -21
  293. package/format-notices/yaml/NOTICE +44 -44
  294. package/package.json +52 -51
  295. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +0 -4
  296. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js.map +0 -1
  297. package/resource-lifecycle.md +0 -679
@@ -0,0 +1,617 @@
1
+ import { decompressBitKnit2 } from './bitknit2.js';
2
+ import { decompressOodle1 } from './oodle1.js';
3
+
4
+ /**
5
+ * Low-level pure-JavaScript Granny .gr2 reader.
6
+ *
7
+ * The reader walks Granny's embedded reflection type tree, applies pointer
8
+ * fixups, decompresses sections through {@link decompressGr2Section}, and
9
+ * reconstructs a shared object graph.
10
+ */
11
+
12
+ /**
13
+ * Granny member type ids used by the reflected type tree.
14
+ *
15
+ * These values mirror the granny_member_type enum and are exposed for callers
16
+ * that need to inspect raw type metadata.
17
+ */
18
+ const GRANNY_MEMBER_TYPES = Object.freeze({
19
+ End: 0,
20
+ Inline: 1,
21
+ Reference: 2,
22
+ ReferenceToArray: 3,
23
+ ArrayOfReferences: 4,
24
+ VariantReference: 5,
25
+ UnsupportedRemove: 6,
26
+ ReferenceToVariantArray: 7,
27
+ String: 8,
28
+ Transform: 9,
29
+ Real32: 10,
30
+ Int8: 11,
31
+ UInt8: 12,
32
+ BinormalInt8: 13,
33
+ NormalUInt8: 14,
34
+ Int16: 15,
35
+ UInt16: 16,
36
+ BinormalInt16: 17,
37
+ NormalUInt16: 18,
38
+ Int32: 19,
39
+ UInt32: 20,
40
+ Real16: 21,
41
+ EmptyReference: 22
42
+ });
43
+ const M = GRANNY_MEMBER_TYPES;
44
+ /** Size in bytes of a reflected Granny transform member. */
45
+ const GRANNY_TRANSFORM_SIZE = 68;
46
+ const TRANSFORM_SIZE = GRANNY_TRANSFORM_SIZE;
47
+
48
+ /**
49
+ * Known GR2 file magic values mapped to their pointer size in bytes.
50
+ *
51
+ * The reader currently supports little-endian 32-bit and 64-bit Granny files.
52
+ */
53
+ const GR2_MAGICS = Object.freeze({
54
+ "29de6cc0baa4532b25f5b7a5f666e2ee": 4,
55
+ "e59b495e6f631f141e13eba990beedc4": 8
56
+ });
57
+ const MAGICS = GR2_MAGICS;
58
+ const HEX_BYTES = Array.from({
59
+ length: 256
60
+ }, (_, value) => value.toString(16).padStart(2, "0"));
61
+ const UTF8_DECODER = new TextDecoder("utf-8");
62
+
63
+ /**
64
+ * Convert bytes to a lowercase hexadecimal string without relying on Node's Buffer.
65
+ *
66
+ * @param {Uint8Array} bytes Source bytes.
67
+ * @returns {string} Two hexadecimal characters per byte.
68
+ */
69
+ function bytesToHex(bytes) {
70
+ let out = "";
71
+ for (let i = 0; i < bytes.length; i++) out += HEX_BYTES[bytes[i]];
72
+ return out;
73
+ }
74
+
75
+ /**
76
+ * Decode UTF-8 bytes using the browser and Node standard Encoding API.
77
+ *
78
+ * @param {Uint8Array} bytes UTF-8 encoded bytes.
79
+ * @returns {string} Decoded text.
80
+ */
81
+ function decodeUtf8(bytes) {
82
+ return UTF8_DECODER.decode(bytes);
83
+ }
84
+
85
+ /**
86
+ * Convert an IEEE 754 binary16 value to a JavaScript number.
87
+ *
88
+ * @param {number} h Unsigned 16-bit half-float bits.
89
+ * @returns {number} Decoded floating-point value.
90
+ */
91
+ function half2float(h) {
92
+ const s = (h & 0x8000) >> 15,
93
+ e = (h & 0x7c00) >> 10,
94
+ f = h & 0x03ff;
95
+ if (e === 0) return (s ? -1 : 1) * Math.pow(2, -14) * (f / 1024);
96
+ if (e === 0x1f) return f ? NaN : s ? -Infinity : Infinity;
97
+ return (s ? -1 : 1) * Math.pow(2, e - 15) * (1 + f / 1024);
98
+ }
99
+
100
+ /** Compression format id for uncompressed GR2 sections. */
101
+ const GR2_COMPRESSION_NONE = 0;
102
+
103
+ /** Compression format id for legacy Granny Oodle0 sections. */
104
+ const GR2_COMPRESSION_OODLE0 = 1;
105
+
106
+ /** Compression format id for legacy Granny Oodle1 sections. */
107
+ const GR2_COMPRESSION_OODLE1 = 2;
108
+
109
+ /** Compression format id for Granny BitKnit2 sections. */
110
+ const GR2_COMPRESSION_BITKNIT2 = 4;
111
+
112
+ /**
113
+ * Decompress one GR2 section payload according to its section-directory format.
114
+ *
115
+ * @param {number} format Section compression format id.
116
+ * @param {Uint8Array} bytes Compressed section payload.
117
+ * @param {number} expandedSize Expected decompressed byte length.
118
+ * @param {{first16: number, first8: number}} sec Section metadata used by Oodle1 streams.
119
+ * @returns {Uint8Array} Raw section bytes, decompressed when needed.
120
+ * @throws {Error} If the format is not supported or the codec rejects the stream.
121
+ */
122
+ function decompressGr2Section(format, bytes, expandedSize, sec) {
123
+ if (format === GR2_COMPRESSION_NONE) return bytes;
124
+ if (format === GR2_COMPRESSION_OODLE0 || format === GR2_COMPRESSION_OODLE1) return decompressOodle1(bytes, expandedSize, {
125
+ first16: sec.first16,
126
+ first8: sec.first8
127
+ });
128
+ if (format === GR2_COMPRESSION_BITKNIT2) return decompressBitKnit2(bytes, expandedSize);
129
+ throw new Error(`section needs codec: format ${format} (0=None,2=Oodle1,4=BitKnit2)`);
130
+ }
131
+
132
+ /**
133
+ * Reflected Granny file graph produced by the low-level reader.
134
+ *
135
+ * @typedef {object} RawGr2ReadResult
136
+ * @property {number} version Granny file format revision from the file header.
137
+ * @property {number} secCount Number of sections in the source file.
138
+ * @property {object} fileInfo Reflected `granny_file_info` object graph with
139
+ * references resolved to shared JavaScript objects.
140
+ */
141
+
142
+ /**
143
+ * Parse raw `.gr2` bytes into the reflected Granny object graph.
144
+ *
145
+ * This is the low-level reader used by the package entry point. It decompresses
146
+ * sections, applies pointer fixups, walks the embedded type tree, and preserves
147
+ * pointer identity for repeated references in the resulting object graph.
148
+ *
149
+ * @param {Uint8Array|Buffer} buf Raw `.gr2` file bytes.
150
+ * @returns {RawGr2ReadResult} Parsed Granny file metadata and object graph.
151
+ * @throws {Error} If the file magic, compression format, or reflected graph is
152
+ * unsupported or malformed.
153
+ */
154
+ function readGr2Raw(buf) {
155
+ const dv0 = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
156
+ /**
157
+ * Read a little-endian uint32 from the original file bytes.
158
+ *
159
+ * @param {number} o Byte offset in the file buffer.
160
+ * @returns {number} Unsigned 32-bit integer.
161
+ */
162
+ const fu32 = o => dv0.getUint32(o, true),
163
+ magic = bytesToHex(buf.subarray(0, 16)),
164
+ P = MAGICS[magic];
165
+ if (!P) throw new Error("unknown gr2 magic " + magic);
166
+ const MEMBER_DEF_SIZE = 20 + 3 * P,
167
+ H = 32,
168
+ version = fu32(H),
169
+ secBase = H + fu32(H + 12),
170
+ secCount = fu32(H + 16),
171
+ rootTypeSec = fu32(H + 20),
172
+ rootTypeOff = fu32(H + 24),
173
+ rootObjSec = fu32(H + 28),
174
+ rootObjOff = fu32(H + 32);
175
+ const secs = [];
176
+ for (let i = 0; i < secCount; i++) {
177
+ const b = secBase + i * 44;
178
+ secs.push({
179
+ format: fu32(b),
180
+ dataOffset: fu32(b + 4),
181
+ dataSize: fu32(b + 8),
182
+ expandedSize: fu32(b + 12),
183
+ first16: fu32(b + 20),
184
+ first8: fu32(b + 24),
185
+ pFixOff: fu32(b + 28),
186
+ pFixCnt: fu32(b + 32)
187
+ });
188
+ }
189
+ const sectionBase = new Array(secCount);
190
+ let total = 0;
191
+ const datas = secs.map(s => {
192
+ const raw = buf.subarray(s.dataOffset, s.dataOffset + s.dataSize);
193
+ return decompressGr2Section(s.format, raw, s.expandedSize, s);
194
+ });
195
+ for (let i = 0; i < secCount; i++) {
196
+ sectionBase[i] = total;
197
+ total += secs[i].expandedSize;
198
+ }
199
+ const mem = new Uint8Array(total);
200
+ for (let i = 0; i < secCount; i++) {
201
+ mem.set(datas[i], sectionBase[i]);
202
+ }
203
+ const dv = new DataView(mem.buffer);
204
+
205
+ /**
206
+ * Read a little-endian uint32 from the relocated section memory.
207
+ *
208
+ * @param {number} o Byte offset in relocated section memory.
209
+ * @returns {number} Unsigned 32-bit integer.
210
+ */
211
+ const u32 = o => dv.getUint32(o, true);
212
+
213
+ /**
214
+ * Read a little-endian int32 from the relocated section memory.
215
+ *
216
+ * @param {number} o Byte offset in relocated section memory.
217
+ * @returns {number} Signed 32-bit integer.
218
+ */
219
+ const i32 = o => dv.getInt32(o, true);
220
+
221
+ /**
222
+ * Read a little-endian float32 from the relocated section memory.
223
+ *
224
+ * @param {number} o Byte offset in relocated section memory.
225
+ * @returns {number} Float32 value widened to a JavaScript number.
226
+ */
227
+ const f32 = o => dv.getFloat32(o, true);
228
+ const reloc = new Map();
229
+ for (let i = 0; i < secCount; i++) {
230
+ const s = secs[i];
231
+ if (s.pFixCnt === 0) continue;
232
+ let rel;
233
+ if (s.format === 4) {
234
+ const csz = fu32(s.pFixOff);
235
+ rel = decompressBitKnit2(buf.subarray(s.pFixOff + 4, s.pFixOff + 4 + csz), s.pFixCnt * 12);
236
+ } else {
237
+ rel = buf.subarray(s.pFixOff, s.pFixOff + s.pFixCnt * 12);
238
+ }
239
+ const rdv = new DataView(rel.buffer, rel.byteOffset, rel.byteLength);
240
+ for (let k = 0; k < s.pFixCnt; k++) {
241
+ const b = k * 12,
242
+ from = rdv.getUint32(b, true),
243
+ toSec = rdv.getUint32(b + 4, true),
244
+ toOff = rdv.getUint32(b + 8, true);
245
+ reloc.set(sectionBase[i] + from, sectionBase[toSec] + toOff);
246
+ }
247
+ }
248
+ const NULL = -1;
249
+
250
+ /**
251
+ * Resolve a relocated pointer field from a global section-memory offset.
252
+ *
253
+ * @param {number} g Global offset of the pointer field.
254
+ * @returns {number} Global target offset, or the null sentinel when absent.
255
+ */
256
+ const ptr = g => reloc.has(g) ? reloc.get(g) : NULL;
257
+
258
+ /**
259
+ * Read a null-terminated UTF-8 string from relocated section memory.
260
+ *
261
+ * @param {number} g Global string offset, or the null sentinel.
262
+ * @returns {string|null} Decoded string, or null for absent references.
263
+ */
264
+ function readString(g) {
265
+ if (g < 0) return null;
266
+ let end = g;
267
+ while (end < mem.length && mem[end] !== 0) end++;
268
+ return decodeUtf8(mem.subarray(g, end));
269
+ }
270
+ const typeCache = new Map();
271
+
272
+ /**
273
+ * Read and cache a reflected Granny type definition.
274
+ *
275
+ * @param {number} g Global offset of the first member definition.
276
+ * @returns {{type: number, name: string|null, refType: number, arrayWidth: number}[]} Member descriptors.
277
+ */
278
+ function readType(g) {
279
+ if (typeCache.has(g)) return typeCache.get(g);
280
+ const members = [];
281
+ let t = g;
282
+ while (true) {
283
+ const type = u32(t);
284
+ if (type === M.End) break;
285
+ members.push({
286
+ type,
287
+ name: readString(ptr(t + 4)),
288
+ refType: ptr(t + 4 + P),
289
+ arrayWidth: i32(t + 4 + 2 * P)
290
+ });
291
+ t += MEMBER_DEF_SIZE;
292
+ if (members.length > 4096) throw new Error("type member overflow");
293
+ }
294
+ typeCache.set(g, members);
295
+ return members;
296
+ }
297
+ const sizeCache = new Map();
298
+
299
+ /**
300
+ * Compute the byte size of an object for a reflected Granny type.
301
+ *
302
+ * @param {number} typeOff Global type-definition offset.
303
+ * @returns {number} Object size in bytes.
304
+ */
305
+ function objectSize(typeOff) {
306
+ if (sizeCache.has(typeOff)) return sizeCache.get(typeOff);
307
+ sizeCache.set(typeOff, 0);
308
+ let sz = 0;
309
+ for (const m of readType(typeOff)) {
310
+ sz += memberSize(m);
311
+ }
312
+ sizeCache.set(typeOff, sz);
313
+ return sz;
314
+ }
315
+
316
+ /**
317
+ * Compute the byte width of one reflected member.
318
+ *
319
+ * @param {{type: number, refType: number, arrayWidth: number}} m Member descriptor.
320
+ * @returns {number} Member size in bytes.
321
+ */
322
+ function memberSize(m) {
323
+ const w = m.arrayWidth > 0 ? m.arrayWidth : 1;
324
+ switch (m.type) {
325
+ case M.Inline:
326
+ return objectSize(m.refType) * w;
327
+ case M.Reference:
328
+ case M.String:
329
+ return P;
330
+ case M.EmptyReference:
331
+ return 4;
332
+ case M.ReferenceToArray:
333
+ case M.ArrayOfReferences:
334
+ return 4 + P;
335
+ case M.VariantReference:
336
+ return 2 * P;
337
+ case M.ReferenceToVariantArray:
338
+ return 2 * P + 4;
339
+ case M.Transform:
340
+ return TRANSFORM_SIZE;
341
+ case M.Real32:
342
+ case M.Int32:
343
+ case M.UInt32:
344
+ return 4 * w;
345
+ case M.Int16:
346
+ case M.UInt16:
347
+ case M.BinormalInt16:
348
+ case M.NormalUInt16:
349
+ case M.Real16:
350
+ return 2 * w;
351
+ case M.Int8:
352
+ case M.UInt8:
353
+ case M.BinormalInt8:
354
+ case M.NormalUInt8:
355
+ return 1 * w;
356
+ default:
357
+ return 0;
358
+ }
359
+ }
360
+ const layoutCache = new Map();
361
+
362
+ /**
363
+ * Compute and cache the fixed per-member byte layout of a reflected type.
364
+ *
365
+ * Granny types are static structural definitions, so every instance of a
366
+ * given type has an identical member layout; this only needs to run once
367
+ * per type instead of once per object instance (as recomputing offsets
368
+ * with {@link memberSize} inside {@link readObject} would).
369
+ *
370
+ * @param {number} typeOff Global type-definition offset.
371
+ * @returns {{members: object[], offsets: number[]}} Cached member list and
372
+ * each member's byte offset relative to the start of the object.
373
+ */
374
+ function typeLayout(typeOff) {
375
+ if (layoutCache.has(typeOff)) return layoutCache.get(typeOff);
376
+ const members = readType(typeOff),
377
+ offsets = new Array(members.length);
378
+ let size = 0;
379
+ for (let i = 0; i < members.length; i++) {
380
+ offsets[i] = size;
381
+ size += memberSize(members[i]);
382
+ }
383
+ const layout = {
384
+ members,
385
+ offsets
386
+ };
387
+ layoutCache.set(typeOff, layout);
388
+ return layout;
389
+ }
390
+
391
+ /**
392
+ * Byte stride between consecutive elements of a fixed-width numeric member type.
393
+ *
394
+ * @param {number} type Granny member type id.
395
+ * @returns {number} Element stride in bytes.
396
+ */
397
+ function numericStride(type) {
398
+ switch (type) {
399
+ case M.Real32:
400
+ case M.Int32:
401
+ case M.UInt32:
402
+ return 4;
403
+ case M.Int16:
404
+ case M.UInt16:
405
+ case M.BinormalInt16:
406
+ case M.NormalUInt16:
407
+ case M.Real16:
408
+ return 2;
409
+ default:
410
+ // Int8, UInt8, BinormalInt8, NormalUInt8
411
+ return 1;
412
+ }
413
+ }
414
+
415
+ /**
416
+ * Read one fixed-width numeric value from relocated memory.
417
+ *
418
+ * @param {number} type Granny member type id.
419
+ * @param {number} o Global byte offset of the value.
420
+ * @returns {number} Decoded value.
421
+ */
422
+ function numericAt(type, o) {
423
+ switch (type) {
424
+ case M.Real32:
425
+ return f32(o);
426
+ case M.Int32:
427
+ return i32(o);
428
+ case M.UInt32:
429
+ return u32(o);
430
+ case M.Int16:
431
+ case M.BinormalInt16:
432
+ return dv.getInt16(o, true);
433
+ case M.UInt16:
434
+ case M.NormalUInt16:
435
+ return dv.getUint16(o, true);
436
+ case M.Real16:
437
+ return half2float(dv.getUint16(o, true));
438
+ case M.Int8:
439
+ case M.BinormalInt8:
440
+ return dv.getInt8(o);
441
+ case M.UInt8:
442
+ case M.NormalUInt8:
443
+ return mem[o];
444
+ }
445
+ }
446
+
447
+ /**
448
+ * Read a scalar or fixed-width numeric member from relocated memory.
449
+ *
450
+ * @param {{type: number, arrayWidth: number}} m Numeric member descriptor.
451
+ * @param {number} off Global offset of the numeric field.
452
+ * @returns {number|number[]} Decoded scalar or array.
453
+ */
454
+ function numeric(m, off) {
455
+ const w = m.arrayWidth > 0 ? m.arrayWidth : 1;
456
+ if (w === 1) return numericAt(m.type, off);
457
+ const stride = numericStride(m.type),
458
+ out = new Array(w);
459
+ for (let i = 0; i < w; i++) {
460
+ out[i] = numericAt(m.type, off + i * stride);
461
+ }
462
+ return out;
463
+ }
464
+
465
+ /**
466
+ * Read a Granny transform struct from relocated memory.
467
+ *
468
+ * @param {number} o Global offset of the transform bytes.
469
+ * @returns {{flags: number, position: number[], orientation: number[], scaleShear: number[]}} Decoded transform.
470
+ */
471
+ function readTransform(o) {
472
+ return {
473
+ flags: u32(o),
474
+ position: [f32(o + 4), f32(o + 8), f32(o + 12)],
475
+ orientation: [f32(o + 16), f32(o + 20), f32(o + 24), f32(o + 28)],
476
+ scaleShear: [f32(o + 32), f32(o + 36), f32(o + 40), f32(o + 44), f32(o + 48), f32(o + 52), f32(o + 56), f32(o + 60), f32(o + 64)]
477
+ };
478
+ }
479
+ let depth = 0;
480
+ const objCache = new Map(); // typeOff -> Map<objOff, object>
481
+
482
+ /**
483
+ * Read a reflected object graph node, preserving pointer identity and cycles.
484
+ *
485
+ * @param {number} typeOff Global type-definition offset.
486
+ * @param {number} objOff Global object-data offset.
487
+ * @returns {object|null} Decoded object, or null for absent references.
488
+ */
489
+ function readObject(typeOff, objOff) {
490
+ if (typeOff < 0 || objOff < 0) return null;
491
+ let byOff = objCache.get(typeOff);
492
+ if (byOff === undefined) {
493
+ byOff = new Map();
494
+ objCache.set(typeOff, byOff);
495
+ } else {
496
+ const cached = byOff.get(objOff);
497
+ if (cached !== undefined) return cached;
498
+ }
499
+ if (++depth > 200) {
500
+ depth--;
501
+ throw new Error("recursion too deep");
502
+ }
503
+ const obj = {};
504
+ byOff.set(objOff, obj);
505
+ const {
506
+ members,
507
+ offsets
508
+ } = typeLayout(typeOff);
509
+ for (let idx = 0; idx < members.length; idx++) {
510
+ const m = members[idx],
511
+ field = objOff + offsets[idx];
512
+ let name = m.name || "_";
513
+ if (Object.prototype.hasOwnProperty.call(obj, name)) {
514
+ let n = 2;
515
+ while (Object.prototype.hasOwnProperty.call(obj, name + "" + n)) n++;
516
+ name = name + "" + n;
517
+ }
518
+ switch (m.type) {
519
+ case M.String:
520
+ obj[name] = readString(ptr(field));
521
+ break;
522
+ case M.Reference:
523
+ obj[name] = readObject(m.refType, ptr(field));
524
+ break;
525
+ case M.Transform:
526
+ obj[name] = readTransform(field);
527
+ break;
528
+ case M.Inline:
529
+ {
530
+ const w = m.arrayWidth > 0 ? m.arrayWidth : 1,
531
+ os = objectSize(m.refType);
532
+ if (m.arrayWidth > 1) {
533
+ obj[name] = [];
534
+ for (let i = 0; i < w; i++) {
535
+ obj[name].push(readObject(m.refType, field + i * os));
536
+ }
537
+ } else {
538
+ obj[name] = readObject(m.refType, field);
539
+ }
540
+ break;
541
+ }
542
+ case M.ReferenceToArray:
543
+ {
544
+ const count = i32(field),
545
+ p = ptr(field + 4),
546
+ os = objectSize(m.refType),
547
+ a = [];
548
+ for (let i = 0; i < count; i++) {
549
+ a.push(readObject(m.refType, p + i * os));
550
+ }
551
+ obj[name] = a;
552
+ break;
553
+ }
554
+ case M.ArrayOfReferences:
555
+ {
556
+ const count = i32(field),
557
+ p = ptr(field + 4),
558
+ a = [];
559
+ for (let i = 0; i < count; i++) {
560
+ a.push(readObject(m.refType, ptr(p + i * P)));
561
+ }
562
+ obj[name] = a;
563
+ break;
564
+ }
565
+ case M.VariantReference:
566
+ obj[name] = readObject(ptr(field), ptr(field + P));
567
+ break;
568
+ case M.ReferenceToVariantArray:
569
+ {
570
+ const vt = ptr(field),
571
+ count = i32(field + P),
572
+ p = ptr(field + P + 4),
573
+ a = [],
574
+ os = vt >= 0 ? objectSize(vt) : 0;
575
+ for (let i = 0; i < count; i++) {
576
+ a.push(readObject(vt, p + i * os));
577
+ }
578
+ if (vt >= 0) Object.defineProperty(a, "__type", {
579
+ value: readType(vt),
580
+ enumerable: false
581
+ });
582
+ obj[name] = a;
583
+ break;
584
+ }
585
+ case M.Real32:
586
+ case M.Int8:
587
+ case M.UInt8:
588
+ case M.BinormalInt8:
589
+ case M.NormalUInt8:
590
+ case M.Int16:
591
+ case M.UInt16:
592
+ case M.BinormalInt16:
593
+ case M.NormalUInt16:
594
+ case M.Int32:
595
+ case M.UInt32:
596
+ case M.Real16:
597
+ obj[name] = numeric(m, field);
598
+ break;
599
+ default:
600
+ obj[name] = null;
601
+ }
602
+ }
603
+ depth--;
604
+ return obj;
605
+ }
606
+ const rootTypeG = sectionBase[rootTypeSec] + rootTypeOff,
607
+ rootObjG = sectionBase[rootObjSec] + rootObjOff,
608
+ fileInfo = readObject(rootTypeG, rootObjG);
609
+ return {
610
+ version,
611
+ secCount,
612
+ fileInfo
613
+ };
614
+ }
615
+
616
+ export { GR2_COMPRESSION_BITKNIT2, GR2_COMPRESSION_NONE, GR2_COMPRESSION_OODLE0, GR2_COMPRESSION_OODLE1, GR2_MAGICS, GRANNY_MEMBER_TYPES, GRANNY_TRANSFORM_SIZE, bytesToHex, decodeUtf8, decompressGr2Section, readGr2Raw };
617
+ //# sourceMappingURL=reader.js.map