123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557 |
- const Path=require("path"),Fs=require("fire-fs"),Encrypt=Editor.require("packages://encrypt-tool/panel/index.js"),CfgUtil=Editor.require("packages://hot-update-tools/core/CfgUtil.js"),Util=Editor.require("packages://hot-update-tools/core/Util.js"),OutPut=Editor.require("packages://hot-update-tools/core/OutPut.js"),GoogleAnalytics=Editor.require("packages://hot-update-tools/core/GoogleAnalytics.js"),Electron=require("electron");
- const pngBufferHeader = {
- bufBegin: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a],
- bufEnd: [0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82],
- suffix: '.png'
- };
- Vue.component("manifest-gen",{
- template:Fs.readFileSync(Editor.url("packages://hot-update-tools/panel/manifest-gen.html"),"utf-8"),
- mixins:[Editor.require("packages://hot-update-tools/panel/mixin.js")],
- data:()=>(
- {
- selectDestType:"",
- isUsedEncrypt:!1,
- version:"",
- serverRootDir:"",
- remoteServerVersion:"",
- hotAddressArray:[],
- resourceRootDir:"",
- genManifestDir:"",
- isShowUseAddrBtn:!1,
- isShowDelAddrBtn:!1,
- encryptSign: "",
- encryptPass: "",
- signKey: "civiTool",
- passWord: "civiTool2020",
- engineVersion:"",
- rootPath: "",
- cfgPath: "",
- totalPng: [],
- hasEncrypt: 0,
- hasBuildProj: !1,
- }
- ),
- computed:{
- isValidResDir(){
- return!(!this.resourceRootDir||!Fs.existsSync(this.resourceRootDir))
- }
- },
- created(){
- this.$nextTick(()=>{
- let e=CfgUtil.cfgData;
- e&&(this.selectDestType=e.selectDestType,this.isUsedEncrypt=e.isUsedEncrypt,this.version=e.version,this.serverRootDir=e.serverRootDir,this.resourceRootDir=e.resourceRootDir,this.hotAddressArray=e.hotAddressArray||[]),this.genManifestDir=OutPut.manifestDir,this._getRemoteServerVersion(),this._initResourceBuild()
- })
- },
- methods:{
- _initResourceBuild(){
- let e=Editor.Project.path,t=Path.join(e,"local/builder.json");
- if(Fs.existsSync(t)){
- let s=JSON.parse(Fs.readFileSync(t,"utf-8")),i=s.buildPath,r=Path.join(e,i),o=Path.join(r,`jsb-${s.template}`);
- if(!Fs.existsSync(o)){
- let e=Path.join(r,s.platform);Fs.existsSync(e)&&(o=e)
- }
- this._checkResourceRootDir(o)
- }else
- this.log("发现没有构建项目, 使用前请先构建项目!")
- //初始化加密资源
- this.initPluginCfg();
- },
- _isVersionPass(e,t){
- if(void 0===e||null===e||void 0===t||null===t)return!1;
- let s=e.split("."),i=t.split("."),r=s.length>i.length?s.length:i.length;
- for(let e=0;e<r;e++){
- let t=s[e],r=i[e];
- if(void 0===t&&void 0!==r)return!1;
- if(void 0!==t&&void 0===r)return!0;
- if(t&&r&&parseInt(t)>parseInt(r))return!0
- }
- return!1
- },
- _updateShowUseAddrBtn(){
- let e=this.$els.address.value;
- this.serverRootDir===e&&(this.isShowUseAddrBtn=!1)
- },
- _updateShowEncryptInfo() {
- if(this.isUsedEncrypt==1) {
- this.encryptSign = "签名:"+this.signKey;
- this.encryptPass = "密码:"+this.passWord;
- }
- },
- _addHotAddress(e){
- let t=!0;
- for(let s=0;s<this.hotAddressArray.length;s++){
- if(this.hotAddressArray[s]===e){
- t=!1;
- break
- }
- }
- t&&(this.hotAddressArray.push(e),this.log("[HotAddress]历史记录添加成功:"+e))
- },
- _getRemoteServerVersion(){
- if(this.serverRootDir.length<=0)return;
- this.remoteServerVersion=null;
- let e=this.serverRootDir+"/version.manifest",t=new XMLHttpRequest;
- t.onreadystatechange=(()=>{
- if(4===t.readyState&&t.status>=200&&t.status<400){
- let e=t.responseText,s=null;
- try{
- s=JSON.parse(e)
- }
- catch(e){
- return void this.log("获取远程版本号失败!")
- }
- this.remoteServerVersion=s.version
- }else t.status
- }),t.open("get",e,!0),t.setRequestHeader("If-Modified-Since","0"),t.send()
- },
- onClickGenCfg(e){
- GoogleAnalytics.eventCustom("GenManifest"),!this.version||this.version.length<=0?this.log("[生成] 版本号未填写"):!this.serverRootDir||this.serverRootDir.length<=0?this.log("[生成] 服务器地址未填写"):0!==this.resourceRootDir.length?this._checkResourceRootDir(this.resourceRootDir)&&(!this.genManifestDir||this.genManifestDir.length<=0?this.log("[生成] manifest文件生成地址未填写"):Fs.existsSync(this.genManifestDir)?(this._saveConfig(),this.onDoEncrypt(this.version,this.serverRootDir,this.resourceRootDir,this.genManifestDir)):this.log("[生成] manifest存储目录不存在: "+this.genManifestDir)):this.log("[生成] 请先指定 <build项目资源文件目录>")
- },
- onClickOpenVersionDir(){this.openDir(OutPut.versionsDir+'/'+this.selectDestType)},
- onOpenManifestDir(){this.openDir(this.genManifestDir)},
- onOpenResourceDir(){this.openDir(this.resourceRootDir)},
- onSelectResourceRootDir(){
- let e=Editor.Dialog.openFile({title:"选择构建后的根目录",defaultPath:Editor.projectInfo.path,properties:["openDirectory"]});
- if(-1!==e){
- let t=e[0];
- this._checkResourceRootDir(t)&&(this.resourceRootDir=t,this._saveConfig())
- }
- },
- onBtnClickDelSelectedHotAddress(){
- let e=this.$els.address.value;
- if(this.hotAddressArray.length>0){
- let t=!1;
- for(let s=0;s<this.hotAddressArray.length;){
- let i=this.hotAddressArray[s];
- i===e?(this.hotAddressArray.splice(s,1),t=!0,this.log("删除历史地址成功: "+i)):s++
- }
- t&&(this.isShowDelAddrBtn=!1,this.isShowUseAddrBtn=!1,this._saveConfig())
- }else this.log("历史地址已经为空")
- },
- onBtnClickUseSelectedHotAddress(){
- this.$els;
- let e=this.$els.address.value;
- this.serverRootDir=e,this.onInPutUrlOver(),this._updateShowUseAddrBtn()
- },
- onClickUse(){
- this.log("开启加密选项")
- this.isUsedEncrypt = 1;
- this._updateShowEncryptInfo();
- },
- onClickNotUse() {
- this.log("关闭加密选项")
- this.isUsedEncrypt = !1;
- },
- onBtnClickTest(){
- this.onChangeSelectType("Test");
- },
- onBtnClickBeta(){
- this.onChangeSelectType("Beta");
- },
- onBtnClickRelease(){
- this.onChangeSelectType("Release");
- },
- onChangeSelectType(e) {
- this.selectDestType = e;
- let h=OutPut.versionsDir+'/'+this.selectDestType;
- if(!Fs.existsSync(h)) {
- this.log(e+"文件夹不存在,自动创建文件夹!!!");
- Fs.mkdir(h,()=>{});
- }
- let d = h+'/UnZip/';
- if(!Fs.existsSync(d)) {
- this.log("UnZip文件夹不存在,自动创建文件夹!!!");
- Fs.mkdir(d,()=>{});
- }
- this._saveConfig();
- },
- onChangeSelectHotAddress(e){
- GoogleAnalytics.eventCustom("ChangeSelectHotAddress"),this.isShowUseAddrBtn=!0,this.isShowDelAddrBtn=!0,this._updateShowUseAddrBtn()
- },
- userLocalIP(){
- GoogleAnalytics.eventCustom("useLocalIP");
- let e=Editor.require("packages://hot-update-tools/core/Util.js").getLocalIP();
- e.length>0&&(this.serverRootDir="http://"+e,this.onInPutUrlOver(null))
- },
- onInPutUrlOver(e){
- let t=this.serverRootDir;
- if("http://"===t||"https://"===t||"http"===t||"https"===t||"http:"===t||"https:"===t)return;
- let s=t.indexOf("http://"),i=t.indexOf("https://");
- if(-1===s&&-1===i){
- /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/.test(t)||(this.log(t+" 不是以http://https://开头,或者不是网址, 已经自动修改"),this.serverRootDir="http://"+this.serverRootDir,this._getRemoteServerVersion())
- }else this._getRemoteServerVersion();
- this._addHotAddress(this.serverRootDir),this._updateShowUseAddrBtn(),this._saveConfig()
- },
- _saveConfig(){
- let e={selectDestType:this.selectDestType,isUsedEncrypt:this.isUsedEncrypt,version:this.version,serverRootDir:this.serverRootDir,resourceRootDir:this.resourceRootDir,genManifestDir:OutPut.manifestDir,localServerPath:this.localServerPath,hotAddressArray:this.hotAddressArray};
- CfgUtil.saveConfig(e)
- },
- onInputVersionOver(){
- let e=CfgUtil.cfgData.genVersion,t=(CfgUtil.cfgData.buildTime,CfgUtil.cfgData.genTime,this.remoteServerVersion);
- null!==t&&void 0!==t&&(this._isVersionPass(this.version,t)?(this.log("上次构建时版本号: "+e),this._isVersionPass(this.version,e)?this.log("版本通过验证!"):this.log("[Warning] 要构建的版本低于上次构建版本: "+this.version+"<="+e)):this.log("[Warning] version 填写的版本低于远程版本")),this._saveConfig()
- },
- onStopTouchEvent(e){e.preventDefault(),e.stopPropagation()},
- onBtnClickHelpDoc(){
- GoogleAnalytics.eventDoc();
- Electron.shell.openExternal("https://tidys.github.io/plugin-docs-oneself/docs/hot-update-tools/")
- },
- onBtnClickTellMe(){
- GoogleAnalytics.eventQQ();
- Electron.shell.openExternal("http://wpa.qq.com/msgrd?v=3&uin=774177933&site=qq&menu=yes")
- },
- _genVersion(e,t,s,i){
- this.log("[Build] 开始生成manifest配置文件....");
- let r={version:e,packageUrl:t,remoteManifestUrl:"",remoteVersionUrl:"",assets:{},searchPaths:[]};"/"===t[t.length-1]?(r.remoteManifestUrl=t+"project.manifest",r.remoteVersionUrl=t+"version.manifest"):(r.remoteManifestUrl=t+"/project.manifest",r.remoteVersionUrl=t+"/version.manifest");
- let o=i,n=s,l=(e,t)=>{
- let s=Fs.statSync(e);
- if(!s.isDirectory())return;
- let i,r,o,a,h,d=Fs.readdirSync(e);
- for(let c=0;c<d.length;++c)
- "."!==d[c][0]&&(i=Path.join(e,d[c]),(s=Fs.statSync(i)).isDirectory()?l(i,t):s.isFile()&&(r=s.size,o=require("crypto").createHash("md5").update(Fs.readFileSync(i)).digest("hex"),a=".zip"===Path.extname(i).toLowerCase(),h=(h=Path.relative(n,i)).replace(/\\/g,"/"),t[h=encodeURI(h)]={size:r,md5:o},a&&(t[h].compressed=!0)))
- };
- l(Path.join(n,"src"),r.assets),l(Path.join(n,Util.manifestResDir),r.assets);
- let a=Path.join(o,"project.manifest"),h=Path.join(o,"version.manifest");
- (e=>{
- try{
- Fs.mkdirSync(e)
- }catch(e){
- if("EEXIST"!==e.code)
- throw e
- }
- })(o),Fs.writeFileSync(a,JSON.stringify(r)),this.log("[Build] 生成 project.manifest成功"),delete r.assets,delete r.searchPaths,Fs.writeFileSync(h,JSON.stringify(r)),this.log("[Build] 生成 version.manifest成功"),this._packageVersion()
- },
- _packageDir(e,t){
- let s=Fs.readdirSync(e);
- for(let i=0;i<s.length;i++){
- let r=s[i],o=Path.join(e,r),n=Fs.statSync(o);
- n.isFile()?t.file(r,Fs.readFileSync(o)):n.isDirectory()&&this._packageDir(o,t.folder(r))
- }
- },
- _packageVersion(){
- let{manifestResDir:e}=Util;
- this.log("[Pack] 开始打包版本 ...");
- let t=new(Editor.require("packages://hot-update-tools/node_modules/jszip")),s=Path.join(this.genManifestDir,"version.manifest");
- t.file("version.manifest",Fs.readFileSync(s));
- let i=Path.join(this.genManifestDir,"project.manifest");
- t.file("project.manifest",Fs.readFileSync(i));
- let r=Path.join(this.resourceRootDir,"src");
- this._packageDir(r,t.folder("src"));
- let o=Path.join(this.resourceRootDir,e);
- this._packageDir(o,t.folder(e));
- let n=Fs.readFileSync(s,"utf-8"),l=JSON.parse(n).version;
- if(this.log("[Pack] 打包版本:"+l),l!==this.version)return void this.log("[Pack] 打包版本和当前填写的版本不一致,出现异常,停止打包!");
- let a="ver_"+(l=l.replace(".","_"))+".zip",h=OutPut.versionsDir+'/'+this.selectDestType,d=Path.join(h,a),f=Path.join(h,"UnZip");
- // Fs.copySync(dd)
- Fs.existsSync(d)&&(Fs.unlinkSync(d),this.log("[Pack] 发现该版本的zip, 已经删除!")),t.generateNodeStream({type:"nodebuffer",streamFiles:!0}).pipe(Fs.createWriteStream(d)).on("finish",()=>{this.log("[Pack] 打包成功: "+d)}).on("error",e=>{this.log("[Pack] 打包失败:"+e.message)})
- // Fs.existsSync(o)?Fs.existsSync(a)?(FsExtra.emptyDirSync(e),FsExtra.copySync(s,Path.join(e,"src")),FsExtra.copySync(l,Path.join(e,t)),FsExtra.copyFileSync(o,Path.join(e,"project.manifest")),FsExtra.copyFileSync(a,Path.join(e,"version.manifest")),this.log(`已经将热更包copy到: ${e}`),this._updateServerVersion()):this.log(a+"不存在, 请点击生成热更包"):this.log(o+"不存在, 请点击生成热更包")
- // this._copyFiles(f);
- },
- _copyFiles(f) {
- let{manifestResDir:t}=Util,{resourceRootDir:r}=this,{manifestDir:i}=OutPut;
- let s=Path.join(r,"src"),l=Path.join(r,t);
- if(!Fs.existsSync(r))return void
- this.log("资源目录不存在: "+r+", 请先构建项目");
- if(!Fs.existsSync(s))return void
- this.log(r+"不存在src目录, 无法拷贝文件");
- if(!Fs.existsSync(l))return void
- this.log(r+"不存在res目录, 无法拷贝文件");
- let o=Path.join(i,"project.manifest"),a=Path.join(i,"version.manifest");
- if(Fs.existsSync(o)){
- if(Fs.existsSync(a)){
- Fs.emptyDirSync(f);
- Fs.copySync(s,Path.join(f,"src"))
- Fs.copySync(l,Path.join(f,t))
- Fs.copyFileSync(o,Path.join(f,"project.manifest"))
- Fs.copyFileSync(a,Path.join(f,"version.manifest"))
- this.log(`已经将热更包copy到: ${f}`)
- // this._updateServerVersion()
- }else{
- this.log(a+"不存在, 请点击生成热更包")
- }
- }else{
- this.log(o+"不存在, 请点击生成热更包")
- }
- },
- _checkResourceRootDir(e){
- if(Fs.existsSync(e)){
- let t=Path.join(e,"src");
- if(!Fs.existsSync(t))return
- this.log(`没有发现 ${t}, 请先构建项目.`),!1;
- let s=["res","assets"];
- for(let t=0;t<s.length;t++){
- let i=s[t];
- if(Fs.existsSync(Path.join(e,i))){
- Util.manifestResDir=i;
- break
- }
- }
- return Util.manifestResDir?(this.resourceRootDir=e,this._saveConfig(),!0):(this.log(`没有发现资源目录${s.toString()}, 请先构建项目.`),!1)
- }return this.log(`没有发现 ${e}, 请先构建项目.`),!1
- },
- onDoEncrypt(e,t,s,ii) {
- if (this.hasBuildProj) {
- this.log("请不要重复点击");
- this._genVersion(e,t,s,ii);
- return;
- }
- this.log("encrypt tool init");
- this.logView = "";
- this.hasEncrypt = 0;
- this.totalPng = [];
- this.hasBuildProj = true;
- let target = this.getBuildTips();
- if (Fs.existsSync(this.rootPath + "/build/"+target)) {
- let imgh = this.rootPath + "/build/jsb-default/frameworks/cocos2d-x/cocos/platform/CCImage.h";
- let imgcpp = this.rootPath + "/build/jsb-default/frameworks/cocos2d-x/cocos/platform/CCImage.cpp";
- if (this.buildTypeSelect == 0) {
- let simulatorP = Editor.url('unpack://simulator', 'utf8');
- let Cpath = Path.resolve(simulatorP, '..');
- imgh = Cpath + "/cocos/platform/CCImage.h";
- imgcpp = Cpath + "/cocos/platform/CCImage.cpp";
- this.log("img.h:" + imgh);
- this.log("img.cpp:" + imgcpp);
- };
- let buf = Fs.readFileSync(imgh, "UTF-8");
-
- let tmp = buf.split("//civi encrypt,don't delete//");
- if (tmp.length < 2) {
- this.log("CCImage尚未添加代码,准备添加代码");
- let finalRes = this.addEncryptScriptHeader(buf);
- Fs.writeFileSync(imgh, finalRes);
- let cppbuf = Fs.readFileSync(imgcpp, "UTF-8");
- let finalSres = this.addEncryptScript(cppbuf);
- Fs.writeFileSync(imgcpp, finalSres);
- this.log("CCImage添加代码完成");
- } else {
- this.log("CCImage已经添加代码");
- this.log("更新签名及密码");
- let cppbuf = Fs.readFileSync(imgcpp, "UTF-8");
- let changeBuf = this.changeEncryptSignPass(cppbuf);
- Fs.writeFileSync(imgcpp, changeBuf);
- }
- } else {
- this.log("项目还未构建,请先构建"+target+"项目");
- this.hasBuildProj = false
- return;
- }
- this.saveCfg();
- this.log("开始加密...请耐心等待");
- if (this.isNewVersion) {
- this.walk(this.rootPath + "/build/"+target+"/assets");
- }else{
- this.walk(this.rootPath + "/library");
- }
- this.log("共检测到" + this.totalPng.length + "张需要加密的png");
- for (var i = 0; i < this.totalPng.length; i++) {
- let pathStr = this.totalPng[i];
- this.beginEncrypt(pathStr, ()=>{
- this._genVersion(e,t,s,ii);
- });
- }
- if(this.totalPng.length <= 0) {
- this.hasBuildProj = false;
- this._genVersion(e,t,s,ii);
- }
- },
- changeEncryptSignPass(buf) {
- let splitStr2 = "//civi encrypt signPass,don't delete//";
- let tmp2 = buf.split(splitStr2);
- if (tmp2.length == 3) {
- buf = tmp2[0] + splitStr2 + "\n\
- ,_signKey(\"" + this.signKey + "\")\n\
- ,_passWord(\"" + this.passWord + "\")\n\
- //civi encrypt signPass,don't delete//" + tmp2[2];
- }
- return buf;
- },
- getBuildTips(){
- // if(this.buildTypeSelect== 1)
- // {
- // return "jsb-default";
- // }
- return "jsb-link";
- },
- versionCompareHandle(versionA, 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;
- }
- },
- initPluginCfg() {
- let e = Editor.libraryPath;
- this.rootPath = e.substring(0, e.length - 7);
- this.cfgPath = this.rootPath + "/packages/encrypt-tool/cfg.json";
- this.log("项目路径为" + e.substring(0, e.length - 7));
- this.readCfg();
- this.checkBuildDir();
- this._updateShowEncryptInfo();
- },
- checkBuildDir(){
- let target = this.getBuildTips()
- this.log("当前加密目标模板为"+target+",即"+target+"目录下的png将被加密");
- if (Fs.existsSync(this.rootPath + "/build/"+target)) {
- let projCfg = this.rootPath + "/build/"+target+"/.cocos-project.json";
- let buf = Fs.readFileSync(projCfg,"UTF-8");
- let cfg = JSON.parse(buf);
- this.engineVersion = cfg.engine_version;
- this.log("当前引擎版本为" + this.engineVersion);
- let ret = this.versionCompareHandle(this.engineVersion,"2.4.0");
- if (ret >= 0) {
- this.isNewVersion = true;
- };
- }else{
- this.log("项目还未构建,请先构建"+target+"项目");
- }
- },
- saveCfg() {
- let newData = this.signKey + "civiEncrypt" + this.passWord + "civiEncrypt"+this.buildTypeSelect;
- Fs.writeFile(this.cfgPath, newData,function(error) {
- });
- },
- readCfg() {
- if (Fs.existsSync(this.cfgPath)) {
- let st = Fs.readFileSync(this.cfgPath, "UTF-8");
- let signData = st.split("civiEncrypt");
- if (signData.length > 1) {
- this.signKey = signData[0];
- this.passWord = signData[1];
- if (signData.length > 2) {
- this.buildTypeSelect = signData[2];
- };
- this.log("上次签名为" + this.signKey);
- this.log("上次密码为" + this.passWord);
- };
- } else {
- this.log("未检测到历史配置");
- }
- },
- stringToHex(str) {
- if (str === "")
- return "";
- var hexCharCode = [];
- for (var i = 0; i < str.length; i++) {
- hexCharCode.push((str.charCodeAt(i)));
- }
- return hexCharCode;
- },
- checkPngSig(fileBuffer) {
- let isEqual = false
- // 判断标识头前缀
- if (pngBufferHeader.bufBegin) {
- const buf = Buffer.from(pngBufferHeader.bufBegin);
- isEqual = buf.equals(fileBuffer.slice(0, pngBufferHeader.bufBegin.length));
- }
- // 判断标识头后缀
- if (isEqual && pngBufferHeader.bufEnd) {
- const buf = Buffer.from(pngBufferHeader.bufEnd);
- isEqual = buf.equals(fileBuffer.slice(-pngBufferHeader.bufEnd.length));
- }
- if (isEqual) {
- return pngBufferHeader.suffix;
- }
- return ''
- },
- walk(path) {
- var isPNG = function(pathStr) {
- let extname = Path.extname(pathStr);
- if (extname == ".png" || extname == ".PNG") {
- let buf = Fs.readFileSync(pathStr);
- let sig = this.checkPngSig(buf);
- if (sig == ".png") {
- return true;
- };
- } else {
- return false;
- }
- }.bind(this);
- //先取出文件夹
- var dirList = Fs.readdirSync(path);
- //遍历文件夹
- dirList.forEach(function(item) {
- //判断是不是文件夹
- if (Fs.statSync(path + '/' + item).isDirectory()) {
- if (this.isNewVersion) {
- if (item == "internal" ) {
- //默认的不管pass
- }else{
- this.walk(path + '/' + item);
- }
- }else{
- this.walk(path + '/' + item);
- }
- } else {
- //判断是不是PNG图片
- if (isPNG(path + '/' + item)) {
- //进行加密
- this.totalPng.push(path + '/' + item);
- } else {
- // pass
- }
- }
- }.bind(this));
- },
- beginEncrypt(pathStr, callback) {
- let buf = Fs.readFileSync(pathStr);
- let newData = this.encryptPng(buf);
- Fs.writeFile(pathStr, newData,function(error) {
- // this.log(pathStr + "加密完成");
- this.hasEncrypt = this.hasEncrypt + 1;
- if (this.hasEncrypt == this.totalPng.length) {
- this.log("全部加密完成");
- this.log("png资源加密完成");
- this.hasBuildProj = false;
- if(callback) {
- callback();
- }
- };
- }.bind(this));
- },
- encryptPng(fileBuffer) {
- //预处理,先切头,再去尾
- let cutHeader = fileBuffer.slice(pngBufferHeader.bufBegin.length);
- let iendData = cutHeader.slice( - pngBufferHeader.bufEnd.length);
- let lastData;
- const buf = Buffer.from(pngBufferHeader.bufEnd);
- if (buf.equals(iendData)) {
- lastData = cutHeader.slice(0, -pngBufferHeader.bufEnd.length);
- } else {
- lastData = cutHeader;
- }
- let k = this.stringToHex(this.passWord);
- let klen = k.length;
- let kindex = 0;
- for (var i = 0; i < lastData.length; i++) {
- if (kindex >= klen) {
- kindex = 0;
- };
- lastData[i] = lastData[i] ^ k[kindex];
- kindex = kindex + 1;
- };
- let sign = new Buffer(this.signKey);
- let finalData = Buffer.concat([sign, lastData]);
- return finalData;
- },
- }
- });
|