/* eslint-disable no-console */ import { _decorator, assetManager, AssetManager, Component, director } from "cc"; import HotupdateConfig from "@max-studio/hotupdate/HotupdateConfig"; import { HotupdateEventData } from "@max-studio/hotupdate/HotupdateEvent"; import { HotupdateInstance } from "@max-studio/hotupdate/HotupdateInstance"; import { HotupdateState } from "@max-studio/hotupdate/HotupdateState"; import { DialogBox } from "./DialogBox"; import { HotupdateLoading } from "./HotupdateLoading"; const { ccclass, property } = _decorator; @ccclass("Startup") export class Startup extends Component { @property(HotupdateLoading) private loading!: HotupdateLoading; protected onLoad(): void { HotupdateInstance.getInstance().register(this.onHotupdateEvent.bind(this)); } protected onDestroy(): void { HotupdateInstance.getInstance().unregister(this.onHotupdateEvent.bind(this)); } private onHotupdateEvent(data: HotupdateEventData) { console.log("HotupdateEvent - msg:", data.msg, "state:", data.state); switch (data.state) { case HotupdateState.ERROR_MANIFEST: DialogBox.showDialog({ title: "更新错误", content: "清单文件错误,请检查网络连接后重试", confirmText: "重试", showCancel: false, onConfirm: () => { // 重新尝试热更新 this.tryHotUpdate(); }, }); break; case HotupdateState.NEW_VERSION_FOUND: DialogBox.showDialog({ title: "发现新版本", content: "发现新的游戏内容,是否立即更新?", confirmText: "立即更新", cancelText: "稍后更新", showCancel: true, onConfirm: () => { // 开始热更新 this.startHotUpdate(); }, onCancel: () => { // 跳过更新,进入游戏 void this.enterHall(); }, }); break; case HotupdateState.UPDATE_FAILED: DialogBox.showDialog({ title: "更新失败", content: data.msg || "更新失败,请检查网络连接", confirmText: "重试", cancelText: "跳过", showCancel: true, onConfirm: () => { // 重新尝试更新 this.retryUpdate(); }, onCancel: () => { // 跳过更新,进入游戏 void this.enterHall(); }, }); break; case HotupdateState.ALREADY_UP_TO_DATE: void this.enterHall(); break; case HotupdateState.UPDATE_FINISHED: // 更新成功,仅打印数据 console.log( "热更新完成 - state:", data.state, "msg:", data.msg, "timestamp:", new Date().toISOString(), ); void this.enterHall(); break; case HotupdateState.UPDATE_PROGRESSION: // 更新进度,可以显示进度条或更新UI this.updateProgress(data); break; default: // 其他状态的处理 console.log("未处理的热更新状态 - state:", data.state, "msg:", data.msg); break; } } async start() { this.loading.updateProgressText("正在检查更新..."); void HotupdateInstance.getInstance().checkUpdate(new HotupdateConfig("main")); } /** * 重新尝试热更新 */ private tryHotUpdate(): void { this.loading.updateProgressText("正在检查更新..."); void HotupdateInstance.getInstance().checkUpdate(new HotupdateConfig("main")); } /** * 开始热更新 */ private startHotUpdate(): void { this.loading.updateProgressText("正在更新..."); HotupdateInstance.getInstance().hotUpdate(); } /** * 重试更新 */ private retryUpdate(): void { this.loading.updateProgressText("正在更新..."); HotupdateInstance.getInstance().hotUpdate(); } /** * 进入游戏 */ private async enterHall() { try { await this.loadBundles("max-core", "max-res", "configs"); const hallBundle = await this.loadBundle("hall"); hallBundle.loadScene("Hall", (err, data) => { if (err) { console.error("加载场景失败:", err); return; } this.loading.node.destroy(); console.log("加载场景成功", data); // 切换到 Hall 场景 director.runScene(data); }); } catch (err) { console.log("err", err); } } private async loadBundles(...bundles: string[]) { for (const bundle of bundles) { await this.loadBundle(bundle); } } private async loadBundle(bundle: string): Promise { return new Promise((resolve, reject) => { assetManager.loadBundle(bundle, (err, data) => { if (err) { reject(err); return; } console.log("loadBundle 成功:", bundle); resolve(data); }); }); } /** * 更新进度 */ private updateProgress(data: HotupdateEventData): void { // 获取进度信息 const instance = HotupdateInstance.getInstance(); const downloadedBytes = instance.getDownloadBytes(); const totalBytes = instance.getTotalBytes(); const progress = instance.getDownloadProgress(); console.log( "更新进度 - progress:", `${progress}%`, "downloaded:", `${(downloadedBytes / 1024 / 1024).toFixed(2)}MB`, "total:", `${(totalBytes / 1024 / 1024).toFixed(2)}MB`, "state:", data.state, ); this.loading.updateProgress(downloadedBytes, totalBytes); } private onLogin(isLogin: boolean) { if (isLogin) { // 登录成功 // const petConfig = PetConfigManager.getInstance().getConfig(1001); // LogUtils.log("Startup", "petConfig:", petConfig); // LogUtils.log("Startup", "PetConfig:", petConfig.bundle); } } public onClick() { // ProtoDefinitions.pkg1.Bar // LogUtils.log("onClick"); // StorageManager.getInstance().setData("Guide", "newbie_guide_test"); } }