forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemscripten-pre.js
More file actions
140 lines (112 loc) · 3.59 KB
/
Copy pathemscripten-pre.js
File metadata and controls
140 lines (112 loc) · 3.59 KB
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
// Note: The `Module` context is already initialized as an
// empty object by emscripten even before the pre script
Module = { ...Module,
preRun: [onPreRun],
postRun: [],
print: (...args) => {
console.log(...args);
},
printErr: (...args) => {
console.error(...args);
},
canvas: (() => {
const canvas = document.getElementById('canvas');
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener('webglcontextlost', event => {
event.preventDefault();
}, false);
canvas.addEventListener('webglcontextrestored', () => {
Module.api.resetCanvas();
});
return canvas;
})(),
setStatus: text => {
if (!Module.setStatus.last) Module.setStatus.last = {
time: Date.now(),
text: ''
};
if (text !== Module.setStatus.text) {
document.getElementById('status').innerHTML = text;
}
},
totalDependencies: 0,
monitorRunDependencies: left => {
Module.totalDependencies = Math.max(Module.totalDependencies, left);
Module.setStatus(left ? `Preparing... (${Module.totalDependencies - left}/${Module.totalDependencies})` : 'Downloading game data...');
}
};
/**
* Parses the current location query to setup a specific game
*/
function parseArgs () {
const items = window.location.search.slice(1).split("&");
let result = [];
// Store saves in subdirectory `Save`
result.push("--save-path");
result.push("Save");
for (let i = 0; i < items.length; i++) {
const tmp = items[i].split("=");
if (tmp[0] === "project-path" || tmp[0] === "save-path") {
// Filter arguments that are set by us
continue;
}
// Filesystem is not ready when processing arguments, store path to game
if (tmp[0] === "game" && tmp.length > 1) {
Module.game = tmp[1];
continue;
}
result.push("--" + tmp[0]);
if (tmp.length > 1) {
const arg = decodeURI(tmp[1]);
// Split except if it's a string
if (arg.length > 0) {
if (arg.startsWith('"') && arg.endsWith('"')) {
result.push(arg.slice(1, -1));
} else {
result = [...result, ...arg.split(" ")];
}
}
}
}
return result;
}
function onPreRun () {
// Move to different directory to prevent save file collisions in IDBFS
FS.mkdir("easyrpg");
FS.chdir("easyrpg");
if (Module.game.length > 0) {
FS.mkdir(Module.game);
FS.chdir(Module.game);
}
// Retrieve save directory from persistent storage before using it
FS.mkdir("Save");
// Use IDBFS for save file storage when the filesystem was not
// overwritten by a custom emscripten shell file
if (Module.saveFs === undefined) {
Module.saveFs = IDBFS;
}
FS.mount(Module.saveFs, {}, 'Save');
// For preserving the configuration. Shared across website
FS.mkdir("/home/web_user/.config");
FS.mount(IDBFS, {}, '/home/web_user/.config');
FS.syncfs(true, function(err) {});
}
Module.setStatus('Downloading...');
Module.arguments = ["easyrpg-player", ...parseArgs()];
if (Module.game === undefined) {
Module.game = "";
} else {
Module.arguments.push("--game", Module.game);
Module.game = Module.game.toLowerCase();
}
// Catch all errors occuring inside the window
window.addEventListener('error', (event) => {
// workaround chrome bug: See https://github.com/EasyRPG/Player/issues/2806
if (event.error.message.includes("side-effect in debug-evaluate") && event.defaultPrevented) {
return;
}
Module.setStatus('Exception thrown, see JavaScript console…');
Module.setStatus = text => {
if (text) Module.printErr(`[post-exception status] ${text}`);
};
});