Files
esengine/packages/behavior-tree/src/Runtime/NodeMetadata.ts

80 lines
2.4 KiB
TypeScript
Raw Normal View History

import { NodeType } from '../Types/TaskStatus';
/**
*
*/
export interface ConfigFieldDefinition {
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
default?: any;
description?: string;
min?: number;
max?: number;
options?: string[];
supportBinding?: boolean;
allowMultipleConnections?: boolean;
}
/**
*
*/
export interface NodeMetadata {
implementationType: string;
nodeType: NodeType;
displayName: string;
description?: string;
category?: string;
configSchema?: Record<string, ConfigFieldDefinition>;
}
/**
*
*/
export class NodeMetadataRegistry {
private static metadataMap: Map<string, NodeMetadata> = new Map();
private static executorClassMap: Map<Function, string> = new Map();
private static executorConstructors: Map<string, new () => any> = new Map();
static register(target: Function, metadata: NodeMetadata): void {
this.metadataMap.set(metadata.implementationType, metadata);
this.executorClassMap.set(target, metadata.implementationType);
this.executorConstructors.set(metadata.implementationType, target as new () => any);
}
static getMetadata(implementationType: string): NodeMetadata | undefined {
return this.metadataMap.get(implementationType);
}
static getAllMetadata(): NodeMetadata[] {
return Array.from(this.metadataMap.values());
}
static getByCategory(category: string): NodeMetadata[] {
return this.getAllMetadata().filter((m) => m.category === category);
}
static getByNodeType(nodeType: NodeType): NodeMetadata[] {
return this.getAllMetadata().filter((m) => m.nodeType === nodeType);
}
static getImplementationType(executorClass: Function): string | undefined {
return this.executorClassMap.get(executorClass);
}
static getExecutorConstructor(implementationType: string): (new () => any) | undefined {
return this.executorConstructors.get(implementationType);
}
static getAllExecutorConstructors(): Map<string, new () => any> {
return new Map(this.executorConstructors);
}
}
/**
*
*/
export function NodeExecutorMetadata(metadata: NodeMetadata) {
return function (target: Function) {
NodeMetadataRegistry.register(target, metadata);
};
}