DataTools/src/PackageData.ts
2020-10-22 11:11:55 +08:00

338 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 用于打包全部配置,将配置全部打包成一个文件
* 服务端版本处理ani.json和pst.json的时候不会打入帧数据
*/
module pakdata
{
let path, fs;
let require = window["nodeRequire"];
if (typeof require === "function") { //是Electron环境
path = require("path");
fs = require("fs");
}
const cfgsSpiltInfoName = "CfgsSpiltInfo.json";
// 此处地址实际从外部传入解析
let IP_HOST = `192.168.92.178`;
let svnRoot = `//${IP_HOST}/web/`;
let clientDir = `//${IP_HOST}/web/config/zhcn/raw/`;
let packageRoot = `//${IP_HOST}/web/config/zhcn/`;
let aniPath = `//${IP_HOST}/web/resource/zhcn/ef/`;
let pstPath = `//${IP_HOST}/web/resource/zhcn/n/a/`;
export function initPathVars(url: string){
let boo = true;
let reg = /\/\/(.*?)\/(.*?)\//;
if (reg.test(url)){
let arg = reg.exec(url);
if (arg){
IP_HOST = arg[0];
svnRoot = `${IP_HOST}`;
clientDir = `${IP_HOST}config/zhcn/raw/`;
packageRoot = `${IP_HOST}config/zhcn/`;
aniPath = `${IP_HOST}resource/zhcn/ef/`;
pstPath = `${IP_HOST}resource/zhcn/n/a/`;
boo = false;
}
}
if (boo){
clientDir = url;
packageRoot = path.join(url, "..");
svnRoot = packageRoot;
IP_HOST = ``;
aniPath = ``;
pstPath = ``;
}
console.log(`IP_HOST=${IP_HOST}`);
console.log(`svnRoot=${svnRoot}`);
console.log(`clientDir=${clientDir}`);
console.log(`packageRoot=${packageRoot}`);
console.log(`aniPath=${aniPath}`);
console.log(`pstPath=${pstPath}`);
}
export var endCallBack;
export var traceLog;
export var traceErrorLog;
export var ifErrorCallBack;
export var isAutoSvn;
export async function packageForClient(){
let aniOutFile = path.join(clientDir, "ani.json");
let pstOutFile = path.join(clientDir, "pst.json");
let cfgsOutFile = path.join(packageRoot, "cfgs.json");
let firstOutFile = path.join(packageRoot, "basic.json");
let runningOutFile = path.join(packageRoot, "running.json");
let p = fs.statSync(clientDir);
if (!p.isDirectory()) {
console.error("文件夹有误");
if (ifErrorCallBack ) ifErrorCallBack();
return;
}
// if (isAutoSvn){
// try{
// await svncmd.update(svnRoot);
// } catch (e) {
// log(`[error svncmdUpdate] @@@ ${svnRoot}`);
// }
// }
// ani文件夹特殊处理
let cfgsOutData = {};
let aniData = parseAni(aniPath);
let pstData = parsePst(pstPath);
let cfgsSpiltInfoData = getCfgsSpiltInfo(clientDir);
console.log("++++++cfgsSpiltInfoData+++++++");
console.log(cfgsSpiltInfoData);
console.log("++++++cfgsSpiltInfoData+++++++");
let firstData = {};
let runningData = {};
let flist = fs.readdirSync(clientDir);
let basic = cfgsSpiltInfoData.basic || [];
let firstKeys = basic.length > 0 ? basic : ["Function", "FunctionOpen", "MapLine", "TribeType"];
let runningKeys = cfgsSpiltInfoData.running || [];
if (aniData){
if (firstKeys.indexOf("ani") != -1){
firstData["ani"] = aniData;
} else if (runningKeys.indexOf("ani") != -1){
runningData["ani"] = aniData;
} else {
cfgsOutData["ani"] = aniData;
}
}
if (pstData) {
if (firstKeys.indexOf("pst") != -1){
firstData["pst"] = pstData;
} else if (runningKeys.indexOf("pst") != -1){
runningData["pst"] = pstData;
} else {
cfgsOutData["pst"] = pstData;
}
}
flist.forEach(file => {
let re = path.parse(file);
let fname = re.name;
if (re.ext != ".json") {
return;
}
if (file == "ani.json" || file == "pst.json") {
// 此2文件已添加到outData中
return;
}
if (file == cfgsSpiltInfoName) {
return;// 配置分包信息不计入配置
}
let data = getData(path.join(clientDir, file));
if (data){
if (firstKeys.indexOf(fname) != -1){
firstData[fname] = data;
} else if (runningKeys.indexOf(fname) != -1){
runningData[fname] = data;
} else {
cfgsOutData[fname] = data;
}
}
});
// let t = JSON.stringify(cfgsOutData);
// let buf = new Buffer(t, "binary");
// if (buf instanceof Uint8Array){
// buf = new Buffer(buf);
// }
// console.log(buf);buf.toJSON();
// let tempP = path.join(packageRoot, "cfgs.bin");
// fs.writeFileSync(tempP, buf);
// let t1 = JSON.stringify(runningData);
// let buf1 = new Buffer(t1, "binary");
// console.log(buf1);
// let tempP1 = path.join(packageRoot, "running.bin");
// fs.writeFileSync(tempP1, buf1);
if (cfgsOutData) fs.writeFileSync(cfgsOutFile, JSON.stringify(cfgsOutData));
if (aniData) fs.writeFileSync(aniOutFile, JSON.stringify(aniData));
if (pstData) fs.writeFileSync(pstOutFile, JSON.stringify(pstData));
if (firstData) fs.writeFileSync(firstOutFile, JSON.stringify(firstData));
if (runningData) fs.writeFileSync(runningOutFile, JSON.stringify(runningData));
// if (isAutoSvn){
// try{
// await svncmd.add(packageRoot);
// } catch (e) {
// errorLog(`[error svncmdAdd]packageRoot:${packageRoot}`, e);
// }
// try{
// await svncmd.commit(packageRoot, "工具自动提交");
// } catch (e) {
// errorLog(`[error svncmdCommit]packageRoot:${packageRoot}`, e);
// }
// }
log("PackageData 脚本执行完成");
if( endCallBack ){
endCallBack();
}
}
function log(msg)
{
if (traceLog) traceLog( msg );
else console.log(msg);
}
function errorLog(msg: string, err?: Error)
{
if (traceErrorLog) traceErrorLog(msg, err);
else if (err) console.error(msg, err.message, err.stack);
else console.log(msg);
}
function getData(file)
{
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
errorLog(`找不到文件${file}`);
return null;
}
log(`getData ${file}`);
let data;
try {
data = JSON.parse(fs.readFileSync(file, "utf8"));
}
catch (e) {
errorLog(`解析${file}数据,${file}时出错`, e);
}
return data;
}
function getCfgsSpiltInfo(root)
{
let file = path.join(root, cfgsSpiltInfoName);
let data = getData(file);
let basic = [];
let running = [];
if (data){
data.forEach(list=>{
let b = list[0];
let r = list[1];
if (b) basic.push(b);
if (r) running.push(r);
});
}
return { "basic": basic, "running":running };
}
/**
* 处理ani文件夹
*
* @param {string} aniPath ani文件处理路径
*/
function parseAni(aniPath, saveTexture = true)
{
log(`parseAni:${aniPath}`);
if(!fs.existsSync(aniPath)){
errorLog(`no aniPath:${aniPath}`);
return null;
}
var data = {};
let p = fs.statSync(aniPath);
if (!p.isDirectory()) {
errorLog("给的ani路径文件夹有误");
return null;
}
// 读取子文件夹
let flist = fs.readdirSync(aniPath);
flist.forEach(subDir => {
//ani文件夹应该为 a0.png和d.json两个文件
let p = path.join(aniPath, subDir);
let dp = path.join(p, "d.json");
//检查子文件夹文件
if (fs.existsSync(path.join(p, "a0.png")) && fs.existsSync(dp)) {
//有这两个文件ani文件夹有效
let aniDat;
try {
aniDat = JSON.parse(fs.readFileSync(dp, "utf8"));
}
catch (e) {
errorLog(`解析ani数据${dp}时出错`, e);
return;
}
if (!aniDat || !Array.isArray(aniDat)) {
errorLog(`解析ani数据${dp}数据有误,不是数组`);
return;
}
if (!saveTexture) {
aniDat.length = 1;
}
data[subDir] = aniDat;
}
});
return data;
}
/**
* 处理pst文件夹
*
* @param {string} pstPath pst文件处理路径
*/
function parsePst(pstPath)
{
log(`parsePst:${pstPath}`);
if(!fs.existsSync(pstPath)){
errorLog(`no pstPath:${pstPath}`);
return null;
}
var data = {};
let p = fs.statSync(pstPath);
if (!p.isDirectory()) {
errorLog("给的pst路径文件夹有误");
return null;
}
// 读取子文件夹
let flist = fs.readdirSync(pstPath);
flist.forEach(subDir => {
//pst文件夹应该都包含 pst.json和d.json两个文件
let p = path.join(pstPath, subDir);
let dfile = path.join(p, "d.json");
let pfile = path.join(p, "pst.json");
//检查子文件夹文件
if (fs.existsSync(pfile) && fs.existsSync(dfile)) {
//有这两个文件pst文件夹有效
let pstDat;
try {
let temp = JSON.parse(fs.readFileSync(pfile, "utf8"));
for (let key in temp){
pstDat = temp[key];
}
}
catch (e) {
errorLog(`解析ani数据${pfile}时出错`, e);
return;
}
if (!pstDat || !Array.isArray(pstDat)) {
errorLog(`解析ani数据${pfile}数据有误,不是数组`);
return;
}
/*
if (subDir == "A10") subDir = "1_0";
else if (subDir == "A11") subDir = "1_1";
*/
data[subDir] = pstDat;
}
});
return data;
}
}