lighty-event 0.8.6

Event system for LightyLauncher
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# lighty-event


[![Crates.io](https://img.shields.io/crates/v/lighty-event.svg)](https://crates.io/crates/lighty-event)
[![Documentation](https://docs.rs/lighty-event/badge.svg)](https://docs.rs/lighty-event)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Event system for LightyLauncher - A simple, efficient broadcast-based event system for tracking launcher operations.

## Features


- **Async-first** - Built on tokio's broadcast channels
- **Multiple subscribers** - Broadcast events to multiple listeners simultaneously
- **Typed events** - Strongly typed event system with compile-time safety
- **Comprehensive** - Events for authentication, downloads, installations, and more
- **Progress tracking** - Real-time progress updates for long-running operations
- **Zero-cost** - No overhead when events feature is disabled
- **Error handling** - Custom error types with thiserror
- **Extensible** - Extend with traits for ergonomic APIs

## Event Categories


### Authentication Events (`AuthEvent`)

- `AuthenticationStarted` - Authentication process begins
- `AuthenticationInProgress` - Ongoing authentication with step info
- `AuthenticationSuccess` - Authentication succeeded
- `AuthenticationFailed` - Authentication failed with error
- `AlreadyAuthenticated` - Valid session already exists

### Java Events (`JavaEvent`)

- `JavaNotFound` - JRE not installed
- `JavaAlreadyInstalled` - JRE already present
- `JavaDownloadStarted` - JRE download begins
- `JavaDownloadProgress` - Download progress updates
- `JavaDownloadCompleted` - Download finished
- `JavaExtractionStarted` - Extraction begins
- `JavaExtractionProgress` - Extraction progress
- `JavaExtractionCompleted` - Extraction finished

### Launch Events (`LaunchEvent`)

- `IsInstalled` - Files already up-to-date
- `InstallStarted` - Installation begins
- `InstallProgress` - Installation progress
- `InstallCompleted` - Installation finished
- `Launching` - Game launch starting
- `Launched` - Game process spawned
- `NotLaunched` - Launch failed
- `ProcessOutput` - Game process output
- `ProcessExited` - Game process exited

### Loader Events (`LoaderEvent`)

- `FetchingData` - Fetching loader manifest
- `DataFetched` - Manifest retrieved
- `ManifestNotFound` - Version not found
- `ManifestCached` - Using cached manifest
- `MergingLoaderData` - Merging loader data
- `DataMerged` - Merge completed

### Core Events (`CoreEvent`)

- `ExtractionStarted` - Archive extraction begins
- `ExtractionProgress` - Extraction progress
- `ExtractionCompleted` - Extraction finished

## Installation


Add this to your `Cargo.toml`:

```toml
[dependencies]
lighty-event = "0.6"
```

## Usage


### Basic Example


```rust
use lighty_event::{EventBus, Event, LaunchEvent, EventReceiveError};

#[tokio::main]

async fn main() {
    // Create event bus with buffer capacity
    let event_bus = EventBus::new(1000);

    // Subscribe to events
    let mut receiver = event_bus.subscribe();

    // Spawn listener task
    tokio::spawn(async move {
        loop {
            match receiver.next().await {
                Ok(event) => {
                    handle_event(event);
                }
                Err(EventReceiveError::BusDropped) => {
                    println!("Event bus closed");
                    break;
                }
                Err(EventReceiveError::Lagged { skipped }) => {
                    eprintln!("Warning: Missed {} events", skipped);
                }
            }
        }
    });

    // Use the event bus with launcher operations...
}

fn handle_event(event: Event) {
    match event {
        Event::Launch(LaunchEvent::InstallStarted { version, total_bytes }) => {
            println!("Installing {} ({} MB)", version, total_bytes / 1_000_000);
        }
        Event::Launch(LaunchEvent::InstallProgress { bytes }) => {
            println!("Downloaded {} bytes", bytes);
        }
        Event::Java(JavaEvent::JavaDownloadStarted { distribution, version, total_bytes }) => {
            println!("Downloading {} {} ({} MB)", distribution, version, total_bytes / 1_000_000);
        }
        _ => {}
    }
}
```

### With LightyLauncher


```rust
use lighty_launcher::{JavaDistribution, Launch, Loader, VersionBuilder};
use lighty_auth::{offline::OfflineAuth, Authenticator};
use lighty_event::{EventBus, Event, LaunchEvent};
use directories::ProjectDirs;

#[tokio::main]

async fn main() -> anyhow::Result<()> {
    let launcher_dir = ProjectDirs::from("fr", ".LightyLauncher", "")
        .expect("Failed to get project directories");

    // Create event bus
    let event_bus = EventBus::new(1000);
    let mut receiver = event_bus.subscribe();

    // Spawn event listener
    tokio::spawn(async move {
        while let Ok(event) = receiver.next().await {
            match event {
                Event::Launch(LaunchEvent::InstallProgress { bytes }) => {
                    println!("Progress: {} bytes", bytes);
                }
                Event::Launch(LaunchEvent::InstallCompleted { version, .. }) => {
                    println!("Installation of {} completed!", version);
                }
                _ => {}
            }
        }
    });

    // Authenticate
    let mut auth = OfflineAuth::new("Player");
    let profile = auth.authenticate(Some(&event_bus)).await?;

    // Build and launch
    let mut version = VersionBuilder::new(
        "my-instance",
        Loader::Vanilla,
        "",
        "1.21.1",
        &launcher_dir
    );

    version.launch(&profile, JavaDistribution::Temurin)
        .with_event_bus(&event_bus)
        .run()
        .await?;

    Ok(())
}
```

### Progress Tracking Example


```rust
use lighty_event::{EventBus, Event, LaunchEvent, JavaEvent};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

#[tokio::main]

async fn main() {
    let event_bus = EventBus::new(1000);
    let mut receiver = event_bus.subscribe();

    let total_bytes = Arc::new(AtomicU64::new(0));
    let downloaded_bytes = Arc::new(AtomicU64::new(0));

    tokio::spawn({
        let total = Arc::clone(&total_bytes);
        let downloaded = Arc::clone(&downloaded_bytes);

        async move {
            while let Ok(event) = receiver.next().await {
                match event {
                    Event::Launch(LaunchEvent::InstallStarted { total_bytes: t, .. }) => {
                        total.store(t, Ordering::Relaxed);
                        downloaded.store(0, Ordering::Relaxed);
                    }
                    Event::Launch(LaunchEvent::InstallProgress { bytes }) => {
                        downloaded.fetch_add(bytes, Ordering::Relaxed);

                        let d = downloaded.load(Ordering::Relaxed);
                        let t = total.load(Ordering::Relaxed);

                        if t > 0 {
                            let percent = (d as f64 / t as f64) * 100.0;
                            print!("\rProgress: {:.1}%", percent);
                            std::io::Write::flush(&mut std::io::stdout()).ok();
                        }
                    }
                    Event::Launch(LaunchEvent::InstallCompleted { .. }) => {
                        println!("\nInstallation completed!");
                    }
                    _ => {}
                }
            }
        }
    });

    // Use launcher...
}
```

### Multi-Subscriber Example


```rust
use lighty_event::{EventBus, Event, LaunchEvent};

#[tokio::main]

async fn main() {
    let event_bus = EventBus::new(1000);

    // UI subscriber
    let mut ui_receiver = event_bus.subscribe();
    tokio::spawn(async move {
        while let Ok(event) = ui_receiver.next().await {
            // Update UI with event
            update_ui(event);
        }
    });

    // Logger subscriber
    let mut log_receiver = event_bus.subscribe();
    tokio::spawn(async move {
        while let Ok(event) = log_receiver.next().await {
            // Log event to file
            log_event(&event);
        }
    });

    // Analytics subscriber
    let mut analytics_receiver = event_bus.subscribe();
    tokio::spawn(async move {
        while let Ok(event) = analytics_receiver.next().await {
            // Send analytics
            send_analytics(event);
        }
    });

    // All subscribers receive the same events
    // Use launcher with event_bus...
}

fn update_ui(event: Event) { /* ... */ }
fn log_event(event: &Event) { /* ... */ }
fn send_analytics(event: Event) { /* ... */ }
```

## Error Handling


The event system provides custom error types:

### `EventReceiveError`

- `BusDropped` - Event bus has been closed
- `Lagged { skipped }` - Receiver fell behind, some events were missed

### `EventTryReceiveError`

- `Empty` - No events available (non-blocking)
- `BusDropped` - Event bus has been closed
- `Lagged { skipped }` - Receiver fell behind

### `EventSendError`

- `NoReceivers` - No active receivers (event not sent)

Example with error handling:

```rust
use lighty_event::{EventReceiver, EventReceiveError};

async fn listen_events(mut receiver: EventReceiver) {
    loop {
        match receiver.next().await {
            Ok(event) => {
                // Handle event
                println!("Received: {:?}", event);
            }
            Err(EventReceiveError::BusDropped) => {
                eprintln!("Event bus closed, exiting listener");
                break;
            }
            Err(EventReceiveError::Lagged { skipped }) => {
                eprintln!("Warning: Receiver lagged, missed {} events", skipped);
                // Continue listening, but some events were lost
            }
        }
    }
}
```

## Buffer Size Recommendations


The buffer size determines how many events can be buffered before older events are dropped:

- **Small applications**: 100-500 events
- **Medium applications**: 500-2000 events
- **Large applications**: 2000-5000 events
- **Very slow receivers**: 5000+ events

```rust
// Small buffer for simple use cases
let event_bus = EventBus::new(100);

// Larger buffer for complex applications with slow receivers
let event_bus = EventBus::new(5000);
```

If receivers are too slow and the buffer fills up, the oldest events will be dropped and receivers will get a `Lagged` error.

## Non-Blocking Receive


For non-blocking event checking, use `try_next()`:

```rust
use lighty_event::{EventReceiver, EventTryReceiveError};

fn poll_events(receiver: &mut EventReceiver) {
    match receiver.try_next() {
        Ok(event) => {
            println!("Got event: {:?}", event);
        }
        Err(EventTryReceiveError::Empty) => {
            // No events available right now
        }
        Err(EventTryReceiveError::BusDropped) => {
            eprintln!("Event bus closed");
        }
        Err(EventTryReceiveError::Lagged { skipped }) => {
            eprintln!("Missed {} events", skipped);
        }
    }
}
```

## Feature Flags


The event system is optional and can be disabled:

```toml
[dependencies]
lighty-launcher = { version = "0.6", features = ["events"] }
lighty-event = "0.6"
```

When the `events` feature is disabled, event-related code is compiled out with zero overhead.

## Serialization


All events implement `Serialize` and `Deserialize` (via serde), making them easy to:
- Send over the network
- Store in files
- Log to JSON
- Integrate with web APIs (in Tauri)

```rust
use lighty_event::{Event, LaunchEvent};
use serde_json;

let event = Event::Launch(LaunchEvent::InstallStarted {
    version: "1.21.1".to_string(),
    total_bytes: 1000000,
});

// Serialize to JSON
let json = serde_json::to_string(&event).unwrap();
println!("{}", json);

// Deserialize from JSON
let deserialized: Event = serde_json::from_str(&json).unwrap();
```

## Module Structure


```
lighty-event/
├── src/
│   ├── lib.rs          # EventBus, EventReceiver
│   ├── errors.rs       # Error types
│   └── module/         # Event definitions
│       ├── mod.rs      # Module exports
│       ├── auth.rs     # AuthEvent
│       ├── core.rs     # CoreEvent
│       ├── java.rs     # JavaEvent
│       ├── launch.rs   # LaunchEvent
│       └── loader.rs   # LoaderEvent
└── README.md
```

## Contributing


Contributions are welcome! Please feel free to submit a Pull Request.

## License


Licensed under the MIT License. See [LICENSE](../../LICENSE) for details.

## Related Crates


- [`lighty-launcher`]../launch - Main launcher crate
- [`lighty-auth`]../auth - Authentication system
- [`lighty-java`]../java - Java runtime management
- [`lighty-core`]../core - Core utilities