Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jun 28, 2024
1 parent 2a8ab79 commit 166885d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 78 deletions.
55 changes: 4 additions & 51 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,7 @@ export enum EditorTableType {
Sound = "sound"
}

/**
* Y3项目的editor_table文件夹下的各个文件夹名和物编数据种类中文名的对应关系
*/
export const englishPathToChinese = {
"editorunit": "单位",
"soundall": "声音",
"abilityall": "技能",
"editordecoration": "装饰物",
"editordestructible": "可破坏物",
"editoritem": "物品",
"modifierall": "魔法效果",
"projectileall": "投射物",
"technologyall": "科技"
} as const;


/**
* 物编数据种类对应的中文名
*/
export const englishTypeNameToChineseTypeName = {
"unit": "单位",
"decoration": "装饰物",
"item": "物品",
"ability": "技能",
"modifier": "魔法效果",
"projectile": "投射物",
"technology": "科技",
"destructible": "可破坏物",
"sound": "声音"
} as const;


/**
* 物编数据种类对应的英文名
*/
export const chineseTypeNameToEnglishTypeName = {
"单位": "unit",
"装饰物": "decoration",
"物品": "item",
"技能": "ability",
"魔法效果": "modifier",
"投射物": "projectile",
"科技": "technology",
"可破坏物": "destructible",
"声音": "sound"
} as const;

