Files
Echoion/docs/12-hot-reload.md
T
2026-02-01 23:09:22 +03:00

8.4 KiB

Hot Reload System

Overview

Echoion's hot reload system allows the Core library to be updated without restarting TeamSpeak or disconnecting from the server. This enables rapid development and bug fixes with minimal disruption.

Hot Reload Architecture

flowchart TB
    subgraph "Static (Plugin)"
        CL[CoreLoader]
        PI[PluginInterface]
    end
    
    subgraph "Dynamic (Core)"
        Old[Old Core Library]
        New[New Core Library]
    end
    
    subgraph "State Preservation"
        State[HotReloadState]
        Playback[Playback State]
        Queue[Queue Contents]
        Position[Current Position]
        Config[Configuration]
    end
    
    CL <-->|Load/Unload| Old
    CL <-->|Load/Unload| New
    
    Old -->|Extract| State
    State -->|Restore| New
    
    State --> Playback
    State --> Queue
    State --> Position
    State --> Config

Hot Reload Process

flowchart TB
    Start[Trigger Reload] --> Save[1. Save State]
    Save --> Unload[2. Unload Old Core]
    Unload --> Load[3. Load New Core]
    Load --> Restore[4. Restore State]
    Restore --> Resume[5. Resume Operation]
    
    subgraph "State Save"
        Save --> Extract[Extract state]
        Extract --> Copy[Copy queue]
        Copy --> Pos[Get position]
    end
    
    subgraph "State Restore"
        Restore --> Queue2[Restore queue]
        Restore --> Play[Resume playback]
        Restore --> Seek[Seek to position]
    end

Step-by-Step Flow

sequenceDiagram
    participant User
    participant CL as CoreLoader
    participant Old as Old Core
    participant State as HotReloadState
    participant New as New Core
    
    User->>CL: Trigger reload
    
    CL->>Old: prepareForHotReload()
    Old->>Old: Extract playback state
    Old->>Old: Copy queue vector
    Old->>Old: Get playback position
    Old-->>State: Return HotReloadState
    
    CL->>Old: shutdown()
    CL->>Old: unload library
    
    Note over CL: Brief pause (100ms)
    
    CL->>New: load library
    CL->>New: initialize()
    CL->>New: restoreAfterHotReload(state)
    
    New->>New: Restore queue
    New->>New: Resume playback
    New->>New: Seek to position
    
    New-->>CL: Ready
    CL-->>User: Reload complete

State Preservation

What Gets Preserved

flowchart LR
    subgraph "Preserved State"
        Queue[Queue<br/>✓ Full track list]
        Track[Current Track<br/>✓ Position & URL]
        Volume[Volume<br/>✓ Level]
        Mode[Modes<br/>✓ Shuffle & Repeat]
        Subs[Subscribers<br/>✓ Re-populated]
        Config[Config<br/>✓ All settings]
    end
State Preserved Notes
Queue ✓ Full track list maintained
Current track ✓ May need fresh stream URL
Playback position ✓ Seek after restore
Volume ✓ Stored in state
Repeat/Shuffle ✓ Stored in state
Subscribers ✓ Re-populated after reload
Config ✓ Full config restored
Database ✓ Persistent anyway

What Doesn't Get Preserved

flowchart LR
    subgraph "Not Preserved"
        Cache[Cache<br/>✗ In-memory only]
        Buffer[Audio Buffer<br/>✗ Must re-decode]
        Network[Network Connections<br/>✗ Re-established]
    end

CoreLoader Lifecycle

stateDiagram-v2
    [*] --> Unloaded
    Unloaded --> Loading: load()
    Loading --> Loaded: Success
    Loading --> Error: Failure
    
    Loaded --> Saving: hotReload()
    Saving --> Unloading: Extract state
    Unloading --> Loading: Load new
    Loading --> Restoring: restore state
    Restoring --> Loaded: Success
    Restoring --> Error: Failure
    
    Loaded --> Unloading: unload()
    Unloading --> Unloaded
    Error --> Unloaded

Reload Triggers

flowchart TB
    subgraph "Trigger Sources"
        Menu["TS Menu<br/>Right-click → Reload Core"]
        Cmd["Chat Command<br/>!reload"]
        File["File Watcher<br/>Auto-detect change"]
        Code["Programmatic<br/>hotReload()"]
    end
    
    Menu --> CL[CoreLoader]
    Cmd --> CL
    File --> CL
    Code --> CL
    
    CL --> Process[Reload Process]

File Watcher Auto-Reload

