UNPKG

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