no message
This commit is contained in:
@@ -20,36 +20,59 @@ export function getUnityCdnBuildRoot() {
|
||||
return `${getUnityCdnRoot()}/Build`;
|
||||
}
|
||||
|
||||
/** 预取关卡库并注入,避免 LevelDatabase 回退到 /unity/ 或 editor 相对路径 */
|
||||
export async function prefetchLevelsDatabase(cdnRoot) {
|
||||
const root = String(cdnRoot || getUnityCdnRoot()).replace(/\/$/, '');
|
||||
const jsonUrl = `${root}/levels-database.json`;
|
||||
window.__tfrhLevelsDatabaseUrl = jsonUrl;
|
||||
|
||||
if (window.__tfrhLevelsDatabaseJson) {
|
||||
return;
|
||||
}
|
||||
|
||||
async function fetchJsonWithBrotli(jsonUrl) {
|
||||
const brUrl = jsonUrl.replace(/\.json(\?.*)?$/i, '.json.br$1');
|
||||
|
||||
if (typeof DecompressionStream !== 'undefined') {
|
||||
try {
|
||||
const brRes = await fetch(brUrl);
|
||||
if (brRes.ok) {
|
||||
const text = await new Response(await brRes.arrayBuffer())
|
||||
.pipeThrough(new DecompressionStream('brotli'))
|
||||
.text();
|
||||
window.__tfrhLevelsDatabaseJson = JSON.parse(text);
|
||||
return;
|
||||
const ab = await brRes.arrayBuffer();
|
||||
let text = new TextDecoder().decode(ab);
|
||||
if (text.charCodeAt(0) !== 0x7b && text.charCodeAt(0) !== 0x5b) {
|
||||
text = await new Response(ab)
|
||||
.pipeThrough(new DecompressionStream('brotli'))
|
||||
.text();
|
||||
}
|
||||
return JSON.parse(text);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[unity-cdn] levels-database.json.br 预取失败,尝试 .json', e);
|
||||
console.warn('[unity-cdn] Brotli 预取失败,尝试 .json', jsonUrl, e);
|
||||
}
|
||||
}
|
||||
|
||||
const res = await fetch(jsonUrl);
|
||||
if (!res.ok) {
|
||||
throw new Error(`levels-database HTTP ${res.status}: ${jsonUrl}`);
|
||||
throw new Error(`HTTP ${res.status}: ${jsonUrl}`);
|
||||
}
|
||||
window.__tfrhLevelsDatabaseJson = await res.json();
|
||||
return res.json();
|
||||
}
|
||||
|
||||
/** 首屏只预取关卡库索引(分片模式);legacy 全量库作回退 URL */
|
||||
export async function prefetchLevelsDatabase(cdnRoot) {
|
||||
const root = String(cdnRoot || getUnityCdnRoot()).replace(/\/$/, '');
|
||||
const indexUrl = `${root}/levels-db-index.json`;
|
||||
const jsonUrl = `${root}/levels-database.json`;
|
||||
window.__tfrhLevelsDbIndexUrl = indexUrl;
|
||||
window.__tfrhLevelsDatabaseUrl = jsonUrl;
|
||||
|
||||
if (window.__tfrhLevelsDbIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const index = await fetchJsonWithBrotli(indexUrl);
|
||||
if (index?.mode === 'sharded' && index.shards?.length) {
|
||||
window.__tfrhLevelsDbIndex = index;
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[unity-cdn] levels-db-index 预取失败,启动时将回退全量库', e);
|
||||
}
|
||||
|
||||
if (window.__tfrhLevelsDatabaseJson) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.__tfrhLevelsDatabaseJson = await fetchJsonWithBrotli(jsonUrl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user