12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202 |
- const path = require('./path.json');
- const readline = require('readline');
- var archiver = require('archiver');
- const fs = require('fs');
- const xl = require('xlsx');
- let excelDir = './excel'; // excel配置表目录
- let outDir = './out'; // 导出目录
- let outJsonFolder = 'excel'; // 导出的json目录
- //前端需要在load界面加载的表
- let loadJson = ["audioInfo","languageLang","languageText","mathInfo","packageInfo","userInfo"]
- //后端配置
- let serverPath = "./common/"
- let apiurl = "../zhetian_backend_kaifa/public/api/assets/main.bundle.js"
- // 前后端解析索引的行
- let hang = 3; // 前端3 后端4
- // 表格列表 example: ["iconopen","mainTask"]
- let excelFiles = [];
- // 表名 +分表名
- let excelFen = [];
- // 表格与表单的数据类型 example:{iconopen:{id:string}}
- let excelAndSheets = {};
- var s_xls = require('./s_xls');
- // 检测传入的参数
- var args = process.argv.splice(2)
- var jiexiTarget = args[0] || "" // s:后端 c:前端
- // console.log("检测传入的参数:", jiexiTarget)
- function exeExcelConfig_c() {
- let str = '';
- // 添加类型定义
- 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) => {
- bundle.loadDir("", (err2, assets: cc.JsonAsset[]) => {
- 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<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 } = {}
- constructor(conf: T[], ...args: string[]) {
- 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.pool)
- ctx.throw("配置错误"+args)
- }
- return back
- }
- `;
- }
- pro += `
- }
- export class ConfListProxy<T> {
- pool: { [key: string]: T[] } = {}
- constructor(conf: T[], ...args: string[]) {
- 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 = '';
- if (hang == 4) {
- pro += 'import { CTX } from "../src/util/master";\n';
- }
- 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 } = {}
- constructor(conf: T[], ...args: string[]) {
- 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.pool)
- ctx.throw("配置错误"+args)
- }
- return back
- }
- `;
- }
- pro += `
- }
- export class ConfListProxy<T> {
- pool: { [key: string]: T[] } = {}
- constructor(conf: T[], ...args: string[]) {
- 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.pool)
- 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.log('...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 (hang == 3 && key == "languageText") {
- //程序翻译文本暂时不需要通过excel转,避免冲突
- continue
- }
- if (outDir2 != "" && loadJson.indexOf(key) >= 0) {
- fs.writeFile(`${outDir2}/${key}.json`, jsonStr, function (err) {
- if (err) {
- console.log(err);
- }
- });
- } else {
- fs.writeFile(`${outDir1}/${key}.json`, jsonStr, function (err) {
- if (err) {
- console.log(err);
- }
- });
- }
- }
- }
- 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]}(xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(key2)}List = new ${val[0]}(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]}(xls.xls${toUpperCaseByStr(
- key1
- )}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )}List = new ${val[1]}(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) {
- console.log('=======过滤========', key1, key2);
- 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]}(xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- key2
- )}List = new ${val[0]}(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]}(xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )}List = new ${val[1]}(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]}(xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs})\n`;
- continue;
- }
- if (val[0] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- key2
- )}List = new ${val[0]}(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]}(xls.xls${toUpperCaseByStr(key1)}${toUpperCaseByStr(key2)} ${cs1})\n`;
- continue;
- }
- if (val[1] == 'ConfListProxy') {
- strs += ` Gamecfg.${key1}${toUpperCaseByStr(
- val[0]
- )}List = new ${val[1]}(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('<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`;
- }
- 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 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 createZip() {
- const output = fs.createWriteStream(path.excelPath + outJsonFolder + '/excel.zip');
- const archive = archiver('zip', {
- zlib: {
- level: 9 // 设置压缩等级
- }
- });
- archive.pipe(output);
- let files = fs.readdirSync(path.jsonPath)
- for (const file of files) {
- if (file.indexOf('json.meta') != -1) {
- continue
- }
- archive.append(fs.createReadStream(path.jsonPath + file), { 'name': file });
- }
- archive.finalize(); // 完成压缩
- archive.on('end', () => {
- // 压缩结束时触发
- console.log('压缩完成');
- });
- }
- /**
- * 延迟多少毫秒
- */
- 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.rmdir(serverPath + outJsonFolder, { recursive: true }, (err, data) => {
- if (err) {
- console.log('删除json文件失败' + err);
- }
- console.log('删除json文件成功');
- // 解析表格
- 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);
- }
- });
-
- });
- }
- if (hang == 3) {
- fs.unlink(path.excelPath + outJsonFolder + '/excel.zip', (err, data) => {
- if (err) {
- console.log('删除zip文件失败' + err);
- }
- console.log('删除zip文件成功');
- // 解析表格
- 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);
- }
- });
- // 生成配置表读取文件(ts)
- exeExcelConfig_c();
- console.log('*******创建配置文件完成*******');
- // createXyc();
- // console.log('*******创建xyc完成*******');
- createGameCfg_c();
- console.log('*******创建xls对应gameCfg完成*******');
- copy_proto_to_c();
- console.log('*******拷贝完成*******');
- console.log("----全部完成----")
- });
- }
- sleep(4000);
- // if (hang == 3) {
- //
- // }
- }
- if (jiexiTarget == "s" || jiexiTarget == "c") {
- doExe(jiexiTarget)
- } else {
- readSyncByRl('exe client->c or server->s:').then((res) => {
- doExe(res)
- });
- }
- // //---------------------end-------------------- 控制台人机交互
|