91 lines
3.7 KiB
JavaScript
91 lines
3.7 KiB
JavaScript
(() => {
|
|
let search = window.location.search;
|
|
// search = search.substring(1, search.length);
|
|
// search.split("&").forEach((itemString: string, index: number) => {
|
|
// const itemValue = itemString.split("=");
|
|
// const key = itemValue[0];
|
|
// const value = itemValue[1];
|
|
// });
|
|
if (!search)
|
|
return;
|
|
search = search.toLowerCase();
|
|
if (!search || search.indexOf('prj=true') < 0)
|
|
return;
|
|
const debug = search.indexOf('prjdebug=true') >= 0;
|
|
const noCacheRex = /\?/;
|
|
const textDownloading = {};
|
|
let itemConfig = null;
|
|
let currentRoomType = -1;
|
|
const log = (text) => debug && console.log(`[PackageRoomJson]${text}`);
|
|
const urlAppendTimestamp = (url) => {
|
|
if (cc.game.config['noCache'] && typeof url === 'string') {
|
|
const timestamp = new Date().getTime();
|
|
url += (noCacheRex.test(url) ? '&_t=' : '?_t=') + timestamp;
|
|
}
|
|
return url;
|
|
};
|
|
const downloadText = (url, callback) => {
|
|
if (textDownloading[url]) {
|
|
textDownloading[url].push(callback);
|
|
return;
|
|
}
|
|
textDownloading[url] = [callback];
|
|
url = urlAppendTimestamp(url);
|
|
const xhr = cc.loader.getXMLHttpRequest(), errInfo = `Load text file failed: ${url}`;
|
|
xhr.open('GET', url, true);
|
|
if (xhr.overrideMimeType)
|
|
xhr.overrideMimeType('text\/plain; charset=utf-8');
|
|
xhr.onload = () => {
|
|
let error = null;
|
|
let text = null;
|
|
if (xhr.readyState === 4)
|
|
if (xhr.status === 200 || xhr.status === 0)
|
|
text = xhr.responseText;
|
|
else
|
|
error = { status: xhr.status, errorMessage: `${errInfo}(wrong status)` };
|
|
else
|
|
error = { status: xhr.status, errorMessage: errInfo + `${errInfo}(wrong readyState)` };
|
|
while (textDownloading[url].length) {
|
|
const callback = textDownloading[url].shift();
|
|
callback(error, text);
|
|
}
|
|
textDownloading[url] = null;
|
|
delete textDownloading[url];
|
|
};
|
|
xhr.onerror = () => callback({ status: xhr.status, errorMessage: `${errInfo}(error)` });
|
|
xhr.ontimeout = () => callback({ status: xhr.status, errorMessage: `${errInfo}(time out)` });
|
|
xhr.send(null);
|
|
};
|
|
const downloadJson = (item, callback) => {
|
|
const url = item.url;
|
|
if (Global.roomType && Global.roomType !== -1 && currentRoomType !== Global.roomType) {
|
|
const configUrl = `${url.substring(0, url.indexOf('res/'))}res_package/`;
|
|
const itemConfigFileUrl = `${configUrl}config_${Global.roomType}.json`;
|
|
log(`Download Item Config: ${itemConfigFileUrl}`);
|
|
downloadText(itemConfigFileUrl, (error, text) => {
|
|
currentRoomType = Global.roomType;
|
|
itemConfig = null;
|
|
if (error)
|
|
log(`Download Item Config Error: ${error.errorMessage}`);
|
|
else
|
|
itemConfig = JSON.parse(text);
|
|
downloadJson(item, callback);
|
|
});
|
|
}
|
|
else {
|
|
const fileNameIndex = url.lastIndexOf('/') + 1;
|
|
const uuid = url.substring(fileNameIndex, url.indexOf('.', fileNameIndex));
|
|
const cache = itemConfig ? itemConfig[uuid] : null;
|
|
if (cache) {
|
|
log(`Return ${uuid} Text From Config${currentRoomType}`);
|
|
callback(null, itemConfig[uuid]);
|
|
}
|
|
else {
|
|
downloadText(url, callback);
|
|
}
|
|
}
|
|
};
|
|
cc.loader.downloader.addHandlers({ 'json': downloadJson });
|
|
log(`ScriptAdded`);
|
|
})();
|