From 290ec4d61f1a67267d7c8856b04e1854554478e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 7 Nov 2024 15:00:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editorTable/editorTable.ts | 109 +++++++++++++++++---------------- src/editorTable/index.ts | 3 - src/env.ts | 10 ++- 3 files changed, 66 insertions(+), 56 deletions(-) diff --git a/src/editorTable/editorTable.ts b/src/editorTable/editorTable.ts index 59eef5b..9b75ac6 100644 --- a/src/editorTable/editorTable.ts +++ b/src/editorTable/editorTable.ts @@ -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"; @@ -66,7 +65,7 @@ export class EditorObject { 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}}`; @@ -190,7 +189,7 @@ export class EditorObject { } public getFieldInfo(field: string) { - let table = openTable(this.tableName); + let table = this.manager.openTable(this.tableName); return table.getFieldInfo(field); } @@ -242,24 +241,6 @@ export class EditorObject { } } -async function loadObject(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 { /** * 新对象的名称,如果不填则使用默认名称 @@ -284,15 +265,15 @@ export class EditorTable extends vscode.Disposable { public nameEN; private _objectCache: { [key: number]: EditorObject | 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() { @@ -306,7 +287,7 @@ export class EditorTable extends vscode.Disposable { */ public async get(key: number): Promise | undefined> { if (this._objectCache[key] === undefined) { - this._objectCache[key] = await loadObject(this.name, key); + this._objectCache[key] = await this.manager.loadObject(this.name, key); } return this._objectCache[key] ?? undefined; } @@ -442,7 +423,7 @@ export class EditorTable 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; @@ -567,25 +548,12 @@ export class EditorTable extends vscode.Disposable { } } -let editorTables: { [key: string]: any } = {}; - -/** - * 打开物编表 - * @param tableName 哪种表 - * @returns 表对象 - */ -export function openTable(tableName: N): EditorTable { - 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; } @@ -627,19 +595,54 @@ export async function getObject(uri: vscode.Uri | string): Promise = {}; + + async loadObject(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(tableName: N): EditorTable { + let table = this.editorTables[tableName] + ?? (this.editorTables[tableName] = new EditorTable(this, tableName)); + return table; + } + @queue() async getAllObjects() { let allObjects: EditorObject[] = []; let promises: Promise[] = []; 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)); @@ -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(tableName: N): EditorTable { + 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(); } diff --git a/src/editorTable/index.ts b/src/editorTable/index.ts index 36823ba..dd6887a 100644 --- a/src/editorTable/index.ts +++ b/src/editorTable/index.ts @@ -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) {} @@ -31,7 +29,6 @@ vscode.commands.registerCommand('y3-helper.testExcel', async () => { }); export function init() { - editorTable.init(); language.init(); treeView.init(); languageFeature.init(); diff --git a/src/env.ts b/src/env.ts index 2164196..a95b51c 100644 --- a/src/env.ts +++ b/src/env.ts @@ -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')); @@ -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 {