[mod] 熱更新調整

This commit is contained in:
2022-09-04 12:01:59 +08:00
parent 5dce041429
commit 3846fa7844
8 changed files with 119 additions and 40 deletions

View File

@@ -1,4 +1,5 @@
import { CoroutineV2 } from "../Engine/CatanEngine/CoroutineV2/CoroutineV2";
import LocalStorageData from "../Engine/Data/LocalStorageData";
import UpdatePanel from "../UpdatePanel";
import BusinessTypeSetting from "../_BusinessTypeSetting/BusinessTypeSetting";
import Enum_HUDM from "./Enum_HUDM";
@@ -45,17 +46,29 @@ export default class HUDM extends cc.Component {
}
HUDM._instance = this;
this._updatePanel = params[0];
let packageUrl: string = `https://jianmiau.tk/Resources/App/JMKA/update/remote-assets/${BusinessTypeSetting.COMPILE_VERSION}`;
// let packageUrl: string = `https://jianmiau.tk/Resources/App/JMKA/update/remote-assets/${BusinessTypeSetting.COMPILE_VERSION}`;
let packageUrl: string = BusinessTypeSetting.UsePatch;
this.CheckCompileVersion();
this.CheckChangePatchUrl();
this._customManifest = JSON.stringify({
"packageUrl": packageUrl,
"remoteManifestUrl": `${packageUrl}/project.manifest`,
"remoteVersionUrl": `${packageUrl}/version.json`,
"version": "0.0",
"version": "0.0.0",
});
this._storagePath = `${(jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "./")}${this._path}`;
// 本地熱更目錄下已存在project.manifest則直接修改已存在的project.manifest
if (this._isChangeUrl) {
if (jsb.fileUtils.isFileExist(this._storagePath + "/project.manifest")) {
this._isChangeUrl = true;
this._modifyAppLoadUrlForManifestFile(this._storagePath, packageUrl);
}
}
this._versionCompareHandle = function (versionA: string, versionB: string): number {
// console.log("Ver A " + versionA + "VerB " + versionB);
let vA: string[] = versionA.split(".");
@@ -120,6 +133,25 @@ export default class HUDM extends cc.Component {
}
}
private _modifyAppLoadUrlForManifestFile(filePath: string, newBundleUrl: string): void {
let allpath: string[] = [filePath, filePath + "_temp"];
let manifestname: string[] = ["project.manifest", "project.manifest.temp"];
for (var i: number = 0; i < allpath.length; ++i) {
let path: string = `${allpath[i]}/${manifestname[i]}`;
if (jsb.fileUtils.isFileExist(path)) {
// console.log(`[HUD] modifyAppLoadUrlForManifestFile: 有下載的manifest文件直接修改熱更地址`);
// 修改project.manifest
let projectManifest: string = jsb.fileUtils.getStringFromFile(path);
let projectManifestObj: any = JSON.parse(projectManifest);
projectManifestObj.packageUrl = newBundleUrl;
projectManifestObj.remoteManifestUrl = newBundleUrl + "/project.manifest";
projectManifestObj.remoteVersionUrl = newBundleUrl + "/version.json";
let afterString: string = JSON.stringify(projectManifestObj);
jsb.fileUtils.writeStringToFile(afterString, path);
}
}
}
//#endregion
public *CheckUpdate(): IterableIterator<any> {
@@ -148,6 +180,42 @@ export default class HUDM extends cc.Component {
yield null;
}
let newBundleUrl: string = BusinessTypeSetting.UsePatch;
this._modifyAppLoadUrlForManifestFile(this._storagePath, newBundleUrl);
this._initAssetManaget();
let manifest: jsb.Manifest = new jsb.Manifest(this._customManifest, this._storagePath);
this._am.loadLocalManifest(manifest, this._storagePath);
if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
// this.tipsLabel.string = "Failed to load local manifest ...";
console.error("checkUpdate -> Failed to load local manifest ...");
return;
}
// 更新動態路徑後再跑一次
this._am.setEventCallback(this.checkCb.bind(this));
this._needUpdateData = null;
this._am.checkUpdate();
this._updating = true;
while (this._needUpdateData === null) {
yield null;
}
if (this._isChangeUrl && ((!this._needUpdateData.IsNeedUpdate && this._needUpdateData.TotalBytes !== "failed") || this._needUpdateData.TotalBytes === "0 B")) {
if (jsb.fileUtils.isFileExist(this._storagePath)) {
let isremoveDirectory: boolean = jsb.fileUtils.removeDirectory(this._storagePath);
let isremoveDirectory_temp: boolean = jsb.fileUtils.removeDirectory(this._storagePath + "_temp");
if (isremoveDirectory_temp) {
console.log(`removeDirectory: ${this._storagePath}_temp`);
}
if (isremoveDirectory) {
console.log(`removeDirectory: ${this._storagePath}`);
this._needUpdateData = null;
this._initAssetManaget();
this._needUpdateData = yield* this.CheckUpdate();
}
}
}
return this._needUpdateData;
}
@@ -184,7 +252,7 @@ export default class HUDM extends cc.Component {
this._updating = false;
if (failed) {
this._needUpdateData = new Enum_HUDM.NeedUpdateDataObj(false, null);
this._needUpdateData = new Enum_HUDM.NeedUpdateDataObj(false, "failed");
}
}
@@ -348,4 +416,31 @@ export default class HUDM extends cc.Component {
this._updateListener = null;
}
}
//#region 清除資料
/** 判斷更改編譯版號.清除BUNDLE記錄 */
public CheckCompileVersion(): void {
let oldCompileVersion: string = LocalStorageData.Instance.CompileVersion;
let newCompileVersion: string = BusinessTypeSetting.COMPILE_VERSION;
if (oldCompileVersion && oldCompileVersion !== newCompileVersion) {
// this.ClearBundleData();
console.warn(`change compile version. ${oldCompileVersion} -> ${newCompileVersion}`);
}
LocalStorageData.Instance.CompileVersion = BusinessTypeSetting.COMPILE_VERSION;
}
/** 判斷更改PATCH環境.清除BUNDLE記錄 */
public CheckChangePatchUrl(): void {
let oldBundleUrl: string = LocalStorageData.Instance.BundleUrl;
let newBundleUrl: string = BusinessTypeSetting.UsePatch;
if (oldBundleUrl && oldBundleUrl !== newBundleUrl) {
// this.ClearBundleData();
console.warn(`change patch url. ${oldBundleUrl} -> ${newBundleUrl}`);
this._isChangeUrl = true;
}
LocalStorageData.Instance.BundleUrl = BusinessTypeSetting.UsePatch;
}
//#endregion
}