flowchart TB
    Dev[Developer] -->|Build| Build[New Binary]
    Build -->|File change| FW[FileWatcher]
    FW -->|Debounce 1s| Check{Loaded?}
    
    Check -->|Yes| Reload[Trigger Reload]
    Check -->|No| Wait[Wait for load]
    
    Reload -->|Success| Active[New Version Active]
    Reload -->|Fail| Keep[Keep Old Version]
    
    FW -->|Disable| Manual[Manual reload only]

State Transfer Details

State Extraction

flowchart TB
    subgraph "Extract from Old Core"
        GetPS[Get PlaybackState]
        GetQ[Get Queue]
        GetPos[Get Position]
        GetCfg[Get Config]
    end
    
    GetPS --> State[HotReloadState]
    GetQ --> State
    GetPos --> State
    GetCfg --> State
    
    State --> Serialize[Serialize for transfer]

State Restoration

flowchart TB
    subgraph "Restore to New Core"
        Parse[Parse HotReloadState]
        RestoreQ[Restore Queue]
        RestoreP[Restore Playback]
        RestoreV[Restore Volume]
    end
    
    Parse --> RestoreQ
    RestoreQ --> RestoreP
    RestoreP --> RestoreV
    
    RestoreP --> Fresh{Need fresh<br/>stream URL?}
    Fresh -->|Yes| Fetch[Fetch new URL]
    Fresh -->|No| Resume[Resume]
    Fetch --> Resume

Error Handling

flowchart TB
    Error{Reload Error} --> LoadFail["Load Failed"]
    Error --> RestoreFail["Restore Failed"]
    
    LoadFail -->|Has state| Rollback1[Rollback to old]
    LoadFail -->|No state| Fail1[Fail - no recovery]
    
    RestoreFail -->|Can rollback| Rollback2[Rollback to old]
    RestoreFail -->|Critical| Fail2[Fail - restart needed]
    
    Rollback1 -->|Try| RestoreOld[Restore old core]
    Rollback2 -->|Try| RestoreOld
    
    RestoreOld -->|Success| Continue[Continue with old]
    RestoreOld -->|Fail| Fail3[Complete failure]

Platform Differences

flowchart TB
    subgraph "Windows"
        W[DLL File] -->|Locked| Copy[Copy to temp]
        Copy --> Load[Load temp copy]
    end
    
    subgraph "Linux/macOS"
        L[SO/DYLIB File] -->|Can overwrite| Load2[Load directly]
    end
Platform Behavior Notes
Windows Copy to temp before load File locking prevents overwrite
Linux Load directly Can overwrite loaded libraries
macOS Load directly Similar to Linux

Potential Issues

Stream URL Expiration

flowchart LR
    Old[Old Core] -->|Save URL| State
    State -->|URL expired?| Check{Check}
    Check -->|Yes| Refresh[Re-fetch URL]
    Check -->|No| Use[Use saved URL]
    Refresh --> New[New Core]
    Use --> New

Solution: Re-fetch stream URL on restore if expired

Audio Gap

flowchart LR
    Stop[Stop Playback] --> Unload[Unload Core]
    Unload --> Load[Load New Core]
    Load --> Start[Start Playback]
    
    subgraph "Gap"
        Gap[100-500ms<br/>Interruption]
    end
    
    Unload --> Gap
    Gap --> Load

Mitigation:

  • Pre-buffer before reload
  • Keep gap under 500ms
  • Consider seamless handoff (future)

Development Workflow

flowchart LR
    subgraph "Hot Reload Dev Workflow"
        Start[Start TS with Plugin] --> Play[Play Music]
        Play --> Code[Edit Code]
        Code --> Build[Build Core]
        Build --> Reload[Auto/Manual Reload]
        Reload --> Verify[Verify State]
        Verify -->|More changes| Code
        Verify -->|Done| Stop[Stop]
    end

Best Practices

flowchart TB
    subgraph "Best Practices"
        Early[Save Early] -->|Before destructive| Safe[Safe State]
        Validate[Validate State] -->|Version check| Compatible[Compatibility]
        PlanB[Have Rollback] -->|Always| Recover[Recoverable]
        Minimal[Minimal State] -->|Only necessary| Efficient[Efficient]
        Test[Thorough Testing] -->|Edge cases| Reliable[Reliable]
        Log[Log Everything] -->|Debug info| Debuggable[Debuggable]
    end
  1. Save Early - Extract state before any destructive operations
  2. Validate State - Check version compatibility before restoration
  3. Have Rollback - Always have a fallback plan
  4. Minimal State - Only preserve what's necessary
  5. Thorough Testing - Hot reload is complex, test edge cases
  6. Log Everything - Detailed logging helps debug issues