# AeThex Studio Bridge Module

Communication bridge between AeThex Studio (web UI) and AeThex Engine (C++ runtime).

## Overview

This module provides a JSON-RPC API that allows AeThex Studio to control the engine remotely. The Studio UI (TypeScript/Next.js) can manipulate scenes, nodes, and properties through this bridge without requiring the traditional Godot editor UI.

## Architecture

```
AeThex Studio (localhost:3000)
    ↕ HTTP/WebSocket
StudioBridge Server (localhost:6007)
    ↕ Direct API Calls  
Engine Scene System (C++ Godot Core)
```

## API Endpoints

All endpoints accept JSON-RPC format:
```json
{
  "method": "loadScene",
  "params": {
    "path": "res://main.tscn"
  }
}
```

### Scene Management
- **loadScene** - Load a scene file
  - Params: `path` (String)
  - Returns: Scene tree structure
  
- **saveScene** - Save current scene
  - Params: `path` (String)
  - Returns: Success confirmation

- **getSceneTree** - Get full scene hierarchy
  - Params: None
  - Returns: Tree of all nodes

### Node Operations
- **createNode** - Create new node
  - Params: `type` (String), `parent` (String), `name` (String)
  - Returns: Created node info
  
- **deleteNode** - Delete node
  - Params: `path` (String)
  - Returns: Deleted node path

- **selectNode** - Select node in editor
  - Params: `path` (String)
  - Returns: Selected node info

### Property Manipulation
- **setProperty** - Set node property
  - Params: `path` (String), `property` (String), `value` (Variant)
  - Returns: Updated property info

- **getProperty** - Get node property value
  - Params: `path` (String), `property` (String)
  - Returns: Property value

### Game Control
- **runGame** - Start game instance
  - Params: None
  - Returns: Success confirmation

- **stopGame** - Stop running game
  - Params: None
  - Returns: Success confirmation

## Events (Engine → Studio)

The bridge emits WebSocket events to notify Studio of changes:

- **scene_changed** - Scene loaded/modified
- **node_selected** - Node selection changed
- **property_changed** - Property value updated
- **console_output** - Engine log message

## Usage from GDScript

```gdscript
# Access the singleton
var bridge = StudioBridge

# Start the server
bridge.start_server(6007)

# Check if running
if bridge.is_server_running():
    print("Bridge active")

# Send console output to Studio
bridge.emit_console_output("Game initialized", "info")

# Stop the server
bridge.stop_server()
```

## Usage from Studio (TypeScript)

```typescript
// studio/src/engine/bridge.ts
const response = await fetch('http://localhost:6007/rpc', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    method: 'loadScene',
    params: { path: 'res://main.tscn' }
  })
});

const result = await response.json();
if (result.success) {
  console.log('Scene loaded:', result.result);
}
```

## Build Configuration

The module is automatically compiled when building the engine:

```bash
cd engine
scons platform=linuxbsd target=editor
```

No additional flags needed - the module is detected automatically.

## Future Enhancements

- [ ] WebSocket server for real-time events
- [ ] Scene viewport texture streaming
- [ ] Debugger integration
- [ ] Profiler data streaming
- [ ] Asset import pipeline hooks
- [ ] Collaborative editing support

## Security Notes

⚠️ **Development Only**: This bridge should only run in development mode. Production games should not expose this API.

The server binds to `localhost` only by default, preventing external access.

## See Also

- [docs/STUDIO_INTEGRATION.md](../../docs/STUDIO_INTEGRATION.md) - Complete integration plan
- [modules/aethex_ai/README.md](../aethex_ai/README.md) - AI assistant module
