diff --git a/src/launchEditor.ts b/src/launchEditor.ts index dba48d2..a3858c4 100644 --- a/src/launchEditor.ts +++ b/src/launchEditor.ts @@ -1,8 +1,22 @@ import * as vscode from 'vscode'; import { env } from './env'; import { runShell } from './runShell'; +import * as y3 from 'y3-helper'; export class EditorLauncher { + private async runPlugin() { + try { + await y3.plugin.runAllPlugins('onEditor'); + } catch (error) { + let res = await vscode.window.showErrorMessage("运行插件时发生错误", { + detail: String(error).replace(/Error: /, ''), + modal: true, + }, '仍要启动'); + if (res !== '仍要启动') { + return false; + } + } + } public async launch(luaArgs?: {[key: string]: string|number|boolean}): Promise<boolean> { await env.editorReady(true); @@ -17,6 +31,9 @@ export class EditorLauncher { vscode.window.showErrorMessage("未找到编辑器!"); return false; } + + await this.runPlugin(); + let project_path = projectUri.fsPath.replaceAll("\\", "/") + '/header.project'; let project_path_base64 = Buffer.from(project_path).toString('base64'); let args = []; diff --git a/src/launchGame.ts b/src/launchGame.ts index 8108459..afd3b5a 100644 --- a/src/launchGame.ts +++ b/src/launchGame.ts @@ -4,6 +4,19 @@ import { runShell } from './runShell'; import * as y3 from 'y3-helper'; export class GameLauncher { + private async runPlugin() { + try { + await y3.plugin.runAllPlugins('onGame'); + } catch (error) { + let res = await vscode.window.showErrorMessage("运行插件时发生错误", { + detail: String(error).replace(/Error: /, ''), + modal: true, + }, '仍要启动'); + if (res !== '仍要启动') { + return false; + } + } + } public async launch(luaArgs?: {[key: string]: string|number|boolean}): Promise<boolean> { await env.editorReady(true); @@ -18,17 +31,9 @@ export class GameLauncher { vscode.window.showErrorMessage("未找到编辑器!"); return false; } - try { - await y3.plugin.runAllPlugins('onGame'); - } catch (error) { - let res = await vscode.window.showErrorMessage("运行插件时发生错误", { - detail: String(error).replace(/Error: /, ''), - modal: true, - }, '仍要启动'); - if (res !== '仍要启动') { - return false; - } - } + + await this.runPlugin(); + let args = []; args.push('type@editor_game'); args.push('subtype@editor_game'); diff --git a/src/plugin/index.ts b/src/plugin/index.ts index 944a7ef..5f49610 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -97,7 +97,11 @@ export async function runAllPlugins(funcName: string) { if (!pluginManager) { return; } - await pluginManager.runAll(funcName); + let count = await pluginManager.runAll(funcName); + if (count > 0) { + // 等待物编文件写入完成 + await y3.sleep(200); + } } export async function init() { diff --git a/src/plugin/plugin.ts b/src/plugin/plugin.ts index 7de2090..293bebc 100644 --- a/src/plugin/plugin.ts +++ b/src/plugin/plugin.ts @@ -225,6 +225,7 @@ export class PluginManager extends vscode.Disposable { public async runAll(funcName: string) { let plugins = await this.getAll(); let errors = []; + let count = 0; for (const plugin of plugins) { const infos = await plugin.getExports(); if (!infos[funcName]) { @@ -232,6 +233,7 @@ export class PluginManager extends vscode.Disposable { } try { await plugin.run(funcName, this.makeSandbox()); + count++; } catch (error) { let errorMessage = String(error).replace(/Error: /, ''); errors.push(`"${plugin.name}/${funcName}":${errorMessage}`); @@ -240,5 +242,6 @@ export class PluginManager extends vscode.Disposable { if (errors.length > 0) { throw new Error(errors.join('\n')); } + return count; } } diff --git a/src/tools/json.ts b/src/tools/json.ts index 1587a1b..11b5328 100644 --- a/src/tools/json.ts +++ b/src/tools/json.ts @@ -72,13 +72,11 @@ export class Json { return; } this._tree = undefined; - let allEdits: jsonc.Edit[] = []; for (const key in this._patch) { const value = this._patch[key]; let edits = jsonc.modify(this._text, [key], value, editOptions); - allEdits.push(...edits); + this._text = jsonc.applyEdits(this._text, edits); } this._patch = undefined; - this._text = jsonc.applyEdits(this._text, allEdits); } } diff --git a/template/plugin/2.js b/template/plugin/2.js index 2b360d7..6e11d6a 100644 --- a/template/plugin/2.js +++ b/template/plugin/2.js @@ -8,14 +8,22 @@ let os = require('os') export async function onGame() { y3.print('启动游戏!') + // 在你的Lua代码里加入 `require 'log.onGame'` 试试看 y3.fs.writeFile(y3.uri(y3.env.scriptUri, 'log/onGame.lua'), ` --- 在你的Lua代码里加入 “require 'log.onGame'” 试试看 print('运行者:${os.userInfo().username},运行时间:${new Date().toLocaleString()}') `) } export async function onEditor() { y3.print('在编辑器中打开!') + // 偷偷生成一个单位 + let unitTable = y3.table.openTable('单位') + let unit = await unitTable.create({ + name: '打开编辑器时自动创建的单位', + key: 55555, + overwrite: true, + }) + unit.data.description = `运行者:${os.userInfo().username},运行时间:${new Date().toLocaleString()}` } export async function onSave() {