101 lines
2.6 KiB
JavaScript
101 lines
2.6 KiB
JavaScript
// usage: node updatePath.json source.json dest.json
|
|
//
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
let Converter = {
|
|
sourceFile: process.argv[2],
|
|
destFile: process.argv[3],
|
|
|
|
start() {
|
|
// 非绝对路径则前缀加上当前目录
|
|
if (!path.isAbsolute(this.sourceFile)) {
|
|
this.sourceFile = path.join(__dirname, this.sourceFile);
|
|
}
|
|
if (!path.isAbsolute(this.destFile)) {
|
|
this.destFile = path.join(__dirname, this.destFile);
|
|
}
|
|
|
|
this.traversalDir(this.sourceFile, this.destFile);
|
|
},
|
|
|
|
// 遍历文件夹找到
|
|
traversalDir: function(sourceFile, destFile) {
|
|
if (sourceFile.length <= 0 || destFile.length <= 0) {
|
|
return;
|
|
}
|
|
|
|
fs.readFile(sourceFile, (err, data) => {
|
|
if (err) {
|
|
console.error(err);
|
|
return;
|
|
}
|
|
|
|
console.log('Success to read file: ' +sourceFile);
|
|
|
|
let str = this.writePath(data);
|
|
if (str && str.length > 0) {
|
|
|
|
fs.writeFile(destFile, str, (err) => {
|
|
if (err) {
|
|
console.error(err);
|
|
return;
|
|
}
|
|
console.log('Success to write file: ' +destFile);
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
writePath(data) {
|
|
if (!data || data.length <= 0) {
|
|
cc.warn('writePath: path is invalid');
|
|
return '';
|
|
}
|
|
|
|
let str = '';
|
|
try {
|
|
let paths = JSON.parse(data);
|
|
for (let i = 0, len = paths.length; i < len; i++) {
|
|
let path = paths[i];
|
|
path.type = 0;
|
|
if (!!path.speeds && path.speeds.length > 0) {
|
|
for (let j = 0; j < path.speeds.length; j++) {
|
|
path.speeds[j] = this.formatValue(path.speeds[j]);
|
|
}
|
|
}
|
|
if (!!path.points && path.points.length > 0) {
|
|
for (let j = 0; j < path.points.length; j++) {
|
|
let point = path.points[j];
|
|
point.x = this.formatValue(point.x);
|
|
point.y = this.formatValue(point.y);
|
|
}
|
|
}
|
|
if (!!path.ctrls && path.ctrls.length > 0) {
|
|
for (let j = 0; j < path.ctrls.length; j++) {
|
|
let ctrl = path.ctrls[j];
|
|
ctrl.x = this.formatValue(ctrl.x);
|
|
ctrl.y = this.formatValue(ctrl.y);
|
|
ctrl.z = this.formatValue(ctrl.z);
|
|
ctrl.w = this.formatValue(ctrl.w);
|
|
}
|
|
}
|
|
}
|
|
str = JSON.stringify(paths, null, '\t');
|
|
} catch(e) {
|
|
cc.error('writePath: error to format path');
|
|
}
|
|
|
|
return str;
|
|
},
|
|
|
|
formatValue(value) {
|
|
return Math.round(value * 100);
|
|
}
|
|
|
|
};
|
|
|
|
module.exports = Converter;
|
|
|
|
Converter.start();
|