Skip to content

Commit

Permalink
暂存物编
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Mar 28, 2024
1 parent 237314d commit adc76f7
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 66 deletions.
78 changes: 78 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@
"@types/fs-extra": "^11.0.4",
"@types/uuid": "^9.0.8",
"csv-parser": "^3.0.0",
"fast-csv": "^5.0.1",
"fast-xml-parser": "^4.3.5",
"fs-extra": "^11.2.0",
"uuid": "^9.0.1",
"winreg": "^1.2.5",
"jszip": "^3.10.1",
"fast-xml-parser": "^4.3.5"
"uuid": "^9.0.1",
"winreg": "^1.2.5"
},
"devDependencies": {
"@types/mocha": "^10.0.3",
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const englishPathToChinese: Readonly<{ [key: string]: string } >= {
/**
* 物编数据种类对应的中文名
*/
export const englishToChinese: Readonly<{ [key: string]: string } >= {
export const englishTypeNameToChineseTypeName: Readonly<{ [key: string]: string } >= {
"unit": "单位",
"decoration": "装饰物",
"item": "物品",
Expand All @@ -48,7 +48,7 @@ export const englishToChinese: Readonly<{ [key: string]: string } >= {
/**
* 物编数据种类对应的英文名
*/
export const chineseToEnglish:Readonly< { [key: string]: string } >= {
export const chineseTypeNameToEnglishTypeName:Readonly< { [key: string]: string } >= {
"单位": "unit",
"装饰物": "decoration",
"物品": "item",
Expand Down
87 changes: 75 additions & 12 deletions src/editorTable/CSVeditor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Env } from "../env";
import csvParser from 'csv-parser';
import { EditorTableType,editorTableTypeToFolderName } from '../constants';
import * as csv from 'fast-csv';
import { EditorTableType, editorTableTypeToFolderName, englishTypeNameToChineseTypeName,chineseTypeNameToEnglishTypeName } from '../constants';
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { isPathValid, isJson, getFileNameByVscodeUri } from '../utility';
import { hash, isJson, getFileNameByVscodeUri, isCSV, isPathValid } from '../utility';
/**
* 物编数据CSV表格的编辑器
*/
export class CSVeditor {
constructor(private readonly env: Env) {
constructor(private env: Env) {

}

Expand All @@ -20,8 +20,66 @@ export class CSVeditor {
/**
* 从工程文件添加
*/
public addEditorTableItemFromProject(editorTableItem:vscode.QuickPickItem) {

public addEditorTableItemFromProject(editorTableItem: vscode.QuickPickItem) {
if (!editorTableItem.description || !this.env.scriptUri) {
vscode.window.showErrorMessage("未初始化Y3项目");
return;
}
let englishEditorTableType = chineseTypeNameToEnglishTypeName[editorTableItem.description];
let csvRelativePath = this.env.tableTypeToCSVfolderPath[englishEditorTableType];
let csvPath = vscode.Uri.joinPath(this.env.scriptUri, csvRelativePath);
if (!isPathValid(csvPath.fsPath)) {
vscode.window.showErrorMessage("未找到CSV文件,请先生成");
return;
}
const files = fs.readdirSync(csvPath.fsPath);
files.forEach(file => {
if (!isCSV(file)) {
return;
}
const rows: any[] = [];
const filePath = path.join(csvPath.fsPath, file);
const fileReadStream = fs.createReadStream(filePath);
let haveItem: boolean = false;
let haveError: boolean = false;
let i = 0;//行号
csv.parseStream(fileReadStream,{ headers: true })
.on(
'data', (row) => {
if (!row.hasOwnProperty('uid')) {
haveError = true;
vscode.window.showErrorMessage('提供的CSV文件格式错误,缺少uid字段,文件路径为:'+filePath);
return;
}
if (row['uid'] === editorTableItem.detail) {
haveItem = true;
}
rows.push(row);

i++;
}
)
.on('end', () => {
if (!haveItem) {
let newRow: any={};
newRow['uid'] = editorTableItem.detail;
newRow['key'] = editorTableItem.detail;
newRow['name'] = editorTableItem.label;
rows.push(newRow);
}
if (!haveError) {
const fileWriteStream = fs.createWriteStream(filePath);
csv.write(rows, { headers: true })
.pipe(fileWriteStream);
}
})
.on('error', (error) => {
console.error('CSV解析错误', error.message);
console.error('CSV解析出错的行行号:' + (i - 1) + "");
let message = 'CSV解析错误:' + filePath + '\n' + '出错的CSV行,其行号为:' + i;
vscode.window.showErrorMessage(message);
});
});
}

public searchAllEditorTableItemInProject(query: string): vscode.QuickPickItem[]{
Expand All @@ -31,7 +89,8 @@ export class CSVeditor {
}
//只搜索九类物编数据的文件夹下的物编数据 不递归搜索
for (let type in EditorTableType) {
let folderName = editorTableTypeToFolderName[type];
let typeStr = EditorTableType[type as keyof typeof EditorTableType];
let folderName: string = editorTableTypeToFolderName[typeStr];
res = res.concat(this.searchEditorTableItemsInFolder(type,path.join(this.env.editorTablePath, folderName), query));
}
return res;
Expand All @@ -40,6 +99,7 @@ export class CSVeditor {
private searchEditorTableItemsInFolder(editorTableType:string,pathStr: string, query: string): vscode.QuickPickItem[] {
let res: vscode.QuickPickItem[] = [];
const files = fs.readdirSync(pathStr);
editorTableType=editorTableType.toLowerCase();
files.forEach(file => {
const filePath: string = path.join(pathStr, file);
const stat = fs.statSync(filePath);
Expand Down Expand Up @@ -67,18 +127,21 @@ export class CSVeditor {
let nameKey: any = editorTableJson['name'];
name = this.env.zhlanguageJson[nameKey];
}
let uid = editorTableJson['uid'];
if (!uid || typeof uid !=='number') {
uid = label.substring(0, label.length - 5);
}
if (name !== undefined && typeof name === "string") {
label = name + "(" + label.substring(0, label.length - 5) + ")";//显示为"这是一个单位(134219828)"的格式
label = name + "(" + uid + ")";//转为"这是一个单位(134219828)"的格式
}


if (label.includes(query)) {
let editorTableJsonName:string = label;
let editorTableJsonUri: vscode.Uri = vscode.Uri.file(filePath);
let quickPickItem: vscode.QuickPickItem = {
label: editorTableJsonName,
description: editorTableType,
detail: editorTableJsonUri.fsPath,
label: name,
description: englishTypeNameToChineseTypeName[editorTableType],
detail: uid,
};
res.push(quickPickItem);
}
Expand Down
45 changes: 19 additions & 26 deletions src/editorTable/CSVimporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import csvParser from 'csv-parser';
import * as fs from 'fs';

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

export class CSVimporter
{
Expand All @@ -22,20 +21,6 @@ export class CSVimporter
this.csvTypeToPath = csvTypeToPath;
}

// 默认情况下的文件夹名与其存放的物编数据类型的对应关系
private readonly defaultFolderNameToTableType: { [key: string]: string } = {
"单位": "unit",
"装饰物": "decoration",
"物品": "item",
"技能":"ability",
"魔法效果": "modifier",
"投射物": "projectile",
"科技": "technology",
"可破坏物": "destructible",
"声音":"sound"
};


/**
* 从插件配置中指定的文件夹 导入对应类型的物编数据CSV文件
* @returns true or false 成功或失败
Expand Down Expand Up @@ -88,9 +73,16 @@ export class CSVimporter
}



private async saveRowToTargetPath(row:any,target_path:vscode.Uri,tableType:string):Promise<boolean>{
if(!isPathValid(target_path.fsPath)){
/**
* 导入CSV文件中的一行数据
* 一行为一个物编数据项目的部分属性
* @param row 行号
* @param targetPath 目标路径
* @param tableType 物编表类型
* @returns
*/
private async saveRowToTargetPath(row:any,targetPath:vscode.Uri,tableType:string):Promise<boolean>{
if(!isPathValid(targetPath.fsPath)){
vscode.window.showErrorMessage('保存Json的路径非有效路径');
return false;
}
Expand All @@ -105,8 +97,8 @@ export class CSVimporter
uid = uid.substring(1);
}

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

let i=0;// 当前读取行号
await fs.createReadStream(csvFilePath)
fs.createReadStream(csvFilePath)
.pipe(csvParser())
.on('data', async (row) => {

Expand All @@ -340,12 +332,13 @@ export class CSVimporter
})
.on('end', () => {
vscode.window.showInformationMessage("全部导入成功");
; console.log('Parsed CSV data Length:', i);
console.log('Parsed CSV data Length:', i);
})
.on('error', (error) => {
console.error('CSV解析错误 Error parsing CSV', error.message);
console.error('Error parsing CSV row number:' + (i-1) + "");
let message = 'CSV解析错误 Error parsing CSV:' + csvFilePath + '\n' + '出错的CSV行,其行号为:' + i;
console.error('CSV解析错误', error.message);
console.error('CSV解析出错的行行号:' + (i-1) + "");
let message = 'CSV解析错误:' + csvFilePath + '\n' + '出错的CSV行,其行号为:' + i;
vscode.window.showErrorMessage(message);
});
return true;
}
Expand Down
Loading

0 comments on commit adc76f7

Please sign in to comment.