Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Nov 7, 2024
1 parent bab0c7b commit 290ec4d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 56 deletions.
109 changes: 57 additions & 52 deletions src/editorTable/editorTable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Table } from "../constants";
import { env } from "../env";
import * as vscode from "vscode";
import * as y3 from 'y3-helper';
import { queue, throttle } from "../utility/decorators";
Expand Down Expand Up @@ -66,7 +65,7 @@ export class EditorObject<N extends Table.NameCN> {
private _name?: string;
public text?: string;
public uri?: vscode.Uri;
constructor(public tableName: N, public key: number) {}
constructor(private manager: EditorManager, public tableName: N, public key: number) {}

toString() {
return `{物编对象|${this.name}|${this.tableName}-${this.key}}`;
Expand Down Expand Up @@ -190,7 +189,7 @@ export class EditorObject<N extends Table.NameCN> {
}

public getFieldInfo(field: string) {
let table = openTable(this.tableName);
let table = this.manager.openTable(this.tableName);
return table.getFieldInfo(field);
}

Expand Down Expand Up @@ -242,24 +241,6 @@ export class EditorObject<N extends Table.NameCN> {
}
}

async function loadObject<N extends Table.NameCN>(tableName: N, key: number) {
let table = openTable(tableName);
let uri = table.getUri(key);
let file = await y3.fs.readFile(uri);
if (!file) {
return null;
}
await ready();
try {
let obj = new EditorObject(tableName, key);
obj.uri = uri;
obj.text = file.string;
return obj;
} catch {
return null;
}
}

interface CreateOptions<N extends Table.NameCN> {
/**
* 新对象的名称,如果不填则使用默认名称
Expand All @@ -284,15 +265,15 @@ export class EditorTable<N extends Table.NameCN> extends vscode.Disposable {
public nameEN;
private _objectCache: { [key: number]: EditorObject<N> | null | undefined } = {};
private watcher?: vscode.FileSystemWatcher;
constructor(public name: N) {
constructor(private manager: EditorManager, public name: N) {
super(() => {
this.watcher?.dispose();
});
if (!env.editorTableUri) {
if (!manager.rootUri) {
throw new Error('未选择地图路径');
}
this.nameEN = Table.name.fromCN[name];
this.uri = vscode.Uri.joinPath(env.editorTableUri, Table.path.fromCN[name]);
this.uri = vscode.Uri.joinPath(manager.rootUri, Table.path.fromCN[name]);
}

toString() {
Expand All @@ -306,7 +287,7 @@ export class EditorTable<N extends Table.NameCN> extends vscode.Disposable {
*/
public async get(key: number): Promise<EditorObject<N> | undefined> {
if (this._objectCache[key] === undefined) {
this._objectCache[key] = await loadObject<N>(this.name, key);
this._objectCache[key] = await this.manager.loadObject<N>(this.name, key);
}
return this._objectCache[key] ?? undefined;
}
Expand Down Expand Up @@ -442,7 +423,7 @@ export class EditorTable<N extends Table.NameCN> extends vscode.Disposable {
json.set('key', key);
json.set('_ref_', key);

let obj = new EditorObject(this.name, key);
let obj = new EditorObject(this.manager, this.name, key);
obj.uri = this.getUri(key);
obj.text = json.text;

Expand Down Expand Up @@ -567,25 +548,12 @@ export class EditorTable<N extends Table.NameCN> extends vscode.Disposable {
}
}

let editorTables: { [key: string]: any } = {};

/**
* 打开物编表
* @param tableName 哪种表
* @returns 表对象
*/
export function openTable<N extends Table.NameCN>(tableName: N): EditorTable<N> {
let table = editorTables[tableName]
?? (editorTables[tableName] = new EditorTable(tableName));
return table;
}

/**
* 根据文件名获取文件对应的key
* @param fileName 文件名
* @returns 文件名对应的key
*/
export function getFileKey(fileName: string): number | undefined {
function getFileKey(fileName: string): number | undefined {
if (!fileName.toLowerCase().endsWith('.json')) {
return;
}
Expand Down Expand Up @@ -627,19 +595,54 @@ export async function getObject(uri: vscode.Uri | string): Promise<EditorObject<
if (!file) {
return;
}
const obj = new EditorObject(nameCN, key);
obj.uri = uri;
obj.text = file.string;
let map = y3.env.project?.findMapByUri(uri);
if (!map) {
return;
}
const obj = await map.editorTable.loadObject(nameCN, key) ?? undefined;
return obj;
}

class Manager {
export class EditorManager {
constructor(public rootUri: vscode.Uri) {}

editorTables: Record<string, any> = {};

async loadObject<N extends Table.NameCN>(tableName: N, key: number) {
let table = this.openTable(tableName);
let uri = table.getUri(key);
let file = await y3.fs.readFile(uri);
if (!file) {
return null;
}
await ready();
try {
let obj = new EditorObject(this, tableName, key);
obj.uri = uri;
obj.text = file.string;
return obj;
} catch {
return null;
}
}

/**
* 打开物编表
* @param tableName 哪种表
* @returns 表对象
*/
openTable<N extends Table.NameCN>(tableName: N): EditorTable<N> {
let table = this.editorTables[tableName]
?? (this.editorTables[tableName] = new EditorTable(this, tableName));
return table;
}

@queue()
async getAllObjects() {
let allObjects: EditorObject<Table.NameCN>[] = [];
let promises: Promise<any>[] = [];
for (const tableName in Table.name.fromCN) {
const table = openTable(tableName as Table.NameCN);
const table = this.openTable(tableName as Table.NameCN);
table.getList().then((list) => {
for (const key of list) {
let promise = table.get(key).then((obj) => obj && allObjects.push(obj));
Expand All @@ -653,15 +656,17 @@ class Manager {
}
}

const ManagerInstance = new Manager();

/**
* 获取所有的对象(速度比较慢)
* @returns 所有对象
* 打开物编表
* @param tableName 哪种表
* @returns 表对象
*/
export async function getAllObjects() {
return await ManagerInstance.getAllObjects();
export function openTable<N extends Table.NameCN>(tableName: N): EditorTable<N> {
let map = y3.env.currentMap!;
return map.editorTable.openTable(tableName);
}

export function init() {
export async function getAllObjects() {
let map = y3.env.currentMap!;
return await map.editorTable.getAllObjects();
}
3 changes: 0 additions & 3 deletions src/editorTable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ export * from './CSV/CSVimporter';
export * from './CSV/CSVeditor';
import * as csv from './CSV';
import * as excel from './excel';
import * as editorTable from './editorTable';
import * as language from './language';
import * as languageFeature from './languageFeature';
import * as treeView from './treeView';
import * as y3 from 'y3-helper';

class Item implements vscode.QuickPickItem {
constructor(public label: string) {}
Expand All @@ -31,7 +29,6 @@ vscode.commands.registerCommand('y3-helper.testExcel', async () => {
});

export function init() {
editorTable.init();
language.init();
treeView.init();
languageFeature.init();
Expand Down
10 changes: 9 additions & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { isPathValid } from './utility';
import { queue } from './utility/decorators';
import * as y3 from 'y3-helper';
import * as jsonc from 'jsonc-parser';
import { EditorManager } from './editorTable/editorTable';

type EditorVersion = '1.0' | '2.0' | 'unknown';

class Map {
id: bigint = 0n;
constructor(public name: string, public uri: vscode.Uri) {}
editorTable: EditorManager;
constructor(public name: string, public uri: vscode.Uri) {
this.editorTable = new EditorManager(vscode.Uri.joinPath(this.uri, 'editor_table'));
}

async start() {
let headerMap = await y3.fs.readFile(vscode.Uri.joinPath(this.uri, 'header.map'));
Expand Down Expand Up @@ -65,6 +69,10 @@ class Project {

this.entryMap = this.maps.find(map => map.id === this.entryMapId);
}

findMapByUri(uri: vscode.Uri) {
return this.maps.find(map => uri.toString().startsWith(map.uri.toString()));
}
}

class Env {
Expand Down

0 comments on commit 290ec4d

Please sign in to comment.