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 84f5a74 commit 2a8ab79
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 41 deletions.
92 changes: 75 additions & 17 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const englishPathToChinese = {
"technologyall": "科技"
} as const;

export type EditorTableName = keyof typeof englishPathToChinese;

/**
* 物编数据种类对应的中文名
Expand All @@ -47,7 +46,6 @@ export const englishTypeNameToChineseTypeName = {
"sound": "声音"
} as const;

export type ObjectTypeNameEN = keyof typeof englishTypeNameToChineseTypeName;

/**
* 物编数据种类对应的英文名
Expand All @@ -64,23 +62,83 @@ export const chineseTypeNameToEnglishTypeName = {
"声音": "sound"
} as const;

export type ObjectTypeNameCN = keyof typeof chineseTypeNameToEnglishTypeName;

/**
* 物编数据类型与其在Y3项目中存放的文件夹名的对应关系
*/
export const editorTableTypeToFolderName = {
"unit": "editorunit",
"decoration": "editordecoration",
"item": "editoritem",
"ability": "abilityall",
"modifier": "modifierall",
"projectile": "projectileall",
"technology": "technologyall",
"destructible": "editordestructible",
"sound":"soundall"
export const table = {
path: {
toCN: {
"editorunit": "单位",
"soundall": "声音",
"abilityall": "技能",
"editordecoration": "装饰物",
"editordestructible": "可破坏物",
"editoritem": "物品",
"modifierall": "魔法效果",
"projectileall": "投射物",
"technologyall": "科技",
},
fromCN: {
"单位": "editorunit",
"声音": "soundall",
"技能": "abilityall",
"装饰物": "editordecoration",
"可破坏物": "editordestructible",
"物品": "editoritem",
"魔法效果": "modifierall",
"投射物": "projectileall",
"科技": "technologyall",
},
toName: {
"editorunit": "unit",
"soundall": "sound",
"abilityall": "ability",
"editordecoration": "decoration",
"editordestructible": "destructible",
"editoritem": "item",
"modifierall": "modifier",
"projectileall": "projectile",
"technologyall": "technology",
},
fromName: {
"unit": "editorunit",
"sound": "soundall",
"ability": "abilityall",
"decoration": "editordecoration",
"destructible": "editordestructible",
"item": "editoritem",
"modifier": "modifierall",
"projectile": "projectileall",
"technology": "technologyall",
},
},
name: {
toCN: {
"unit": "单位",
"decoration": "装饰物",
"item": "物品",
"ability": "技能",
"modifier": "魔法效果",
"projectile": "投射物",
"technology": "科技",
"destructible": "可破坏物",
"sound": "声音",
},
fromCN: {
"单位": "unit",
"装饰物": "decoration",
"物品": "item",
"技能": "ability",
"魔法效果": "modifier",
"投射物": "projectile",
"科技": "technology",
"可破坏物": "destructible",
"声音": "sound",
},
},
} 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;

/**
* 不同类型的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, ObjectTypeNameEN, ObjectTypeNameCN
import {EditorTableType, englishTypeNameToChineseTypeName,chineseTypeNameToEnglishTypeName, TableNameEN, TableNameCN
} from '../../constants';
/**
* 物编数据CSV表格的编辑器s
Expand Down Expand Up @@ -140,7 +140,7 @@ export class CSVeditor {
* @param typeStr 物编数据种类
* @param name 物编项目名称
*/
public async addNewUIDandNameInCSVwithoutConflict(typeStr:ObjectTypeNameEN,name:string) {
public async addNewUIDandNameInCSVwithoutConflict(typeStr:TableNameEN,name:string) {
let uid: number =await this.allocateNewUIDofEditorTableItemToCSV();
this.addNewUIDandNameInCSV(typeStr, uid, name);
let englishTypeStr = englishTypeNameToChineseTypeName[typeStr];
Expand Down Expand Up @@ -226,7 +226,7 @@ export class CSVeditor {
vscode.window.showErrorMessage("选择的项目的label或description或label不存在");
return;
}
let englishEditorTableType = chineseTypeNameToEnglishTypeName[editorTableItem.description as ObjectTypeNameCN];
let englishEditorTableType = chineseTypeNameToEnglishTypeName[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/excelExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs-extra';
import * as vscode from 'vscode';
import * as path from 'path';

import { EditorTableName, ObjectTypeNameCN, ObjectTypeNameEN, chineseTypeNameToEnglishTypeName, csvTypeToPath, englishPathToChinese } from '../../constants';
import { TablePath, TableNameCN, TableNameEN, chineseTypeNameToEnglishTypeName, csvTypeToPath, englishPathToChinese } from '../../constants';
import { excel2Json } from './excel2Json';
import { ImportRule } from './importRule';
import { env } from '../../env';
Expand Down Expand Up @@ -43,7 +43,7 @@ export class excelExporter {
const filePath = path.join(editorTablePath, file);
let editorTableType: string = file;
if (editorTableType in englishPathToChinese) {
editorTableType = englishPathToChinese[editorTableType as EditorTableName];
editorTableType = englishPathToChinese[editorTableType as TablePath];
const jsFileList = await fs.promises.readdir(filePath);// 此目录下的编文件js文件目录
this.editorTableDatas[editorTableType] = new editorTableDir(jsFileList, filePath);
}
Expand Down
6 changes: 3 additions & 3 deletions src/editorTable/EXCEL/importRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Int, Str, Templete } from "./fieldTypes";
import * as constants from "./../../constants";

export class ImportRule {
public editorTableType: constants.ObjectTypeNameCN;
public editorTableType: constants.TableNameCN;
public excelRelativePath?: string;
public sheet?: string;
public fieldDefs: {
Expand All @@ -14,7 +14,7 @@ export class ImportRule {
public dataRehandle: Function | undefined;

constructor(
editorTableType: constants.ObjectTypeNameCN,
editorTableType: constants.TableNameCN,
excelRelativePath?: string,
sheetName?: string
) {
Expand All @@ -23,7 +23,7 @@ export class ImportRule {
this.sheet = sheetName;
}
public resetRule(
editorTableType: constants.ObjectTypeNameCN,
editorTableType: constants.TableNameCN,
excelRelativePath?: string,
sheetName?: string
) {
Expand Down
2 changes: 1 addition & 1 deletion src/editorTable/EXCEL/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as y3 from 'y3-helper';

export function test() {
let rule = new y3.excel.ImportRule('单位');
let rule = y3.table.open('单位');

}
16 changes: 16 additions & 0 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TableNameCN } from "src/constants";
import { env } from "src/env";

class EditorTable {
constructor(
public tableName: TableNameCN
) {
}

}

export function open(tableName: TableNameCN) {
if (!env.editorTableUri) {
throw new Error('未选择地图路径');
}
}
6 changes: 3 additions & 3 deletions src/editorTable/editorTableProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import * as fs from 'fs';
import { env } from '../env';
import { addNewEditorTableItemInProject } from './editorTableUtility';
import { englishPathToChinese, chineseTypeNameToEnglishTypeName, ObjectTypeNameCN, ObjectTypeNameEN } from '../constants';
import { englishPathToChinese, chineseTypeNameToEnglishTypeName, TableNameCN, TableNameEN } from '../constants';
import { isPathValid, isJson, getFileNameByVscodeUri, hash, toUnicodeIgnoreASCII } from '../utility';


Expand Down Expand Up @@ -33,11 +33,11 @@ export class EditorTableDataProvider implements vscode.TreeDataProvider<FileNode
* @returns true or false 成功或失败
*/
public createNewTableItemByFileNode(fileNode: FileNode,name:string) :boolean{
let editorTableType = chineseTypeNameToEnglishTypeName[fileNode.label as ObjectTypeNameCN];
let editorTableType = chineseTypeNameToEnglishTypeName[fileNode.label as TableNameCN];
if (!editorTableType) {
return false;
}
if (addNewEditorTableItemInProject(editorTableType as ObjectTypeNameEN, name)) {
if (addNewEditorTableItemInProject(editorTableType as TableNameEN, name)) {
this.refresh();
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions src/editorTable/editorTableUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import * as path from 'path';
import * as fs from 'fs';
import * as vscode from 'vscode';
import { env } from "../env";
import { ObjectTypeNameCN, ObjectTypeNameEN, csvTypeToPath } from '../constants';
import { TableNameCN, TableNameEN, csvTypeToPath } from '../constants';
import { EditorTableItemInfo } from './types';
import {
isFileValid, randomInt, isJson, isCSV, isPathValid,
HashSet, SpinLock, hash, toUnicodeIgnoreASCII
} from '../utility';
import {
englishPathToChinese, editorTableTypeToFolderName,
table, englishPathToChinese,
EditorTableType, englishTypeNameToChineseTypeName
} from '../constants';
import { error } from 'console';
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 = editorTableTypeToFolderName[typeStr];
let folderName: string = table.path.fromName[typeStr];
res = res.concat(searchEditorTableItemsInFolder(typeStr, path.join(env.editorTablePath, folderName), query));
}
return res;
Expand All @@ -86,7 +86,7 @@ export function allocateNewUIDofEditorTableItem(editorTableUri: vscode.Uri) :num
* @param query
* @returns
*/
export function searchEditorTableItemsInFolder(editorTableType: ObjectTypeNameEN, pathStr: string, query: string): vscode.QuickPickItem[] {
export function searchEditorTableItemsInFolder(editorTableType: TableNameEN, pathStr: string, query: string): vscode.QuickPickItem[] {
let res: vscode.QuickPickItem[] = [];
const files = fs.readdirSync(pathStr);
files.forEach(file => {
Expand Down Expand Up @@ -298,7 +298,7 @@ function getAllEditorTableItemInfoInProject(): EditorTableItemInfo[]{
//只搜索九类物编数据的文件夹下的物编数据 不递归搜索
Object.values(EditorTableType).forEach(type => {
let typeStr = type;
let folderName: string = editorTableTypeToFolderName[typeStr];
let folderName: string = table.path.fromName[typeStr];
res = res.concat(getAllEditorTableItemInfoInFolder(type, path.join(env.editorTablePath, folderName)));
});

Expand Down Expand Up @@ -358,7 +358,7 @@ function getAllEditorTableItemInfoInFolder(editorTableType: EditorTableType, pat
return res;
}

export function addNewEditorTableItemInProject(editorTableType: ObjectTypeNameEN,name:string):boolean {
export function addNewEditorTableItemInProject(editorTableType: TableNameEN,name:string):boolean {
if (!env.editorTableUri) {
return false;
}
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,7 +2,7 @@ import * as vscode from 'vscode';
import * as fs from 'fs-extra';
import * as path from 'path';
import { isPathValid } from '../utility';
import { ObjectTypeNameCN, ObjectTypeNameEN, chineseTypeNameToEnglishTypeName, englishTypeNameToChineseTypeName } from '../constants';
import { TableNameCN, TableNameEN, chineseTypeNameToEnglishTypeName, englishTypeNameToChineseTypeName } from '../constants';


export class TemplateGenerator {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class TemplateGenerator {
private async renameTemplateCSVtoChinese() {
for (const key in this.englishToChinese) {
let oldFile: string = key;
let newFile: string = this.englishToChinese[key as ObjectTypeNameEN];
let newFile: string = this.englishToChinese[key as TableNameEN];
oldFile = path.join(__dirname, "../../template/csv/" + oldFile);
newFile = path.join(__dirname, "../../template/csv/" + newFile);
if (isPathValid(newFile)) {
Expand All @@ -65,7 +65,7 @@ export class TemplateGenerator {
private async renameTemplateCSVtoEnglish() {
for (let key in this.chineseToEnglish) {
let oldFile: string = key;
let newFile: string = this.chineseToEnglish[key as ObjectTypeNameCN];
let newFile: string = this.chineseToEnglish[key as TableNameCN];
oldFile = path.join(__dirname, "../../template/csv/" + oldFile);
newFile = path.join(__dirname, "../../template/csv/" + newFile);
if (isPathValid(newFile)) {
Expand Down
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import moduleAlias from 'module-alias';

moduleAlias.addAliases({
'y3-helper': __dirname + '/y-3helper'
'y3-helper': __dirname + '/y3-helper'
});

import * as fs from 'fs';
Expand All @@ -14,7 +14,7 @@ import { runShell } from './runShell';
import { LuaDocMaker } from './makeLuaDoc';
import { GameLauncher } from './launchGame';
import { TemplateGenerator } from './editorTable/templateGenerator';
import { ObjectTypeNameEN, englishPathToChinese } from './constants';
import { TableNameEN, englishPathToChinese } from './constants';
import { NetworkServer } from './networkServer';
import * as console from './console';
import {
Expand Down Expand Up @@ -346,7 +346,7 @@ class Helper {
vscode.window.showInformationMessage('未选择');
return;
}
csvEditor.addNewUIDandNameInCSVwithoutConflict(selection.description as ObjectTypeNameEN, value);
csvEditor.addNewUIDandNameInCSVwithoutConflict(selection.description as TableNameEN, value);
}
});
});
Expand Down
1 change: 1 addition & 0 deletions src/y3-helper.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as excel from './editorTable/EXCEL';
export * as table from './editorTable/editorTable';
export * from './tools';

0 comments on commit 2a8ab79

Please sign in to comment.