Compare commits

...

2 Commits

Author SHA1 Message Date
b6b4383569 修正没有配置 override 时报错的问题 2024-05-24 12:53:01 +08:00
d130ad1838 移除C#导出支持
支持针对表配置输出目录
2024-05-17 11:56:41 +08:00
13 changed files with 103 additions and 118 deletions

View File

@@ -1,8 +1,16 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
insert_final_newline = true
[*.{yml,yaml,tiny,yaml.txt,html}]
indent_style = space
indent_size = 2

29
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,29 @@
{
"files.associations": {
"*.tiny": "yaml",
"*.yaml.txt": "yaml",
"*.js.txt": "javascript",
"*.meta": "yaml",
"*.manifest": "yaml",
"*.asset": "yaml",
"project.tiny": "yaml"
},
"files.exclude": {
"Library": true,
"Logs": true,
"Temp": true,
"**/*.meta": true,
"Packages/packages-lock.json": true,
".temp": true,
"UI/.objs": true,
"yarn.lock": true,
"TextToolDatas": true,
"ProjectSettings": true,
"UserSettings": true,
"node_modules": true,
"qrcodes": true,
"obj": true,
"*.csproj": true,
"*.sln": true
},
}

View File

@@ -1,10 +1,12 @@
input:
- 配置表.xlsx
# - 配置表.xlsx
- 车辆.xlsx
parser:
first_column_as_id: true # 第一列用作 ID 列
constant_array_length: [
# 这里填入需要固定数组长度的表名称
]
output:
json:
enabled: true
@@ -14,13 +16,6 @@ output:
enabled: true
directory: output/yaml
indent: 2
csharp:
enabled: true
directory: output/csharp
namespace: game.data
base_type: tiny.data.UniqueIDObject
file_name: data
ignore_id: true
typescript:
enabled: true
declaration: false
@@ -28,4 +23,7 @@ output:
class_name_prefix: ''
class_name_extension: Data
directory: output/typescript
file_name: data
file_name: data
override:
车辆/Vehicle: output/车辆/Vehicle

View File

@@ -21,6 +21,7 @@
"dependencies": {
"colors": "^1.4.0",
"js-yaml": "^3.14.0",
"source-map-support": "^0.5.21",
"xlsx": "^0.16.0"
}
}

10
project.tiny Normal file
View File

@@ -0,0 +1,10 @@
develop:
actions:
- name: dev
title: 脚本编译服务
description: 脚本编译服务
command: yarn dev
- name: test
title: 单元测试
description: 单元测试
command: node ./dist/binary.js ./excel-exporter.yaml

View File

@@ -2,25 +2,28 @@ import { FileAccess, ModeFlags } from "tiny/io";
import { ParserConfigs, TableParser, TableData } from "./TableParser";
import { ExporterConfigs, TableExporter } from "./TableExporter";
import { JSONExporter } from "./exporters/JSONExporter";
import { CSharpExporter } from "./exporters/CSharpExporter";
import * as colors from "colors";
import { TypeScriptExporter } from "./exporters/TypeScriptExporter";
import * as yaml from "js-yaml";
import { YAMLExporter } from "./exporters/YAMLExporter";
import * as path from "path";
import * as fs from "fs";
export interface Configurations {
/** 解析配置 */
parser?: ParserConfigs,
/** 要读取的 XLSL 文档 */
input: {"file": string, encode: string}[],
input: string[],
/** 导出配置 */
output: { [key: string]: ExporterConfigs }
output: {
[key: string]: ExporterConfigs,
}
override?: Record<string, string>,
}
const exporters: {[key:string]: new(config: ExporterConfigs) => TableExporter } = {
json: JSONExporter,
csharp: CSharpExporter,
typescript: TypeScriptExporter,
yaml: YAMLExporter,
}
@@ -53,8 +56,14 @@ export class ExcelExporterApplication {
for (const file of this.configs.input) {
console.log(colors.grey(`解析配表文件: ${file}`));
let sheets = this.parser.parse_xlsl(file);
const base = path.basename(file).replace(/\.xlsx?$/, '');
for (const name in sheets) {
this.tables[name] = sheets[name];
const table = sheets[name];
if (this.configs.override) {
const override = this.configs.override[`${base}/${name}`];
table.output = override;
}
this.tables[name] = table;
}
}
console.log(colors.green(`解析所有配表文件完成`));
@@ -66,11 +75,22 @@ export class ExcelExporterApplication {
if (exporter.configs.enabled) {
console.log(colors.white(`执行 ${exporter.name} 导出:`));
for (const name in this.tables) {
exporter.export(name, this.tables[name]);
const table = this.tables[name];
const data = exporter.export(name, table);
if (data) {
let file = path.join(exporter.configs.directory, `${name}.${exporter.extension}`);
if (table.output) {
file = table.output + `.${exporter.extension}`;
}
const dir = path.dirname(file);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(file, data);
console.log(colors.white(`\t ${name} => ${file}`));
}
}
exporter.finalize();
console.log();
}
}
}
}
}

View File

@@ -45,8 +45,10 @@ export class TableExporter {
* @param name 表名称
* @param table 表数据
*/
export(name: string, table: TableData) { }
export(name: string, table: TableData): string | Buffer {
return null;
}
/** 全部配置表导出完毕后保存文件 */
finalize() {}
}
}

View File

