rw-parser-ng 2.1.0 → 2.3.0

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 (142) hide show
  1. package/README.md +252 -52
  2. package/lib/codecs/bitmap.d.ts +6 -0
  3. package/lib/codecs/bitmap.js +77 -0
  4. package/lib/codecs/color.d.ts +6 -0
  5. package/lib/codecs/color.js +43 -0
  6. package/lib/codecs/dxt.d.ts +3 -0
  7. package/lib/codecs/dxt.js +212 -0
  8. package/lib/codecs/formats.d.ts +25 -0
  9. package/lib/codecs/formats.js +32 -0
  10. package/lib/codecs/index.d.ts +5 -0
  11. package/lib/codecs/index.js +21 -0
  12. package/lib/codecs/palette.d.ts +6 -0
  13. package/lib/codecs/palette.js +86 -0
  14. package/lib/common/types.d.ts +50 -0
  15. package/lib/common/types.js +2 -0
  16. package/lib/core/byte-stream.d.ts +20 -0
  17. package/lib/core/byte-stream.js +71 -0
  18. package/lib/core/errors.d.ts +5 -0
  19. package/lib/core/errors.js +34 -0
  20. package/lib/core/index.d.ts +5 -0
  21. package/lib/core/index.js +21 -0
  22. package/lib/core/rw-file.d.ts +9 -0
  23. package/lib/core/rw-file.js +36 -0
  24. package/lib/core/rw-sections.d.ts +24 -0
  25. package/lib/core/rw-sections.js +28 -0
  26. package/lib/core/rw-version.d.ts +3 -0
  27. package/lib/core/rw-version.js +27 -0
  28. package/lib/dff/dff-model-type.d.ts +5 -0
  29. package/lib/dff/dff-model-type.js +9 -0
  30. package/lib/dff/index.d.ts +4 -0
  31. package/lib/dff/index.js +20 -0
  32. package/lib/dff/parser.d.ts +5 -0
  33. package/lib/dff/parser.js +154 -0
  34. package/lib/dff/readers/2d-effect.d.ts +3 -0
  35. package/lib/dff/readers/2d-effect.js +211 -0
  36. package/lib/dff/readers/geometry.d.ts +4 -0
  37. package/lib/dff/readers/geometry.js +229 -0
  38. package/lib/dff/readers/index.d.ts +5 -0
  39. package/lib/dff/readers/index.js +21 -0
  40. package/lib/dff/readers/material.d.ts +5 -0
  41. package/lib/dff/readers/material.js +72 -0
  42. package/lib/dff/readers/mesh.d.ts +5 -0
  43. package/lib/dff/readers/mesh.js +84 -0
  44. package/lib/dff/readers/structure.d.ts +6 -0
  45. package/lib/dff/readers/structure.js +67 -0
  46. package/lib/dff/types.d.ts +237 -0
  47. package/lib/dff/types.js +44 -0
  48. package/lib/fxp/index.d.ts +2 -0
  49. package/lib/fxp/index.js +18 -0
  50. package/lib/fxp/info-parser.d.ts +2 -0
  51. package/lib/fxp/info-parser.js +169 -0
  52. package/lib/fxp/parser.d.ts +3 -0
  53. package/lib/fxp/parser.js +285 -0
  54. package/lib/fxp/types/common.d.ts +36 -0
  55. package/lib/fxp/types/common.js +2 -0
  56. package/lib/fxp/types/index.d.ts +3 -0
  57. package/lib/fxp/types/index.js +19 -0
  58. package/lib/fxp/types/info.d.ts +186 -0
  59. package/lib/fxp/types/info.js +2 -0
  60. package/lib/fxp/types/system.d.ts +27 -0
  61. package/lib/fxp/types/system.js +2 -0
  62. package/lib/ifp/index.d.ts +4 -0
  63. package/lib/ifp/index.js +20 -0
  64. package/lib/ifp/parser.d.ts +5 -0
  65. package/lib/ifp/parser.js +38 -0
  66. package/lib/ifp/readers/anp3.d.ts +3 -0
  67. package/lib/ifp/readers/anp3.js +56 -0
  68. package/lib/ifp/readers/anpk.d.ts +3 -0
  69. package/lib/ifp/readers/anpk.js +86 -0
  70. package/lib/ifp/types.d.ts +28 -0
  71. package/lib/ifp/types.js +9 -0
  72. package/lib/index.d.ts +6 -9
  73. package/lib/index.js +6 -13
  74. package/lib/renderware/fxp/FxpData.d.ts +246 -0
  75. package/lib/renderware/fxp/FxpData.js +2 -0
  76. package/lib/renderware/fxp/FxpParser.d.ts +3 -0
  77. package/lib/renderware/fxp/FxpParser.js +512 -0
  78. package/lib/renderware/fxp/index.d.ts +2 -0
  79. package/lib/renderware/fxp/index.js +18 -0
  80. package/lib/renderware/txd/TxdParser.js +3 -2
  81. package/lib/txd/bitmap-decoder.d.ts +3 -0
  82. package/lib/txd/bitmap-decoder.js +57 -0
  83. package/lib/txd/index.d.ts +3 -0
  84. package/lib/txd/index.js +19 -0
  85. package/lib/txd/parser.d.ts +8 -0
  86. package/lib/txd/parser.js +122 -0
  87. package/lib/txd/types.d.ts +27 -0
  88. package/lib/txd/types.js +2 -0
  89. package/package.json +16 -9
  90. package/src/codecs/bitmap.ts +75 -0
  91. package/src/codecs/color.ts +40 -0
  92. package/src/codecs/dxt.ts +243 -0
  93. package/src/codecs/formats.ts +28 -0
  94. package/src/codecs/index.ts +5 -0
  95. package/src/codecs/palette.ts +106 -0
  96. package/src/common/types.ts +59 -0
  97. package/src/core/byte-stream.ts +87 -0
  98. package/src/core/errors.ts +7 -0
  99. package/src/core/index.ts +5 -0
  100. package/src/core/rw-file.ts +23 -0
  101. package/src/core/rw-sections.ts +24 -0
  102. package/src/core/rw-version.ts +24 -0
  103. package/src/dff/dff-model-type.ts +5 -0
  104. package/src/dff/index.ts +4 -0
  105. package/src/dff/parser.ts +142 -0
  106. package/src/dff/readers/2d-effect.ts +265 -0
  107. package/src/dff/readers/geometry.ts +264 -0
  108. package/src/dff/readers/index.ts +5 -0
  109. package/src/dff/readers/material.ts +89 -0
  110. package/src/dff/readers/mesh.ts +93 -0
  111. package/src/dff/readers/structure.ts +79 -0
  112. package/src/dff/types.ts +278 -0
  113. package/src/fxp/index.ts +2 -0
  114. package/src/fxp/info-parser.ts +166 -0
  115. package/src/fxp/parser.ts +323 -0
  116. package/src/fxp/types/common.ts +23 -0
  117. package/src/fxp/types/index.ts +3 -0
  118. package/src/fxp/types/info.ts +251 -0
  119. package/src/fxp/types/system.ts +30 -0
  120. package/src/ifp/index.ts +4 -0
  121. package/src/ifp/parser.ts +16 -0
  122. package/src/ifp/readers/anp3.ts +75 -0
  123. package/src/ifp/readers/anpk.ts +110 -0
  124. package/src/ifp/types.ts +33 -0
  125. package/src/index.ts +6 -12
  126. package/src/txd/bitmap-decoder.ts +73 -0
  127. package/src/txd/index.ts +3 -0
  128. package/src/txd/parser.ts +148 -0
  129. package/src/txd/types.ts +29 -0
  130. package/src/renderware/RwFile.ts +0 -26
  131. package/src/renderware/RwSections.ts +0 -27
  132. package/src/renderware/common/types.ts +0 -59
  133. package/src/renderware/dff/DffModelType.ts +0 -5
  134. package/src/renderware/dff/DffParser.ts +0 -611
  135. package/src/renderware/errors/RwParseError.ts +0 -11
  136. package/src/renderware/ifp/IfpData.ts +0 -33
  137. package/src/renderware/ifp/IfpParser.ts +0 -203
  138. package/src/renderware/txd/TxdParser.ts +0 -234
  139. package/src/renderware/utils/ImageDecoder.ts +0 -571
  140. package/src/renderware/utils/ImageFormatEnums.ts +0 -28
  141. package/src/renderware/utils/RwVersion.ts +0 -28
  142. package/src/utils/ByteStream.ts +0 -80
