Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename jsb to native #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Cocos Creator 中的热更新主要源于 Cocos 引擎中的 AssetsManager 模

**服务端和本地均保存完整版本的游戏资源**,热更新过程中通过比较服务端和本地版本的差异来决定更新哪些内容。这样即可天然支持跨版本更新,比如本地版本为 A,远程版本是 C,则直接更新 A 和 C 之间的差异,并不需要生成 A 到 B 和 B 到 C 的更新包,依次更新。所以,在这种设计思路下,新版本的文件以离散的方式保存在服务端,更新时以文件为单位下载。

除此之外,由于 WEB 版本可以通过服务器直接进行版本更新,所以资源热更新只适用于原生发布版本。AssetsManager 类也只在 jsb 命名空间下,在使用的时候需要注意判断运行环境。
除此之外,由于 WEB 版本可以通过服务器直接进行版本更新,所以资源热更新只适用于原生发布版本。AssetsManager 类也只在 native 命名空间下,在使用的时候需要注意判断运行环境。

## Manifest 文件

Expand Down
51 changes: 25 additions & 26 deletions assets/hotupdate/HotUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { UpdatePanel } from './UpdatePanel';

const jsb = (<any>window).jsb;
import {native } from 'cc';

// Custom manifest removed the following assets:
// 1. res/raw-assets/2a/2a40e5e7-4c4a-4350-9e5d-76757755cdd2.png
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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:
Expand All @@ -229,15 +228,15 @@ 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);
// This value will be retrieved and appended to the default search path during game startup,
// 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(() => {
Expand All @@ -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';
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions assets/hotupdate/HotUpdate.ts.meta
Original file line number Diff line number Diff line change
@@ -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": []
Expand Down
3 changes: 1 addition & 2 deletions assets/hotupdate/UpdatePanel.ts.meta
Original file line number Diff line number Diff line change
@@ -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": []
Expand Down