1 | import { stringToCStr } from "./swf/util.mjs";
|
2 | import { Tag } from "./swf/tag.mjs";
|
3 | import { Swf } from "./swf/swf.mjs";
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function asvm1TypeString(str) {
|
12 | return Buffer.concat([Buffer.alloc(1), stringToCStr(str)]);
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function asmv1ActionConstantPool(strs) {
|
23 | const data = Buffer.concat([Buffer.alloc(5), ...strs.map(stringToCStr)]);
|
24 | data.writeUInt8(0x88, 0);
|
25 | data.writeUInt16LE(data.length - 3, 1);
|
26 | data.writeUInt16LE(strs.length, 3);
|
27 | return data;
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function asvm1TypeConstant8(i) {
|
38 | const data = Buffer.alloc(2);
|
39 | data.writeUInt8(0x08, 0);
|
40 | data.writeUInt8(i, 1);
|
41 | return data;
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function asmv1ActionPush(pushed) {
|
52 | const data = Buffer.concat([Buffer.alloc(3), ...pushed]);
|
53 | data.writeUInt8(0x96, 0);
|
54 | data.writeUInt16LE(data.length - 3, 1);
|
55 | return data;
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function asvm1ActionLoadMovie() {
|
65 | const data = Buffer.alloc(4);
|
66 | data.writeUInt8(0x9A, 0);
|
67 | data.writeUInt16LE(data.length - 3, 1);
|
68 | data.writeUInt8(0x40, 3);
|
69 | return data;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function asvm1ActionEnd() {
|
79 | return Buffer.alloc(1);
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function bytecodeLoadMovieSwf4(url) {
|
90 | return Buffer.concat([asmv1ActionPush([asvm1TypeString(url)]), asmv1ActionPush([asvm1TypeString('/')]), asvm1ActionLoadMovie(), asvm1ActionEnd()]);
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function bytecodeLoadMovieSwf5(url) {
|
101 | return Buffer.concat([asmv1ActionConstantPool([url, '_level0']), asmv1ActionPush([asvm1TypeConstant8(0), asvm1TypeConstant8(1)]), asvm1ActionLoadMovie(), asvm1ActionEnd()]);
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | export function loader(swfv, width, height, fps, color, url, delay = 0) {
|
119 | if (swfv < 4) {
|
120 | throw new Error('SWF format version must be 4+');
|
121 | }
|
122 |
|
123 | delay = delay < 0 ? 0 : delay;
|
124 | const swf = new Swf();
|
125 | swf.version = swfv;
|
126 | swf.frameSize.xMax = Math.round(width * 20);
|
127 | swf.frameSize.yMax = Math.round(height * 20);
|
128 | swf.frameRate.value = fps;
|
129 | swf.frameCount = delay + 1;
|
130 | const setBackgroundColor = new Tag();
|
131 | setBackgroundColor.code = 9;
|
132 | setBackgroundColor.data = Buffer.alloc(3);
|
133 |
|
134 | setBackgroundColor.data.writeUInt8(color & 0xFF, 2);
|
135 |
|
136 | setBackgroundColor.data.writeUInt8(color >> 8 & 0xFF, 1);
|
137 |
|
138 | setBackgroundColor.data.writeUInt8(color >> 16 & 0xFF, 0);
|
139 | swf.tags.push(setBackgroundColor);
|
140 | const showFrame = new Tag();
|
141 | showFrame.code = 1;
|
142 |
|
143 | for (let i = 0; i < delay; i++) {
|
144 | swf.tags.push(showFrame);
|
145 | }
|
146 |
|
147 | const doAction = new Tag();
|
148 | doAction.code = 12;
|
149 | doAction.data = swfv < 5 ? bytecodeLoadMovieSwf4(url) : bytecodeLoadMovieSwf5(url);
|
150 | swf.tags.push(doAction);
|
151 | swf.tags.push(showFrame);
|
152 | const end = new Tag();
|
153 | end.code = 0;
|
154 | swf.tags.push(end);
|
155 | return swf.encode();
|
156 | }
|
157 |
|