UNPKG

3.92 kBJavaScriptView Raw
1import { stringEncode } from "./swf/util.mjs";
2import { Tag } from "./swf/tag.mjs";
3import { Swf } from "./swf/swf.mjs";
4import { concat } from "./util/internal/data.mjs";
5
6/**
7 * Type string.
8 *
9 * @param str The string.
10 * @returns Bytecode data.
11 */
12function asvm1TypeString(str) {
13 return concat([new Uint8Array(1), stringEncode(str)]);
14}
15
16/**
17 * Action: ConstantPoolEnd.
18 *
19 * @param strs Constant strings.
20 * @returns Bytecode data.
21 */
22function 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 * Type constant 8-bit.
33 *
34 * @param i Constant index.
35 * @returns Bytecode data.
36 */
37function asvm1TypeConstant8(i) {
38 const data = new Uint8Array(2);
39 data[0] = 0x08;
40 data[1] = i;
41 return data;
42}
43
44/**
45 * Action: Push.
46 *
47 * @param pushed Pushed data.
48 * @returns Bytecode data.
49 */
50function 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 * Action: LoadMovie.
60 *
61 * @returns Bytecode data.
62 */
63function 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 * Action: End.
74 *
75 * @returns Bytecode data.
76 */
77function asvm1ActionEnd() {
78 return new Uint8Array(1);
79}
80
81/**
82 * Bytecode in SWF4 format.
83 *
84 * @param url The URL to load.
85 * @returns Bytecode data.
86 */
87function bytecodeLoadMovieSwf4(url) {
88 return concat([asmv1ActionPush([asvm1TypeString(url)]), asmv1ActionPush([asvm1TypeString('/')]), asvm1ActionLoadMovie(), asvm1ActionEnd()]);
89}
90
91/**
92 * Bytecode in SWF5 format.
93 *
94 * @param url The URL to load.
95 * @returns Bytecode data.
96 */
97function bytecodeLoadMovieSwf5(url) {
98 return concat([asmv1ActionConstantPool([url, '_level0']), asmv1ActionPush([asvm1TypeConstant8(0), asvm1TypeConstant8(1)]), asvm1ActionLoadMovie(), asvm1ActionEnd()]);
99}
100
101/**
102 * Generate a loader stub SWF movie.
103 * Optionally include a delay to give player a change to initialize first.
104 *
105 * @param swfv SWF format version number.
106 * @param width Frame width.
107 * @param height Frame height.
108 * @param fps Frames-per-second.
109 * @param color Background color.
110 * @param url The URL to load.
111 * @param delay Number of frame to delay loading.
112 * @returns Movie data.
113 */
114export 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 // eslint-disable-next-line no-bitwise
129 setBackgroundColor.data[2] = color & 0xff;
130 // eslint-disable-next-line no-bitwise
131 setBackgroundColor.data[1] = color >> 8 & 0xff;
132 // eslint-disable-next-line no-bitwise
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//# sourceMappingURL=loader.mjs.map
\No newline at end of file