95 lines
3.8 KiB
TypeScript
95 lines
3.8 KiB
TypeScript
|
|
(() => {
|
|
|
|
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: { [url: string]: DownloadCallback[] } = {};
|
|
let itemConfig: { [uuid: string]: string } = null;
|
|
let currentRoomType: number = -1;
|
|
|
|
const log = (text: string) => debug && console.log(`[PackageRoomJson]${text}`)
|
|
|
|
const urlAppendTimestamp = (url: string | any) => {
|
|
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: string, callback: DownloadCallback): void => {
|
|
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: DownloadError = null;
|
|
let text: string | null = 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: string = 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: DownloadError, text?: string) => {
|
|
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`);
|
|
|
|
})(); |