HJ_Client/extensions/update_res/dist/update.js
DESKTOP-15R5JU0\legu 62fef0360c init
2023-11-17 14:13:11 +08:00

112 lines
4.5 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Update = void 0;
const crypto_1 = require("crypto");
const fs_1 = require("fs");
const path_1 = require("path");
/**获取文件md5 */
function getFileMd5(filePath) {
return (0, crypto_1.createHash)('md5').update((0, fs_1.readFileSync)(filePath, 'utf-8')).digest('hex');
}
class Update {
constructor(configList) {
this.configList = configList;
this.taskList = [];
this.dbPath = 'db://assets';
this.rootPath = (0, path_1.join)(Editor.Project.path);
this.targetPath = (0, path_1.join)(Editor.Project.path, 'assets');
this.check();
}
check() {
for (let v of this.configList) {
this.checkFun(v);
}
console.log(this.taskList);
}
checkFun(config) {
let map = {};
let pathList = config.root instanceof Array ? config.root : [config.root];
let rootFiles = pathList.map(path => {
let files = (0, fs_1.readdirSync)((0, path_1.join)(this.rootPath, path)).filter(f => f != '.svn');
files.forEach(file => map[file] = path);
return files;
}).reduce((a, b) => a.concat(b));
rootFiles = Array.from(new Set(rootFiles));
let hasDir = (0, fs_1.existsSync)((0, path_1.join)(this.targetPath, config.target));
if (!hasDir) {
this.taskList.push({
type: 'mkdir',
args: [this.dbPath + '/' + config.target]
});
}
let targetFiles = hasDir ? (0, fs_1.readdirSync)((0, path_1.join)(this.targetPath, config.target)).filter(v => v.indexOf('.meta') == -1) : [];
/**存在的资源 */
let existArr = targetFiles.filter(file => rootFiles.indexOf(file) != -1);
/**需要删除的资源 */
let delArr = targetFiles.filter(file => rootFiles.indexOf(file) == -1 && file.indexOf('.pac') == -1);
/**需要新增的资源 */
let addArr = rootFiles.filter(file => targetFiles.indexOf(file) == -1);
for (let exist of existArr) {
let rfu = (0, path_1.join)(this.rootPath, map[exist] + '/' + exist);
let tfu = (0, path_1.join)(this.targetPath, config.target + '/' + exist);
if ((0, fs_1.statSync)(tfu).isDirectory()) {
this.checkFun({
root: map[exist] + '/' + exist,
target: config.target + '/' + exist
});
}
else {
if (getFileMd5(rfu) != getFileMd5(tfu)) {
this.taskList.push({
args: [rfu, this.dbPath + '/' + config.target + '/' + exist],
type: 'changeFile'
});
}
}
}
for (let del of delArr) {
this.taskList.push({
args: [this.dbPath + '/' + config.target + '/' + del],
type: 'delFile'
});
}
for (let add of addArr) {
let rfu = (0, path_1.join)(this.rootPath, map[add] + '/' + add);
this.taskList.push({
args: [rfu, this.dbPath + '/' + config.target + '/' + add],
type: 'addFile'
});
}
}
async start() {
let mkList = this.taskList.filter(t => t.type == 'mkdir');
let delList = this.taskList.filter(t => t.type == 'delFile');
let addList = this.taskList.filter(t => t.type == 'addFile');
let changeList = this.taskList.filter(t => t.type == 'changeFile');
delList.sort((a, b) => {
let isGga = a.args[0].indexOf('.json') != -1 ? 0 : 1;
let isGgb = b.args[0].indexOf('.json') != -1 ? 0 : 1;
return isGga - isGgb;
});
addList.sort((a, b) => {
let isGga = a.args[0].indexOf('.json') != -1 ? 0 : 1;
let isGgb = b.args[0].indexOf('.json') != -1 ? 0 : 1;
return isGgb - isGga;
});
for (let d of mkList) {
await Editor.Message.request('asset-db', 'create-asset', d.args[0], null);
}
for (let d of delList) {
await Editor.Message.request('asset-db', 'delete-asset', d.args[0]);
}
for (let d of addList) {
await Editor.Message.request('asset-db', 'import-asset', d.args[0], d.args[1]);
}
for (let d of changeList) {
await Editor.Message.request('asset-db', 'import-asset', d.args[0], d.args[1], { overwrite: true });
}
Editor.Message.broadcast("update_res:update-over");
}
}
exports.Update = Update;