Skip to content

@web-engine-dev/reflection

Runtime type introspection and metadata system for game engines. Supports decorators, property inspection, and editor integration.

Features

  • Type Registry: Central registry for type metadata
  • Decorators: Annotate classes and properties
  • Property Inspection: Query types, validation, editor hints
  • Schema Generation: Generate serialization schemas
  • Inheritance Support: Full class hierarchy support

Installation

bash
npm install @web-engine-dev/reflection
# or
pnpm add @web-engine-dev/reflection

Quick Start

typescript
import {
  Reflect,
  Property,
  Range,
  Label,
  globalTypeRegistry,
  generateSchema,
} from '@web-engine-dev/reflection';

@Reflect({ category: 'Component' })
class Transform {
  @Property('f32')
  @Label('X Position')
  x: number = 0;

  @Property('f32')
  @Label('Y Position')
  y: number = 0;

  @Property('f32')
  @Range(0, 360)
  @Label('Rotation')
  rotation: number = 0;
}

// Query type metadata
const metadata = globalTypeRegistry.getByConstructor(Transform);
console.log(metadata?.properties);

// Generate serialization schema
const schema = generateSchema(metadata!.id);

API Reference

Class Decorators

DecoratorDescription
@ReflectRegister class with type registry
@ReflectWithRegister with custom options
@ComponentRegister as ECS component
@ResourceRegister as resource type

Property Decorators

DecoratorDescription
@Property()Define property type
@OptionalMark as optional
@ReadonlyMark as read-only
@Default(v)Set default value
@Label(s)Display name for editors
@DescriptionProperty description
@Category(s)Group in editor
@Editor(opts)Editor widget hints
@Validate(fn)Validation function
@HiddenHide from editor

Value Decorators

DecoratorDescription
@Range(a, b)Numeric range constraint
@ColorColor picker widget
@Vector22D vector editor
@Vector33D vector editor
@AssetRefAsset reference picker
@EntityRefEntity reference picker

Type Registry

typescript
import { globalTypeRegistry } from '@web-engine-dev/reflection';

// Get by constructor
const meta = globalTypeRegistry.getByConstructor(Transform);

// Get by type ID
const meta2 = globalTypeRegistry.get('Transform');

// Find by category
const components = globalTypeRegistry.getByCategory('Component');

// Iterate properties
for (const prop of meta.properties) {
  console.log(prop.name, prop.type);
}

Schema Generation

typescript
import { generateSchema, generateSchemaForCategory } from '@web-engine-dev/reflection';

// Single type
const schema = generateSchema('Transform');

// All components
const schemas = generateSchemaForCategory('Component');

Proprietary software. All rights reserved.