swf2js 0.0.0-stage → 0.1.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 (38) hide show
  1. package/LICENSE-GPL +680 -0
  2. package/README.md +208 -2
  3. package/package.json +24 -4
  4. package/src/cli.js +272 -0
  5. package/src/decoder/button-decoder.js +136 -0
  6. package/src/decoder/shape-decoder.js +786 -0
  7. package/src/decoder/symbol-table.js +36 -0
  8. package/src/decoder/tag-decoder.js +1190 -0
  9. package/src/decoder/tag-types.js +258 -0
  10. package/src/emitter/asset-encoder.js +708 -0
  11. package/src/emitter/bundle-emitter.js +596 -0
  12. package/src/emitter/embedded-swf.js +246 -0
  13. package/src/emitter/font-encoder.js +365 -0
  14. package/src/emitter/timeline-data-codec.js +58 -0
  15. package/src/model/swf-document.js +59 -0
  16. package/src/model/transpile-result.js +32 -0
  17. package/src/parser/bit-reader.js +237 -0
  18. package/src/parser/decompressor.js +91 -0
  19. package/src/parser/swf-parser.js +164 -0
  20. package/src/pretty-printer/pretty-printer.js +137 -0
  21. package/src/runtime/avm1-runtime.js +4907 -0
  22. package/src/runtime/avm2-runtime.js +1591 -0
  23. package/src/runtime/embedded-movie-runtime.js +208 -0
  24. package/src/runtime/index.js +17 -0
  25. package/src/runtime/movie-runtime.js +306 -0
  26. package/src/runtime/swflib.js +14050 -0
  27. package/src/transpiler/actionscript.js +390 -0
  28. package/src/transpiler/avm1/avm1-ir.js +205 -0
  29. package/src/transpiler/avm1/avm1-opcodes.js +149 -0
  30. package/src/transpiler/avm1/avm1-rewrite.js +291 -0
  31. package/src/transpiler/avm1/avm1-transpiler.js +2082 -0
  32. package/src/transpiler/avm2/abc-reader.js +416 -0
  33. package/src/transpiler/avm2/avm2-transpiler.js +2560 -0
  34. package/src/transpiler/cfg-structurer.js +876 -0
  35. package/src/transpiler/timeline/display-list.js +643 -0
  36. package/src/transpiler/timeline/timeline-codegen.js +6354 -0
  37. package/src/util/diagnostics.js +50 -0
  38. package/src/util/logger.js +40 -0
