35 lines
2.0 KiB
TypeScript
35 lines
2.0 KiB
TypeScript
/** Native ZIP cache is installed by the WeChat build extension; editor/web use Creator directly. */
|
|
export default class ActivityBundleLoader {
|
|
private static runtime(): any { return typeof window !== "undefined" && (window as any).__activityBundles; }
|
|
/** Login and subsequent resource-check cycles prepare disk caches without blocking navigation. */
|
|
static beginCycle(): void {
|
|
const runtime = this.runtime();
|
|
if (!runtime) return;
|
|
runtime.beginCycle();
|
|
runtime.preloadAll().catch(error => cc.warn('[ActivityBundles] 后台资源准备失败', error));
|
|
}
|
|
/** Reserve a coherent dependency set without downloading any bundle. */
|
|
static retainGroup(name: string): () => void {
|
|
const runtime = this.runtime();
|
|
if (runtime) return runtime.retainGroup(name);
|
|
if (cc.sys && cc.sys.platform === cc.sys.WECHAT_GAME) throw new Error("活动分包加载器未初始化,请重新构建小游戏");
|
|
return () => {};
|
|
}
|
|
static prepareGroup(name: string): Promise<void> {
|
|
const runtime = this.runtime();
|
|
if (runtime) return runtime.prepareGroup(name);
|
|
try { this.retainGroup(name)(); return Promise.resolve(); } catch (error) { return Promise.reject(error); }
|
|
}
|
|
static ensureBundle(name: string): Promise<cc.AssetManager.Bundle> {
|
|
const runtime = this.runtime();
|
|
if (runtime) return runtime.ensureBundle(name);
|
|
if (cc.sys && cc.sys.platform === cc.sys.WECHAT_GAME)
|
|
return Promise.reject(new Error("活动分包加载器未初始化,请重新构建小游戏"));
|
|
return new Promise((resolve, reject) => cc.assetManager.loadBundle(name, (error, bundle) => error ? reject(error) : resolve(bundle)));
|
|
}
|
|
static async acquireBundle(name: string): Promise<{ bundle: cc.AssetManager.Bundle; release: () => void }> {
|
|
const runtime = this.runtime();
|
|
return runtime ? runtime.acquireBundle(name) : { bundle: await this.ensureBundle(name), release: () => {} };
|
|
}
|
|
}
|