export const table = {
export const Table = {
path: {
toCN: {
"editorunit": "单位",
Expand Down Expand Up @@ -135,9 +88,9 @@ export const table = {
},
} as const;

export type TablePath = keyof typeof table.path.toCN;
export type TableNameEN = keyof typeof table.name.toCN;
export type TableNameCN = keyof typeof table.name.fromCN;
export type TablePath = keyof typeof Table.path.toCN;
export type TableNameEN = keyof typeof Table.name.toCN;
export type TableNameCN = keyof typeof Table.name.fromCN;

/**
* 不同类型的CSV文件导入为Json后会放入不同的文件夹
Expand Down
6 changes: 3 additions & 3 deletions src/editorTable/CSV/CSVeditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path';
import { env } from "../../env";
import { SpinLock, isCSV, isPathValid } from "../../utility";
import { allocateNewUIDofEditorTableItem } from '../editorTableUtility';
import {EditorTableType, englishTypeNameToChineseTypeName,chineseTypeNameToEnglishTypeName, TableNameEN, TableNameCN
import {EditorTableType, Table, TableNameEN, TableNameCN
} from '../../constants';
/**
* 物编数据CSV表格的编辑器s
Expand Down Expand Up @@ -143,7 +143,7 @@ export class CSVeditor {
public async addNewUIDandNameInCSVwithoutConflict(typeStr:TableNameEN,name:string) {
let uid: number =await this.allocateNewUIDofEditorTableItemToCSV();
this.addNewUIDandNameInCSV(typeStr, uid, name);
let englishTypeStr = englishTypeNameToChineseTypeName[typeStr];
let englishTypeStr = Table.name.toCN[typeStr];
vscode.window.showInformationMessage("添加 " + englishTypeStr +": "+name+" 成功");
}

Expand Down Expand Up @@ -226,7 +226,7 @@ export class CSVeditor {
vscode.window.showErrorMessage("选择的项目的label或description或label不存在");
return;
}
let englishEditorTableType = chineseTypeNameToEnglishTypeName[editorTableItem.description as TableNameCN];
let englishEditorTableType = Table.name.fromCN[editorTableItem.description as TableNameCN];
let uid: number = Number(editorTableItem.detail);
this.addNewUIDandNameInCSV(englishEditorTableType, uid, editorTableItem.label);

Expand Down
4 changes: 2 additions & 2 deletions src/editorTable/EXCEL/excel2Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode';

import { ImportRule } from './importRule';
import { saveEditorTableItemJson } from '../editorTableItemJson';
import { chineseTypeNameToEnglishTypeName } from '../../constants';
import { Table } from '../../constants';
import { excelConverter } from './excelConverter';


Expand All @@ -11,7 +11,7 @@ export class excel2Json extends excelConverter {
super(rule, excelPath, targetPath);
}
public interpret(){
let editorTableType = chineseTypeNameToEnglishTypeName[this.rule.editorTableType];
let editorTableType = Table.name.fromCN[this.rule.editorTableType];
for(let index in this.targetDatas){
//把获取到的数据转换成json
//异步的
Expand Down
9 changes: 5 additions & 4 deletions src/editorTable/EXCEL/excelExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import * as fs from 'fs-extra';
import * as vscode from 'vscode';
import * as path from 'path';

import { TablePath, TableNameCN, TableNameEN, chineseTypeNameToEnglishTypeName, csvTypeToPath, englishPathToChinese } from '../../constants';
import { TablePath, csvTypeToPath, Table } from '../../constants';
import { excel2Json } from './excel2Json';
import { ImportRule } from './importRule';
import { env } from '../../env';
import { table } from 'y3-helper';

class editorTableDir{
public fileNameList: string[];
Expand Down Expand Up @@ -42,8 +43,8 @@ export class excelExporter {
for (const file of files) {
const filePath = path.join(editorTablePath, file);
let editorTableType: string = file;
if (editorTableType in englishPathToChinese) {
editorTableType = englishPathToChinese[editorTableType as TablePath];
if (editorTableType in Table.path.toCN) {
editorTableType = Table.path.toCN[editorTableType as TablePath];
const jsFileList = await fs.promises.readdir(filePath);// 此目录下的编文件js文件目录
this.editorTableDatas[editorTableType] = new editorTableDir(jsFileList, filePath);
}
Expand All @@ -55,7 +56,7 @@ export class excelExporter {

async runRule(rule: ImportRule) {
let excelPath = rule.excelRelativePath ? vscode.Uri.joinPath(this.excelPath, rule.excelRelativePath) : undefined;
let editorTableType = chineseTypeNameToEnglishTypeName[rule.editorTableType];
let editorTableType = Table.name.fromCN[rule.editorTableType];
let targetPath: vscode.Uri = vscode.Uri.joinPath(this.editorTablePath, csvTypeToPath[editorTableType]);
//let converter = new excel2Json(rule, excelPath, targetPath);
//await converter.convert();//TODO: 把excel数据通过converter转换为目标数据
Expand Down
10 changes: 4 additions & 6 deletions src/editorTable/editorTableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import * as path from 'path';
import * as fs from 'fs';
import { env } from '../env';
import { addNewEditorTableItemInProject } from './editorTableUtility';
import { englishPathToChinese, chineseTypeNameToEnglishTypeName, TableNameCN, TableNameEN } from '../constants';
import { Table, TableNameCN, TableNameEN, TablePath } from '../constants';
import { isPathValid, isJson, getFileNameByVscodeUri, hash, toUnicodeIgnoreASCII } from '../utility';


export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode> {
private _onDidChangeTreeData: vscode.EventEmitter<FileNode | undefined> = new vscode.EventEmitter<FileNode | undefined>();
readonly onDidChangeTreeData: vscode.Event<FileNode | undefined> = this._onDidChangeTreeData.event;
public readonly englishPathToChinese: { [key: string]: string };
private editorTablePath: string = "";
private languageJson: any = undefined;

constructor() {
this.englishPathToChinese = englishPathToChinese;
if (!vscode.workspace.workspaceFolders) {
vscode.window.showErrorMessage("当前未打开工作目录");
return;
Expand All @@ -33,7 +31,7 @@ export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode
* @returns true or false 成功或失败
*/
public createNewTableItemByFileNode(fileNode: FileNode,name:string) :boolean{
let editorTableType = chineseTypeNameToEnglishTypeName[fileNode.label as TableNameCN];
let editorTableType = Table.name.fromCN[fileNode.label as TableNameCN];
if (!editorTableType) {
return false;
}
Expand Down Expand Up @@ -142,8 +140,8 @@ export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode
fileNodes.push(fileNode);
}
else if (stat.isDirectory()) {
if (label in this.englishPathToChinese) {
label = this.englishPathToChinese[label];
if (label in Table.path.toCN) {
label = Table.path.toCN[label as TablePath];
const files = await fs.promises.readdir(filePath);// 检查此目录下有多少个物编文件
label += '(' + files.length + ')';//显示为 单位(10) 括号内的数字为有多少个物编项目
const fileNode = new FileNode(
Expand Down
14 changes: 7 additions & 7 deletions src/editorTable/editorTableUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
HashSet, SpinLock, hash, toUnicodeIgnoreASCII
} from '../utility';
import {
table, englishPathToChinese,
EditorTableType, englishTypeNameToChineseTypeName
Table,
EditorTableType,
} from '../constants';
import { error } from 'console';

Expand All @@ -32,7 +32,7 @@ export function haveEditorTableJson(editorTableUri:vscode.Uri,uid: number): bool
return false;
}
let jsonName: string = String(uid) + '.json';
for (let folderName in englishPathToChinese) {
for (let folderName in Table.path.toCN) {
let filePath = vscode.Uri.joinPath(editorTableUri, folderName, jsonName);
if (isFileValid(filePath)) {
return true;
Expand Down Expand Up @@ -73,7 +73,7 @@ export function allocateNewUIDofEditorTableItem(editorTableUri: vscode.Uri) :num
//只搜索九类物编数据的文件夹下的物编数据 不递归搜索
for (let type in EditorTableType) {
let typeStr = EditorTableType[type as keyof typeof EditorTableType];
let folderName: string = table.path.fromName[typeStr];
let folderName: string = Table.path.fromName[typeStr];
res = res.concat(searchEditorTableItemsInFolder(typeStr, path.join(env.editorTablePath, folderName), query));
}
return res;
Expand Down Expand Up @@ -124,7 +124,7 @@ export function searchEditorTableItemsInFolder(editorTableType: TableNameEN, pat
label = name + "(" + uid + ")";//转为"这是一个单位(134219828)"的格式
}

let chineseTypeName = englishTypeNameToChineseTypeName[editorTableType];
let chineseTypeName = Table.name.toCN[editorTableType];
if (label.includes(query)||chineseTypeName.includes(query)) {
let editorTableJsonUri: vscode.Uri = vscode.Uri.file(filePath);
let quickPickItem: vscode.QuickPickItem = {
Expand Down Expand Up @@ -213,7 +213,7 @@ export async function searchAllEditorTableItemInCSV(query: string):Promise< vsco
}
let uid: number = row['uid'];
let name: string = row['name'];
let ChinesTypeName = englishTypeNameToChineseTypeName[typeStr];
let ChinesTypeName = Table.name.toCN[typeStr];

// 如果uid匹配或者名称匹配或者类型匹配都纳入结果
if (String(uid).includes(query)||name.includes(query)||ChinesTypeName.includes(query)) {
Expand Down Expand Up @@ -298,7 +298,7 @@ function getAllEditorTableItemInfoInProject(): EditorTableItemInfo[]{
//只搜索九类物编数据的文件夹下的物编数据 不递归搜索
Object.values(EditorTableType).forEach(type => {
let typeStr = type;
let folderName: string = table.path.fromName[typeStr];
let folderName: string = Table.path.fromName[typeStr];
res = res.concat(getAllEditorTableItemInfoInFolder(type, path.join(env.editorTablePath, folderName)));
});

Expand Down
6 changes: 3 additions & 3 deletions src/editorTable/templateGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import * as vscode from 'vscode';
import * as fs from 'fs-extra';
import * as path from 'path';
import { isPathValid } from '../utility';
import { TableNameCN, TableNameEN, chineseTypeNameToEnglishTypeName, englishTypeNameToChineseTypeName } from '../constants';
import { TableNameCN, TableNameEN, Table } from '../constants';


export class TemplateGenerator {
private readonly englishToChinese;
private readonly chineseToEnglish;
public constructor() {
this.chineseToEnglish = chineseTypeNameToEnglishTypeName;
this.englishToChinese = englishTypeNameToChineseTypeName;
this.chineseToEnglish = Table.name.fromCN;
this.englishToChinese = Table.name.toCN;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { runShell } from './runShell';
import { LuaDocMaker } from './makeLuaDoc';
import { GameLauncher } from './launchGame';
import { TemplateGenerator } from './editorTable/templateGenerator';
import { TableNameEN, englishPathToChinese } from './constants';
import { TableNameEN, Table } from './constants';
import { NetworkServer } from './networkServer';
import * as console from './console';
import {
Expand Down Expand Up @@ -579,7 +579,7 @@ class Helper {
const goEditorTableSymbolProvider = new GoEditorTableSymbolProvider(
env.editorTablePath,
env.languageJson,
englishPathToChinese
Table.path.toCN,
);

this.context.subscriptions.push(vscode.languages.registerWorkspaceSymbolProvider(goEditorTableSymbolProvider));
Expand Down

0 comments on commit 166885d

Please sign in to comment.