swf2js 0.0.0-stage → 0.1.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.
- package/LICENSE-GPL +680 -0
- package/README.md +208 -2
- package/package.json +24 -4
- package/src/cli.js +272 -0
- package/src/decoder/button-decoder.js +136 -0
- package/src/decoder/shape-decoder.js +786 -0
- package/src/decoder/symbol-table.js +36 -0
- package/src/decoder/tag-decoder.js +1190 -0
- package/src/decoder/tag-types.js +258 -0
- package/src/emitter/asset-encoder.js +708 -0
- package/src/emitter/bundle-emitter.js +596 -0
- package/src/emitter/embedded-swf.js +246 -0
- package/src/emitter/font-encoder.js +365 -0
- package/src/emitter/timeline-data-codec.js +58 -0
- package/src/model/swf-document.js +59 -0
- package/src/model/transpile-result.js +32 -0
- package/src/parser/bit-reader.js +237 -0
- package/src/parser/decompressor.js +91 -0
- package/src/parser/swf-parser.js +164 -0
- package/src/pretty-printer/pretty-printer.js +137 -0
- package/src/runtime/avm1-runtime.js +4907 -0
- package/src/runtime/avm2-runtime.js +1591 -0
- package/src/runtime/embedded-movie-runtime.js +208 -0
- package/src/runtime/index.js +17 -0
- package/src/runtime/movie-runtime.js +306 -0
- package/src/runtime/swflib.js +14050 -0
- package/src/transpiler/actionscript.js +390 -0
- package/src/transpiler/avm1/avm1-ir.js +205 -0
- package/src/transpiler/avm1/avm1-opcodes.js +149 -0
- package/src/transpiler/avm1/avm1-rewrite.js +291 -0
- package/src/transpiler/avm1/avm1-transpiler.js +2082 -0
- package/src/transpiler/avm2/abc-reader.js +416 -0
- package/src/transpiler/avm2/avm2-transpiler.js +2560 -0
- package/src/transpiler/cfg-structurer.js +876 -0
- package/src/transpiler/timeline/display-list.js +643 -0
- package/src/transpiler/timeline/timeline-codegen.js +6354 -0
- package/src/util/diagnostics.js +50 -0
- package/src/util/logger.js +40 -0
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Build timeline frames + character table from decoded SWF tags.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { decodeTag } from '../../decoder/tag-decoder.js';
|
|
6
|
+
import { TAG } from '../../decoder/tag-types.js';
|
|
7
|
+
import { decodeShape, decodeMorphShape } from '../../decoder/shape-decoder.js';
|
|
8
|
+
import { decodeButton } from '../../decoder/button-decoder.js';
|
|
9
|
+
import { BitReader } from '../../parser/bit-reader.js';
|
|
10
|
+
import { transpileAvm1WithMeta } from '../avm1/avm1-transpiler.js';
|
|
11
|
+
import { OP } from '../avm1/avm1-opcodes.js';
|
|
12
|
+
import { DiagnosticsCollector } from '../../util/diagnostics.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {Object} PlaceMutation
|
|
16
|
+
* @property {number} depth
|
|
17
|
+
* @property {number} [characterId]
|
|
18
|
+
* @property {{ a: number, b: number, c: number, d: number, tx: number, ty: number }} [matrix]
|
|
19
|
+
* @property {string} [name]
|
|
20
|
+
* @property {boolean} [move]
|
|
21
|
+
* @property {number} [tabIndex] - Standalone SetTabIndex mutation at this depth
|
|
22
|
+
* @property {boolean} [remove] - Removal in the ordered depth mutations
|
|
23
|
+
* @property {number} [removeCharacterId] - Character match required by legacy RemoveObject
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {Object} TimelineFrame
|
|
28
|
+
* @property {number} frameIndex
|
|
29
|
+
* @property {PlaceMutation[]} places
|
|
30
|
+
* @property {number[]} removes - Unconditional removals before the ordered mutations
|
|
31
|
+
* @property {Array<{soundId?:number,soundClassName?:string,soundInfo:object}>} [sounds] - Event sounds for this frame
|
|
32
|
+
* @property {string} [label]
|
|
33
|
+
* @property {string[]} [labels] - All names when more than one names the same frame
|
|
34
|
+
* @property {string[]} [namedAnchors]
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {Object} CharacterInfo
|
|
39
|
+
* @property {string} type - bitmap | shape | sprite | button | text | other
|
|
40
|
+
* @property {number} [assetId] - bitmap asset id (== characterId)
|
|
41
|
+
* @property {{ x: number, y: number, w: number, h: number }} [bounds] - pixels
|
|
42
|
+
* @property {string} [color] - placeholder fill for buttons/text
|
|
43
|
+
* @property {object[]} [draws] - shape path draw list (from shape-decoder)
|
|
44
|
+
* @property {boolean} [evenOdd]
|
|
45
|
+
* @property {{ up: object[], over: object[], down: object[] }} [buttonStates] - Depth-ordered visual records
|
|
46
|
+
* @property {TimelineFrame[]} [frames] - sprite timeline
|
|
47
|
+
* @property {number} [frameCount]
|
|
48
|
+
* @property {{ x: number, y: number, w: number, h: number }} [scalingGrid] - 9-slice center in pixels
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {object[]} tags - decoded root tags
|
|
53
|
+
* @param {import('../../decoder/symbol-table.js').SymbolTable} symbolTable
|
|
54
|
+
* @param {object} [diagnostics]
|
|
55
|
+
* @param {object} [opts]
|
|
56
|
+
* @param {boolean} [opts.sharedAvm1Runtime=false]
|
|
57
|
+
* @param {number} [opts.swfVersion]
|
|
58
|
+
* @param {Map<number, string[]>} [opts.legacyButtonScripts]
|
|
59
|
+
* @returns {{ frames: TimelineFrame[], characters: Record<string, CharacterInfo> }}
|
|
60
|
+
*/
|
|
61
|
+
export function buildDisplayList(tags, symbolTable, diagnostics = null, opts = {}) {
|
|
62
|
+
const frames = buildFramesFromTags(tags);
|
|
63
|
+
const sceneTag = (tags || []).find((tag) => tag && tag.type === 'SceneAndFrameLabels');
|
|
64
|
+
const rawScenes = sceneTag?.scenes?.length ? sceneTag.scenes : [{ offset: 0, name: 'Scene 1' }];
|
|
65
|
+
const scenes = rawScenes.map((scene, index) => ({
|
|
66
|
+
name: scene.name || `Scene ${index + 1}`,
|
|
67
|
+
offset: scene.offset | 0,
|
|
68
|
+
numFrames: Math.max(0, (rawScenes[index + 1]?.offset ?? frames.length) - (scene.offset | 0)),
|
|
69
|
+
}));
|
|
70
|
+
for (const label of sceneTag?.labels || []) {
|
|
71
|
+
const frame = frames[label.frame | 0];
|
|
72
|
+
if (frame) addFrameLabel(frame, label.name);
|
|
73
|
+
}
|
|
74
|
+
/** @type {Record<string, CharacterInfo>} */
|
|
75
|
+
const characters = {};
|
|
76
|
+
const fontClasses = new Map(buildSymbolClasses(tags)
|
|
77
|
+
.filter(({ id }) => symbolTable.get(id)?.type === 'Font')
|
|
78
|
+
.map(({ id, name }) => [name.replace('::', '.'), id]));
|
|
79
|
+
const buttonSounds = (tags || []).filter(tag => tag?.type === 'ButtonSound');
|
|
80
|
+
const buttonColorTransforms = new Map((tags || [])
|
|
81
|
+
.filter(tag => tag?.type === 'ButtonColorTransforms')
|
|
82
|
+
.map(tag => [tag.buttonId, tag.colorTransforms]));
|
|
83
|
+
|
|
84
|
+
for (const id of symbolTable.ids()) {
|
|
85
|
+
const char = symbolTable.get(id);
|
|
86
|
+
if (!char) continue;
|
|
87
|
+
if (char.type === 'Sprite') {
|
|
88
|
+
for (const raw of char.tags || []) {
|
|
89
|
+
if (raw.code !== TAG.DefineButtonSound && raw.code !== TAG.DefineButtonCxform) continue;
|
|
90
|
+
try {
|
|
91
|
+
const tag = decodeTag(raw);
|
|
92
|
+
if (tag.type === 'ButtonSound') buttonSounds.push(tag);
|
|
93
|
+
else buttonColorTransforms.set(tag.buttonId, tag.colorTransforms);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
diagnostics?.warn?.(`Sprite ${id} button supplementary tag decode failed: ${err.message}`, 'transpile');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
for (const id of symbolTable.ids()) {
|
|
101
|
+
const char = symbolTable.get(id);
|
|
102
|
+
if (!char) continue;
|
|
103
|
+
characters[String(id)] = characterInfoFromDefine(char, diagnostics, symbolTable, opts, fontClasses, buttonColorTransforms.get(id));
|
|
104
|
+
}
|
|
105
|
+
for (const { buttonId, sounds } of buttonSounds) {
|
|
106
|
+
const button = characters[buttonId];
|
|
107
|
+
if (button?.type === 'button') button.buttonSounds = sounds;
|
|
108
|
+
}
|
|
109
|
+
for (const tag of tags || []) {
|
|
110
|
+
if (tag?.type !== 'DefineScalingGrid' || !tag.splitter) continue;
|
|
111
|
+
const character = characters[tag.characterId];
|
|
112
|
+
if (character?.type !== 'sprite' && character?.type !== 'button') continue;
|
|
113
|
+
const { xMin, xMax, yMin, yMax } = tag.splitter;
|
|
114
|
+
if (xMax - xMin < 1 || yMax - yMin < 1) continue;
|
|
115
|
+
character.scalingGrid = { x: xMin / 20, y: yMin / 20, w: (xMax - xMin) / 20, h: (yMax - yMin) / 20 };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Ensure every PlaceObject characterId has an entry (even if define missing)
|
|
119
|
+
for (const frame of frames) {
|
|
120
|
+
for (const p of frame.places) {
|
|
121
|
+
if (p.characterId == null) continue;
|
|
122
|
+
const key = String(p.characterId);
|
|
123
|
+
if (!characters[key]) {
|
|
124
|
+
characters[key] = { type: 'other', color: colorForId(p.characterId) };
|
|
125
|
+
diagnostics?.warn?.(
|
|
126
|
+
`PlaceObject references missing character ${p.characterId}`,
|
|
127
|
+
'transpile',
|
|
128
|
+
{ characterId: p.characterId }
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Nested sprite places may reference characters — already in table from defines
|
|
135
|
+
for (const info of Object.values(characters)) {
|
|
136
|
+
if (info.type !== 'sprite' || !info.frames) continue;
|
|
137
|
+
for (const frame of info.frames) {
|
|
138
|
+
for (const p of frame.places) {
|
|
139
|
+
if (p.characterId == null) continue;
|
|
140
|
+
const key = String(p.characterId);
|
|
141
|
+
if (!characters[key]) {
|
|
142
|
+
characters[key] = { type: 'other', color: colorForId(p.characterId) };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return { frames, characters, scenes };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function addFrameLabel(frame, name) {
|
|
152
|
+
if (frame.label != null && frame.label !== name) {
|
|
153
|
+
const labels = frame.labels ?? (frame.labels = [frame.label]);
|
|
154
|
+
if (!labels.includes(name)) labels.push(name);
|
|
155
|
+
}
|
|
156
|
+
frame.label = name;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @param {object[]} tags
|
|
161
|
+
* @returns {TimelineFrame[]}
|
|
162
|
+
*/
|
|
163
|
+
export function buildFramesFromTags(tags) {
|
|
164
|
+
/** @type {TimelineFrame[]} */
|
|
165
|
+
const frames = [];
|
|
166
|
+
/** @type {PlaceMutation[]} */
|
|
167
|
+
let places = [];
|
|
168
|
+
/** @type {number[]} */
|
|
169
|
+
let removes = [];
|
|
170
|
+
/** @type {TimelineFrame['sounds']} */
|
|
171
|
+
let sounds = [];
|
|
172
|
+
let streamHead = null;
|
|
173
|
+
let streamBlocks = [];
|
|
174
|
+
let frameLabels = { label: undefined };
|
|
175
|
+
let namedAnchors = [];
|
|
176
|
+
|
|
177
|
+
for (const tag of tags) {
|
|
178
|
+
if (!tag || !tag.type) continue;
|
|
179
|
+
|
|
180
|
+
if (tag.type === 'FrameLabel') {
|
|
181
|
+
addFrameLabel(frameLabels, tag.name);
|
|
182
|
+
if (tag.namedAnchor && tag.name) namedAnchors.push(tag.name);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (
|
|
187
|
+
tag.type === 'PlaceObject' ||
|
|
188
|
+
tag.type === 'PlaceObject2' ||
|
|
189
|
+
tag.type === 'PlaceObject3'
|
|
190
|
+
) {
|
|
191
|
+
places.push(serializePlace(tag));
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (tag.type === 'SetTabIndex') {
|
|
196
|
+
places.push({ depth: tag.depth, tabIndex: tag.tabIndex });
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (tag.type === 'RemoveObject' || tag.type === 'RemoveObject2') {
|
|
201
|
+
const depth = tag.depth | 0;
|
|
202
|
+
if (tag.type === 'RemoveObject') {
|
|
203
|
+
places.push({ depth, remove: true, removeCharacterId: tag.characterId });
|
|
204
|
+
} else if (places.length) {
|
|
205
|
+
places.push({ depth, remove: true });
|
|
206
|
+
} else {
|
|
207
|
+
removes.push(depth);
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (tag.type === 'StartSound' && tag.soundId != null) {
|
|
213
|
+
sounds.push({ soundId: tag.soundId | 0, soundInfo: tag.soundInfo || {} });
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (tag.type === 'StartSound2' && tag.soundClassName) {
|
|
217
|
+
sounds.push({ soundClassName: tag.soundClassName, soundInfo: tag.soundInfo || {} });
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (tag.type === 'SoundStreamHead') {
|
|
222
|
+
streamHead = tag;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (tag.type === 'SoundStreamBlock') {
|
|
227
|
+
if (streamHead) {
|
|
228
|
+
let sampleCount = tag.sampleCount;
|
|
229
|
+
let seekSamples = tag.seekSamples;
|
|
230
|
+
if (streamHead.streamCompression === 2 && (sampleCount == null || seekSamples == null)) {
|
|
231
|
+
const bytes = tag.rawData || new Uint8Array(0);
|
|
232
|
+
if (bytes.length >= 4) {
|
|
233
|
+
sampleCount = bytes[0] | (bytes[1] << 8);
|
|
234
|
+
seekSamples = (bytes[2] | (bytes[3] << 8));
|
|
235
|
+
if (seekSamples & 0x8000) seekSamples -= 0x10000;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
streamBlocks.push({ ...tag, sampleCount, seekSamples });
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (tag.type === 'ShowFrame') {
|
|
244
|
+
/** @type {TimelineFrame} */
|
|
245
|
+
const frame = {
|
|
246
|
+
frameIndex: frames.length,
|
|
247
|
+
places,
|
|
248
|
+
removes,
|
|
249
|
+
...frameLabels,
|
|
250
|
+
};
|
|
251
|
+
if (namedAnchors.length) frame.namedAnchors = namedAnchors;
|
|
252
|
+
if (sounds.length) frame.sounds = sounds;
|
|
253
|
+
if (streamHead) frame.streamHead = streamHead;
|
|
254
|
+
if (streamBlocks.length) {
|
|
255
|
+
frame.streamBlocks = streamBlocks;
|
|
256
|
+
const block = streamBlocks[0];
|
|
257
|
+
if (streamHead.streamCompression === 2 && block.sampleCount != null && block.seekSamples != null) {
|
|
258
|
+
frame.streamSeek = { sampleCount: block.sampleCount, seekSamples: block.seekSamples };
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
frames.push(frame);
|
|
262
|
+
places = [];
|
|
263
|
+
removes = [];
|
|
264
|
+
sounds = [];
|
|
265
|
+
streamBlocks = [];
|
|
266
|
+
frameLabels = { label: undefined };
|
|
267
|
+
namedAnchors = [];
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Trailing mutations without ShowFrame — still one frame (common for single-frame SWFs)
|
|
272
|
+
if (places.length || removes.length || sounds.length || frames.length === 0) {
|
|
273
|
+
if (places.length || removes.length || sounds.length || frameLabels.label) {
|
|
274
|
+
/** @type {TimelineFrame} */
|
|
275
|
+
const frame = {
|
|
276
|
+
frameIndex: frames.length,
|
|
277
|
+
places,
|
|
278
|
+
removes,
|
|
279
|
+
...frameLabels,
|
|
280
|
+
};
|
|
281
|
+
if (namedAnchors.length) frame.namedAnchors = namedAnchors;
|
|
282
|
+
if (sounds.length) frame.sounds = sounds;
|
|
283
|
+
if (streamHead) frame.streamHead = streamHead;
|
|
284
|
+
if (streamBlocks.length) {
|
|
285
|
+
frame.streamBlocks = streamBlocks;
|
|
286
|
+
const block = streamBlocks[0];
|
|
287
|
+
if (streamHead.streamCompression === 2 && block.sampleCount != null && block.seekSamples != null) {
|
|
288
|
+
frame.streamSeek = { sampleCount: block.sampleCount, seekSamples: block.seekSamples };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
frames.push(frame);
|
|
292
|
+
} else if (frames.length === 0) {
|
|
293
|
+
frames.push({ frameIndex: 0, places: [], removes: [] });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return frames;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Parse SymbolClass tag → linkage map (characterId 0 = document class).
|
|
302
|
+
* @param {object[]} tags
|
|
303
|
+
* @returns {{ id: number, name: string }[]}
|
|
304
|
+
*/
|
|
305
|
+
export function buildSymbolClasses(tags) {
|
|
306
|
+
/** @type {{ id: number, name: string }[]} */
|
|
307
|
+
const out = [];
|
|
308
|
+
for (const tag of tags) {
|
|
309
|
+
if (!tag || tag.type !== 'SymbolClass' || !tag.rawData) continue;
|
|
310
|
+
try {
|
|
311
|
+
const r = new BitReader(tag.rawData);
|
|
312
|
+
const n = r.readUI16();
|
|
313
|
+
for (let i = 0; i < n && !r.eof; i++) {
|
|
314
|
+
const id = r.readUI16();
|
|
315
|
+
const name = r.readString();
|
|
316
|
+
if (name) out.push({ id, name });
|
|
317
|
+
}
|
|
318
|
+
} catch (_) {
|
|
319
|
+
/* skip malformed */
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Parse ExportAssets tags → AVM1 attachMovie linkage (name → characterId).
|
|
327
|
+
* @param {object[]} tags
|
|
328
|
+
* @returns {{ id: number, name: string }[]}
|
|
329
|
+
*/
|
|
330
|
+
export function buildExportAssets(tags) {
|
|
331
|
+
/** @type {{ id: number, name: string }[]} */
|
|
332
|
+
const out = [];
|
|
333
|
+
const seen = new Set();
|
|
334
|
+
for (const tag of tags) {
|
|
335
|
+
if (!tag || tag.type !== 'ExportAssets' || !tag.assets) continue;
|
|
336
|
+
for (const a of tag.assets) {
|
|
337
|
+
if (!a || !a.name) continue;
|
|
338
|
+
const key = a.name + '#' + (a.id | 0);
|
|
339
|
+
if (seen.has(key)) continue;
|
|
340
|
+
seen.add(key);
|
|
341
|
+
out.push({ id: a.id | 0, name: String(a.name) });
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* @param {object} tag
|
|
349
|
+
* @returns {PlaceMutation}
|
|
350
|
+
*/
|
|
351
|
+
function serializePlace(tag) {
|
|
352
|
+
/** @type {PlaceMutation} */
|
|
353
|
+
const p = {
|
|
354
|
+
depth: tag.depth | 0,
|
|
355
|
+
move: !!tag.move,
|
|
356
|
+
};
|
|
357
|
+
if (tag.characterId != null) p.characterId = tag.characterId | 0;
|
|
358
|
+
if (tag.name != null) p.name = String(tag.name);
|
|
359
|
+
if (tag.matrix) {
|
|
360
|
+
p.matrix = {
|
|
361
|
+
a: num(tag.matrix.a, 1),
|
|
362
|
+
b: num(tag.matrix.b, 0),
|
|
363
|
+
c: num(tag.matrix.c, 0),
|
|
364
|
+
d: num(tag.matrix.d, 1),
|
|
365
|
+
tx: num(tag.matrix.tx, 0),
|
|
366
|
+
ty: num(tag.matrix.ty, 0),
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
for (const key of ['ratio', 'clipDepth', 'blendMode', 'visible', 'filters', 'className', 'hasImage', 'cacheAsBitmap', 'opaqueBackground']) {
|
|
370
|
+
if (tag[key] != null) p[key] = tag[key];
|
|
371
|
+
}
|
|
372
|
+
if (tag.colorTransform) p.colorTransform = { ...tag.colorTransform };
|
|
373
|
+
return p;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function fontFaceStyle(font) {
|
|
377
|
+
// FontInfo puts WideCodes below the style bits, unlike DefineFont2/3.
|
|
378
|
+
const flags = font?.version === 1 ? font.fontInfoFlags >> 1 : font?.flags;
|
|
379
|
+
return { bold: flags & 1 ? true : undefined, italic: flags & 2 ? true : undefined };
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* @param {object} char
|
|
384
|
+
* @param {object|null} diagnostics
|
|
385
|
+
* @returns {CharacterInfo}
|
|
386
|
+
*/
|
|
387
|
+
function characterInfoFromDefine(char, diagnostics, symbolTable, opts, fontClasses, buttonColorTransforms) {
|
|
388
|
+
switch (char.type) {
|
|
389
|
+
case 'Bitmap':
|
|
390
|
+
return { type: 'bitmap', assetId: char.characterId | 0 };
|
|
391
|
+
case 'Shape': {
|
|
392
|
+
try {
|
|
393
|
+
const decoded = decodeShape(char.rawData, char.version || 1);
|
|
394
|
+
return {
|
|
395
|
+
type: 'shape',
|
|
396
|
+
bounds: boundsToPixels(decoded.bounds || char.bounds),
|
|
397
|
+
draws: decoded.draws,
|
|
398
|
+
evenOdd: decoded.evenOdd,
|
|
399
|
+
};
|
|
400
|
+
} catch (err) {
|
|
401
|
+
diagnostics?.warn?.(
|
|
402
|
+
`Shape ${char.characterId} decode failed: ${err.message}`,
|
|
403
|
+
'transpile',
|
|
404
|
+
{ characterId: char.characterId }
|
|
405
|
+
);
|
|
406
|
+
return {
|
|
407
|
+
type: 'shape',
|
|
408
|
+
bounds: boundsToPixels(char.bounds),
|
|
409
|
+
draws: [],
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
case 'MorphShape': {
|
|
414
|
+
try {
|
|
415
|
+
const morph = decodeMorphShape(char.rawData, char.version || 1);
|
|
416
|
+
return {
|
|
417
|
+
type: 'shape',
|
|
418
|
+
bounds: boundsToPixels(morph.startBounds, 0),
|
|
419
|
+
morph: {
|
|
420
|
+
startDraws: morph.startDraws, endDraws: morph.endDraws,
|
|
421
|
+
endBounds: boundsToPixels(morph.endBounds, 0),
|
|
422
|
+
},
|
|
423
|
+
draws: morph.startDraws,
|
|
424
|
+
};
|
|
425
|
+
} catch (err) {
|
|
426
|
+
diagnostics?.warn?.(`MorphShape ${char.characterId} decode failed: ${err.message}`, 'transpile', { characterId: char.characterId });
|
|
427
|
+
return { type: 'shape', bounds: boundsToPixels(char.bounds), draws: [] };
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
case 'Sprite': {
|
|
431
|
+
const labelContext = opts.swfVersion == null ? null : { header: { version: opts.swfVersion } };
|
|
432
|
+
const decoded = (char.tags || []).map((raw) => {
|
|
433
|
+
try {
|
|
434
|
+
return decodeTag(raw, raw.code === TAG.FrameLabel ? labelContext : null, diagnostics);
|
|
435
|
+
} catch (err) {
|
|
436
|
+
diagnostics?.warn?.(
|
|
437
|
+
`Failed to decode sprite ${char.characterId} child tag: ${err.message}`,
|
|
438
|
+
'transpile',
|
|
439
|
+
{ characterId: char.characterId }
|
|
440
|
+
);
|
|
441
|
+
return { type: 'Unknown' };
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
const frames = buildFramesFromTags(decoded);
|
|
445
|
+
let buttonVisualStopFrame;
|
|
446
|
+
let visualFrame = 0;
|
|
447
|
+
for (const tag of decoded) {
|
|
448
|
+
if (tag.type === 'ShowFrame') visualFrame++;
|
|
449
|
+
if (tag.type !== 'DoAction') continue;
|
|
450
|
+
const bytes = tag.bytecode;
|
|
451
|
+
if (!bytes?.length || bytes[0] === OP.End) continue;
|
|
452
|
+
if (bytes[0] !== OP.Stop || bytes.length > 1 && bytes[1] !== OP.End) {
|
|
453
|
+
buttonVisualStopFrame = undefined;
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
buttonVisualStopFrame ??= visualFrame;
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
type: 'sprite',
|
|
460
|
+
frameCount: char.frameCount || frames.length || 1,
|
|
461
|
+
tagStreamBytes: char.tagStreamBytes,
|
|
462
|
+
hasStream: frames.some((frame) => frame.streamBlocks?.length),
|
|
463
|
+
frames,
|
|
464
|
+
buttonVisualStopFrame,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
case 'Button': {
|
|
468
|
+
try {
|
|
469
|
+
const btn = decodeButton(char.rawData, char.version || 2);
|
|
470
|
+
// Legacy transforms correspond to raw records before filtering by state or sorting by depth.
|
|
471
|
+
if (char.version === 1 && buttonColorTransforms) {
|
|
472
|
+
btn.states.forEach((record, index) => {
|
|
473
|
+
record.colorTransform = buttonColorTransforms[index];
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
const actionDiagnostics = diagnostics || new DiagnosticsCollector();
|
|
477
|
+
const compiledActions = opts.legacyButtonScripts?.get(char.characterId);
|
|
478
|
+
const serializeButtonRecord = (record) => ({
|
|
479
|
+
characterId: record.characterId,
|
|
480
|
+
depth: record.depth,
|
|
481
|
+
matrix: {
|
|
482
|
+
a: num(record.matrix?.a, 1), b: num(record.matrix?.b, 0),
|
|
483
|
+
c: num(record.matrix?.c, 0), d: num(record.matrix?.d, 1),
|
|
484
|
+
tx: num(record.matrix?.tx, 0), ty: num(record.matrix?.ty, 0),
|
|
485
|
+
},
|
|
486
|
+
colorTransform: record.colorTransform,
|
|
487
|
+
filters: record.filters,
|
|
488
|
+
blendMode: record.blendMode,
|
|
489
|
+
});
|
|
490
|
+
return {
|
|
491
|
+
type: 'button',
|
|
492
|
+
trackAsMenu: btn.trackAsMenu,
|
|
493
|
+
actions: btn.actions.map((action, index) => {
|
|
494
|
+
if (compiledActions?.[index] != null) {
|
|
495
|
+
return { conditions: action.conditions, script: compiledActions[index] };
|
|
496
|
+
}
|
|
497
|
+
const meta = transpileAvm1WithMeta(action.bytecode, actionDiagnostics, {
|
|
498
|
+
timelineDeclarations: true,
|
|
499
|
+
sharedAvm1Runtime: opts.sharedAvm1Runtime,
|
|
500
|
+
nameSpellings: opts.swfVersion >= 7 ? null : undefined,
|
|
501
|
+
label: `button_${char.characterId}_action_${index}`,
|
|
502
|
+
});
|
|
503
|
+
return { conditions: action.conditions, script: meta.js };
|
|
504
|
+
}),
|
|
505
|
+
buttonHitStates: btn.hitStates.map(serializeButtonRecord),
|
|
506
|
+
buttonStates: Object.fromEntries(['up', 'over', 'down'].map((state) => [state,
|
|
507
|
+
btn.stateRecords[state].map(serializeButtonRecord).sort((a, b) => a.depth - b.depth)])),
|
|
508
|
+
};
|
|
509
|
+
} catch (err) {
|
|
510
|
+
diagnostics?.warn?.(
|
|
511
|
+
`Button ${char.characterId} decode failed: ${err.message}`,
|
|
512
|
+
'transpile',
|
|
513
|
+
{ characterId: char.characterId }
|
|
514
|
+
);
|
|
515
|
+
return { type: 'button' };
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
case 'Text':
|
|
519
|
+
const textRuns = (char.records || []).map((record) => {
|
|
520
|
+
const font = symbolTable?.get?.(record.fontId);
|
|
521
|
+
const glyphs = (record.glyphs || []).map((glyph) => {
|
|
522
|
+
const code = font?.codes?.[glyph.glyphIndex];
|
|
523
|
+
const cp = code == null ? glyph.glyphIndex : code;
|
|
524
|
+
let glyphText;
|
|
525
|
+
try { glyphText = String.fromCodePoint(cp); } catch (_) { glyphText = '\uFFFD'; }
|
|
526
|
+
return {
|
|
527
|
+
glyphIndex: glyph.glyphIndex,
|
|
528
|
+
advance: glyph.advance,
|
|
529
|
+
hasOutline: !!font?.glyphs?.[glyph.glyphIndex]?.path?.length,
|
|
530
|
+
text: glyphText,
|
|
531
|
+
};
|
|
532
|
+
});
|
|
533
|
+
return {
|
|
534
|
+
text: glyphs.map((glyph) => glyph.text).join(''),
|
|
535
|
+
fontId: record.fontId,
|
|
536
|
+
glyphs,
|
|
537
|
+
x: (record.x || 0) / 20,
|
|
538
|
+
y: (record.y || 0) / 20,
|
|
539
|
+
font: font?.name || 'Arial',
|
|
540
|
+
fontSize: Math.max(1, (record.fontHeight || 240) / 20),
|
|
541
|
+
color: record.color || '#000000',
|
|
542
|
+
};
|
|
543
|
+
});
|
|
544
|
+
const plainText = textRuns.map((run) => run.text).join('');
|
|
545
|
+
const firstRun = textRuns.find((run) => run.text) || textRuns[0] || { fontSize: 12, color: '#000000' };
|
|
546
|
+
const hasOutlineGlyphs = textRuns.some((run) => run.glyphs.length > 0) &&
|
|
547
|
+
textRuns.every((run) => run.glyphs.every((glyph) => glyph.hasOutline || /^\s+$/u.test(glyph.text)));
|
|
548
|
+
const textBounds = boundsToPixels(char.bounds) || { x: 0, y: 0, w: 80, h: 16 };
|
|
549
|
+
const positionedRuns = textRuns.map(({ text: _text, glyphs, ...run }) => ({
|
|
550
|
+
...run,
|
|
551
|
+
glyphs: glyphs.map(({ hasOutline: _hasOutline, ...glyph }) => glyph),
|
|
552
|
+
}));
|
|
553
|
+
return {
|
|
554
|
+
type: 'text',
|
|
555
|
+
kind: 'static',
|
|
556
|
+
bounds: textBounds,
|
|
557
|
+
matrix: char.matrix || undefined,
|
|
558
|
+
color: firstRun.color,
|
|
559
|
+
text: plainText,
|
|
560
|
+
outlineGlyphRuns: hasOutlineGlyphs ? positionedRuns : undefined,
|
|
561
|
+
fallbackGlyphRuns: hasOutlineGlyphs ? undefined : positionedRuns,
|
|
562
|
+
fontSize: firstRun.fontSize,
|
|
563
|
+
csmTextSettings: char.csmTextSettings,
|
|
564
|
+
readOnly: true,
|
|
565
|
+
editable: false,
|
|
566
|
+
};
|
|
567
|
+
case 'EditText': {
|
|
568
|
+
const fontSize = (char.fontHeight || 240) / 20;
|
|
569
|
+
const fontId = char.fontId || (char.fontClass && fontClasses.get(char.fontClass.replace('::', '.')));
|
|
570
|
+
const font = symbolTable?.get?.(fontId);
|
|
571
|
+
return {
|
|
572
|
+
type: 'text',
|
|
573
|
+
kind: 'edit',
|
|
574
|
+
bounds: boundsToPixels(char.bounds) || { x: 0, y: 0, w: 80, h: 16 },
|
|
575
|
+
color: char.color || '#ffffff',
|
|
576
|
+
text: char.initialText != null ? String(char.initialText).replace(/\r\n/g, '\n').replace(/\r/g, '\n') : '',
|
|
577
|
+
hasText: !!char.hasText,
|
|
578
|
+
fontSize,
|
|
579
|
+
font: font?.name || char.fontClass || 'Arial',
|
|
580
|
+
...fontFaceStyle(font),
|
|
581
|
+
outlineFontId: char.useOutlines && fontId || undefined,
|
|
582
|
+
csmTextSettings: char.csmTextSettings,
|
|
583
|
+
readOnly: !!char.readOnly,
|
|
584
|
+
editable: !char.readOnly,
|
|
585
|
+
selectable: char.noSelect ? false : undefined,
|
|
586
|
+
border: !!char.border,
|
|
587
|
+
wasStatic: !!char.wasStatic,
|
|
588
|
+
password: !!char.password,
|
|
589
|
+
wordWrap: !!char.wordWrap,
|
|
590
|
+
multiline: !!char.multiline,
|
|
591
|
+
autoSize: char.autoSize && (opts.swfVersion ?? 6) >= 6 || undefined,
|
|
592
|
+
html: !!char.html,
|
|
593
|
+
maxLength: char.maxLength || 0,
|
|
594
|
+
align: ['left', 'right', 'center', 'justify'][char.align | 0] || 'left',
|
|
595
|
+
leftMargin: (char.leftMargin || 0) / 20,
|
|
596
|
+
rightMargin: (char.rightMargin || 0) / 20,
|
|
597
|
+
indent: (char.indent || 0) / 20,
|
|
598
|
+
leading: (char.leading || 0) / 20,
|
|
599
|
+
variableName: char.variableName || '',
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
case 'Font':
|
|
603
|
+
return { type: 'font', name: char.name || '', version: char.version || 1, glyphs: char.glyphs || [],
|
|
604
|
+
codes: char.layout ? char.codes : undefined, layout: char.layout, ...fontFaceStyle(char) };
|
|
605
|
+
default:
|
|
606
|
+
return { type: 'other', color: colorForId(char.characterId) };
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* @param {import('../../model/swf-document.js').Rect|undefined} bounds
|
|
612
|
+
* @returns {{ x: number, y: number, w: number, h: number }|undefined}
|
|
613
|
+
*/
|
|
614
|
+
function boundsToPixels(bounds, minimum = 1) {
|
|
615
|
+
if (!bounds) return undefined;
|
|
616
|
+
const x = (bounds.xMin || 0) / 20;
|
|
617
|
+
const y = (bounds.yMin || 0) / 20;
|
|
618
|
+
const w = Math.max(minimum, ((bounds.xMax || 0) - (bounds.xMin || 0)) / 20);
|
|
619
|
+
const h = Math.max(minimum, ((bounds.yMax || 0) - (bounds.yMin || 0)) / 20);
|
|
620
|
+
return { x, y, w, h };
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* @param {number} id
|
|
625
|
+
* @returns {string}
|
|
626
|
+
*/
|
|
627
|
+
function colorForId(id) {
|
|
628
|
+
const n = (Number(id) * 2654435761) >>> 0;
|
|
629
|
+
const r = 80 + (n & 0x7f);
|
|
630
|
+
const g = 80 + ((n >> 8) & 0x7f);
|
|
631
|
+
const b = 80 + ((n >> 16) & 0x7f);
|
|
632
|
+
return `#${[r, g, b].map((v) => v.toString(16).padStart(2, '0')).join('')}`;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* @param {unknown} v
|
|
637
|
+
* @param {number} fallback
|
|
638
|
+
* @returns {number}
|
|
639
|
+
*/
|
|
640
|
+
function num(v, fallback) {
|
|
641
|
+
const n = Number(v);
|
|
642
|
+
return Number.isFinite(n) ? n : fallback;
|
|
643
|
+
}
|