diff --git a/package.json b/package.json index 2ed2788..2735029 100644 --- a/package.json +++ b/package.json @@ -354,6 +354,11 @@ "command": "y3-helper.enableGlobalScript", "title": "启用全局脚本", "category": "Y3开发助手" + }, + { + "command": "y3-helper.compileECA", + "title": "编译ECA", + "category": "Y3开发助手" } ], "configuration": { diff --git a/src/ecaCompiler/index.ts b/src/ecaCompiler/index.ts index 0305f6a..6813d79 100644 --- a/src/ecaCompiler/index.ts +++ b/src/ecaCompiler/index.ts @@ -1 +1,52 @@ import * as compiler from './compiler'; +import * as vscode from 'vscode'; +import * as y3 from 'y3-helper'; + +async function fullCompileOne(inUri: vscode.Uri, outUri: vscode.Uri) { + let c = new compiler.Compiler(); + let eca = await c.compile(inUri); + let content = eca.make(); + await y3.fs.writeFile(outUri, content); +} + +export function init() { + vscode.commands.registerCommand('y3-helper.compileECA', async () => { + if (!y3.env.scriptUri) { + vscode.window.showErrorMessage('请先打开地图'); + return; + } + let outTriggerDir = y3.uri(y3.env.scriptUri, 'y3-trigger'); + await vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: '编译中...', + cancellable: true, + }, async (progress, token) => { + progress.report({ + message: '正在搜索触发器文件...', + }); + let inTriggerDir = y3.uri(y3.env.mapUri!, 'global_trigger/trigger'); + let scanResult = await y3.fs.scan(inTriggerDir, undefined, () => { + if (token.isCancellationRequested) { + throw new vscode.CancellationError(); + } + }); + let fileNames = scanResult + . filter((file) => file[1] === vscode.FileType.File && file[0].endsWith('.json')) + . map((file) => file[0]); + + if (fileNames.length === 0) { + return; + } + for (let i = 0; i < fileNames.length; i++) { + if (token.isCancellationRequested) { + throw new vscode.CancellationError(); + } + progress.report({ + message: `正在编译触发器文件(${i}/${fileNames.length})...`, + }); + await fullCompileOne(y3.uri(inTriggerDir, fileNames[i]), y3.uri(outTriggerDir, fileNames[i].replace('.json', '.lua'))); + } + }); + vscode.window.showInformationMessage('编译完成'); + }); +} diff --git a/src/extension.ts b/src/extension.ts index 1dde38c..c9405c0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,6 +23,7 @@ import * as y3 from 'y3-helper'; import { config } from './config'; import * as globalScript from './globalScript'; import * as luaLanguage from './luaLanguage'; +import * as ecaCompiler from './ecaCompiler'; class Helper { private context: vscode.ExtensionContext; @@ -305,6 +306,7 @@ class Helper { plugin.init(); globalScript.init(); luaLanguage.init(); + ecaCompiler.init(); }, 100); } } diff --git a/src/tools/fs.ts b/src/tools/fs.ts index db4a5dd..25645f9 100644 --- a/src/tools/fs.ts +++ b/src/tools/fs.ts @@ -100,7 +100,7 @@ export async function dir(uri: vscode.Uri | string, relativePath?: string) { } } -export async function scan(uri: vscode.Uri | string, relativePath?: string) { +export async function scan(uri: vscode.Uri | string, relativePath?: string, partail?: (result: [string, vscode.FileType][]) => void) { if (typeof uri === 'string') { uri = vscode.Uri.file(uri); } @@ -115,6 +115,7 @@ export async function scan(uri: vscode.Uri | string, relativePath?: string) { for (const [name, fileType] of files) { let fullPath = path ? `${path}/${name}` : name; result.push([fullPath, fileType]); + partail?.(result); if (fileType === vscode.FileType.Directory) { await doScan(uri, fullPath); }