-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (62 loc) · 2.84 KB
/
main.js
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
module.exports = {
load() {
// 当 package 被正确加载的时候执行
},
unload() {
// 当 package 被正确卸载的时候执行
},
messages: {
'showPanel'() {
Editor.Panel.open('hot-update-tools');
},
'test'(event, args) {
console.log("1111111");
Editor.log(args);
Editor.Ipc.sendToPanel('hot-update-tools', 'hot-update-tools:onBuildFinished');
},
// 当插件构建完成的时候触发
'editor:build-finished': function (event, target) {
let Fs = require("fire-fs");
let Path = require("fire-path");
Editor.log("[HotUpdateTools] build platform:" + target.platform);
if (target.platform === "web-mobile" || target.platform === "web-desktop") {
Editor.log("[HotUpdateTools] don't need update main.js");
} else {
let root = Path.normalize(target.dest);
let url = Path.join(root, "main.js");
Fs.readFile(url, "utf8", function (err, data) {
if (err) {
throw err;
}
let newStr =
"(function () { \n" +
"\n" +
" if (cc && cc.sys.isNative) { \n" +
" var hotUpdateSearchPaths = cc.sys.localStorage.getItem('HotUpdateSearchPaths'); \n" +
" if (hotUpdateSearchPaths) { \n" +
" jsb.fileUtils.setSearchPaths(JSON.parse(hotUpdateSearchPaths)); \n" +
" console.log('[main.js] 热更新SearchPath: ' + JSON.parse(hotUpdateSearchPaths));\n" +
" }else {\n" +
" console.log('[main.js] 未获取到热更新资源路径!');\n" +
" }\n" +
" }else {\n" +
" console.log('[main.js] 不是native平台!');\n" +
" }\n";
let newData = data.replace("(function () {", newStr);
Fs.writeFile(url, newData, function (error) {
if (err) {
throw err;
}
Editor.log("[HotUpdateTools] SearchPath updated in built main.js for hot update");
});
});
}
let time = new Date().getTime();
// 通知panel更新时间
Editor.Ipc.sendToPanel('hot-update-tools', 'hot-update-tools:onBuildFinished', time);
// 写入本地
let CfgUtil = Editor.require('packages://hot-update-tools/core/CfgUtil.js');
CfgUtil.updateBuildTimeByMain(time);
}
},
};