@@ -0,0 +1,137 @@
1
+ /**
2
+ * @fileoverview Pretty_Printer — formats a parsed SwfDocument as human-readable text.
3
+ * Used for debugging parse output.
4
+ */
5
+
6
+ /** @import { SwfDocument } from '../model/swf-document.js' */
7
+ /** @import { SymbolTable } from '../decoder/symbol-table.js' */
8
+
9
+ /**
10
+ * @param {SwfDocument} doc
11
+ * @param {import('../decoder/symbol-table.js').SymbolTable} [symbolTable]
12
+ * @returns {string}
13
+ */
14
+ export function prettyPrint(doc, symbolTable) {
15
+ const lines = [];
16
+ const { header } = doc;
17
+
18
+ lines.push('SWF Document');
19
+ lines.push(` signature : ${header.signature}`);
20
+ lines.push(` version : ${header.version}`);
21
+ lines.push(` frameSize : ${twips(header.frameSize.xMax)} x ${twips(header.frameSize.yMax)} px`);
22
+ lines.push(` frameRate : ${header.frameRate.toFixed(2)} fps`);
23
+ lines.push(` frameCount : ${header.frameCount}`);
24
+ lines.push(` tags : ${doc.tags.length}`);
25
+ lines.push('');
26
+
27
+ for (let i = 0; i < doc.tags.length; i++) {
28
+ const raw = doc.tags[i];
29
+ lines.push(formatTag(raw, i, symbolTable));
30
+ }
31
+
32
+ return lines.join('\n');
33
+ }
34
+
35
+ /**
36
+ * @param {import('../model/swf-document.js').RawTag} raw
37
+ * @param {number} index
38
+ * @param {import('../decoder/symbol-table.js').SymbolTable} [symbolTable]
39
+ * @returns {string}
40
+ */
41
+ function formatTag(raw, index, symbolTable) {
42
+ const name = TAG_NAMES[raw.code] ?? `Unknown(${raw.code})`;
43
+ const prefix = ` [${String(index).padStart(4, '0')}] ${name.padEnd(32)} (code=${raw.code}, len=${raw.length})`;
44
+
45
+ // Annotate character-definition tags
46
+ const charId = getCharacterId(raw);
47
+ if (charId !== undefined) {
48
+ return `${prefix} charId=${charId}`;
49
+ }
50
+
51
+ // Annotate PlaceObject tags with depth + charId
52
+ if (raw.code === 4 || raw.code === 26 || raw.code === 70) {
53
+ try {
54
+ const info = parsePlaceInfo(raw);
55
+ const parts = [`depth=${info.depth}`];
56
+ if (info.characterId !== undefined) parts.push(`charId=${info.characterId}`);
57
+ if (info.matrix) parts.push(`matrix=[${info.matrix.a.toFixed(2)},${info.matrix.d.toFixed(2)},tx=${info.matrix.tx},ty=${info.matrix.ty}]`);
58
+ return `${prefix} ${parts.join(' ')}`;
59
+ } catch (_) {}
60
+ }
61
+
62
+ return prefix;
63
+ }
64
+
65
+ /**
66
+ * Quick character ID extraction without full decoding.
67
+ * @param {import('../model/swf-document.js').RawTag} raw
68
+ * @returns {number|undefined}
69
+ */
70
+ function getCharacterId(raw) {
71
+ const CHAR_ID_TAGS = new Set([
72
+ 2, 22, 32, 83, // DefineShape 1-4
73
+ 46, 84, // DefineMorphShape 1-2
74
+ 6, 21, 35, 90, // DefineBits/JPEG
75
+ 20, 36, // DefineBitsLossless 1-2
76
+ 14, // DefineSound
77
+ 39, // DefineSprite
78
+ 10, 48, 75, 91, // DefineFont 1-4
79
+ 11, 33, // DefineText 1-2
80
+ 37, // DefineEditText
81
+ 7, 34, // DefineButton 1-2
82
+ 60, // DefineVideoStream
83
+ ]);
84
+ if (!CHAR_ID_TAGS.has(raw.code)) return undefined;
85
+ if (raw.data.length < 2) return undefined;
86
+ return raw.data[0] | (raw.data[1] << 8);
87
+ }
88
+
89
+ /**
90
+ * @param {import('../model/swf-document.js').RawTag} raw
91
+ */
92
+ function parsePlaceInfo(raw) {
93
+ const data = raw.data;
94
+ if (raw.code === 4) {
95
+ // PlaceObject1: characterId UI16, depth UI16, matrix...
96
+ const characterId = data[0] | (data[1] << 8);
97
+ const depth = data[2] | (data[3] << 8);
98
+ return { characterId, depth, matrix: undefined };
99
+ } else if (raw.code === 26 || raw.code === 70) {
100
+ // PlaceObject2/3: flags UI8, depth UI16
101
+ const depth = data[1] | (data[2] << 8);
102
+ const flags = data[0];
103
+ const hasCharId = !!(flags & 0x02);
104
+ const characterId = hasCharId ? (data[3] | (data[4] << 8)) : undefined;
105
+ return { characterId, depth, matrix: undefined };
106
+ }
107
+ return { depth: 0 };
108
+ }
109
+
110
+ /** @param {number} twipsVal @returns {number} */
111
+ function twips(twipsVal) {
112
+ return (twipsVal / 20).toFixed(0);
113
+ }
114
+
115
+ /** Human-readable names for the most common SWF tag codes */
116
+ const TAG_NAMES = {
117
+ 0: 'End', 1: 'ShowFrame', 2: 'DefineShape', 4: 'PlaceObject',
118
+ 5: 'RemoveObject', 6: 'DefineBits', 7: 'DefineButton', 8: 'JPEGTables',
119
+ 9: 'SetBackgroundColor', 10: 'DefineFont', 11: 'DefineText', 12: 'DoAction',
120
+ 13: 'DefineFontInfo', 14: 'DefineSound', 15: 'StartSound', 17: 'DefineButtonSound',
121
+ 18: 'SoundStreamHead', 19: 'SoundStreamBlock', 20: 'DefineBitsLossless',
122
+ 21: 'DefineBitsJPEG2', 22: 'DefineShape2', 23: 'DefineButtonCxform',
123
+ 24: 'Protect', 26: 'PlaceObject2', 28: 'RemoveObject2', 32: 'DefineShape3',
124
+ 33: 'DefineText2', 34: 'DefineButton2', 35: 'DefineBitsJPEG3',
125
+ 36: 'DefineBitsLossless2', 37: 'DefineEditText', 39: 'DefineSprite',
126
+ 43: 'FrameLabel', 45: 'SoundStreamHead2', 46: 'DefineMorphShape',
127
+ 48: 'DefineFont2', 56: 'ExportAssets', 57: 'ImportAssets', 59: 'DoInitAction',
128
+ 60: 'DefineVideoStream', 61: 'VideoFrame', 62: 'DefineFontInfo2',
129
+ 64: 'EnableDebugger2', 65: 'ScriptLimits', 66: 'SetTabIndex',
130
+ 69: 'FileAttributes', 70: 'PlaceObject3', 71: 'ImportAssets2',
131
+ 73: 'DefineFontAlignZones', 74: 'CSMTextSettings', 75: 'DefineFont3',
132
+ 76: 'SymbolClass', 77: 'Metadata', 78: 'DefineScalingGrid',
133
+ 82: 'DoABC', 83: 'DefineShape4', 84: 'DefineMorphShape2',
134
+ 86: 'DefineSceneAndFrameLabelData', 87: 'DefineBinaryData',
135
+ 88: 'DefineFontName', 89: 'StartSound2', 90: 'DefineBitsJPEG4',
136
+ 91: 'DefineFont4', 93: 'EnableTelemetry',
137
+ };