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 76df555 commit e9f7caa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
43 changes: 23 additions & 20 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,29 @@ export namespace Table {
export type NameCN = keyof typeof name.fromCN;
}

/**
* 不同类型的CSV文件导入为Json后会放入不同的文件夹
*/
export const csvTypeToPath = {
"unit": "editorunit",
"sound": "soundall",
"ability": "abilityall",
"model": "editormodel",
"decoration": "editordecoration",
"destructible": "editordestructible",
"effect": "editoreffect",
"icon": "editoricon",
"item": "editoritem",
"physics_object": "editorphysicsobject",
"physics_object_logic": "editorphysicsobjectlogic",
"modifier": "modifierall",
"projectile": "projectileall",
"store": "storeall",
"technology": "technologyall"
} as const;
export namespace CSV {
export const type = {
toPath: {
"unit": "editorunit",
"sound": "soundall",
"ability": "abilityall",
"model": "editormodel",
"decoration": "editordecoration",
"destructible": "editordestructible",
"effect": "editoreffect",
"icon": "editoricon",
"item": "editoritem",
"physics_object": "editorphysicsobject",
"physics_object_logic": "editorphysicsobjectlogic",
"modifier": "modifierall",
"projectile": "projectileall",
"store": "storeall",
"technology": "technologyall"
} as const,
} as const;

export type Type = keyof typeof type.toPath;
}

// 默认情况下各类型物编数据CSV文件的相对路径 (相对于工程项目的script文件)
export const defaultTableTypeToCSVfolderPath = {
Expand Down
10 changes: 4 additions & 6 deletions src/editorTable/CSV/CSVimporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import * as vscode from 'vscode';
import * as fs from 'fs';
import { env } from "../../env";
import { isPathValid } from '../../utility';
import { csvTypeToPath } from "../../constants";
import { CSV } from "../../constants";
import { saveRowOfCSV } from '../editorTableItemJson';


export class CSVimporter
{
private readonly csvTypeToPath: Readonly<{ [key: string]: string }>;
public constructor() {
this.csvTypeToPath = csvTypeToPath;
}

/**
Expand All @@ -33,7 +31,7 @@ export class CSVimporter
vscode.window.showErrorMessage("未找到CSV表格的路径,请从模板中生成");
return false;
}
if (!await this.importAllCSVinFolder(csvFolderUri, key)) {
if (!await this.importAllCSVinFolder(csvFolderUri, key as CSV.Type)) {
return false;
}
}
Expand All @@ -47,12 +45,12 @@ export class CSVimporter
* @param tableType
* @returns
*/
private async importAllCSVinFolder(folder: vscode.Uri, tableType:string): Promise<boolean> {
private async importAllCSVinFolder(folder: vscode.Uri, tableType: CSV.Type): Promise<boolean> {
if (!env.editorTableUri) {
vscode.window.showErrorMessage("物编数据表路径为空");
return false;
}
let targetTableFolder = this.csvTypeToPath[tableType];
let targetTableFolder = CSV.type.toPath[tableType];
let targetEditorTablePath: vscode.Uri = vscode.Uri.joinPath(env.editorTableUri, targetTableFolder);
let files = await vscode.workspace.fs.readDirectory(folder);
for (const file of files) {
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 { csvTypeToPath, Table } from '../../constants';
import { CSV, Table } from '../../constants';
import { excel2Json } from './excel2Json';
import { ImportRule } from './importRule';
import { env } from '../../env';
Expand Down Expand Up @@ -57,7 +57,7 @@ export class excelExporter {
async runRule(rule: ImportRule) {
let excelPath = rule.excelRelativePath ? vscode.Uri.joinPath(this.excelPath, rule.excelRelativePath) : undefined;
let editorTableType = Table.name.fromCN[rule.editorTableType];
let targetPath: vscode.Uri = vscode.Uri.joinPath(this.editorTablePath, csvTypeToPath[editorTableType]);
let targetPath: vscode.Uri = vscode.Uri.joinPath(this.editorTablePath, CSV.type.toPath[editorTableType]);
//let converter = new excel2Json(rule, excelPath, targetPath);
//await converter.convert();//TODO: 把excel数据通过converter转换为目标数据
}
Expand Down
6 changes: 3 additions & 3 deletions src/editorTable/editorTableUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as path from 'path';
import * as fs from 'fs';
import * as vscode from 'vscode';
import { env } from "../env";
import { Table, csvTypeToPath } from '../constants';
import { Table, CSV } from '../constants';
import { EditorTableItemInfo } from './types';
import {
isFileValid, randomInt, isJson, isCSV, isPathValid,
Expand Down Expand Up @@ -347,12 +347,12 @@ function getAllEditorTableItemInfoInFolder(editorTableType: Table.NameEN, pathSt
return res;
}

export function addNewEditorTableItemInProject(editorTableType: Table.NameEN,name:string):boolean {
export function addNewEditorTableItemInProject(editorTableType: Table.NameEN, name:string):boolean {
if (!env.editorTableUri) {
return false;
}
let uid: number = allocateNewUIDofEditorTableItem(env.editorTableUri);
let targetPath: vscode.Uri = vscode.Uri.joinPath(env.editorTableUri, csvTypeToPath[editorTableType], String(uid) + '.json');
let targetPath: vscode.Uri = vscode.Uri.joinPath(env.editorTableUri, CSV.type.toPath[editorTableType], String(uid) + '.json');

try {
let templateJsonStr:string = fs.readFileSync(path.join(__dirname, "../../template/json_template/" + editorTableType + ".json"), 'utf8');
Expand Down

0 comments on commit e9f7caa

Please sign in to comment.