123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- const fs = require('fs');
- const path = require('path');
- const setting = require('./build-setting');
- const utils = require('./libs/utils');
- module.exports = (function (){
- var _instance;
- function AssetsCompress (params) {
- return {
- _ready: false,
- _map: {},
- _oldMap: null,
- _currDir: null,
- _dirPath: null,
- _tempDirMap: {},
- init (actualPlatform) {
- this._ready = false;
- this._map = {};
- this._currDir = null;
- this._dirPath = null;
- this._tempDirMap = {};
- this._actualPlatform = actualPlatform;
- let assetsObfuscator = `assetsObfuscator`;
- let obfuscator = setting.getInstance().getItem(assetsObfuscator);
- if (!obfuscator) { Editor.warn('缺少配置,跳过资源混淆!'); return false; }
- if (!obfuscator.enable) { Editor.warn('未开启资源混淆!'); return false; }
- let pwd = obfuscator.pwd;
- if (!pwd || pwd.length == 0) {
- this._oldMap = null;
- Editor.warn('混淆密码为空!');
- return false;
- }
- let pwdLen = pwd.length;
- let keys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
- if (pwdLen !== keys.length) {
- this._oldMap = null;
- Editor.warn(`混淆密码长度为16位,是从 0123456789abcdef 中产生的乱序`);
- return false;
- }
- for (let i = 0; i < pwdLen; i++) {
- let key = keys[i];
- this._map[key] = pwd[i];
- }
- this._ready = true;
- return true;
- },
- isReady () {
- return this._ready;
- },
- getMima () {
- return this._map;
- },
- modifyDecodeUuidFile () {
- // 写文件前先删除缓存文件
- let cache = path.join(Editor.url('unpack://engine'), 'bin/.cache', this._actualPlatform);
- if (fs.existsSync(cache)) {
- utils.rmdirSync(cache);
- }
- // 原逻辑
- let mapTxt = '';
- let base64 = `if (base64.length !== 22) {
- return base64;
- }`;
- let ele = '';
- // 如果加密了,将原逻辑改成解密逻辑
- if (Object.keys(this._map).length > 0) {
- this._oldMap = this._map;
- mapTxt = `
- var mima = ${JSON.stringify(this._map)};
- `;
- base64 = `if (22 !== base64.length) {
- if (base64.indexOf("/") !== -1) {
- return base64;
- }
- var array = new Array(base64.length);
- for (var index = 0; index < array.length; index++) {
- var item = base64.charAt(index);
- array[index] = mima[item];
- }
- base64 = array.join("");
- return base64;
- }`;
- ele = `
- for (let index = 0; index < UuidTemplate.length; index++) {
- let element = UuidTemplate[index];
- if (element !== '-') {
- UuidTemplate[index] = mima[UuidTemplate[index]];
- }
- }`;
- }
- // 将上面的逻辑写入文件
- let filePath = path.join(Editor.url('unpack://engine'), 'cocos2d/core/utils/decode-uuid.js');
- if (fs.existsSync(filePath)) {
- try {
- var newStr = `
- /****************************************************************************
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- https://www.cocos.com/
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated engine source code (the "Software"), a limited,
- worldwide, royalty-free, non-assignable, revocable and non-exclusive license
- to use Cocos Creator solely to develop games on your target platforms. You shall
- not use Cocos Creator software for developing other software or tools that's
- used for developing games. You are not granted to publish, distribute,
- sublicense, and/or sell copies of Cocos Creator.
- The software or tools in this License Agreement are licensed, not sold.
- Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- var Base64Values = require('./misc').BASE64_VALUES;
- var HexChars = '0123456789abcdef'.split('');
- var _t = ['', '', '', ''];
- var UuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t);
- var Indices = UuidTemplate.map(function (x, i) { return x === '-' ? NaN : i; }).filter(isFinite);
- ${mapTxt}
- // fcmR3XADNLgJ1ByKhqcC5Z -> fc991dd7-0033-4b80-9d41-c8a86a702e59
- module.exports = function (base64) {
- ${base64}
- UuidTemplate[0] = base64[0];
- UuidTemplate[1] = base64[1];
- for (var i = 2, j = 2; i < 22; i += 2) {
- var lhs = Base64Values[base64.charCodeAt(i)];
- var rhs = Base64Values[base64.charCodeAt(i + 1)];
- UuidTemplate[Indices[j++]] = HexChars[lhs >> 2];
- UuidTemplate[Indices[j++]] = HexChars[((lhs & 3) << 2) | rhs >> 4];
- UuidTemplate[Indices[j++]] = HexChars[rhs & 0xF];
- }
- ${ele}
- return UuidTemplate.join('');
- };
- if (CC_TEST) {
- cc._Test.decodeUuid = module.exports;
- }
- `;
- fs.writeFileSync(filePath, newStr);
- Editor.log('混淆逻辑写入' + filePath + '成功');
- } catch (error) {
- Editor.error('混淆逻辑写入' + filePath + '失败');
- }
- } else {
- Editor.error('读取' + filePath + '失败');
- }
- },
- modifyResUuid (buildDest, cb) {
- let importFolder = path.join(buildDest, 'res/import');
- let rawAssetsFolder = path.join(buildDest, 'res/raw-assets');
- if (!fs.existsSync(importFolder)) {
- Editor.warn(`${importFolder}文件夹不存在`);
- return;
- }
- if (!fs.existsSync(rawAssetsFolder)) {
- Editor.warn(`${rawAssetsFolder}文件夹不存在`);
- return;
- }
- this.syncFindFile(importFolder, 'import');
- this.syncFindFile(rawAssetsFolder, 'raw-assets');
- for (const temPath in this._tempDirMap) {
- const newPath = this._tempDirMap[temPath];
- fs.renameSync(temPath, newPath);
- }
- },
- getNewName (mima, oldName) {
- let mimaLen = Object.keys(mima).length;
- if (mimaLen <= 0) {
- Editor.warn('没有混淆密码');
- return;
- }
- if (oldName.length <= 0) {
- Editor.warn('混淆名称为空');
- return;
- }
- let name;
- let ext;
- let idx = oldName.lastIndexOf('.');
- if (idx !== -1) {
- name = oldName.substr(0, idx);
- ext = oldName.substr(idx);
- } else {
- name = oldName;
- ext = '';
- }
- let newName = '';
- for (let i = 0; i < name.length; i++) {
- const c = name[i];
- newName += mima[c] || c;
- }
- newName += ext;
- return newName;
- },
- syncFindFile (filePath, flag) {
- let total = 0;
- let dirs = fs.readdirSync(filePath);
- dirs.forEach((ele, index) => {
- let needRename = true;
- let fullpath = path.join(filePath, ele);
- let info = fs.statSync(fullpath);
- if (info.isDirectory() && ele.length == 2) {
- needRename = false;
- this._dirName = ele;
- this._dirPath = fullpath;
- this.syncFindFile(fullpath, flag);
- }
- if (needRename) {
- let newName = this.getNewName(this._map, ele);
- let newPath = path.join(fullpath.substr(0, fullpath.lastIndexOf('/')), newName);
- fs.renameSync(fullpath, newPath);
- ++total;
- if (total === dirs.length && this._dirPath && this._dirName) {
- let newDirName = this.getNewName(this._map, this._dirName);
- let newDirPath = path.join(this._dirPath.substr(0, this._dirPath.lastIndexOf('/')), newDirName);
- let newDirPathTemp = newDirPath + '_' + flag;
- fs.renameSync(this._dirPath, newDirPathTemp);
- this._tempDirMap[newDirPathTemp] = newDirPath;
- }
- }
- });
- }
- };
- }
- return {
- getInstance (params) {
- if (!_instance){
- _instance = AssetsCompress(params);
- }
- return _instance;
- }
- };
- })();
|