forked from ynoproject/forest-orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preloads.js
239 lines (225 loc) · 9.19 KB
/
preloads.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
let preloadFiles;
let preloadedFiles = {};
let gameLoadedFiles = new Set();
let preloadLocalizationFiles = {};
function initPreloadList() {
fetch(`../ynopreloads/${gameId}.json`)
.then(response => {
if (!response.ok)
return;
return response.text();
})
.then(responseText => {
if (!responseText)
return;
preloadFiles = JSON.parse(responseText.replaceAll('\"*/', '\"/'));
if (preloadFiles) {
const preloadFilesReplace = {};
for (let preload of Object.entries(preloadFiles)) {
for (let mapId of preload[0].split('/')) {
if (!preloadFilesReplace.hasOwnProperty(mapId))
preloadFilesReplace[mapId] = [];
preloadFilesReplace[mapId] = preloadFilesReplace[mapId].concat(preload[1]);
}
}
preloadFiles = preloadFilesReplace;
// Get game localization files
fetch("https://api.github.com/repos/ynoproject/ynotranslations/git/trees/master?recursive=1")
.then(response => response.json())
.then(jsonResponse => {
const gameFiles = [];
const gameFilesLangs = [];
const gameFilesLowerCase = [];
for (let path of jsonResponse.tree) {
path = path.path;
if (path.startsWith(gameId + "/")) {
path = path.replace(gameId + "/", "/");
let lang = path.substring(1, path.substring(1, path.length).indexOf("/") + 1);
let fileName = path.replace("/" + lang, "").replace(".lmu", ".po");
gameFiles.push(fileName);
gameFilesLowerCase.push(fileName.toLowerCase());
gameFilesLangs.push(lang);
}
}
for (let map of Object.values(preloadFiles)) {
let file;
for (let fi = 0; fi < map.length; fi++) {
file = map[fi];
if (typeof file === "object") {
if (!file.length) {
let filesAdd = [];
for (let f = 0; f < file.files.length; f++)
filesAdd.push([file.files[f], file.lang || ""]);
map = map.concat(filesAdd);
continue;
} else
file = file[0];
}
let fileLocalizedName = file.toLowerCase().replace(".lmu", ".po");
for (let i = 0; i < gameFiles.length; i++) {
if (gameFilesLowerCase[i] === fileLocalizedName) {
if (!preloadLocalizationFiles.hasOwnProperty(gameFilesLangs[i]))
preloadLocalizationFiles[gameFilesLangs[i]] = {};
preloadLocalizationFiles[gameFilesLangs[i]][file] = gameFiles[i];
}
}
}
}
}).catch(err => console.error(err));
}
}).catch(err => console.error(err));
}
function preloadFileAndSave(link, languageLink, translatedFileName) {
link = languageLink + (translatedFileName ? translatedFileName : link);
if (gameLoadedFiles.has(link))
return;
gameLoadedFiles.add(link);
const request = new XMLHttpRequest();
request.onload = function () {
if (request.status >= 200 && request.status < 300)
preloadedFiles[link] = request.response;
};
request.responseType = "arraybuffer";
let versionAdd = "";
if (gameVersion)
versionAdd = `${link.indexOf('?') > -1 ? '&' : '?'}v=${gameVersion}`;
request.open("GET", `/data/${gameId}${languageLink ? "/Language" : ""}${encodeURIComponent(link)}${versionAdd}`);
request.send();
}
function preloadFilesFromMapId(mapId) {
if (!preloadFiles || !preloadFiles.hasOwnProperty(mapId))
return;
let langLink;
let translatedFileName;
for (let preloadFile of preloadFiles[mapId]) {
if (typeof preloadFile === "object") {
let pfX = preloadFile.x?.toString() || tpX.toString();
let pfY;
let index = 0;
for (let pfI of pfX.split('/')) {
if (tpX.toString() === pfI || pfI === "-1") {
pfX = tpX;
pfY = preloadFile.y?.toString().split('/')[index] || tpY.toString();
if (tpY.toString() === pfY || pfY === "-1") {
pfY = tpY;
break;
}
}
index++;
}
if (tpX === pfX && tpY === pfY && (!preloadFile.questionable || globalConfig.questionablePreloads)) {
let current = 0;
let currentTimeout = preloadFile.delay || 0;
const totalAtOnce = 40;
const delayAdd = 450;
for (let preloadFileInside of preloadFile.files) {
if (gameLoadedFiles.has(preloadFileInside))
continue;
if (preloadLocalizationFiles.hasOwnProperty(preloadsGameLang) && preloadLocalizationFiles[preloadsGameLang].hasOwnProperty(preloadFileInside)) {
translatedFileName = preloadLocalizationFiles[preloadsGameLang][preloadFileInside];
langLink = "/" + preloadsGameLang;
if (preloadFileInside.endsWith(".lmu")) {
if (preloadFile.slow)
setTimeout(preloadFileAndSave, currentTimeout, preloadFileInside, langLink, translatedFileName);
else
preloadFileAndSave(preloadFileInside, langLink, translatedFileName);
current++;
if (current % totalAtOnce === 0)
currentTimeout += delayAdd;
translatedFileName = "";
langLink = "";
}
} else {
langLink = "";
translatedFileName = "";
}
if (preloadFile.slow)
setTimeout(preloadFileAndSave, currentTimeout, preloadFileInside, langLink, translatedFileName);
else
preloadFileAndSave(preloadFileInside, langLink, translatedFileName);
current++;
if (current % totalAtOnce === 0)
currentTimeout += delayAdd;
}
}
continue;
}
if (!gameLoadedFiles.has(preloadFile)) {
if (preloadLocalizationFiles.hasOwnProperty(preloadsGameLang) && preloadLocalizationFiles[preloadsGameLang].hasOwnProperty(preloadFile)) {
translatedFileName = preloadLocalizationFiles[preloadsGameLang][preloadFile];
langLink = "/" + preloadsGameLang;
if (preloadFile.endsWith(".lmu")) {
preloadFileAndSave(preloadFile, langLink, translatedFileName);
translatedFileName = "";
langLink = "";
}
} else {
langLink = "";
translatedFileName = "";
}
preloadFileAndSave(preloadFile, langLink, translatedFileName);
}
}
}
function getFilePathForPreloads(path) {
return path.replace(`/data/${gameId}`, "").replace("/Language", "");
}
let preloadsGameLang = "default";
let preloadsLangDetected = false;
let prevLoadedFiles = [];
function initPreloads() {
const ca = wasmImports.ca;
wasmImports.ca = function (url, file, request, param, arg, onload, onerror, onprogress) {
if (preloadFiles) {
const _url = UTF8ToString(url);
const filePath = getFilePathForPreloads(decodeURIComponent(_url));
gameLoadedFiles.add(filePath);
// Game language detection
if (_url.indexOf("/Language") !== -1 && !filePath.endsWith("/meta.ini"))
preloadsGameLang = filePath.substring(1, filePath.substring(1, filePath.length).indexOf("/") + 1);
if (filePath.indexOf("/Title") !== -1) {
gameLoadedFiles = new Set(Object.keys(preloadedFiles));
preloadsGameLang = "default";
for (let prevFile of prevLoadedFiles) {
if (prevFile.indexOf("/Language") !== -1) {
let folderFilePath = getFilePathForPreloads(decodeURIComponent(prevFile));
preloadsGameLang = folderFilePath.substring(1, folderFilePath.substring(1, folderFilePath.length).indexOf("/") + 1);
break
}
}
if (preloadsLangDetected)
preloadFilesFromMapId("title");
preloadsLangDetected = true;
}
prevLoadedFiles.push(_url);
if (prevLoadedFiles.length > 4)
prevLoadedFiles.shift(1);
if (filePath.endsWith(".lmu"))
preloadFilesFromMapId("event" + filePath.replace("/Map", "").replace(".lmu", ""));
if (preloadedFiles.hasOwnProperty(filePath)) {
runtimeKeepalivePush();
let _file = UTF8ToString(file);
_file = PATH_FS.resolve(_file);
const index = _file.lastIndexOf("/");
const http = new XMLHttpRequest();
const handle = wget.getNextWgetRequestHandle();
const destinationDirectory = PATH.dirname(_file);
try {
FS.unlink(_file);
} catch (e) {}
FS.mkdirTree(destinationDirectory);
FS.createDataFile(_file.substr(0, index), _file.substr(index + 1), new Uint8Array(preloadedFiles[filePath]), true, true, false);
if (onload) {
withStackSave((function() {
((a1,a2,a3)=>dynCall_viii.apply(null, [onload, a1, a2, a3]))(handle, arg, stringToUTF8OnStack(_file));
}));
}
if (filePath !== "/CharSet/syujinkou_effect_action_01.png")
delete preloadedFiles[filePath];
wget.wgetRequests[handle] = http;
return handle;
}
}
ca(url, file, request, param, arg, onload, onerror, onprogress);
};
}