-
Notifications
You must be signed in to change notification settings - Fork 82
/
preloads.js
216 lines (201 loc) · 8.03 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
let preloadFiles;
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.replace(/\"\*\//g, '\"/'));
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));
}
const graphicPattern = /.(png|bmp|xyz)$/i;
const keepExtensionPattern = /.(po|lmu)$/i;
function preloadFileAndSave(link, languageLink, translatedFileName) {
// languageLink and translatedFileName is not used, since we depend on EasyRPG
// to automatically translate the paths to their translated counterparts.
if (link.startsWith('/'))
link = link.slice(1);
if (gameLoadedFiles.has(link))
return;
gameLoadedFiles.add(link);
let idx, file, dir;
if ((idx = link.lastIndexOf('/')) !== -1) {
file = link.slice(idx + 1);
dir = link.slice(0, idx)
} else {
file = link;
dir = '.';
}
const graphic = graphicPattern.test(file);
if (!keepExtensionPattern.test(file) && (idx = file.lastIndexOf('.')) !== -1)
file = file.slice(0, file.lastIndexOf('.'));
easyrpgPlayer.api.preloadFile(dir, file, graphic);
}
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")) {
translatedFileName = '/Language/' + preloadsGameLang + translatedFileName;
if (preloadFile.slow)
setTimeout(preloadFileAndSave, currentTimeout, translatedFileName, langLink, translatedFileName);
else
preloadFileAndSave(translatedFileName, 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('/Language/' + preloadsGameLang + translatedFileName, 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 prevLoadedFiles = [];
// EXTERNAL
function onRequestFile(_url) {
if (!preloadFiles) return;
const filePath = getFilePathForPreloads(decodeURIComponent(encodeURIComponent(_url)));
gameLoadedFiles.add(filePath);
// Game language detection
if (_url.indexOf("Language/") !== -1 && !filePath.endsWith("/meta.ini"))
preloadsGameLang = filePath.substring(0, filePath.substring(1, filePath.length).indexOf("/") + 1);
if (filePath.indexOf("Title/") !== -1) {
gameLoadedFiles.clear();
preloadsGameLang = 'default';
for (let prevFile of prevLoadedFiles) {
if (prevFile.indexOf("Language/") !== -1) {
let folderFilePath = getFilePathForPreloads(decodeURIComponent(prevFile));
preloadsGameLang = folderFilePath.substring(0, folderFilePath.substring(1, folderFilePath.length).indexOf("/") + 1);
break
}
}
preloadFilesFromMapId("title");
}
prevLoadedFiles.push(_url);
if (prevLoadedFiles.length > 4)
prevLoadedFiles.shift();
if (filePath.endsWith(".lmu")) {
preloadFilesFromMapId("event" + filePath.replace("./Map", "").replace(".lmu", ""));
}
}