123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- const Fs = require('fire-fs');
- const Path = require('fire-path');
- const FsExtra = require('fs-extra');
- module.exports = {
- load () {
- },
- unload () {
- },
- messages: {
- showPanel () {
- Editor.Panel.open('hot-update-tools');
- // let engineCode = "if(jsb.fileUtils.isDirectoryExist(jsb.fileUtils.getWritablePath()+\"blackjack-remote-asset/assets/\"+nameOrUrl+\"/assets\")){url += \"/assets\";}\n";
- // let findCode = "if(jsb.fileUtils.isDirectoryExist(jsb.fileUtils.getWritablePath()+";
- // let codeUrl = Editor.url('packages://hot-update-tools/../../build/jsb-link');
- // let root = Path.normalize(codeUrl);
- // let url = Path.join(root, 'jsb-adapter/jsb-engine.js');
- // let data = Fs.readFileSync(url, 'utf8');
- // let findStr = "var config = \"\".concat(url, \"/config.\").concat(version ? version + '.' : '', \"json\");";
- // let findJ = data.indexOf(engineCode);
-
- // Editor.log(findJ);
- // if(findJ != -1){
- // Editor.log(`代码片段已存在: ${engineCode}, 无需添加`);
- // return
- // }
- // let findI = data.indexOf(findStr);
- // let startStr = data.slice(0, findI);
- // let lastStr = data.slice(findI);
- // Editor.log(findI);
- // Editor.log(lastStr);
- // Editor.log(data.length);
- // let newData = startStr + engineCode + lastStr;
- // Editor.log(newData);
- // Editor.log(newData.length);
- // Fs.writeFileSync(url, newData);
- // Editor.log(newData);
- // Editor.log(newData.length);
- // const CfgUtil = Editor.require('packages://hot-update-tools/core/CfgUtil.js');
- // const Util = Editor.require('packages://hot-update-tools/core/Util.js');
- // const OutPut = Editor.require('packages://hot-update-tools/core/OutPut.js');
- // let data = CfgUtil._getConfigData();
- // Editor.log(data)
- // if(data) {
- // let subPackArr = data.subPackArr;
- // let projectDir = data.resourceRootDir;
- // Editor.log(`项目路径:${projectDir}`)
- // let keys = Object.keys(subPackArr);
- // Editor.log(keys)
- // for(let i = 0; i < keys.length; ++i) {
- // const name = keys[i];
- // const element = subPackArr[name];
- // Editor.log(name)
- // Editor.log(element)
- // if(element) {
- // let destDir = Path.join(projectDir, 'assets\\'+name);
- // Editor.log(destDir)
- // if(Fs.existsSync(destDir)) {
- // Editor.log(`${name} 存在`);
- // let assetsDir = Path.join(destDir, 'assets');
- // if(!Fs.existsSync(assetsDir)) {
- // Fs.mkdirSync(assetsDir);
- // }
- // FsExtra.emptyDirSync(assetsDir);
- // FsExtra.copySync(Path.join(assetsDir, 'import'), Path.join(destDir, 'import'));
- // // FsExtra.move(Path.join(assetsDir, 'native'), Path.join(destDir, 'native'));
- // FsExtra.copyFileSync(Path.join(assetsDir, 'config.json'), Path.join(destDir, 'config.json'));
- // }else{
- // Editor.log(`${name} 不存在`);
- // }
- // }
- // }
- // }
- },
- 'editor:build-finished': (event, target) => {
- // Editor.log('[HotUpdateTools] build platform:' + target.platform);
- if (
- target.platform === 'win32' ||
- target.platform === 'android' ||
- target.platform === 'ios' ||
- target.platform === 'mac'
- ) {
- //修改main.js
- let codeUrl = Editor.url('packages://hot-update-tools/main_code.js');
- if (Fs.existsSync(codeUrl)) {
- const injectScript = Fs.readFileSync(codeUrl, 'utf-8');
- let root = Path.normalize(target.dest);
- let url = Path.join(root, 'main.js');
- let data = Fs.readFileSync(url, 'utf8');
- let newData = injectScript + data;
- Fs.writeFileSync(url, newData);
- Editor.log(
- '[HotUpdateTools] SearchPath updated in built main.js for hot update',
- );
- let time = new Date().getTime();
- 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);
- } else {
- Editor.log(`文件丢失: ${codeUrl}`);
- }
- //修改jsb-engine.js
- let engineCode = "if(jsb.fileUtils.isDirectoryExist(jsb.fileUtils.getWritablePath()+\"blackjack-remote-asset/assets/\"+nameOrUrl+\"/assets\")){url += \"/assets\";}\n";
- let root = Path.normalize(target.dest);
- let url = Path.join(root, 'jsb-adapter/jsb-engine.js');
- let data = Fs.readFileSync(url, 'utf8');
- let findStr = "var config = \"\".concat(url, \"/config.\").concat(version ? version + '.' : '', \"json\");";
- let findJ = data.indexOf(engineCode);
- if(findJ != -1){
- Editor.log(`代码片段已存在: ${engineCode}, 无需添加`);
- return
- }
- let findI = data.indexOf(findStr);
- // Editor.log(findI);
- let startStr = data.slice(0, findI);
- let lastStr = data.slice(findI);
- let newData = startStr + engineCode + lastStr;
- // setTimeout(() => {
- // Fs.writeFileSync(url, newData);
- // }, 2000);
- Fs.writeFileSync(url, newData);
- Editor.log(`代码片段添加完成`);
-
- // Editor.log(data);
- // Editor.log(engineCode);
- } else {
- Editor.log(
- `[HotUpdateTools] don't need update main.js, platform: ${target.platform}`,
- );
- }
- },
- },
- };
|