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