-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
152 lines (133 loc) · 4.54 KB
/
main.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
//=============================================================================
// main.js v1.1.0
//=============================================================================
const scriptUrls = [
"js/libs/pixi.js",
"js/libs/pako.min.js",
"js/libs/localforage.min.js",
"js/libs/effekseer.min.js",
"js/libs/vorbisdecoder.js",
"js/rmmz_core.js",
"js/rmmz_managers.js",
"js/rmmz_objects.js",
"js/rmmz_scenes.js",
"js/rmmz_sprites.js",
"js/rmmz_windows.js",
"js/plugins.js"
];
const effekseerWasmUrl = "js/libs/effekseer.wasm";
class Main {
constructor() {
this.xhrSucceeded = false;
this.loadCount = 0;
this.error = null;
}
run() {
this.showLoadingSpinner();
this.testXhr();
this.loadMainScripts();
}
showLoadingSpinner() {
const loadingSpinner = document.createElement("div");
const loadingSpinnerImage = document.createElement("div");
loadingSpinner.id = "loadingSpinner";
loadingSpinnerImage.id = "loadingSpinnerImage";
loadingSpinner.appendChild(loadingSpinnerImage);
document.body.appendChild(loadingSpinner);
}
eraseLoadingSpinner() {
const loadingSpinner = document.getElementById("loadingSpinner");
if (loadingSpinner) {
document.body.removeChild(loadingSpinner);
}
}
testXhr() {
const xhr = new XMLHttpRequest();
xhr.open("GET", document.currentScript.src);
xhr.onload = () => (this.xhrSucceeded = true);
xhr.send();
}
loadMainScripts() {
for (const url of scriptUrls) {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
script.async = false;
script.defer = true;
script.onload = this.onScriptLoad.bind(this);
script.onerror = this.onScriptError.bind(this);
script._url = url;
document.body.appendChild(script);
}
this.numScripts = scriptUrls.length;
window.addEventListener("load", this.onWindowLoad.bind(this));
window.addEventListener("error", this.onWindowError.bind(this));
}
onScriptLoad() {
if (++this.loadCount === this.numScripts) {
PluginManager.setup($plugins);
}
}
onScriptError(e) {
this.printError("Failed to load", e.target._url);
}
printError(name, message) {
this.eraseLoadingSpinner();
if (!document.getElementById("errorPrinter")) {
const errorPrinter = document.createElement("div");
errorPrinter.id = "errorPrinter";
errorPrinter.innerHTML = this.makeErrorHtml(name, message);
document.body.appendChild(errorPrinter);
}
}
makeErrorHtml(name, message) {
const nameDiv = document.createElement("div");
const messageDiv = document.createElement("div");
nameDiv.id = "errorName";
messageDiv.id = "errorMessage";
nameDiv.innerHTML = name;
messageDiv.innerHTML = message;
return nameDiv.outerHTML + messageDiv.outerHTML;
}
onWindowLoad() {
if (!this.xhrSucceeded) {
const message = "Your browser does not allow to read local files.";
this.printError("Error", message);
} else if (this.isPathRandomized()) {
const message = "Please move the Game.app to a different folder.";
this.printError("Error", message);
} else if (this.error) {
this.printError(this.error.name, this.error.message);
} else {
this.initEffekseerRuntime();
}
}
onWindowError(event) {
if (!this.error) {
this.error = event.error;
}
}
isPathRandomized() {
// [Note] We cannot save the game properly when Gatekeeper Path
// Randomization is in effect.
return (
Utils.isNwjs() &&
process.mainModule.filename.startsWith("/private/var")
);
}
initEffekseerRuntime() {
const onLoad = this.onEffekseerLoad.bind(this);
const onError = this.onEffekseerError.bind(this);
effekseer.initRuntime(effekseerWasmUrl, onLoad, onError);
}
onEffekseerLoad() {
this.eraseLoadingSpinner();
SceneManager.run(Scene_Boot);
}
onEffekseerError() {
this.printError("Failed to load", effekseerWasmUrl);
}
}
const main = new Main();
main.run();
//-----------------------------------------------------------------------------