const path = require('./path.json'); const readline = require('readline'); const fs = require('fs'); const xl = require('xlsx'); let excelDir = './excel'; // excel配置表目录 let outDir = './out'; // 导出目录 let outJsonFolder = 'excel'; // 导出的json目录 //前端需要在load界面加载的表 let loadJson = ["audioInfo", "languageLang", "languageClientText", "mathInfo", "packageInfo"] const cluster = require('cluster'); //后端配置 let serverPath = "./common/" let apiurl = "" // 前后端解析索引的行 let hang = 3; // 前端3 后端4 // 表格列表 example: ["iconopen","mainTask"] let excelFiles = []; // 表名 +分表名 let excelFen = []; // 表格与表单的数据类型 example:{iconopen:{id:string}} let excelAndSheets = {}; var s_xls = require('./s_xls'); const { exit } = require('process'); // 检测传入的参数 var args = process.argv.splice(2) var jiexiTarget = args[0] || "" // s:后端 c:前端 function exeExcelConfig_c() { let str = ''; str += `import GameController from "../GameController"\nimport { gameMethod } from "./gameMethod"\n` // 添加类型定义 excelFiles.forEach((excelName) => { str += `// type for ${excelName}.excel\n`; for (const sheetName in excelAndSheets[excelName]) { str += `export type xls${toUpperCaseByStr(excelName)}${toUpperCaseByStr(sheetName)} = {`; //遍历索引存入类型定义 let keyObjList = excelAndSheets[excelName][sheetName]; for (const key in keyObjList) { str += `${key.split(',')[0]}:${keyObjList[key].value},`; } // 去掉最后一个逗号 "," if (str.charAt(str.length - 1) == ',') { str = str.substring(0, str.length - 1); } str += '}'; str += '\n'; } }); str += '\n'; //导出 str += `export class XlsConfig {\n`; excelFen.forEach((element) => { str += ` xls${toUpperCaseByStr(element)}: xls${toUpperCaseByStr(element)}[]\n`; }); // str += ` constructor(callback: Function) {\n` // let str111 = []; // excelFen.forEach((element) => { // str111.push('"' + element + '"'); // }); // str += ` }\n` str += ` constructor(bundleName:string, callback: Function) { cc.assetManager.loadBundle(bundleName, (err, bundle) => { if (err) { GameController.clear() cc.game.restart() return } if (gameMethod.isEmpty(bundle) && cc.sys.platform == cc.sys.WECHAT_GAME) { //小游戏平台,缓存过期,自动清理缓存,重启游戏 console.error("加载配置表的bundle失败,清理缓存重新加载") cc.assetManager.cacheManager.clearCache() GameController.clear() cc.game.restart() return } bundle.loadDir("", (err2, assets: cc.JsonAsset[]) => { if (err2) { GameController.clear() cc.game.restart() return } assets.forEach(element => { this["xls" + this.firstUpCase(element.name)] = element.json }); callback(this) }) }) } // 首字母大写 private firstUpCase(str: string) { return str[0].toUpperCase() + str.substring(1) } }`; if (hang == 3) { outDir = path.configPath; } else { outDir = serverPath; } // 写入文件 let outPath = `${outDir}xlsConfig.ts`; fs.writeFile(outPath, str, function (err) { if (err) { console.log('xlsConfig写入失败:', err); } }); let pro = ''; if (hang == 4) { pro += 'import { CTX } from "../src/util/master";\n'; } pro += `export default class ConfProxy { static getKey(args: string[]): string { let out = "" args.forEach(element => { out += element out += "_" }); return out } static setKey(target: any, args: string[]): string { let out = "" args.forEach(element => { out += target[element] out += "_" }); return out } pool: { [key: string]: T } = {} name:string = "" constructor(xlsname:string,conf: T[], ...args: string[]) { this.name = xlsname conf.forEach(element => { this.pool[ConfProxy.setKey(element, args)] = element }); } getItem(...args: string[]): T | null { return this.pool[ConfProxy.getKey(args)] }`; if (hang == 4) { pro += ` getItemCtx(ctx:CTX,...args: string[]): T { let back = this.pool[ConfProxy.getKey(args)] if(back == null){ console.error("配置错误"+args,this.name) ctx.throw("配置错误"+args) } return back } `; } pro += ` } export class ConfListProxy { pool: { [key: string]: T[] } = {} name:string = "" constructor(xlsname:string,conf: T[], ...args: string[]) { this.name = xlsname conf.forEach(element => { let key = ConfProxy.setKey(element, args) if (this.pool[key] == null) { this.pool[key] = [] } this.pool[key].push(element) }); } getItemList(...args: string[]): T[] | null { return this.pool[ConfProxy.getKey(args)] } }`; pro += '\n'; // 写入文件 let proPath = ''; if (hang == 3) { proPath = `${path.configPath}confProxy.ts`; } else { proPath = serverPath + `confProxy.ts`; } fs.writeFile(proPath, pro, function (err) { if (err) { console.log('ConfProxy写入失败:', err); } }); } function exeExcelConfig() { let str = ''; // 引入ts文件 excelFen.forEach((element) => { str += `const ${element} = require( "./excel/${element}.json")\n`; }); str += '\n'; // 添加类型定义 excelFiles.forEach((excelName) => { str += `// type for ${excelName}.excel\n`; for (const sheetName in excelAndSheets[excelName]) { str += `export type xls${toUpperCaseByStr(excelName)}${toUpperCaseByStr(sheetName)} = {`; //遍历索引存入类型定义 let keyObjList = excelAndSheets[excelName][sheetName]; for (const key in keyObjList) { str += `${key.split(',')[0]}:${keyObjList[key].value},`; } // 去掉最后一个逗号 "," if (str.charAt(str.length - 1) == ',') { str = str.substring(0, str.length - 1); } str += '}'; str += '\n'; } }); str += '\n'; //导出 str += `export class XlsConfig {\n`; excelFen.forEach((element) => { str += ` xls${toUpperCaseByStr(element)}: xls${toUpperCaseByStr(element)}[]\n`; }); str += ` constructor() {\n`; excelFen.forEach((element) => { str += ` this.xls${toUpperCaseByStr(element)} = ${element}\n`; }); str += ` }\n`; str += '}\n'; if (hang == 3) { outDir = path.configPath; } else { outDir = serverPath } // 写入文件 let outPath = `${outDir}xlsConfig.ts`; fs.writeFile(outPath, str, function (err) { if (err) { console.log('xlsConfig写入失败:', err); } }); let pro = ''; if (hang == 4) { pro += 'import { CTX } from "../src/util/master";\n'; } pro += `export default class ConfProxy { static getKey(args: string[]): string { let out = "" args.forEach(element => { out += element out += "_" }); return out } static setKey(target: any, args: string[]): string { let out = "" args.forEach(element => { out += target[element] out += "_" }); return out } pool: { [key: string]: T } = {} name:string = "" constructor(xlsname:string,conf: T[], ...args: string[]) { this.name = xlsname conf.forEach(element => { this.pool[ConfProxy.setKey(element, args)] = element }); } getItem(...args: string[]): T | null { return this.pool[ConfProxy.getKey(args)] }`; if (hang == 4) { pro += ` getItemCtx(ctx:CTX,...args: string[]): T { let back = this.pool[ConfProxy.getKey(args)] if(back == null){ console.error("配置错误"+args,this.name) ctx.throw("配置错误"+args) } return back } `; } pro += ` } export class ConfListProxy { pool: { [key: string]: T[] } = {} name:string = "" constructor(xlsname:string,conf: T[], ...args: string[]) { this.name = xlsname conf.forEach(element => { let key = ConfProxy.setKey(element, args) if (this.pool[key] == null) { this.pool[key] = [] } this.pool[key].push(element) }); } getItemList(...args: string[]): T[] | null { return this.pool[ConfProxy.getKey(args)] }`; if (hang == 4) { pro += ` getItemListCtx(ctx:CTX,...args: string[]): T[] { let back = this.pool[ConfProxy.getKey(args)] if(back == null){ console.error("list配置错误"+args,this.name) ctx.throw("list配置错误"+args) } return back } `; } pro += ` } ` pro += '\n'; // 写入文件 let proPath = ''; if (hang == 3) { proPath = `${path.configPath}confProxy.ts`; } else { proPath = serverPath + `confProxy.ts`; } fs.writeFile(proPath, pro, function (err) { if (err) { console.log('ConfProxy写入失败:', err); } }); } // 字符串开头变为大写 function toUpperCaseByStr(txt) { return txt.slice(0, 1).toUpperCase() + txt.slice(1); } // 分析配置表 function doExcel(excel) { let excelName = excel.split('_')[0]; if (s_xls.XlsBack[excelName] == null) { return } let fileDir = `${excelDir}/${excel}`; let workbook = xl.readFile(fileDir); // 获取 Excel 中所有表名 const sheetNames = workbook.SheetNames; // 返回 ['sheet1', 'sheet2'] let _sheetNames = []; for (const element of sheetNames) { if (s_xls.XlsBack[excelName][element] == null) { continue } _sheetNames.push(element); } if (_sheetNames.length == 0) { return; } console.time("...start excel " + excel) // 加到【excelAndSheets】中 let obj = {}; excelAndSheets[excelName] = {}; _sheetNames.forEach((sheetName) => { let sheet = workbook.Sheets[sheetName]; excelAndSheets[excelName][sheetName] = {}; doSheet(obj, excelName, sheetName, sheet); }); let outDir1 = "" let outDir2 = "" if (hang == 3) { outDir1 = `${path.jsonPath}`; if (!fs.existsSync(outDir1)) { fs.mkdirSync(outDir1) } outDir2 = `${path.loadJsonPath}`; if (!fs.existsSync(outDir2)) { fs.mkdirSync(outDir2) } } else { outDir1 = serverPath + `${outJsonFolder}`; if (!fs.existsSync(outDir1)) { fs.mkdirSync(outDir1) } } for (const key in obj) { let jsonStr = `${geshihua(obj[key])}`; if (outDir2 != "" && loadJson.indexOf(key) >= 0) { fs.writeFileSync(`${outDir2}/${key}.json`, jsonStr, function (err) { if (err) { console.log(err); } }); } else { fs.writeFileSync(`${outDir1}/${key}.json`, jsonStr, function (err) { if (err) { console.log(err); } }); } } console.timeEnd("...start excel " + excel) } function geshihua(data) { let count = 1; let _data = '[\n'; for (const val of data) { if (count == 1) { _data += ' ' + JSON.stringify(val); } else { _data += `,\n`; _data += ' ' + JSON.stringify(val); } count++; } _data += '\n]'; return _data; } //分析表单 obj数据,表名,表单名,表单数据内容 function doSheet(obj, excelName, sheetName, sheet) { let newKey = excelName + toUpperCaseByStr(sheetName); excelFen.push(newKey); if (obj[newKey] == null) { obj[newKey] = []; } // 索引列表 example:{A:"",B:"id",C:"hp",D:"attack"} let keyList = {}; // 类型列表 example:{A:"number",B:"string",C:"boolean",D:"{}[]"} let typeList = {}; //备注 let psList = {}; let _tempObj = {}; for (const key in sheet) { // 单元格定位id example: A25 const sheetKey = sheet[key]; // A25->25 let _hang = key.replace(/[^0-9]+/gi, ''); // A25->A let _lie = key.replace(/[^A-Z]+/gi, ''); if (_lie == 'A') { if (Object.keys(_tempObj).length > 0) { //这边检测是否有放空配置的。如果有 , 补上默认类型 let _tempObj_1 = {}; for (const key in keyList) { if (_tempObj[keyList[key]] == null) { if (typeList[key] == 'number') { _tempObj_1[keyList[key]] = 0; } else if (typeList[key] == 'string') { _tempObj_1[keyList[key]] = ''; } else if (typeList[key] == 'boolean') { _tempObj_1[keyList[key]] = false; } else if (typeList[key].indexOf('[]') != -1) { _tempObj_1[keyList[key]] = null; } else if (typeList[key].substring(0, 0 + 1) == '{') { _tempObj_1[keyList[key]] = null; } else if (typeList[key][0] == "[" && typeList[key][typeList[key].length - 1] == "]") { // 元组 _tempObj_1[keyList[key]] = null; } else { console.error('配置表有未知定义', excelName, sheetName, keyList[key], typeList[key]); } } else { _tempObj_1[keyList[key]] = _tempObj[keyList[key]]; } } obj[newKey].push(_tempObj_1); _tempObj = {}; } } if (sheet['A' + _hang] == null) { // 第一列没有配置参数,就跳过 continue; } if (_hang == 1) { psList[_lie] = sheetKey.v; } // 储存类型 if (_hang == 2) { typeList[_lie] = sheetKey.v; } // 储存索引 if (_hang == hang && sheetKey.v && typeList[_lie]) { // 正则抽取真正的值(去掉所有的*) let realVal = sheetKey.v.replace(/[\*]/g, ''); // 判断下作为key的字段 let isKey = 0; // 0:不是key,1:单key,2:列表key if (sheetKey.v.slice(0, 2) == '**') { isKey = 2; } else if (sheetKey.v.slice(0, 1) == '*') { isKey = 1; } keyList[_lie] = realVal.split(',')[0].trim(); // id:string excelAndSheets[excelName][sheetName][keyList[_lie]] = { ps: psList[_lie], isKey: isKey, value: typeList[_lie], keys: realVal }; } // 储存数据 if (_hang >= 5 && keyList[_lie] != null && typeList[_lie] != null) { // 根据类型转化数据内容,在做储存处理 let _val; if (typeList[_lie] == 'number') { if (typeof sheetKey.v != 'number') { _val = Number(sheetKey.v); if (isNaN(_val)) { console.log( '\x1b[33m%s\x1b[0m', '警告,出现NaN数据类型,表名:' + excelName + ' 表单:' + sheetName + ' 索引:' + keyList[_lie] + ' 表格ID:' + key ); } _val = 0; } else { _val = sheetKey.v; } } else if (typeList[_lie] == 'boolean') { if (sheetKey.v == 'true' || sheetKey.v == 'TRUE' || sheetKey.v == true) { _val = true; } else { _val = false; } } else if (typeList[_lie][0] == '{' || typeList[_lie].slice(-1) == ']') { try { _val = JSON.parse(sheetKey.v); } catch (error) { console.log( '\x1b[33m%s\x1b[0m', '警告:json格式错误,表名:' + excelName + ' 表单:' + sheetName + ' 索引:' + keyList[_lie] + ' 表格ID:' + key ); } } else if (typeList[_lie] == 'string') { if (typeof sheetKey.v == 'number') { _val = Math.floor(sheetKey.v).toString(); } else { _val = sheetKey.v; } } else { _val = sheetKey.v; } _tempObj[keyList[_lie].trim()] = _val; } } //这边检测是否有放空配置的。如果有 , 补上默认类型 let _tempObj_1 = {}; for (const key in keyList) { if (_tempObj[keyList[key]] == null) { if (typeList[key] == 'number') { _tempObj_1[keyList[key]] = 0; } else if (typeList[key] == 'string') { _tempObj_1[keyList[key]] = ''; } else if (typeList[key] == 'boolean') { _tempObj_1[keyList[key]] = false; } else if (typeList[key].indexOf('[]') != -1) { _tempObj_1[keyList[key]] = null; } else if (typeList[key].substring(0, 0 + 1) == '{') { _tempObj_1[keyList[key]] = null; } else if (typeList[key][0] == "[" && typeList[key][typeList[key].length - 1] == "]") { // 元组 _tempObj_1[keyList[key]] = null; } else { console.error('配置表有未知定义', excelName, sheetName, keyList[key], typeList[key]); } } else { _tempObj_1[keyList[key]] = _tempObj[keyList[key]]; } } // 储存最后一个 obj[newKey].push(_tempObj_1); } // //--------------------start------------------- 控制台人机交互 function readSyncByRl(tips) { tips = tips || '> '; return new Promise((resolve) => { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question(tips, (answer) => { rl.close(); resolve(answer.trim()); }); }); } /** * 后端创建xls对应gameCfg完成脚本 */ function createGameCfg_s() { let strs = ''; strs += `import ConfProxy, { ConfListProxy } from "./confProxy"\n`; let strHead = `XlsConfig`; let count = 2; for (const key1 in s_xls.XlsBack) { for (const key2 in s_xls.XlsBack[key1]) { if (s_xls.XlsBack[key1][key2].length <= 0) { continue; } strHead += `, xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)}`; count++; if (count % 9 == 0) { strHead += `\n`; } } } strs += `import { ${strHead} } from "./xlsConfig"\n\n`; strs += `export default class Gamecfg {\n`; strs += ` static init() {\n`; strs += ` let xls = new XlsConfig()\n`; for (const key1 in s_xls.XlsBack) { for (const key2 in s_xls.XlsBack[key1]) { if (s_xls.XlsBack[key1][key2].length <= 0) { continue; } for (const val of s_xls.XlsBack[key1][key2]) { let cs = ``; for (let index = 1; index < val.length; index++) { cs += `,"${val[index]}"`; } if (val[0] == 'ConfProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr(key2)} = new ${val[0]}("${key1}${toUpperCaseByStr(key2)}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`; continue; } if (val[0] == 'ConfListProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr(key2)}List = new ${val[0]}("${key1}${toUpperCaseByStr(key2)}List",xls.xls${toUpperCaseByStr( key1 )}${toUpperCaseByStr(key2)} ${cs})\n`; continue; } let cs1 = ``; for (let index = 2; index < val.length; index++) { cs1 += `,"${val[index]}"`; } //别名 if (val[1] == 'ConfProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr(val[0])} = new ${val[1]}("${key1}${toUpperCaseByStr(val[0])}",xls.xls${toUpperCaseByStr( key1 )}${toUpperCaseByStr(key2)} ${cs1})\n`; continue; } if (val[1] == 'ConfListProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( val[0] )}List = new ${val[1]}("${key1}${toUpperCaseByStr( val[0] )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`; continue; } } } } strs += ` }\n`; for (const key1 in s_xls.XlsBack) { for (const key2 in s_xls.XlsBack[key1]) { if (s_xls.XlsBack[key1][key2].length <= 0) { continue; } for (const val of s_xls.XlsBack[key1][key2]) { if (val[0] == 'ConfProxy') { strs += ` static ${key1}${toUpperCaseByStr(key2)} : ${val[0]}\n`; continue; } if (val[0] == 'ConfListProxy') { strs += ` static ${key1}${toUpperCaseByStr(key2)}List : ${val[0]}\n`; continue; } //别名 if (val[1] == 'ConfProxy') { strs += ` static ${key1}${toUpperCaseByStr(val[0])} : ${val[1]}\n`; continue; } if (val[1] == 'ConfListProxy') { strs += ` static ${key1}${toUpperCaseByStr( val[0] )}List : ${val[1]}\n`; continue; } } } } strs += `}\n`; // 写入文件 let proPath = ''; if (hang == 3) { proPath = `${path.configPath}gameCfg.ts`; } else { proPath = serverPath + `gameCfg.ts`; } fs.writeFile(proPath, strs, function (err) { if (err) { console.log('gameCfg写入失败:', err); } }); } /** * 前端创建xls对应gameCfg完成脚本 */ function createGameCfg_c() { let strs = ''; strs += `import ConfProxy, { ConfListProxy } from "./confProxy"\n`; let strHead = `XlsConfig`; let count = 2; for (const key1 in s_xls.XlsBack) { for (const key2 in s_xls.XlsBack[key1]) { if (s_xls.XlsBack[key1][key2].length <= 0) { continue; } strHead += `, xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)}`; count++; if (count % 9 == 0) { strHead += `\n`; } } } strs += `import { ${strHead} } from "./xlsConfig"\n\n`; strs += `export default class Gamecfg {\n`; for (const key1 in s_xls.XlsBack) { for (const key2 in s_xls.XlsBack[key1]) { if (s_xls.XlsBack[key1][key2].length <= 0) { continue; } for (const val of s_xls.XlsBack[key1][key2]) { if (val[0] == 'ConfProxy') { strs += ` static ${key1}${toUpperCaseByStr(key2)}:${val[0]}\n`; continue; } if (val[0] == 'ConfListProxy') { strs += ` static ${key1}${toUpperCaseByStr(key2)}List:${val[0]}\n`; continue; } //别名 if (val[1] == 'ConfProxy') { strs += ` static ${key1}${toUpperCaseByStr(val[0])}:${val[1]}\n`; continue; } //别名 if (val[1] == 'ConfListProxy') { strs += ` static ${key1}${toUpperCaseByStr(val[0])}List:${val[1]}\n`; continue; } } } } strs += `\n`; strs += ` static initLoading(loadSuccess: Function) {\n`; strs += ` try {\n`; strs += ` new XlsConfig("loadJson",(xls: XlsConfig) => {\n`; for (const key1 in s_xls.XlsBack) { for (const key2 in s_xls.XlsBack[key1]) { if (s_xls.XlsBack[key1][key2].length <= 0) { continue; } let jsonName = key1 + toUpperCaseByStr(key2) if (loadJson.indexOf(jsonName) < 0) { continue } for (const val of s_xls.XlsBack[key1][key2]) { let cs = ``; for (let index = 1; index < val.length; index++) { cs += `,"${val[index]}"`; } if (val[0] == 'ConfProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( key2 )} = new ${val[0]}("${key1}${toUpperCaseByStr( key2 )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`; continue; } if (val[0] == 'ConfListProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( key2 )}List = new ${val[0]}("${key1}${toUpperCaseByStr( key2 )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`; continue; } let cs1 = ``; for (let index = 2; index < val.length; index++) { cs1 += `,"${val[index]}"`; } //别名 if (val[1] == 'ConfProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( val[0] )} = new ${val[1]}("${key1}${toUpperCaseByStr( val[0] )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`; continue; } if (val[1] == 'ConfListProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( val[0] )}List = new ${val[1]}("${key1}${toUpperCaseByStr( val[0] )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`; continue; } } } } strs += ` loadSuccess(true) }) } catch (error) { loadSuccess(false) } }`; strs += `\n`; strs += ` static init(loadSuccess: Function) {\n`; strs += ` try {\n`; strs += ` new XlsConfig("pzwj",(xls: XlsConfig) => {\n`; for (const key1 in s_xls.XlsBack) { for (const key2 in s_xls.XlsBack[key1]) { if (s_xls.XlsBack[key1][key2].length <= 0) { continue; } let jsonName = key1 + toUpperCaseByStr(key2) if (loadJson.indexOf(jsonName) >= 0) { continue } for (const val of s_xls.XlsBack[key1][key2]) { let cs = ``; for (let index = 1; index < val.length; index++) { cs += `,"${val[index]}"`; } if (val[0] == 'ConfProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( key2 )} = new ${val[0]}("${key1}${toUpperCaseByStr( key2 )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`; continue; } if (val[0] == 'ConfListProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( key2 )}List = new ${val[0]}("${key1}${toUpperCaseByStr( key2 )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`; continue; } let cs1 = ``; for (let index = 2; index < val.length; index++) { cs1 += `,"${val[index]}"`; } //别名 if (val[1] == 'ConfProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( val[0] )} = new ${val[1]}("${key1}${toUpperCaseByStr( val[0] )}",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`; continue; } if (val[1] == 'ConfListProxy') { strs += ` Gamecfg.${key1}${toUpperCaseByStr( val[0] )}List = new ${val[1]}("${key1}${toUpperCaseByStr( val[0] )}List",xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`; continue; } } } } strs += ` loadSuccess(true) }) } catch (error) { loadSuccess(false) } } }`; // 写入文件 let proPath = ''; if (hang == 3) { proPath = `${path.configPath}gameCfg.ts`; } else { proPath = serverPath + `gameCfg.ts`; } fs.writeFile(proPath, strs, function (err) { if (err) { console.log('gameCfg写入失败:', err); } }); } function createXyc() { const body = fs.readFileSync(apiurl, "utf-8"); let arr = body.split('/a/apidoc"}]},') arr[1] = '[' + arr[1] let arr1 = arr[1].split('}];const'); let str2 = arr1[0].replace(/!1/g, '0') + '}]'; let apidoc = `//所有接口定义\n` apidoc += `export let apidoc:{[key:string]:string} = {\n` try { let strc = ''; var jsons = eval('(' + str2 + ')'); for (const json of jsons) { let jsontitle = json.url.split('/'); let title = toUpperCaseByStr(jsontitle[1]) + toUpperCaseByStr(jsontitle[2]); let jsonUrl = json.url.slice(1); strc += `//${json.name}\n`; strc += `export class ${title}\n`; strc += `{\n`; strc += ` static url:string = "${jsonUrl}"\n`; strc += `}\n`; strc += `export interface ${title}Pram{\n`; apidoc += ` "/${jsonUrl}":"${json.name}",\n` let _Parameter = []; let hasUuid = 0; let hasToken = 0; if (json.parameter != null) { for (const Parameter of json.parameter.fields.Parameter) { let description = Parameter.description; let arr11 = description.split('

