# 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 ```mermaid 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 ```mermaid 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 ```mermaid 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 ```mermaid flowchart LR subgraph "Preserved State" Queue[Queue
✓ Full track list] Track[Current Track
✓ Position & URL] Volume[Volume
✓ Level] Mode[Modes
✓ Shuffle & Repeat] Subs[Subscribers
✓ Re-populated] Config[Config
✓ 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 ```mermaid flowchart LR subgraph "Not Preserved" Cache[Cache
✗ In-memory only] Buffer[Audio Buffer
✗ Must re-decode] Network[Network Connections
✗ Re-established] end ``` ## CoreLoader Lifecycle ```mermaid 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 ```mermaid flowchart TB subgraph "Trigger Sources" Menu["TS Menu
Right-click → Reload Core"] Cmd["Chat Command
!reload"] File["File Watcher
Auto-detect change"] Code["Programmatic
hotReload()"] end Menu --> CL[CoreLoader] Cmd --> CL File --> CL Code --> CL CL --> Process[Reload Process] ``` ## File Watcher Auto-Reload ```mermaid 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 ```mermaid 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 ```mermaid 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
stream URL?} Fresh -->|Yes| Fetch[Fetch new URL] Fresh -->|No| Resume[Resume] Fetch --> Resume ``` ## Error Handling ```mermaid 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 ```mermaid 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 ```mermaid 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 ```mermaid flowchart LR Stop[Stop Playback] --> Unload[Unload Core] Unload --> Load[Load New Core] Load --> Start[Start Playback] subgraph "Gap" Gap[100-500ms
Interruption] end Unload --> Gap Gap --> Load ``` **Mitigation**: - Pre-buffer before reload - Keep gap under 500ms - Consider seamless handoff (future) ## Development Workflow ```mermaid 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 ```mermaid 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