loading.js
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var Network = require('Network');
var Application = require('Application');
var Common = require('Common');
var BusinessParameter = require('BusinessParameter');
var TVCanvas = require('TVCanvas');
var Log = require('Log');
var CCMediaPlayer = require('CCMediaPlayer');
//热更新相关
var UpdatePanel = require('UpdatePanel');
cc.Class({
extends: TVCanvas,
properties: {
//热更新弹窗
panel: UpdatePanel,
manifestUrl: {
type: cc.Asset,
default: null
},
},
onLoad: function () {
if (cc.sys.isNative) {
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "removeLaunchImage", "()V");
}
//播放器使用
let nodeVideoPlayer = new cc.Node('VideoPlayer');
let nodeBg = new cc.Node('Bg');
nodeBg.parent = nodeVideoPlayer;
nodeVideoPlayer.parent = cc.find("PlayerContainer");
//nodeVideoPlayer.addComponent(cc.IjkPlayer);
//nodeVideoPlayer.addComponent(cc.VitamioPlayer);
// nodeVideoPlayer.addComponent(cc.VideoPlayer);
//替换集成的播放器 fix jerry
nodeVideoPlayer.addComponent(CCMediaPlayer);
// let strLoadImagePath = "Texture/splash_black";
// Common.renderSpriteByLocalImage(strLoadImagePath, nodeBg.addComponent(cc.Sprite));
nodeVideoPlayer.parent.zIndex = cc.macro.MIN_ZINDEX;
cc.game.addPersistRootNode(nodeVideoPlayer.parent);
this.checkUpdate(); //热更新
if (!this._bAbleHotUpdate) {
// cc.director.loadScene("sceneMain");
}
},
checkUpdate: function () {
if (!cc.sys.isNative) { //不是原生的就不进行检查了
return;
}
//--------------onLoad--------------------
this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'blackjack-remote-asset');
cc.log('Storage path for remote asset : ' + this._storagePath);
// Setup your own version compare handler, versionA and B is versions in string
// if the return value greater than 0, versionA is greater than B,
// if the return value equals 0, versionA equals to B,
// if the return value smaller than 0, versionA is smaller than B.
this.versionCompareHandle = function (versionA, versionB) {
cc.log("JS Custom Version Compare: version A is " + versionA + ', version B is ' + versionB);
var vA = versionA.split('.');
var vB = versionB.split('.');
for (var i = 0; i < vA.length; ++i) {
var a = parseInt(vA[i]);
var b = parseInt(vB[i] || 0);
if (a === b) {
continue;
}
else {
return a - b;
}
}
if (vB.length > vA.length) {
return -1;
}
else {
return 0;
}
};
// Init with empty manifest url for testing custom manifest
this._am = new jsb.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, asset) {
// 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.
var expectedMD5 = asset.md5;
// asset.path is relative path and path is absolute.
var relativePath = asset.path;
// The size of asset file, but this value could be absent.
var size = asset.size;
if (compressed) {
panel.info.string = "Verification passed : " + relativePath;
return true;
}
else {
panel.info.string = "Verification passed : " + relativePath + ' (' + expectedMD5 + ')';
return true;
}
});
this.panel.info.string = 'Hot update is ready, please check or directly update.';
if (cc.sys.os === cc.sys.OS_ANDROID) {
// Some Android device may slow down the download process when concurrent tasks is too much.
// The value may not be accurate, please do more test and find what's most suitable for your game.
this._am.setMaxConcurrentTask(2);
this.panel.info.string = "Max concurrent tasks count have been limited to 2";
}
this.panel.fileProgress.progress = 0;
//----------------------------------------
if (this._updating) {
this.panel.info.string = 'Checking or updating ...';
return;
}
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
// Resolve md5 url
var url = this.manifestUrl.nativeUrl;
// cc.log("///////////"+this.manifestUrl);
if (cc.loader.md5Pipe) {
url = cc.loader.md5Pipe.transformURL(url);
}
this._am.loadLocalManifest(url);
}
if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
this.panel.info.string = 'Failed to load local manifest ...';
return;
}
this._am.setEventCallback(this.checkCb.bind(this));
this._am.checkUpdate();
this._updating = true;
},
checkCb: function (event) {
cc.log('Code: ' + event.getEventCode());
switch (event.getEventCode()) {
case jsb.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:
this.panel.info.string = "Fail to download manifest file, hot update skipped.";
break;
case jsb.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: //可以更新了
cc.log("可以更新了"); //处理焦点框跳转
this.panel.info.string = 'New version found, please try to update.';
this.panel.fileProgress.progress = 0;
// this.panel.byteProgress.progress = 0;
this._bAbleHotUpdate = true;
// this._iSceneStatus = 1; //焦点框跳转层级
// cc.find("update", this.node).active = true; //显示出来更新面板
// this._cFocus.show();
// let fiHotUpdateBtn = cc.find('update/update_panel/update_btn', this.node).getComponent(FocusInfo); //热更新按钮
// this._cFocus.flyFocus(this._fiCurrentFocus, fiHotUpdateBtn, Common.MOVE_DIRECTION_RIGHT, null, null);
break;
default:
return;
}
this._am.setEventCallback(null);
this._checkListener = null;
this._updating = false;
if (this._bAbleHotUpdate) {
this.hotUpdate(); //放在这里可以静默升级
}else{
cc.director.loadScene("sceneMain");
}
},
hotUpdate: function () {
cc.log("hotUpdate start...");
//隐藏更新说明,显示更新进度条
// cc.find("update/update_panel/NewLabel", this.node).active = false;
// cc.find("update/update_panel/versionLabel", this.node).active = false;
// cc.find("update/update_panel/update_info", this.node).active = false;
// cc.find("update/update_panel/ProgressLabel", this.node).active = true;
// cc.find("update/update_panel/fileProgress", this.node).active = true;
// cc.find("update/update_panel/filep", this.node).active = true;
if (this._am && !this._updating) {
this._am.setEventCallback(this.updateCb.bind(this));
if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
// Resolve md5 url
var url = this.manifestUrl.nativeUrl;
if (cc.loader.md5Pipe) {
url = cc.loader.md5Pipe.transformURL(url);
}
this._am.loadLocalManifest(url);
}
this._failCount = 0;
this._am.update();
// this.panel.updateBtn.active = false;
this._updating = true;
}
},
updateCb: function (event) {
var needRestart = false;
var failed = false;
switch (event.getEventCode()) {
case jsb.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:
// this.panel.byteProgress.progress = event.getPercent();
this.panel.fileProgress.progress = event.getPercentByFile();
this.panel.fileLabel.string = event.getDownloadedFiles() + ' / ' + event.getTotalFiles();
// this.panel.byteLabel.string = event.getDownloadedBytes() + ' / ' + event.getTotalBytes();
var msg = event.getMessage();
if (msg) {
this.panel.info.string = 'Updated file: ' + msg;
// cc.log(event.getPercent()/100 + '% : ' + msg);
}
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.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:
this.panel.info.string = 'Already up to date with the latest remote version.';
failed = true;
break;
case jsb.EventAssetsManager.UPDATE_FINISHED:
this.panel.info.string = 'Update finished. ' + event.getMessage();
needRestart = true;
break;
case jsb.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:
this.panel.info.string = 'Asset update error: ' + event.getAssetId() + ', ' + event.getMessage();
break;
case jsb.EventAssetsManager.ERROR_DECOMPRESS:
this.panel.info.string = event.getMessage();
break;
default:
break;
}
if (failed) {
this._am.setEventCallback(null);
this._updateListener = null;
this._updating = false;
}
if (needRestart) {
this._am.setEventCallback(null);
this._updateListener = null;
// Prepend the manifest's search path
var searchPaths = jsb.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.
cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
jsb.fileUtils.setSearchPaths(searchPaths);
cc.audioEngine.stopAll();
cc.game.restart();
}
},
});