Skip to content

Commit

Permalink
展示自定义事件
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Apr 28, 2024
1 parent 9d64232 commit b05dcef
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 49 deletions.
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
"command": "y3-helper.copyPlayerAttrName",
"when": "viewItem == 玩家属性",
"group": "copy"
},
{
"command": "y3-helper.copyEventID",
"when": "viewItem == 自定义事件",
"group": "copy"
},
{
"command": "y3-helper.copyEventName",
"when": "viewItem == 自定义事件",
"group": "copy"
}
]
},
Expand Down Expand Up @@ -246,6 +256,18 @@
"enablement": "viewItem == 玩家属性",
"category": "Y3开发助手"
},
{
"command": "y3-helper.copyEventID",
"title": "复制ID",
"enablement": "viewItem == 自定义事件",
"category": "Y3开发助手"
},
{
"command": "y3-helper.copyEventName",
"title": "复制名称",
"enablement": "viewItem == 自定义事件",
"category": "Y3开发助手"
},
{
"command": "y3-helper.networkServer",
"title": "启动网络服务器(用于测试`network`库)",
Expand Down
48 changes: 0 additions & 48 deletions src/ECAInfo/customEvent.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,22 @@ export const defaultTableTypeToCSVfolderPath: Readonly<{ [key: string]: string }
destructible: "./y3-helper/editor_table/csv/可破坏物",
sound: "./y3-helper/editor_table/csv/声音"
};

export const typeID: { [key: number]: [string, string]} = {
100000: ["number", "实数"],
100001: ["boolean", "布尔"],
100002: ["integer", "整数"],
100003: ["string", "字符串"],
100004: ["Point", "点"],
100006: ["Unit", "单位"],
100010: ["UnitKey", "单位类型"],
100011: ["table", "表"],
100014: ["Ability", "技能"],
100025: ["Player", "玩家"],
100026: ["UnitGroup", "单位组"],
100027: ["PlayerGroup", "玩家组"],
100031: ["Item", "物品"],
100032: ["ItemKey", "物品类型"],
100039: ["AbilityKey", "技能类型"],
100263: ["Mover", "运动器"],
};
93 changes: 93 additions & 0 deletions src/customDefine/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { RelativePattern } from "vscode";
import { env } from "../env";
import * as tools from '../tools';
import { BaseDefine } from "./baseDefine";
import { typeID } from "../constants";

const filePath = 'customevent.json';

type Event = {
name: string;
id: number;
args: EventArg[];
};

type EventArg = {
name: string;
type: number;
luaType: string;
desc: string;
};

export class Events extends BaseDefine {
constructor() {
super();

this.onDidChange(() => {
this._eventsCache = undefined;
});
}

private _eventsCache?: Event[];

get watchPattern() {
if (!env.mapUri) {
return;
}
return new RelativePattern(env.mapUri, filePath);
}

private async loadEvents() {
let events: Event[] = [];
try {
if (!env.mapUri) {
return events;
}
let jsonFile = await tools.readFile(env.mapUri, filePath);
if (!jsonFile) {
return events;
}
let json = JSON.parse(jsonFile.string);
if (typeof json !== 'object') {
return events;
}
// 自定义单位属性
if (Array.isArray(json.group_info)) {
for (let item of json.group_info) {
let id = item.items?.[0];
let name = decodeURI(item.items?.[1]);
if (id && name) {
events.push({name, id: id, args: []});
}
}
}
for (const event of events) {
let conf = json.conf?.[event.id.toString()];
if (!conf) {
continue;
}
for (let item of conf) {
let name = decodeURI(item[0]);
let type = item[1];
if (name && type) {
event.args.push({
name,
type,
luaType: typeID[type]?.[0] ?? '不支持的类型',
desc: typeID[type]?.[1] ?? '不支持的类型',
});
}
}
}
} finally {
return events;
}
}

public async getEvents() {
if (!this._eventsCache) {
this._eventsCache = await this.loadEvents();
}
return this._eventsCache;
}
}
4 changes: 3 additions & 1 deletion src/customDefine/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { PlayerAttrs } from "./playerAttrs";
import { UnitAttrs } from "./unitAttrs";
import { Events } from "./event";

export let define = {
export const define = {
单位属性: new UnitAttrs,
玩家属性: new PlayerAttrs,
自定义事件: new Events,
};
2 changes: 2 additions & 0 deletions src/mainMenu/mainMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { 功能 } from './pages/features';
import { 环境 } from './pages/environments';
import { 单位属性 } from './pages/unitAttrs';
import { 玩家属性 } from './pages/playerAttrs';
import { 自定义事件 } from './pages/events';

let mainNode = new TreeNode('主菜单', {
childs: [
new 功能,
new 单位属性,
new 玩家属性,
new 自定义事件,
new 环境,
new TreeNode('重新选择Y3地图路径', {
command: {
Expand Down
45 changes: 45 additions & 0 deletions src/mainMenu/pages/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { TreeNode } from "../treeNode";
import * as vscode from 'vscode';
import { define } from "../../customDefine";
import { env } from "../../env";

export class 自定义事件 extends TreeNode {
constructor() {
super('自定义事件', {
iconPath: new vscode.ThemeIcon('group-by-ref-type'),
show: async () => {
await env.mapReady();
return env.projectUri !== undefined;
},

update: async (node) => {
node.childs = (await define.自定义事件.getEvents()).map(event => {
let args = event.args.map(arg => arg.name);
return new TreeNode(event.name, {
description: `${event.id.toString()}(${args.join(',')})`,
contextValue: '自定义事件',
data: event.id,
});
});
},
});

define.自定义事件.onDidChange(() => {
this.refresh();
});
}
};

vscode.commands.registerCommand('y3-helper.copyEventName', (node: TreeNode) => {
if (typeof node.label !== 'string') {
return;
}
vscode.env.clipboard.writeText(node.label);
});

vscode.commands.registerCommand('y3-helper.copyEventID', (node: TreeNode) => {
if (typeof node.data !== 'number') {
return;
}
vscode.env.clipboard.writeText(node.data.toString());
});
3 changes: 3 additions & 0 deletions src/mainMenu/treeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ export interface TreeNodeOptional {
childs?: TreeNode[];
update?: (node: TreeNode) => void | Thenable<void>;
show?: boolean | ((node: TreeNode) => boolean | Promise<boolean>);
data?: any;
}

export class TreeNode extends vscode.TreeItem {
childs?: TreeNode[];
parent?: TreeNode;
show?: TreeNodeOptional["show"] = true;
update?: TreeNodeOptional["update"];
data?: any;
constructor(label: string, optional?: TreeNodeOptional) {
super(label, vscode.TreeItemCollapsibleState.None);
if (optional) {
Expand All @@ -29,6 +31,7 @@ export class TreeNode extends vscode.TreeItem {
this.update = optional.update;
this.show = optional.show ?? true;
this.collapsibleState = optional.collapsibleState;
this.data = optional.data;
}
this.updateChilds();
}
Expand Down

0 comments on commit b05dcef

Please sign in to comment.