From cae6ed79f3c75a0a7191323e68f5c31502f21429 Mon Sep 17 00:00:00 2001 From: Patrice Jiang <397136899@qq.com> Date: Fri, 29 Jul 2022 17:06:05 +0800 Subject: [PATCH] rename jsb to native --- README.md | 2 +- assets/hotupdate/HotUpdate.ts | 51 ++++++++++++++-------------- assets/hotupdate/HotUpdate.ts.meta | 3 +- assets/hotupdate/UpdatePanel.ts.meta | 3 +- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 25590ae..562b1a0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Cocos Creator 中的热更新主要源于 Cocos 引擎中的 AssetsManager 模 **服务端和本地均保存完整版本的游戏资源**,热更新过程中通过比较服务端和本地版本的差异来决定更新哪些内容。这样即可天然支持跨版本更新,比如本地版本为 A,远程版本是 C,则直接更新 A 和 C 之间的差异,并不需要生成 A 到 B 和 B 到 C 的更新包,依次更新。所以,在这种设计思路下,新版本的文件以离散的方式保存在服务端,更新时以文件为单位下载。 -除此之外,由于 WEB 版本可以通过服务器直接进行版本更新,所以资源热更新只适用于原生发布版本。AssetsManager 类也只在 jsb 命名空间下,在使用的时候需要注意判断运行环境。 +除此之外,由于 WEB 版本可以通过服务器直接进行版本更新,所以资源热更新只适用于原生发布版本。AssetsManager 类也只在 native 命名空间下,在使用的时候需要注意判断运行环境。 ## Manifest 文件 diff --git a/assets/hotupdate/HotUpdate.ts b/assets/hotupdate/HotUpdate.ts index fb69320..1606db0 100644 --- a/assets/hotupdate/HotUpdate.ts +++ b/assets/hotupdate/HotUpdate.ts @@ -1,6 +1,5 @@ import { UpdatePanel } from './UpdatePanel'; - -const jsb = (window).jsb; +import {native } from 'cc'; // Custom manifest removed the following assets: // 1. res/raw-assets/2a/2a40e5e7-4c4a-4350-9e5d-76757755cdd2.png @@ -134,7 +133,7 @@ export class HotUpdate extends Component { private _updating = false; private _canRetry = false; private _storagePath = ''; - private _am: jsb.AssetsManager = null!; + private _am: native.AssetsManager = null!; private _checkListener = null; private _updateListener = null; private _failCount = 0; @@ -143,17 +142,17 @@ export class HotUpdate extends Component { checkCb(event: any) { console.log('Code: ' + event.getEventCode()); switch (event.getEventCode()) { - case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST: + case native.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST: this.panel.info.string = "No local manifest file found, hot update skipped."; break; - case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST: - case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST: + case native.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST: + case native.EventAssetsManager.ERROR_PARSE_MANIFEST: this.panel.info.string = "Fail to download manifest file, hot update skipped."; break; - case jsb.EventAssetsManager.ALREADY_UP_TO_DATE: + case native.EventAssetsManager.ALREADY_UP_TO_DATE: this.panel.info.string = "Already up to date with the latest remote version."; break; - case jsb.EventAssetsManager.NEW_VERSION_FOUND: + case native.EventAssetsManager.NEW_VERSION_FOUND: this.panel.info.string = 'New version found, please try to update. (' + Math.ceil(this._am.getTotalBytes() / 1024) + 'kb)'; this.panel.checkBtn.active = false; this.panel.fileProgress.progress = 0; @@ -173,11 +172,11 @@ export class HotUpdate extends Component { var needRestart = false; var failed = false; switch (event.getEventCode()) { - case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST: + case native.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST: this.panel.info.string = 'No local manifest file found, hot update skipped.'; failed = true; break; - case jsb.EventAssetsManager.UPDATE_PROGRESSION: + case native.EventAssetsManager.UPDATE_PROGRESSION: this.panel.byteProgress.progress = event.getPercent(); this.panel.fileProgress.progress = event.getPercentByFile(); @@ -190,29 +189,29 @@ export class HotUpdate extends Component { // cc.log(event.getPercent()/100 + '% : ' + msg); } break; - case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST: - case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST: + case native.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST: + case native.EventAssetsManager.ERROR_PARSE_MANIFEST: this.panel.info.string = 'Fail to download manifest file, hot update skipped.'; failed = true; break; - case jsb.EventAssetsManager.ALREADY_UP_TO_DATE: + case native.EventAssetsManager.ALREADY_UP_TO_DATE: this.panel.info.string = 'Already up to date with the latest remote version.'; failed = true; break; - case jsb.EventAssetsManager.UPDATE_FINISHED: + case native.EventAssetsManager.UPDATE_FINISHED: this.panel.info.string = 'Update finished. ' + event.getMessage(); needRestart = true; break; - case jsb.EventAssetsManager.UPDATE_FAILED: + case native.EventAssetsManager.UPDATE_FAILED: this.panel.info.string = 'Update failed. ' + event.getMessage(); this.panel.retryBtn.active = true; this._updating = false; this._canRetry = true; break; - case jsb.EventAssetsManager.ERROR_UPDATING: + case native.EventAssetsManager.ERROR_UPDATING: this.panel.info.string = 'Asset update error: ' + event.getAssetId() + ', ' + event.getMessage(); break; - case jsb.EventAssetsManager.ERROR_DECOMPRESS: + case native.EventAssetsManager.ERROR_DECOMPRESS: this.panel.info.string = event.getMessage(); break; default: @@ -229,7 +228,7 @@ export class HotUpdate extends Component { this._am.setEventCallback(null!); this._updateListener = null; // Prepend the manifest's search path - var searchPaths = jsb.fileUtils.getSearchPaths(); + var searchPaths = native.fileUtils.getSearchPaths(); var newPaths = this._am.getLocalManifest().getSearchPaths(); console.log(JSON.stringify(newPaths)); Array.prototype.unshift.apply(searchPaths, newPaths); @@ -237,7 +236,7 @@ export class HotUpdate extends Component { // please refer to samples/js-tests/main.js for detailed usage. // !!! Re-add the search paths in main.js is very important, otherwise, new scripts won't take effect. localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths)); - jsb.fileUtils.setSearchPaths(searchPaths); + native.fileUtils.setSearchPaths(searchPaths); // restart game. setTimeout(() => { @@ -247,8 +246,8 @@ export class HotUpdate extends Component { } loadCustomManifest() { - if (this._am.getState() === jsb.AssetsManager.State.UNINITED) { - var manifest = new jsb.Manifest(customManifestStr, this._storagePath); + if (this._am.getState() === native.AssetsManager.State.UNINITED) { + var manifest = new native.Manifest(customManifestStr, this._storagePath); this._am.loadLocalManifest(manifest, this._storagePath); this.panel.info.string = 'Using custom manifest'; } @@ -269,7 +268,7 @@ export class HotUpdate extends Component { this.panel.info.string = 'Checking or updating ...'; return; } - if (this._am.getState() === jsb.AssetsManager.State.UNINITED) { + if (this._am.getState() === native.AssetsManager.State.UNINITED) { var url = this.manifestUrl.nativeUrl; this._am.loadLocalManifest(url); } @@ -287,7 +286,7 @@ export class HotUpdate extends Component { if (this._am && !this._updating) { this._am.setEventCallback(this.updateCb.bind(this)); - if (this._am.getState() === jsb.AssetsManager.State.UNINITED) { + if (this._am.getState() === native.AssetsManager.State.UNINITED) { var url = this.manifestUrl.nativeUrl; this._am.loadLocalManifest(url); } @@ -311,7 +310,7 @@ export class HotUpdate extends Component { if (!jsb) { return; } - this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'blackjack-remote-asset'); + this._storagePath = ((native.fileUtils ? native.fileUtils.getWritablePath() : '/') + 'blackjack-remote-asset'); console.log('Storage path for remote asset : ' + this._storagePath); // Setup your own version compare handler, versionA and B is versions in string @@ -341,12 +340,12 @@ export class HotUpdate extends Component { }; // Init with empty manifest url for testing custom manifest - this._am = new jsb.AssetsManager('', this._storagePath, this.versionCompareHandle); + this._am = new native.AssetsManager('', this._storagePath, this.versionCompareHandle); var panel = this.panel; // Setup the verification callback, but we don't have md5 check function yet, so only print some message // Return true if the verification passed, otherwise return false - this._am.setVerifyCallback(function (path: string, asset: any) { + this._am.setVerifyCallback(function (path: string, asset: any){ // When asset is compressed, we don't need to check its md5, because zip file have been deleted. var compressed = asset.compressed; // Retrieve the correct md5 value. diff --git a/assets/hotupdate/HotUpdate.ts.meta b/assets/hotupdate/HotUpdate.ts.meta index 24cd4e3..bf84feb 100644 --- a/assets/hotupdate/HotUpdate.ts.meta +++ b/assets/hotupdate/HotUpdate.ts.meta @@ -1,12 +1,11 @@ { - "ver": "4.0.22", + "ver": "4.0.23", "importer": "typescript", "imported": true, "uuid": "8ee7ab5e-ece5-4312-9876-aee14260b419", "files": [], "subMetas": {}, "userData": { - "importAsPlugin": false, "moduleId": "project:///assets/hotupdate/HotUpdate.js", "importerSettings": 7, "simulateGlobals": [] diff --git a/assets/hotupdate/UpdatePanel.ts.meta b/assets/hotupdate/UpdatePanel.ts.meta index c78d9c0..ef13854 100644 --- a/assets/hotupdate/UpdatePanel.ts.meta +++ b/assets/hotupdate/UpdatePanel.ts.meta @@ -1,12 +1,11 @@ { - "ver": "4.0.22", + "ver": "4.0.23", "importer": "typescript", "imported": true, "uuid": "d896f161-064f-40e7-b80b-32f123dca67b", "files": [], "subMetas": {}, "userData": { - "importAsPlugin": false, "moduleId": "project:///assets/hotupdate/UpdatePanel.js", "importerSettings": 7, "simulateGlobals": []