kunpolibrary/src/hotupdate/HotUpdateManager.ts

178 lines
5.7 KiB
TypeScript
Raw Normal View History

2025-03-20 16:47:19 +08:00
/**
* @Author: Gongxh
* @Date: 2025-03-20
* @Description:
*/
2025-04-18 17:51:42 +08:00
import { native } from "cc";
import { ICheckUpdatePromiseResult } from "../interface/PromiseResult";
import { Platform } from "../kunpocc";
import { log } from "../tool/log";
import { HotUpdate, HotUpdateCode } from "./HotUpdate";
2025-03-20 16:47:19 +08:00
const TAG = "hotupdate:";
export class HotUpdateManager {
private static instance: HotUpdateManager;
public static getInstance(): HotUpdateManager {
if (!HotUpdateManager.instance) {
HotUpdateManager.instance = new HotUpdateManager();
}
return HotUpdateManager.instance;
}
2025-04-18 17:51:42 +08:00
/** 是否初始化了 */
private _isInitialized: boolean = false;
/** 本地manifest路径 */
private _manifestUrl: string = '';
2025-03-20 16:47:19 +08:00
/** 版本号 */
private _version: string = '';
2025-04-18 17:51:42 +08:00
/** 资源版本号 */
private _resVersion: string = null;
2025-03-20 16:47:19 +08:00
/** 可写路径 */
private _writablePath: string = '';
/** 是否正在更新 或者 正在检查更新 */
private _updating: boolean = false;
2025-04-19 15:10:54 +08:00
/** 更新实例 */
2025-04-18 17:51:42 +08:00
private _hotUpdate: HotUpdate = null;
/**
*
*/
public get writablePath(): string {
return this._writablePath;
}
2025-03-20 16:47:19 +08:00
/**
2025-04-18 17:51:42 +08:00
* manifest路径
2025-03-20 16:47:19 +08:00
*/
2025-04-18 17:51:42 +08:00
public get manifestUrl(): string {
return this._manifestUrl;
}
/**
*
*/
public get version(): string {
return this._version;
}
/**
* , 使
* @return 0
*/
public get resVersion(): string {
if (this._resVersion === null) {
this._resVersion = new HotUpdate().resVersion;
}
return this._resVersion;
}
public set resVersion(version: string) {
if (this._resVersion === null) {
this._resVersion = version;
2025-03-20 16:47:19 +08:00
}
2025-04-18 17:51:42 +08:00
}
/**
* 1.
* @param manifestUrl manifest文件地址 assets.nativeUrl
* @param version eg: 1.0.0
*/
public init(manifestUrl: string, version: string): void {
if (this._isInitialized) {
log(`${TAG} 热更新管理器不需要重复初始化`);
2025-03-20 16:47:19 +08:00
return;
}
2025-04-18 17:51:42 +08:00
this._isInitialized = true;
this._manifestUrl = manifestUrl;
2025-03-20 16:47:19 +08:00
this._version = version;
let writablePath = native?.fileUtils?.getWritablePath() || "";
if (!writablePath.endsWith("/")) {
writablePath += "/";
}
this._writablePath = `${writablePath}hot-update/${version}/`;
log(`${TAG}可写路径:${this._writablePath}`);
}
/**
2025-04-18 17:51:42 +08:00
*
*
* @return {Promise<ICheckUpdatePromiseResult>}
2025-03-20 16:47:19 +08:00
*/
2025-04-18 17:51:42 +08:00
public checkUpdate(): Promise<ICheckUpdatePromiseResult> {
return new Promise((resolve, reject) => {
if (!Platform.isNativeMobile) {
resolve({ code: HotUpdateCode.PlatformNotSupported, message: "当前平台不需要热更新" });
return;
}
if (!this._isInitialized) {
resolve({ code: HotUpdateCode.NotInitialized, message: "未初始化, 需要先调用init方法" });
return;
}
if (this._updating) {
resolve({ code: HotUpdateCode.Updating, message: "正在更新或者正在检查更新中" });
return;
}
this._updating = true;
2025-04-19 15:10:54 +08:00
this._hotUpdate = new HotUpdate();
this._hotUpdate.checkUpdate().then((res) => {
2025-04-18 17:51:42 +08:00
this._updating = false;
resolve(res);
});
});
2025-03-20 16:47:19 +08:00
}
/**
2025-04-18 17:51:42 +08:00
*
* @param res.skipCheck
2025-03-20 16:47:19 +08:00
* @param res.progress kb: 已下载的资源大小, total: 总资源大小 (kb)
2025-04-18 17:51:42 +08:00
* @param res.complete
2025-03-20 16:47:19 +08:00
*/
2025-04-18 17:51:42 +08:00
public startUpdate(res: { skipCheck: boolean, progress: (kb: number, total: number) => void, complete: (code: HotUpdateCode, message: string) => void }): void {
if (!Platform.isNativeMobile) {
res.complete(HotUpdateCode.PlatformNotSupported, "当前平台不需要热更新");
return;
}
if (!this._isInitialized) {
res.complete(HotUpdateCode.NotInitialized, "未初始化, 需要先调用init方法");
2025-03-20 16:47:19 +08:00
return;
}
2025-04-18 17:51:42 +08:00
if (this._updating) {
res.complete(HotUpdateCode.Updating, "正在更新或者正在检查更新");
2025-03-20 16:47:19 +08:00
return;
}
this._updating = true;
2025-04-19 15:10:54 +08:00
if (res.skipCheck && this._hotUpdate) {
this._hotUpdate.startUpdate({
skipCheck: res.skipCheck,
progress: res.progress,
complete: (code: HotUpdateCode, message: string) => {
this._updating = false;
res.complete(code, message);
}
});
} else {
this._hotUpdate = new HotUpdate();
this._hotUpdate.startUpdate({
skipCheck: false,
progress: res.progress,
complete: (code: HotUpdateCode, message: string) => {
this._updating = false;
res.complete(code, message);
}
});
}
2025-03-20 16:47:19 +08:00
}
/** 重试失败的资源 */
public retryUpdate(): void {
2025-04-18 17:51:42 +08:00
if (!this._hotUpdate) {
throw new Error(`${TAG} 使用前 必须使用过startUpdate方法`);
2025-03-20 16:47:19 +08:00
}
2025-04-18 17:51:42 +08:00
this._hotUpdate.retryUpdate();
2025-03-20 16:47:19 +08:00
}
}