@@ -1,6 +1,7 @@
import * as xlsl from "xlsx";
import { FileAccess, ModeFlags } from "tiny/io";
import * as colors from "colors";
import * as path from 'path';
export enum Keywords {
SKIP = '@skip',
@@ -32,6 +33,7 @@ type RawTableData = RawTableCell[][];
export interface TableData {
struct: Field;
data: {[key: string]: any}[];
output?: string;
}
export interface ParserConfigs {
@@ -230,8 +232,8 @@ export class TableParser {
return this.load_raw_xlsl_data(path);
}
protected load_raw_xlsl_data(path: string): { [key: string]: TableData } {
var file = FileAccess.open(path, ModeFlags.READ);
protected load_raw_xlsl_data(filePath: string): { [key: string]: TableData } {
var file = FileAccess.open(filePath, ModeFlags.READ);
let wb = xlsl.read(file.get_as_array());
file.close();
let raw_tables: {[key: string]: RawTableData } = {};
@@ -444,4 +446,4 @@ export class TableParser {
protected format_cell_position(cell: RawTableCell): string {
return xlsl.utils.encode_cell({c: cell.column, r: cell.row});
}
}
}

View File

@@ -1,85 +0,0 @@
import { TableExporter, ExporterConfigs } from "excel-exporter/TableExporter";
import { TableData, DataType } from "excel-exporter/TableParser";
import { path } from "tiny/path";
import * as colors from "colors";
interface CSharpExporterConfigs extends ExporterConfigs {
namespace: string,
base_type: string,
file_name: string,
ignore_id: boolean
}
export class CSharpExporter extends TableExporter {
protected declear_content = "";
protected classes: string[] = [];
get extension(): string { return 'cs' }
constructor(configs: ExporterConfigs) {
super(configs);
if ( typeof ((this.configs as CSharpExporterConfigs).namespace) != 'string') {
(this.configs as CSharpExporterConfigs).namespace = "game.data";
}
if ( typeof ((this.configs as CSharpExporterConfigs).base_type) != 'string') {
(this.configs as CSharpExporterConfigs).namespace = "object";
}
if ( typeof ((this.configs as CSharpExporterConfigs).file_name) != 'string') {
(this.configs as CSharpExporterConfigs).file_name = "data";
}
this.declear_content += this.line("// Tool generated file DO NOT MODIFY");
this.declear_content += this.line("using System;");
this.declear_content += this.line();
this.declear_content += this.line("namespace " + (this.configs as CSharpExporterConfigs).namespace + " {")
this.declear_content += this.line("%CLASSES%");
this.declear_content += this.line("}");
}
export(name: string, table: TableData) {
const base_type = (this.configs as CSharpExporterConfigs).base_type;
let body = "";
for (const field of table.headers) {
if (field.name == 'id' && (this.configs as CSharpExporterConfigs).ignore_id) {
continue;
}
let type = "object";
switch (field.type) {
case DataType.bool:
case DataType.float:
case DataType.string:
case DataType.int:
type = field.type;
break;
default:
type = "object";
break;
}
if (field.is_array) {
type += "[]";
}
if (field.comment) {
let comment = field.comment.split("\r\n").join("\t");
comment = comment.split("\n").join("\t");
body += this.line(`/// <summary>${comment}</summary>`, 1);
}
body += this.line(`public ${type} ${field.name};`, 1);
}
let class_text = this.line(`public class ${name} : ${base_type} {\n${body}\n}`);
this.classes.push(class_text);
}
finalize() {
let class_text = "";
for (const cls of this.classes) {
class_text += cls;
class_text += this.line();
}
let file = path.join(this.configs.directory, (this.configs as CSharpExporterConfigs).file_name);
if (!file.endsWith(".cs")) {
file += "." + this.extension;
}
this.save_text(file, this.declear_content.replace("%CLASSES%", class_text));
console.log(colors.green(`\t${file}`));
}
}

View File

@@ -59,9 +59,7 @@ export class JSONExporter extends TableExporter {
}
export(name: string, table: TableData) {
const file = path.join(this.configs.directory, `${name}.${this.extension}`);
const text = JSON.stringify(this.export_json_object(name, table), null, this.indent);
this.save_text(file, text);
console.log(colors.green(`\t ${name} ==> ${file}`));
return text;
}
}
}

View File

@@ -29,6 +29,7 @@ export class TypeScriptExporter extends TableExporter {
(this.configs as TypeScriptExporterConfigs).type,
(this.configs as TypeScriptExporterConfigs).declaration,
);
return null;
}
protected export_field(field: Field, indent = 0, ignore_root = false) {
@@ -119,4 +120,4 @@ export class TypeScriptExporter extends TableExporter {
console.log(colors.green(`\t${file}`));
}
}
}

View File

@@ -22,7 +22,6 @@ export class YAMLExporter extends JSONExporter {
}
export(name: string, table: TableData) {
const file = path.join(this.configs.directory, `${name}.${this.extension}`);
const text = yaml.dump(
this.export_json_object(name, table),
{
@@ -30,7 +29,6 @@ export class YAMLExporter extends JSONExporter {
sortKeys: true,
}
);
this.save_text(file, text);
console.log(colors.green(`\t ${name} ==> ${file}`));
return text;
}
}
}

View File

@@ -8,7 +8,10 @@ module.exports = (env) => {
console.log("Compile environment:", env);
return ({
target: 'node',
entry: path.join(workSpaceDir, 'src/main.ts'),
entry: [
'source-map-support/register',
path.join(workSpaceDir, 'src/main.ts'),
],
output: {
path: path.join(workSpaceDir, 'dist'),
filename: 'binary.js'
@@ -35,4 +38,4 @@ module.exports = (env) => {
devtool: env.production ? "" : "source-map",
mode: "development",
});
};
};