@@ -1,203 +0,0 @@
1
- import { RwFile } from '../RwFile';
2
- import { IfpVersion, RwIfp, RwIfpAnimation, RwIfpBone, RwIfpKeyframe } from './IfpData';
3
-
4
- export class IfpParser extends RwFile
5
- {
6
- constructor(buffer: Buffer) {
7
- super(buffer);
8
- }
9
-
10
- public parse(): RwIfp {
11
- const fileSignature = this.readString(4);
12
- this.setPosition(0);
13
-
14
- let version: IfpVersion;
15
- if (fileSignature === 'ANP3') {
16
- version = IfpVersion.ANP3;
17
- return this.readAnp3();
18
- } else if (fileSignature === 'ANPK') {
19
- version = IfpVersion.ANPK;
20
- return this.readAnpk();
21
- } else {
22
- version = IfpVersion.UNSUPPORTED;
23
- throw new Error('Unsupported IFP version');
24
- }
25
- }
26
-
27
- private readAnp3(): RwIfp {
28
- this.skip(4); // ANP3
29
- const size = this.readUint32();
30
- const name = this.readString(24);
31
- const animationsCount = this.readUint32();
32
- const animations: RwIfpAnimation[] = [];
33
-
34
- for (let i = 0; i < animationsCount; i++) {
35
- animations.push(this.readAnp3Animation());
36
- }
37
-
38
- return {
39
- version: IfpVersion.ANP3,
40
- name,
41
- animations
42
- };
43
- }
44
-
45
- private readAnp3Animation(): RwIfpAnimation {
46
- const name = this.readString(24);
47
- const bonesCount = this.readUint32();
48
- this.skip(4); // keyframes_size
49
- this.skip(4); // unk
50
- const bones: RwIfpBone[] = [];
51
-
52
- for (let i = 0; i < bonesCount; i++) {
53
- bones.push(this.readAnp3Bone());
54
- }
55
-
56
- return { name, bones };
57
- }
58
-
59
- private readAnp3Bone(): RwIfpBone {
60
- const name = this.readString(24);
61
- const keyframeTypeNum = this.readUint32();
62
- const keyframesCount = this.readUint32();
63
- const keyframeType = keyframeTypeNum === 4 ? 'KRT0' : 'KR00';
64
- const boneId = this.readInt32();
65
- const keyframes: RwIfpKeyframe[] = [];
66
-
67
- for (let i = 0; i < keyframesCount; i++) {
68
- const qx = this.readInt16() / 4096.0;
69
- const qy = this.readInt16() / 4096.0;
70
- const qz = this.readInt16() / 4096.0;
71
- const qw = this.readInt16() / 4096.0;
72
- const time = this.readInt16();
73
-
74
- let px = 0, py = 0, pz = 0;
75
- if (keyframeType[2] === 'T') {
76
- px = this.readInt16() / 1024.0;
77
- py = this.readInt16() / 1024.0;
78
- pz = this.readInt16() / 1024.0;
79
- }
80
-
81
- keyframes.push({
82
- time,
83
- position: { x: px, y: py, z: pz },
84
- rotation: { w: qw, x: qx, y: qy, z: qz },
85
- scale: { x: 1, y: 1, z: 1 }
86
- });
87
- }
88
-
89
- return {
90
- name,
91
- keyframeType,
92
- useBoneId: true,
93
- boneId,
94
- keyframes
95
- };
96
- }
97
-
98
- private readAnpk(): RwIfp {
99
- this.skip(4); // ANPK
100
- const size = this.readUint32();
101
- this.skip(4); // INFO
102
- const infoLen = this.readUint32();
103
- const animationsCount = this.readUint32();
104
- const name = this.readString(infoLen - 4);
105
- const nameAlignLen = (4 - infoLen % 4) % 4;
106
- this.skip(nameAlignLen);
107
-
108
- const animations: RwIfpAnimation[] = [];
109
- for (let i = 0; i < animationsCount; i++) {
110
- animations.push(this.readAnpkAnimation());
111
- }
112
-
113
- return {
114
- version: IfpVersion.ANPK,
115
- name,
116
- animations
117
- };
118
- }
119
-
120
- private readAnpkAnimation(): RwIfpAnimation {
121
- this.skip(4); // NAME
122
- const nameLen = this.readUint32();
123
- const name = this.readString(nameLen);
124
- this.skip((4 - nameLen % 4) % 4);
125
- this.skip(4); // DGAN
126
- this.skip(4); // animation_size
127
- this.skip(4); // INFO
128
- this.skip(4); // unk_size
129
- const bonesCount = this.readUint32();
130
- this.skip(4); // unk
131
-
132
- const bones: RwIfpBone[] = [];
133
- for (let i = 0; i < bonesCount; i++) {
134
- bones.push(this.readAnpkBone());
135
- }
136
-
137
- return { name, bones };
138
- }
139
-
140
- private readAnpkBone(): RwIfpBone {
141
- this.skip(4); // CPAN
142
- this.skip(4); // bone_len
143
- this.skip(4); // ANIM
144
- const animLen = this.readUint32();
145
- const name = this.readString(28);
146
- const keyframesCount = this.readUint32();
147
- this.skip(8); // unk
148
-
149
- let boneId = 0;
150
- const useBoneId = animLen === 44;
151
- if (useBoneId) {
152
- boneId = this.readInt32();
153
- } else {
154
- this.skip(8); // sibling_x, sibling_y
155
- }
156
-
157
- let keyframeType = 'K000';
158
- const keyframes: RwIfpKeyframe[] = [];
159
-
160
- if (keyframesCount > 0) {
161
- keyframeType = this.readString(4);
162
- this.skip(4); // keyframes_len
163
-
164
- for (let i = 0; i < keyframesCount; i++) {
165
- const qx = this.readFloat();
166
- const qy = this.readFloat();
167
- const qz = this.readFloat();
168
- const qw = this.readFloat();
169
-
170
- let px = 0, py = 0, pz = 0;
171
- if (keyframeType[2] === 'T') {
172
- px = this.readFloat();
173
- py = this.readFloat();
174
- pz = this.readFloat();
175
- }
176
-
177
- let sx = 1, sy = 1, sz = 1;
178
- if (keyframeType[3] === 'S') {
179
- sx = this.readFloat();
180
- sy = this.readFloat();
181
- sz = this.readFloat();
182
- }
183
-
184
- const time = this.readFloat();
185
-
186
- keyframes.push({
187
- time,
188
- position: { x: px, y: py, z: pz },
189
- rotation: { w: qw, x: qx, y: qy, z: qz },
190
- scale: { x: sx, y: sy, z: sz }
191
- });
192
- }
193
- }
194
-
195
- return {
196
- name,
197
- keyframeType,
198
- useBoneId,
199
- boneId,
200
- keyframes
201
- };
202
- }
203
- }
@@ -1,234 +0,0 @@
1
- import { RwFile } from '../RwFile';
2
- import { ImageDecoder } from '../utils/ImageDecoder';
3
- import {
4
- D3DFormat,
5
- PaletteType,
6
- PlatformType,
7
- RasterFormat
8
- } from '../utils/ImageFormatEnums';
9
-
10
- export interface RwTxd {
11
- textureDictionary: RwTextureDictionary,
12
- }
13
-
14
- export interface RwTextureDictionary {
15
- textureCount: number,
16
- textureNatives: RwTextureNative[],
17
- }
18
-
19
- export interface RwTextureNative {
20
- platformId: number,
21
- filterMode: number,
22
- uAddressing: number,
23
- vAddressing: number,
24
- textureName: string,
25
- maskName: string,
26
- rasterFormat: number,
27
- d3dFormat: string,
28
- width: number,
29
- height: number,
30
- depth: number,
31
- mipmapCount: number,
32
- rasterType: number,
33
- alpha: boolean,
34
- cubeTexture: boolean,
35
- autoMipMaps: boolean,
36
- compressed: boolean,
37
- mipmaps: number[][],
38
- }
39
-
40
- export class TxdParser extends RwFile {
41
-
42
- constructor(stream: Buffer) {
43
- super(stream);
44
- }
45
-
46
- parse(): RwTxd {
47
- return {
48
- textureDictionary: this.readTextureDictionary(),
49
- };
50
- }
51
-
52
- public readTextureDictionary(): RwTextureDictionary {
53
- this.readSectionHeader();
54
- this.readSectionHeader();
55
-
56
- const textureCount = this.readUint16();
57
- this.skip(2);
58
-
59
- let textureNatives: RwTextureNative[] = [];
60
-
61
- for (let i = 0; i < textureCount; i++) {
62
- let textureNative = this.readTextureNative();
63
- textureNatives.push(textureNative);
64
- }
65
-
66
- // Skip unused extension
67
- this.skip(this.readSectionHeader().sectionSize);
68
-
69
- return { textureCount, textureNatives };
70
- }
71
-
72
- public readTextureNative() : RwTextureNative {
73
- this.readSectionHeader();
74
- this.readSectionHeader();
75
-
76
- // TODO: Structure this part better
77
- // Texture format
78
- const platformId = this.readUint32();
79
- const flags = this.readUint32();
80
-
81
- const filterMode = (flags & 0xFF);
82
- const uAddressing = (flags & 0xF00) >> 8;
83
- const vAddressing = (flags & 0xF000) >> 12;
84
-
85
- const textureName = this.readString(32);
86
- const maskName = this.readString(32);
87
-
88
- // Raster format
89
- const rasterFormat = this.readUint32();
90
-
91
- const d3dFormat = this.readString(4);
92
- const width = this.readUint16();
93
- const height = this.readUint16();
94
- const depth = this.readUint8();
95
- const mipmapCount = this.readUint8();
96
- const rasterType = this.readUint8();
97
-
98
- const compressionFlags = this.readUint8(); // Is "dxtType" for III/VC
99
-
100
- // SA
101
- const alpha = (compressionFlags & (1 << 0)) !== 0;
102
- const cubeTexture = (compressionFlags & (1 << 1)) !== 0;
103
- const autoMipMaps = (compressionFlags & (1 << 2)) !== 0;
104
- const compressed = (compressionFlags & (1 << 3)) !== 0;
105
-
106
- const paletteType = (rasterFormat >> 13) & 0b11;
107
-
108
- let mipWidth = width;
109
- let mipHeight = height;
110
-
111
- let mipmaps: number[][] = [];
112
-
113
- const palette = (paletteType !== PaletteType.PALETTE_NONE ? this.readPalette(paletteType, depth) : new Uint8Array(0));
114
-
115
- for (let i = 0; i < mipmapCount; i++) {
116
-
117
- const rasterSize = this.readUint32();
118
- const raster = this.read(rasterSize);
119
-
120
- if (i == 0) {
121
- // Raw RGBA presentation
122
- let bitmap: number[];
123
-
124
- if (palette.length !== 0) {
125
- const rasterFormatsWithoutAlpha = [
126
- RasterFormat.RASTER_565,
127
- RasterFormat.RASTER_LUM,
128
- RasterFormat.RASTER_888,
129
- RasterFormat.RASTER_555
130
- ];
131
-
132
- const hasAlpha = ((platformId === PlatformType.D3D9 && alpha) || (platformId == PlatformType.D3D8 && !rasterFormatsWithoutAlpha.includes(rasterFormat)));
133
-
134
- bitmap = Array.from(this.getBitmapWithPalette(paletteType, depth, hasAlpha, raster, palette, width, height));
135
- }
136
- else if (platformId === PlatformType.D3D8 && compressionFlags !== 0) {
137
- bitmap = Array.from(this.getBitmapWithDXT('DXT' + compressionFlags, raster, width, height));
138
- }
139
- else if (platformId === PlatformType.D3D9 && compressed) {
140
- bitmap = Array.from(this.getBitmapWithDXT(d3dFormat, raster, width, height));
141
- }
142
- else {
143
- bitmap = Array.from(this.getBitmapWithRasterFormat(rasterFormat, raster, width, height))
144
- }
145
-
146
- mipmaps.push(bitmap);
147
- }
148
-
149
- mipWidth /= 2;
150
- mipHeight /= 2;
151
- }
152
-
153
- // Skip extension
154
- this.skip(this.readSectionHeader().sectionSize);
155
-
156
- return {
157
- platformId,
158
- filterMode,
159
- uAddressing, vAddressing,
160
- textureName, maskName,
161
- rasterFormat,
162
- d3dFormat,
163
- width, height, depth,
164
- mipmapCount,
165
- rasterType,
166
- alpha,
167
- cubeTexture,
168
- autoMipMaps,
169
- compressed,
170
- mipmaps,
171
- };
172
- }
173
-
174
- public readPalette(paletteType: number, depth: number): Uint8Array {
175
- const size = (paletteType === PaletteType.PALETTE_8 ? 1024 : (depth === 4 ? 64 : 128))
176
-
177
- return this.read(size);
178
- }
179
-
180
- public getBitmapWithPalette(paletteType: number, depth: number, hasAlpha: boolean, raster: Uint8Array, palette: Uint8Array, width: number, height: number): Uint8Array {
181
- if (paletteType !== PaletteType.PALETTE_8 && depth == 4) {
182
- return (hasAlpha
183
- ? ImageDecoder.pal4(raster, palette, width, height)
184
- : ImageDecoder.pal4NoAlpha(raster, palette, width, height)
185
- );
186
- }
187
-
188
- return (hasAlpha
189
- ? ImageDecoder.pal8(raster, palette, width, height)
190
- : ImageDecoder.pal8NoAlpha(raster, palette, width, height)
191
- )
192
- }
193
-
194
- public getBitmapWithDXT(dxtType:string, raster: Uint8Array, width: number, height: number): Uint8Array {
195
- switch (dxtType) {
196
- case D3DFormat.D3D_DXT1:
197
- return ImageDecoder.bc1(raster, width, height);
198
- case D3DFormat.D3D_DXT2:
199
- return ImageDecoder.bc2(raster, width, height, true);
200
- case D3DFormat.D3D_DXT3:
201
- return ImageDecoder.bc2(raster, width, height, false);
202
- case D3DFormat.D3D_DXT4:
203
- return ImageDecoder.bc3(raster, width, height, true);
204
- case D3DFormat.D3D_DXT5:
205
- return ImageDecoder.bc3(raster, width, height, false);
206
- // LUM8_A8 has compressed flag
207
- case D3DFormat.D3DFMT_A8L8:
208
- return ImageDecoder.lum8a8(raster, width, height);
209
- default:
210
- return new Uint8Array(0);
211
- }
212
- }
213
-
214
- public getBitmapWithRasterFormat (rasterFormat: number, raster: Uint8Array, width: number, height: number): Uint8Array {
215
- switch (rasterFormat) {
216
- case RasterFormat.RASTER_1555:
217
- return ImageDecoder.bgra1555(raster, width, height);
218
- case RasterFormat.RASTER_565:
219
- return ImageDecoder.bgra565(raster, width, height);
220
- case RasterFormat.RASTER_4444:
221
- return ImageDecoder.bgra4444(raster, width, height);
222
- case RasterFormat.RASTER_LUM:
223
- return ImageDecoder.lum8(raster, width, height);
224
- case RasterFormat.RASTER_8888:
225
- return ImageDecoder.bgra8888(raster, width, height);
226
- case RasterFormat.RASTER_888:
227
- return ImageDecoder.bgra888(raster, width, height);
228
- case RasterFormat.RASTER_555:
229
- return ImageDecoder.bgra555(raster, width, height);
230
- default:
231
- return new Uint8Array(0);
232
- }
233
- }
234
- }