'); let arr12 = arr11[1].split('

'); let _type = Parameter.type; _Parameter.push({ type: _type, field: Parameter.field, description: arr12[0] }); if (Parameter.field == 'uuid') { hasUuid = 1; } if (Parameter.field == 'token') { hasToken = 1; } } } for (const _rameter of _Parameter) { if (hasUuid == 1 && hasToken == 1) { if (_rameter.field == 'uuid' || _rameter.field == 'token') { continue; } } strc += ` ${_rameter.field}:${_rameter.type} //${_rameter.description}\n`; } strc += `}\n\n`; } apidoc += `}` if (hang == 3) { outDir = path.configPath; } else { outDir = serverPath } strc += ` //前端返回的特殊事件 export interface keyValue{ [key:string]:number } export interface keyValues{ [key:string]:number[] } export interface keyValueString{ [key:string]:string } export type ZixuanSelectCs = [string,string,number]; export type Wxmspinzhi = [number,number] export type Wxmskeys = [number,string,string]; export type Wxmslm = [number,string]; ` let proPath = `${outDir}Xyc.ts`; fs.writeFileSync(proPath, strc, function (err) { if (err) { console.log('Xyc写入失败:', err); } }); let proPath1 = `${outDir}apidoc.ts`; fs.writeFileSync(proPath1, apidoc, function (err) { if (err) { console.log('Xyc写入失败:', err); } }); } catch (error) { console.log('===', error); } } //拷贝 s_to_proto function copy_s_to_proto() { fs.copyFile(serverPath + 'Xys.ts', './s_to_proto/Xys.ts', (err) => { console.log('=', err); }); fs.copyFile(serverPath + 'Xyc.ts', './s_to_proto/Xyc.ts', (err) => { console.log('=', err); }); fs.copyFile(serverPath + 'gameMethod.ts', './s_to_proto/gameMethod.ts', (err) => { console.log('=', err); }); fs.copyFile(serverPath + 'fight.ts', './s_to_proto/fight.ts', (err) => { console.log('=', err); }); } //拷贝 proto function copy_proto_to_c() { outDir = path.configPath; fs.copyFile('./s_to_proto/Xys.ts', outDir + 'Xys.ts', (err) => { console.log('=', err); }); fs.copyFile('./s_to_proto/Xyc.ts', outDir + 'Xyc.ts', (err) => { console.log('=', err); }); fs.copyFile('./s_to_proto/gameMethod.ts', outDir + 'gameMethod.ts', (err) => { console.log('=', err); }); fs.copyFile('./s_to_proto/fight.ts', outDir + 'fight.ts', (err) => { console.log('=', err); }); } /** * 延迟多少毫秒 */ function sleep(ms) { if (ms < 1) { return; } return new Promise((reslove, reject) => { setTimeout(() => { reslove(1); }, ms); }); } // 开始解析配置表 function doExe(res) { // console.log(res); if (res != 'c' && res != 's' && res != 'C' && res != 'S') { console.log('非法输入'); return; } if (res == 'c' || res == 'C') { hang = 3; } else { hang = 4; } if (hang == 3) { outDir = path.configPath; } else { outDir = serverPath; } console.log('\x1b[37m'); // 控制台文字切为白色 if (hang == 4) { fs.rm(serverPath + outJsonFolder, { recursive: true }, (err, data) => { if (err) { console.log('删除json文件失败' + err); } console.log('删除json文件成功'); let sendNum = 0 let overNum = 0 // 解析表格 fs.readdirSync(excelDir).forEach((excelName) => { if ((excelName.substr(-4) == '.xls' || excelName.substr(-5) == '.xlsx') && excelName.substr(0, 1) != '~') { excelFiles.push(excelName.split('_')[0]); const worker = cluster.fork(excelName); sendNum += 1 worker.send({ "hang": 4, "excelName": excelName }) worker.on('message', function (mes) { let mesarr = JSON.parse(mes) for (const ar of mesarr.excelFen) { excelFen.push(ar) } for (const key in mesarr.excelAndSheets) { excelAndSheets[key] = mesarr.excelAndSheets[key] } overNum += 1 console.log(`配置表解析 发送${sendNum}个,完成${overNum}个`) }) } }); const intervalId = setInterval(() => { if (sendNum <= overNum) { console.log("----全部完成----") clearInterval(intervalId); } }, 1000); }); } if (hang == 3) { fs.unlink(path.excelPath + outJsonFolder + '/excel.zip', (err, data) => { if (err) { console.log('删除zip文件失败' + err); } console.log('删除zip文件成功'); let sendNum = 0 let overNum = 0 let xls = {} // 解析表格 fs.readdirSync(excelDir).forEach((excelName) => { if ((excelName.substr(-4) == '.xls' || excelName.substr(-5) == '.xlsx') && excelName.substr(0, 1) != '~') { excelFiles.push(excelName.split('_')[0]); // doExcel(excelName); xls[excelName] = 1 const worker = cluster.fork(excelName); sendNum += 1 worker.send({ "hang": 3, "excelName": excelName }) worker.on('message', function (mes) { let mesarr = JSON.parse(mes) for (const ar of mesarr.excelFen) { excelFen.push(ar) } for (const key in mesarr.excelAndSheets) { excelAndSheets[key] = mesarr.excelAndSheets[key] } overNum += 1 delete xls[excelName] console.log(`配置表解析 发送${sendNum}个,完成${overNum}个`) worker.destroy(); }) } }); const intervalId = setInterval(() => { if (sendNum <= overNum) { // 生成配置表读取文件(ts) exeExcelConfig_c(); console.log('*******创建配置文件完成*******'); // createXyc(); // console.log('*******创建xyc完成*******'); createGameCfg_c(); console.log('*******创建xls对应gameCfg完成*******'); copy_proto_to_c(); console.log('*******拷贝完成*******'); console.log("----全部完成----") clearInterval(intervalId); } }, 1000); }); } sleep(4000); // if (hang == 3) { // // } } if (!cluster.isMaster) { process.on("message", function (message) { hang = message.hang doExcel(message.excelName) process.send(JSON.stringify({ "excelFen": excelFen, "excelAndSheets": excelAndSheets })) process.exit() }); return } if (jiexiTarget == "s" || jiexiTarget == "c") { doExe(jiexiTarget) } else { readSyncByRl('exe client->c or server->s:').then((res) => { doExe(res) }); } // //---------------------end-------------------- 控制台人机交互