Skip to content

Commit

Permalink
更新物编
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Apr 7, 2024
1 parent 30bb5fc commit dfce902
Show file tree
Hide file tree
Showing 69 changed files with 910 additions and 71 deletions.
693 changes: 675 additions & 18 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 23 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
"command": "y3-helper.generateAllTemplateCSV",
"title": "Y3:生成物编模板(CSV)"
},
{
"command": "y3-helper.generateExcelTemplate",
"title": "Y3:生成物编模板(Excel)"
},
{
"command": "y3-helper.editorTableView.refresh",
"title": "Refresh",
Expand Down Expand Up @@ -191,6 +195,12 @@
"ignoreSync": true,
"pattern": "(^.*[\\\\/]Editor.exe$)|(^$)"
},
"Y3-Helper.editorTablceDataExcelFolder": {
"title": "存放物编数据的Excel表格所在的文件夹的相对路径",
"description": "注意:Execl表中的物编数据会按此相对路径下的importRules.ts代码所实现的导入规则来导入,生成Excel物编数据模板时也会生成到此文件夹下",
"type": "string",
"default": "./y3helper/editor_table/excel/"
},
"Y3-Helper.CSVPath": {
"title": "CSV物编路径",
"description": "",
Expand All @@ -200,47 +210,47 @@
"properties": {
"unit": {
"type": "string",
"default": "./y3helper/editor_table/单位",
"default": "./y3helper/editor_table/csv/单位",
"description": "单位的CSV物编数据的默认存放文件夹名"
},
"decoration": {
"type": "string",
"default": "./y3helper/editor_table/装饰物",
"default": "./y3helper/editor_table/csv/装饰物",
"description": "装饰物的CSV物编数据的默认存放文件夹名"
},
"item": {
"type": "string",
"default": "./y3helper/editor_table/物品",
"default": "./y3helper/editor_table/csv/物品",
"description": "物品的CSV物编数据的默认存放文件夹名"
},
"ability": {
"type": "string",
"default": "./y3helper/editor_table/技能",
"default": "./y3helper/editor_table/csv/技能",
"description": "技能的CSV物编数据的默认存放文件夹名"
},
"modifier": {
"type": "string",
"default": "./y3helper/editor_table/魔法效果",
"default": "./y3helper/editor_table/csv/魔法效果",
"description": "魔法效果的CSV物编数据的默认存放文件夹名"
},
"projectile": {
"type": "string",
"default": "./y3helper/editor_table/投射物",
"default": "./y3helper/editor_table/csv/投射物",
"description": "投射物的CSV物编数据的默认存放文件夹名"
},
"technology": {
"type": "string",
"default": "./y3helper/editor_table/科技",
"default": "./y3helper/editor_table/csv/科技",
"description": "科技的CSV物编数据的默认存放文件夹名"
},
"destructible": {
"type": "string",
"default": "./y3helper/editor_table/可破坏物",
"default": "./y3helper/editor_table/csv/可破坏物",
"description": "可破坏物的CSV物编数据的默认存放文件夹名"
},
"sound": {
"type": "string",
"default": "./y3helper/editor_table/声音",
"default": "./y3helper/editor_table/csv/声音",
"description": "声音的CSV物编数据的默认存放文件夹名"
}
}
Expand All @@ -266,12 +276,14 @@
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js",
"feasibility_test": "node ./out/test/feasibility.test.js"
"feasibility_test": "ts-node ./src/test/feasibility.test.ts"
},
"dependencies": {
"@types/exceljs": "^1.3.0",
"@types/fs-extra": "^11.0.4",
"@types/uuid": "^9.0.8",
"csv-parser": "^3.0.0",
"exceljs": "^4.4.0",
"fast-csv": "^5.0.1",
"fast-xml-parser": "^4.3.5",
"fs-extra": "^11.2.0",
Expand All @@ -291,6 +303,6 @@
"glob": "^10.3.10",
"mocha": "^10.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.2.2"
"typescript": "^5.4.3"
}
}
18 changes: 9 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ export const csvTypeToPath: Readonly<{ [key: string]: string }> = {

// 默认情况下各类型物编数据CSV文件的相对路径 (相对于工程项目的script文件)
export const defaultTableTypeToCSVfolderPath: Readonly<{ [key: string]: string }> = {
unit: "./y3helper/editor_table/单位",
decoration: "./y3helper/editor_table/装饰物",
item: "./y3helper/editor_table/物品",
ability: "./y3helper/editor_table/技能",
modifier: "./y3helper/editor_table/魔法效果",
projectile: "./y3helper/editor_table/投射物",
technology: "./y3helper/editor_table/科技",
destructible: "./y3helper/editor_table/可破坏物",
sound: "./y3helper/editor_table/声音"
unit: "./y3helper/editor_table/csv/单位",
decoration: "./y3helper/editor_table/csv/装饰物",
item: "./y3helper/editor_table/csv/物品",
ability: "./y3helper/editor_table/csv/技能",
modifier: "./y3helper/editor_table/csv/魔法效果",
projectile: "./y3helper/editor_table/csv/投射物",
technology: "./y3helper/editor_table/csv/科技",
destructible: "./y3helper/editor_table/csv/可破坏物",
sound: "./y3helper/editor_table/csv/声音"
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { env } from "../env";
import { env } from "../../env";
import * as csv from 'fast-csv';
import { EditorTableType, englishTypeNameToChineseTypeName,chineseTypeNameToEnglishTypeName } from '../constants';
import { EditorTableType, englishTypeNameToChineseTypeName,chineseTypeNameToEnglishTypeName } from '../../constants';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { SpinLock, isCSV, isPathValid } from "../utility";
import { allocateNewUIDofEditorTableItem } from './editorTableUtility';
import { SpinLock, isCSV, isPathValid } from "../../utility";
import { allocateNewUIDofEditorTableItem } from '../editorTableUtility';
/**
* 物编数据CSV表格的编辑器
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import * as vscode from 'vscode';
import csvParser from 'csv-parser';
import * as fs from 'fs';

import { env } from "../env";
import { isInDirectory, isPathValid, toUnicodeIgnoreASCII, hash } from '../utility';
import { csvTypeToPath } from "../constants";
import { env } from "../../env";
import { isInDirectory, isPathValid, toUnicodeIgnoreASCII, hash } from '../../utility';
import { csvTypeToPath } from "../../constants";
import * as path from 'path';
import { v4 as uuidv4 } from 'uuid';

Expand Down Expand Up @@ -98,7 +98,7 @@ export class CSVimporter
let jsonFilePath=targetPath.fsPath+'\\'+uid+'.json';
if(!isInDirectory(targetPath.fsPath,uid+'.json')){
console.log("没有检测到对应物品的Json,从模板新建了Json文件存储物编数据:" + jsonFilePath);
let templateJson = fs.readFileSync(path.join(__dirname, "../../template/json_template/" + tableType + ".json"));
let templateJson = fs.readFileSync(path.join(__dirname, "../../../template/json_template/" + tableType + ".json"));
fs.writeFileSync(jsonFilePath,templateJson);
}

Expand Down
4 changes: 4 additions & 0 deletions src/editorTable/EXCEL/EXCELimporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { env } from '../../env';
export class EXCELimporter{

}
31 changes: 31 additions & 0 deletions src/editorTable/EXCEL/importRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export abstract class ImportRule {

/**
* 物编数据所属的种类,总共可以填入以下几种类型:
* 单位 装饰物 物品 技能 魔法效果 投射物 科技 可破坏物 声音
*/
public abstract editorTableType: string;

/**
* 要导入的表格的相对于当前vscode工作区的路径
*/
public abstract inputRelativePath: string;

/**
* 所属的工作表名
*/
public abstract sheet: string;

/**
* 属性与Json字段的对应关系
*/
public abstract attrDef: { [key: string]: string };

/**
* 请重写此方法以实现自定义转换规则
* 返回值的对象需要包含'uid'字段,以确定物编项目的uid,以便本插件导入
* @param row 表格中的一行
* @returns 此行被解析得到的Json对象 本插件会把它的属性覆盖到物编项目的Json 物编项目Json中的
*/
public abstract rowImport(row:any): any;
}
4 changes: 2 additions & 2 deletions src/editorTable/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './editorTableUtility';
export * from './types';
export * from './editorTableProvider';
export * from './CSVimporter';
export * from './CSVeditor';
export * from './CSV/CSVimporter';
export * from './CSV/CSVeditor';
39 changes: 27 additions & 12 deletions src/editorTable/templateGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { env } from '../env';
import * as path from 'path';
import { isPathValid } from '../utility';
import { chineseTypeNameToEnglishTypeName,englishTypeNameToChineseTypeName } from '../constants';
export class TemplateGenerator{
export class TemplateGenerator {
private readonly englishToChinese;
private readonly chineseToEnglish;
public constructor() {
Expand All @@ -19,22 +19,21 @@ export class TemplateGenerator{
* @param targetPath 目标路径
* @returns true or false 生成成功或失败
*/
public generateTemplateCSVToTargetPath(templateType: string, targetPath: vscode.Uri): boolean{
public async generateTemplateCSVToTargetPath(templateType: string, targetPath: vscode.Uri): Promise<boolean> {
try {
fs.copy(path.join(path.join(__dirname, "../../template/csv_template"), templateType), targetPath.fsPath,{ overwrite: false });
fs.copySync(path.join(__dirname, "../../template/csv", templateType), targetPath.fsPath, { overwrite: false });
}
catch (error) {
vscode.window.showErrorMessage("模板生成异常:"+error);
vscode.window.showErrorMessage("模板生成异常:" + error);
return false;
}
return true;
}

public async generateAllTemplateCSVtoTargetPath(targetPath: vscode.Uri): Promise<boolean>{

public async generateAllTemplateCSVtoTargetPath(targetPath: vscode.Uri): Promise<boolean> {
try {
await this.renameTemplateCSVtoChinese();
fs.copySync(path.join(__dirname, "../../template/csv_template"), targetPath.fsPath, { overwrite: false });
fs.copySync(path.join(__dirname, "../../template/csv"), targetPath.fsPath, { overwrite: false });
await this.renameTemplateCSVtoEnglish();
}
catch (error) {
Expand All @@ -52,25 +51,41 @@ export class TemplateGenerator{
for (let key in this.englishToChinese) {
let oldFile: string = key;
let newFile: string = this.englishToChinese[key];
oldFile = path.join(__dirname, "../../template/csv_template/" + oldFile);
newFile = path.join(__dirname, "../../template/csv_template/" + newFile);
oldFile = path.join(__dirname, "../../template/csv/" + oldFile);
newFile = path.join(__dirname, "../../template/csv/" + newFile);
if (isPathValid(newFile)) {
continue;
}
fs.renameSync(oldFile, newFile);
}
}

/**
* vsocde插件的发布打包程序不支持中文路径 只能被迫转换成英文名
*/
private async renameTemplateCSVtoEnglish() {
for (let key in this.chineseToEnglish) {
let oldFile: string = key;
let newFile: string = this.chineseToEnglish[key];
oldFile = path.join(__dirname, "../../template/csv_template/" + oldFile);
newFile = path.join(__dirname, "../../template/csv_template/" + newFile);
oldFile = path.join(__dirname, "../../template/csv/" + oldFile);
newFile = path.join(__dirname, "../../template/csv/" + newFile);
if (isPathValid(newFile)) {
continue;
}
fs.renameSync(oldFile, newFile);
}
}

/**
* 生成excel模板
*/
public async generateExcelTemplate(targetPath: vscode.Uri):Promise<boolean> {
try {
fs.copySync(path.join(__dirname, "../../template/excel"), targetPath.fsPath, { overwrite: false });
}
catch (error) {
vscode.window.showErrorMessage("ExcelTemplate生成异常:" + error);
return false;
}
return true;
}
}
14 changes: 12 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,17 @@ class EnvPath {
public csvTableUri?: vscode.Uri;// CSV表格路径


public get excelTablePath() {
let relativePath: string | undefined = vscode.workspace.getConfiguration('Y3-Helper').get<string>('editorTablceDataExcelFolder');
if (relativePath && this.scriptUri) {
return vscode.Uri.joinPath(this.scriptUri,relativePath);
}
return undefined;
}


// 实际情况下各类型物编数据CSV文件的相对路径 (相对于工程项目的script文件)
private readonly _tableTypeToCSVfolderPath: { [key: string]: string } = {};
private _tableTypeToCSVfolderPath: { [key: string]: string } = {};
public get tableTypeToCSVfolderPath(): { [key: string]: string }{
return this._tableTypeToCSVfolderPath;
}
Expand Down Expand Up @@ -295,7 +304,7 @@ class EnvPath {
this.scriptUri = vscode.Uri.joinPath(this.mapUri, 'script');
this.y3Uri = vscode.Uri.joinPath(this.scriptUri, 'y3');
this.editorTableUri = vscode.Uri.joinPath(this.mapUri, "editor_table");
this.csvTableUri = vscode.Uri.joinPath(this.scriptUri, "./y3helper/editor_table/");
this.csvTableUri = vscode.Uri.joinPath(this.scriptUri, "./y3helper/editor_table/csv/");
this.initTableTypeToCSVfolderPath();
}
tools.log.info(`mapUri: ${this.mapUri}`);
Expand All @@ -317,6 +326,7 @@ class EnvPath {
this.mapUri = undefined;
this.editorUri = undefined;
this.fireOnDidReload();
this.initTableTypeToCSVfolderPath();//重新载入要导入的CSV表格的相对路径
}
}

Expand Down
37 changes: 31 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,14 @@ class Helper {

}

private registerCommandOfGenerateAllTemplateCSV() {
private registerCommandOfGenerateTemplates() {

let templateGenerator = new TemplateGenerator();



// 生成CSV
vscode.commands.registerCommand('y3-helper.generateAllTemplateCSV', async () => {
console.log("y3-helper.generateTemplateCSV");
await env.mapReady(true);
let projectUri = env.projectUri;
if (!projectUri) {
Expand All @@ -457,12 +462,32 @@ class Helper {
vscode.window.showErrorMessage("未找到合适的位置生成CSV");
return false;
}
// 生成csv模板
let templateGenerator = new TemplateGenerator();

let targetUri: vscode.Uri = env.csvTableUri;
await templateGenerator.generateAllTemplateCSVtoTargetPath(targetUri);
});


// 生成Excel
vscode.commands.registerCommand('y3-helper.generateExcelTemplate', async () => {
await env.mapReady(true);
let projectUri = env.projectUri;
if (!projectUri) {
vscode.window.showErrorMessage("没有打开工作目录!,请先初始化");
return false;
}
if (!env.csvTableUri) {
vscode.window.showErrorMessage("未找到合适的位置生成物编数据Excel表模板");
return false;
}
let targetUri: vscode.Uri|undefined = env.excelTablePath;
if (targetUri) {
// 把模板template/excel文件夹生成到模板文件夹的父级路径下
await templateGenerator.generateExcelTemplate(targetUri);
}
else {
vscode.window.showErrorMessage("找不到正确的路径生成物编数据Excel表模板,请检查插件配置Y3-Helper.editorTablceDataExcelFolder");
}
});
}

private registerCommandOfDownloadPresetUI() {
Expand Down Expand Up @@ -646,7 +671,7 @@ class Helper {
this.registerCommandOfLaunchGame();
this.registerCommandOfLaunchGameAndAttach();
this.registerCommandOfImportObjectDataFromAllCSVbyConfig();
this.registerCommandOfGenerateAllTemplateCSV();
this.registerCommandOfGenerateTemplates();
this.registerCommandOfDownloadPresetUI();
this.registerCommandOfRevealMainMenu();

Expand Down
Loading

0 comments on commit dfce902

Please sign in to comment.