no message
This commit is contained in:
55
scratch-gui/src/lib/unity-cdn.js
Normal file
55
scratch-gui/src/lib/unity-cdn.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import CONFIG from '../playground/config.js';
|
||||
|
||||
/**
|
||||
* 纯 CDN 根路径。
|
||||
* 方案 A(默认):始终返回 unityCdnRemote,浏览器直连 OSS(须配置 OSS CORS)。
|
||||
* 方案 B:unityCdnUseSiteProxy / unityCdnUseDevProxy 为 true 时走 /cdn-unity 同源代理。
|
||||
*/
|
||||
export function getUnityCdnRoot() {
|
||||
if (!CONFIG.usecdn) {
|
||||
return '/unity';
|
||||
}
|
||||
const useProxy = CONFIG.unityCdnUseSiteProxy || CONFIG.unityCdnUseDevProxy;
|
||||
if (useProxy && CONFIG.unityCdnDevProxy) {
|
||||
return CONFIG.unityCdnDevProxy;
|
||||
}
|
||||
return CONFIG.unityCdnRemote || CONFIG.unitycdndir;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[unity-cdn] levels-database.json.br 预取失败,尝试 .json', e);
|
||||
}
|
||||
}
|
||||
|
||||
const res = await fetch(jsonUrl);
|
||||
if (!res.ok) {
|
||||
throw new Error(`levels-database HTTP ${res.status}: ${jsonUrl}`);
|
||||
}
|
||||
window.__tfrhLevelsDatabaseJson = await res.json();
|
||||
}
|
||||
Reference in New Issue
Block a user