forked from nettleweb/nettleweb
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplayer.js
204 lines (181 loc) · 4.58 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict"; (async ({ window: win, document: doc }) => {
if (doc.readyState !== "complete") {
await new Promise((resolve) => {
const callback = () => {
if (doc.readyState === "complete") {
doc.removeEventListener("readystatechange", callback);
setTimeout(resolve, 1000, null);
}
};
doc.addEventListener("readystatechange", callback, { passive: true });
});
}
const sid = new URLSearchParams(win.location.search).get("s");
const his = win.history;
const body = doc.body;
const parent = win.parent || win;
win.stop();
win.focus();
his.scrollRestoration = "manual";
his.replaceState(void 0, "", "/");
body.innerHTML = "Loading... (1)";
if (sid == null || sid.length < 2 || parent === win) {
body.innerHTML = "Error: Invalid session context";
return;
}
/**
* @param {string} url
*/
function loadJS(url) {
return new Promise((resolve, reject) => {
const elem = doc.createElement("script");
elem.type = "text/javascript";
elem.src = url;
elem.async = true;
elem.defer = true;
elem.onload = () => {
resolve(null);
elem.onload = null;
elem.onerror = null;
};
elem.onerror = (e) => {
reject(e);
elem.onload = null;
elem.onerror = null;
};
body.appendChild(elem);
});
}
/**
* @param {Uint8Array} data
*/
async function loadPs2(data) {
body.innerHTML = "<canvas id=\"outputCanvas\" width=\"1280\" height=\"720\" tabIndex=\"1\"></canvas>";
const module = await (await import("/lib/playjs/Play.js")).default();
await module.ccall("initVm", "", [], []);
module.discImageDevice = {
getFileSize: () => data.byteLength,
isDone: () => true,
read: (ptr, off, len) => {
module.HEAPU8.set(data.subarray(off, off + len), ptr);
}
};
module.bootDiscImage("file.iso");
}
/**
* @param {ArrayBuffer} data
*/
async function loadSwf(data) {
await loadJS("/lib/ruffle/ruffle.js");
const player = win.RufflePlayer;
if (player == null)
throw new Error("Failed to load Ruffle player.");
const frame = player.newest().createPlayer();
body.innerHTML = "";
body.appendChild(frame);
await frame.load({
data: data,
wmode: "opaque",
scale: "showAll",
quality: "best",
autoplay: "auto",
logLevel: "warn",
letterbox: "on",
openUrlMode: "confirm",
upgradeToHttps: true
});
}
/**
* @param {Uint8Array} data
*/
async function loadDos(data) {
await loadJS("/lib/jsdos/js-dos.js");
const Dos = win.Dos;
if (Dos == null)
throw new Error("Failed to load player API.");
const frame = doc.createElement("div");
body.innerHTML = "";
body.appendChild(frame);
const url = URL.createObjectURL(new Blob([data], { type: "application/octet-stream", endings: "native" }));
Dos(frame, {
onEvent: (e) => {
if (e === "emu-ready")
URL.revokeObjectURL(url);
},
url: url,
theme: "light",
backend: "dosboxX",
noCloud: true,
noCursor: true,
autoStart: true,
pathPrefix: "/lib/jsdos/emulators/",
workerThread: true,
mouseCapture: true,
renderAspect: "4/3",
renderBackend: "webgl"
});
}
/**
* @param {string} url
* @param {string} bios
* @param {string} core
*/
async function loadEmu(url, bios, core) {
await loadJS("/lib/emulatorjs/emulator.min.js");
const EmulatorJS = win.EmulatorJS;
if (EmulatorJS == null)
throw new Error("Failed to load player API.");
body.innerHTML = "";
body.appendChild(doc.createElement("div"));
const obj = new win.EmulatorJS("body>div", {
system: core,
gameUrl: url,
biosUrl: bios,
threads: true,
dataPath: "/lib/emulatorjs/",
gameName: "Game",
startOnLoad: true
});
await new Promise((resolve) => {
const timer = setInterval(() => {
if (obj.started) {
clearInterval(timer);
resolve(null);
}
}, 1000);
});
if (url.startsWith("blob:"))
URL.revokeObjectURL(url);
if (bios.startsWith("blob:"))
URL.revokeObjectURL(bios);
}
/**
* @param {MessageEvent} e
*/
function messageCallback(e) {
if (e.origin === win.origin) {
const { id, buf, off, len } = e.data || {};
switch (id) {
case "ps2":
loadPs2(new Uint8Array(buf, off, len));
break;
case "swf":
case "flash":
loadSwf(buf);
break;
case "dos":
loadDos(new Uint8Array(buf, off, len));
break;
case "emu":
loadEmu(buf[0], buf[1], buf[2]);
break;
default:
console.error("Unknown player type: ", id);
break;
}
win.removeEventListener("message", messageCallback);
}
}
win.addEventListener("message", messageCallback, { passive: true });
parent.postMessage(sid, win.origin, []);
})(window);