123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326 |
- const path = require('./path.json');
- const readline = require('readline');
- var archiver = require('archiver');
- const fs = require('fs');
- const fse = require('fs-extra');
- 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 = "../mergerpg_backend_kaifa/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, progressCallback?: Function) {
- cc.assetManager.loadBundle(bundleName, (err, bundle) => {
- if (err) {
- GameController.clear()
- cc.game.restart()
- if (progressCallback) {
- progressCallback(0, 0.99);
- }
- return
- }
- if (gameMethod.isEmpty(bundle) && cc.sys.platform == cc.sys.WECHAT_GAME) {
- //小游戏平台,缓存过期,自动清理缓存,重启游戏
- console.error("加载配置表的bundle失败,清理缓存重新加载")
- cc.assetManager.cacheManager.clearCache()
- GameController.clear()
- cc.game.restart()
- if (progressCallback) {
- progressCallback(0, 0.99);
- }
- return
- }
- bundle.loadDir("",(finish: number, total: number, item: any)=>{
- if (progressCallback) {
- progressCallback(finish, total);
- }
- }, (err2, assets: cc.JsonAsset[]) => {
- if (err2) {
- GameController.clear()
- cc.game.restart()
- if (progressCallback) {
- progressCallback(0, 0.99);
- }
- return
- }
- let current = 0;
- assets.forEach(element => {
- this["xls" + this.firstUpCase(element.name)] = element.json;
- current++;
- });
- 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 = '';
- pro += `export default class ConfProxy<T> {
- 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<T> {
- 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)} = <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 = '';
- pro += `export default class ConfProxy<T> {
- 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)] = this.deepFreeze(element)
- });
- }
-
- deepFreeze<T>(obj: T): T {
- Object.freeze(obj);
- Object.getOwnPropertyNames(obj).forEach(prop => {
- const value = (obj as Record<string, any>)[prop];
- if (value !== null
- && (typeof value === "object" || typeof value === "function")
- && !Object.isFrozen(value)) {
- this.deepFreeze(value);
- }
- });
- return obj as T;
- }
- 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
- // }
- // deepFreeze<T>(obj: T): T {
- // Object.freeze(obj);
- // Object.getOwnPropertyNames(obj).forEach(prop => {
- // const value = (obj as Record<string, any>)[prop];
- // if (value !== null
- // && (typeof value === "object" || typeof value === "function")
- // && !Object.isFrozen(value)) {
- // this.deepFreeze(value);
- // }
- // });
- // return obj as T;
- // }
- // `;
- // }
- pro += `
- }
- export class ConfListProxy<T> {
- 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]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)} >\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(key2)}List : ${val[0]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- //别名
- if (val[1] == 'ConfProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(val[0])} : ${val[1]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(
- val[0]
- )}List : ${val[1]}<xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)}>\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]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(key2)}List:${val[0]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- //别名
- if (val[1] == 'ConfProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(val[0])}:${val[1]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\n`;
- continue;
- }
- //别名
- if (val[1] == 'ConfListProxy') {
- strs += ` static ${key1}${toUpperCaseByStr(val[0])}List:${val[1]}<xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)}>\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, progressCallback: 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)
- }, progressCallback)
- } 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]: {name:string,parms: {[key:string]: {type: string, desc: 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}":{name:"${json.name}",parms:{`
- 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('<p>');
- let arr12 = arr11[1].split('</p>');
- 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`;
- apidoc += `"${_rameter.field}": {type: "${_rameter.type}", desc: "${_rameter.description}"},`
- }
- strc += `}\n\n`;
- apidoc += `}},\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() {
- // 或者同步拷贝
- try {
- fse.emptyDirSync('./s_to_proto/shared/');
- fse.copySync('../mergerpg_backend_kaifa/src/shared/', './s_to_proto/shared/');
- fse.emptyDirSync('./s_to_proto/docs/');
- fse.copySync('../mergerpg_backend_kaifa/docs/', './s_to_proto/docs/');
- console.log('目录拷贝完成');
- } catch (err) {
- console.error('拷贝失败:', err);
- }
- }
- //拷贝 proto
- function copy_proto_to_c() {
- outDir = path.sharedPath;
- try {
- // fse.emptyDirSync(outDir);
- fse.copySync('./s_to_proto/shared/', outDir);
- console.log('目录拷贝完成');
- } catch (err) {
- console.error('拷贝失败:', 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) {
- createGameCfg_s();
- console.log('*******创建xls对应gameCfg完成*******');
- copy_s_to_proto();
- console.log('*******拷贝完成*******');
-
- exeExcelConfig();
- console.log('*******创建配置文件完成*******');
- 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 }))
- if(hang == 4){
- process.exit()
- }
- });
- return
- }
- if (jiexiTarget == "s" || jiexiTarget == "c") {
- doExe(jiexiTarget)
- } else {
- readSyncByRl('exe client->c or server->s:').then((res) => {
- doExe(res)
- });
- }
- // //---------------------end-------------------- 控制台人机交互
|