Skip to content

Commit

Permalink
更新编辑器打开事件
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 12, 2024
1 parent 87ed5de commit bc3fc29
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
17 changes: 17 additions & 0 deletions src/launchEditor.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 = [];
Expand Down
27 changes: 16 additions & 11 deletions src/launchGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand Down
6 changes: 5 additions & 1 deletion src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,15 @@ 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]) {
continue;
}
try {
await plugin.run(funcName, this.makeSandbox());
count++;
} catch (error) {
let errorMessage = String(error).replace(/Error: /, '');
errors.push(`"${plugin.name}/${funcName}":${errorMessage}`);
Expand All @@ -240,5 +242,6 @@ export class PluginManager extends vscode.Disposable {
if (errors.length > 0) {
throw new Error(errors.join('\n'));
}
return count;
}
}
4 changes: 1 addition & 3 deletions src/tools/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 9 additions & 1 deletion template/plugin/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit bc3fc29

Please sign in to comment.