bevy_map_editor 0.3.0

Full-featured map editor for Bevy games with autotile support
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Game process launching and management
//!
//! This module provides functions to launch, monitor, and terminate
//! game processes from the editor.

use std::io::{self, BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::Mutex;
use std::thread;

/// Messages sent from the build thread to the UI
#[derive(Debug, Clone)]
pub enum BuildOutput {
    /// Raw output line from cargo
    Line(String),
    /// Parsed progress: (current, total) crates
    Progress(u32, u32),
    /// Name of crate currently being compiled
    CurrentCrate(String),
    /// Build completed successfully
    BuildComplete,
    /// Build failed with error message
    BuildFailed(String),
    /// Game process has started
    GameStarted,
    /// Game process has exited
    GameExited(Option<i32>),
}

/// State of a game build/run operation
#[derive(Debug, Clone, Default)]
pub enum GameBuildState {
    /// No build in progress
    #[default]
    Idle,
    /// Currently building
    Building {
        /// Progress as (current, total) crates
        progress: Option<(u32, u32)>,
        /// Name of crate currently compiling
        current_crate: Option<String>,
        /// Recent output lines (last N lines for UI display)
        output_lines: Vec<String>,
        /// Path to the full log file
        log_file_path: Option<PathBuf>,
    },
    /// Build complete, game running
    Running {
        /// Path to the full log file
        log_file_path: Option<PathBuf>,
    },
    /// Build/run finished (success or stopped)
    Finished {
        /// Path to the full log file
        log_file_path: Option<PathBuf>,
    },
    /// Build or run failed
    Failed {
        /// Error message
        message: String,
        /// Path to the full log file
        log_file_path: Option<PathBuf>,
    },
}

/// Get the log file path for the current build
pub fn get_build_log_path() -> PathBuf {
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);

    // Use temp directory for build logs
    let temp_dir = std::env::temp_dir();
    temp_dir.join(format!("bevy_map_editor_build_{}.log", timestamp))
}

/// Error type for game launching operations
#[derive(Debug)]
pub enum LaunchError {
    /// IO error during command execution
    IoError(io::Error),
    /// Game project path not configured
    ProjectNotConfigured,
    /// Map file not saved
    MapNotSaved,
    /// Cargo not found in PATH
    CargoNotFound,
    /// Game launch failed
    LaunchFailed(String),
    /// Project directory doesn't exist
    ProjectNotFound(PathBuf),
}

impl std::fmt::Display for LaunchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LaunchError::IoError(e) => write!(f, "IO error: {}", e),
            LaunchError::ProjectNotConfigured => {
                write!(
                    f,
                    "Game project not configured. Go to Project > Game Settings."
                )
            }
            LaunchError::MapNotSaved => write!(f, "Save the map project before running the game."),
            LaunchError::CargoNotFound => {
                write!(f, "Cargo not found. Please install Rust toolchain.")
            }
            LaunchError::LaunchFailed(msg) => write!(f, "Failed to launch game: {}", msg),
            LaunchError::ProjectNotFound(path) => {
                write!(f, "Game project not found at: {}", path.display())
            }
        }
    }
}

impl std::error::Error for LaunchError {}

impl From<io::Error> for LaunchError {
    fn from(e: io::Error) -> Self {
        LaunchError::IoError(e)
    }
}

/// Options for launching a game
pub struct LaunchOptions {
    /// Path to the game project directory (contains Cargo.toml)
    pub project_path: PathBuf,
    /// Whether to use release mode
    pub release: bool,
    /// Whether to enable hot-reload (adds 'dev' feature)
    pub hot_reload: bool,
}

/// Result of a game launch attempt
pub struct LaunchResult {
    /// The spawned child process (if successful)
    pub child: Option<Child>,
    /// Error if launch failed
    pub error: Option<LaunchError>,
}

impl LaunchResult {
    /// Create a successful launch result
    pub fn success(child: Child) -> Self {
        Self {
            child: Some(child),
            error: None,
        }
    }

    /// Create a failed launch result
    pub fn failure(error: LaunchError) -> Self {
        Self {
            child: None,
            error: Some(error),
        }
    }
}

/// Launch a game project
///
/// Runs `cargo run` in the game project directory with optional flags:
/// - `--release` if release mode is enabled
/// - `--features dev` if hot-reload is enabled
pub fn launch_game(options: &LaunchOptions) -> LaunchResult {
    // Verify project exists
    if !options.project_path.exists() {
        return LaunchResult::failure(LaunchError::ProjectNotFound(options.project_path.clone()));
    }

    if !options.project_path.join("Cargo.toml").exists() {
        return LaunchResult::failure(LaunchError::ProjectNotFound(options.project_path.clone()));
    }

    // Build cargo command
    let mut cmd = Command::new("cargo");
    cmd.arg("run");
    cmd.current_dir(&options.project_path);

    if options.release {
        cmd.arg("--release");
    }

    if options.hot_reload {
        cmd.args(["--features", "dev"]);
    }

    // Don't capture output - let it go to console
    cmd.stdout(Stdio::inherit());
    cmd.stderr(Stdio::inherit());

    // Spawn the process
    match cmd.spawn() {
        Ok(child) => LaunchResult::success(child),
        Err(e) => LaunchResult::failure(LaunchError::LaunchFailed(e.to_string())),
    }
}

/// Check if a game process is still running
///
/// Returns true if the process is running, false if it has exited.
/// Also clears the Option if the process has exited.
pub fn is_game_running(child: &mut Option<Child>) -> bool {
    if let Some(ref mut c) = child {
        match c.try_wait() {
            Ok(Some(_)) => {
                // Process has exited
                *child = None;
                false
            }
            Ok(None) => true, // Still running
            Err(_) => {
                *child = None;
                false
            }
        }
    } else {
        false
    }
}

/// Kill a running game process
pub fn kill_game(child: &mut Option<Child>) {
    if let Some(ref mut c) = child {
        let _ = c.kill();
        let _ = c.wait(); // Reap the zombie
        *child = None;
    }
}

/// Copy map file to game's assets folder
///
/// Copies the map project file to `{game_project}/assets/maps/{filename}`
pub fn sync_map_to_game(
    map_path: &Path,
    game_project_path: &Path,
) -> Result<PathBuf, std::io::Error> {
    let game_assets = game_project_path.join("assets").join("maps");

    // Create the maps directory if it doesn't exist
    std::fs::create_dir_all(&game_assets)?;

    // Get the map filename
    let map_filename = map_path
        .file_name()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid map path"))?;

    let dest_path = game_assets.join(map_filename);

    // Copy the file
    std::fs::copy(map_path, &dest_path)?;

    Ok(dest_path)
}

/// Copy tileset images to game's assets folder
///
/// Copies tileset images maintaining relative path structure.
/// Uses read-then-write to work around file locks from the editor's asset system.
/// Handles both absolute and relative tileset paths.
pub fn sync_tileset_to_game(
    tileset_path: &Path,
    source_assets_dir: &Path,
    game_project_path: &Path,
) -> Result<PathBuf, std::io::Error> {
    let game_assets = game_project_path.join("assets");

    // Determine relative path for destination
    let dest_path = if let Ok(rel) = tileset_path.strip_prefix(source_assets_dir) {
        // Path is relative to source_assets_dir
        game_assets.join(rel)
    } else {
        // Path might be absolute - try to find "assets/" or "assets\" and use everything after
        let path_str = tileset_path.to_string_lossy();
        if let Some(pos) = path_str
            .find("assets/")
            .or_else(|| path_str.find("assets\\"))
        {
            let after_assets = &path_str[pos + 7..]; // Skip "assets/"
            game_assets.join(after_assets)
        } else {
            // Just use the filename as fallback
            let filename = tileset_path
                .file_name()
                .unwrap_or_else(|| std::ffi::OsStr::new("unknown"));
            game_assets.join(filename)
        }
    };

    // Create parent directories
    if let Some(parent) = dest_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Copy if source exists
    // Use read-then-write instead of fs::copy to work around file locks
    // (Bevy's asset system keeps handles open which blocks fs::copy on Windows)
    if tileset_path.exists() {
        let contents = std::fs::read(tileset_path)?;
        std::fs::write(&dest_path, contents)?;
    }

    Ok(dest_path)
}

/// Sync all assets from the editor project to the game project
///
/// This syncs:
/// 1. The map file to `{game}/assets/maps/`
/// 2. All tileset images to `{game}/assets/` (preserving relative paths)
/// 3. All sprite sheet images to `{game}/assets/` (preserving relative paths)
///
/// Returns the path to the synced map file
pub fn sync_all_assets_to_game(
    project: &crate::project::Project,
    map_path: &Path,
    game_project_path: &Path,
    assets_base_path: &Path,
) -> Result<PathBuf, std::io::Error> {
    // 1. Sync the map file
    let map_dest = sync_map_to_game(map_path, game_project_path)?;
    bevy::log::info!("Synced map to: {}", map_dest.display());

    // 2. Sync all tileset images
    for tileset in &project.tilesets {
        // Sync images from multi-image tilesets
        for image in &tileset.images {
            let image_path = assets_base_path.join(&image.path);
            if image_path.exists() {
                match sync_tileset_to_game(&image_path, assets_base_path, game_project_path) {
                    Ok(dest) => bevy::log::info!("Synced tileset image: {}", dest.display()),
                    Err(e) => {
                        bevy::log::warn!("Failed to sync tileset image {}: {}", image.path, e)
                    }
                }
            } else {
                bevy::log::warn!("Tileset image not found: {}", image_path.display());
            }
        }

        // Sync legacy single-image path if present
        if let Some(path) = &tileset.path {
            let image_path = assets_base_path.join(path);
            if image_path.exists() {
                match sync_tileset_to_game(&image_path, assets_base_path, game_project_path) {
                    Ok(dest) => bevy::log::info!("Synced legacy tileset: {}", dest.display()),
                    Err(e) => bevy::log::warn!("Failed to sync legacy tileset {}: {}", path, e),
                }
            }
        }
    }

    // 3. Sync all sprite sheet images
    for sprite_sheet in &project.sprite_sheets {
        let sheet_path = assets_base_path.join(&sprite_sheet.sheet_path);
        if sheet_path.exists() {
            match sync_tileset_to_game(&sheet_path, assets_base_path, game_project_path) {
                Ok(dest) => bevy::log::info!("Synced sprite sheet: {}", dest.display()),
                Err(e) => {
                    bevy::log::warn!(
                        "Failed to sync sprite sheet {}: {}",
                        sprite_sheet.sheet_path,
                        e
                    )
                }
            }
        } else {
            bevy::log::warn!("Sprite sheet not found: {}", sheet_path.display());
        }
    }

    Ok(map_dest)
}

/// Handle for controlling an async build/run operation
pub struct AsyncBuildHandle {
    /// Receiver for build output messages (wrapped in Mutex for Sync)
    pub receiver: Mutex<Receiver<BuildOutput>>,
    /// Sender to signal cancellation (send any value to cancel)
    pub cancel_sender: Sender<()>,
}

impl AsyncBuildHandle {
    /// Try to receive a message from the build thread
    pub fn try_recv(&self) -> Option<BuildOutput> {
        self.receiver.lock().ok()?.try_recv().ok()
    }

    /// Send a cancellation signal to stop the build/game process
    pub fn cancel(&self) {
        let _ = self.cancel_sender.send(());
    }
}

/// Launch a game project asynchronously with progress reporting
///
/// Spawns a background thread that:
/// 1. Runs `cargo run` with piped stdout/stderr
/// 2. Parses output for progress information
/// 3. Sends BuildOutput messages through the channel
///
/// Returns a handle with a receiver for progress updates and a sender to cancel.
pub fn launch_game_async(options: LaunchOptions) -> Result<AsyncBuildHandle, LaunchError> {
    // Verify project exists before spawning thread
    if !options.project_path.exists() {
        return Err(LaunchError::ProjectNotFound(options.project_path.clone()));
    }

    if !options.project_path.join("Cargo.toml").exists() {
        return Err(LaunchError::ProjectNotFound(options.project_path.clone()));
    }

    let (output_tx, output_rx) = mpsc::channel();
    let (cancel_tx, cancel_rx) = mpsc::channel();

    thread::spawn(move || {
        run_build_thread(options, output_tx, cancel_rx);
    });

    Ok(AsyncBuildHandle {
        receiver: Mutex::new(output_rx),
        cancel_sender: cancel_tx,
    })
}

/// Background thread function that runs cargo and parses output
fn run_build_thread(options: LaunchOptions, tx: Sender<BuildOutput>, cancel_rx: Receiver<()>) {
    // Build cargo command
    let mut cmd = Command::new("cargo");
    cmd.arg("run");
    cmd.current_dir(&options.project_path);

    if options.release {
        cmd.arg("--release");
    }

    if options.hot_reload {
        cmd.args(["--features", "dev"]);
    }

    // Force cargo to show progress even when piped
    // CARGO_TERM_PROGRESS_WHEN=always forces progress display
    // CARGO_TERM_PROGRESS_WIDTH sets the width (we use 80 for parsing)
    cmd.env("CARGO_TERM_PROGRESS_WHEN", "always");
    cmd.env("CARGO_TERM_PROGRESS_WIDTH", "80");

    // Pipe stderr to capture build progress (cargo outputs to stderr)
    cmd.stdout(Stdio::piped());
    cmd.stderr(Stdio::piped());

    // Spawn the process
    let mut child = match cmd.spawn() {
        Ok(child) => child,
        Err(e) => {
            let _ = tx.send(BuildOutput::BuildFailed(format!(
                "Failed to spawn cargo: {}",
                e
            )));
            return;
        }
    };

    // Read stderr in a separate thread (cargo build output goes to stderr)
    // Note: Cargo uses \r for progress updates and \n for other messages
    let stderr = child.stderr.take().unwrap();
    let tx_stderr = tx.clone();
    let stderr_handle = thread::spawn(move || {
        use std::io::Read;
        let mut reader = BufReader::new(stderr);
        let mut buffer = String::new();
        let mut byte_buf = [0u8; 1];
        let mut build_complete_sent = false;

        // Read byte by byte to handle both \r and \n as line terminators
        loop {
            match reader.read(&mut byte_buf) {
                Ok(0) => break, // EOF
                Ok(_) => {
                    let ch = byte_buf[0] as char;
                    if ch == '\r' || ch == '\n' {
                        if !buffer.is_empty() {
                            let line = buffer.clone();
                            buffer.clear();

                            // Parse progress from line
                            if let Some((current, total)) = parse_progress(&line) {
                                let _ = tx_stderr.send(BuildOutput::Progress(current, total));
                            }

                            // Parse crate name from "Compiling <crate> v<version>" lines
                            if let Some(crate_name) = parse_compiling_crate(&line) {
                                let _ = tx_stderr.send(BuildOutput::CurrentCrate(crate_name));
                            }

                            // Detect build completion: "Finished `dev` profile..." or "Finished `release` profile..."
                            // This happens BEFORE the game starts running
                            if !build_complete_sent && line.trim_start().starts_with("Finished") {
                                let _ = tx_stderr.send(BuildOutput::BuildComplete);
                                build_complete_sent = true;
                            }

                            // Detect game started: "Running `target/debug/...`" or "Running `target/release/...`"
                            if line.trim_start().starts_with("Running `") {
                                let _ = tx_stderr.send(BuildOutput::GameStarted);
                            }

                            // Send the raw line (but skip progress bar updates to reduce noise)
                            if !line.contains("Building [") {
                                let _ = tx_stderr.send(BuildOutput::Line(line));
                            }
                        }
                    } else {
                        buffer.push(ch);
                    }
                }
                Err(_) => break,
            }
        }
    });

    // Read stdout in another thread (game output goes to stdout)
    let stdout = child.stdout.take().unwrap();
    let tx_stdout = tx.clone();
    let stdout_handle = thread::spawn(move || {
        let reader = BufReader::new(stdout);
        for line in reader.lines().map_while(Result::ok) {
            let _ = tx_stdout.send(BuildOutput::Line(line));
        }
    });

    // Wait for process or cancellation
    loop {
        // Check for cancellation
        if cancel_rx.try_recv().is_ok() {
            let _ = child.kill();
            let _ = child.wait();
            let _ = tx.send(BuildOutput::BuildFailed("Build cancelled".to_string()));
            return;
        }

        // Check if process has finished
        match child.try_wait() {
            Ok(Some(status)) => {
                // Wait for output threads to finish
                let _ = stderr_handle.join();
                let _ = stdout_handle.join();

                // GameExited is sent when the process exits (game window closed)
                // BuildComplete and GameStarted are sent earlier from stderr parsing
                if status.success() {
                    let _ = tx.send(BuildOutput::GameExited(status.code()));
                } else {
                    // Could be build failure or game crash
                    let _ = tx.send(BuildOutput::BuildFailed(format!(
                        "Process exited with code: {:?}",
                        status.code()
                    )));
                }
                return;
            }
            Ok(None) => {
                // Still running, sleep briefly
                thread::sleep(std::time::Duration::from_millis(50));
            }
            Err(e) => {
                let _ = tx.send(BuildOutput::BuildFailed(format!(
                    "Error waiting for process: {}",
                    e
                )));
                return;
            }
        }
    }
}

/// Strip ANSI escape codes from a string
fn strip_ansi_codes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '\x1b' {
            // Skip escape sequence: ESC [ ... (letter)
            if chars.peek() == Some(&'[') {
                chars.next(); // consume '['
                              // Skip until we hit a letter (the terminator)
                while let Some(&c) = chars.peek() {
                    chars.next();
                    if c.is_ascii_alphabetic() {
                        break;
                    }
                }
            }
        } else {
            result.push(ch);
        }
    }

    result
}

/// Parse progress from cargo output line
/// Looks for patterns like: "Building [=====>    ] 113/413: ..."
fn parse_progress(line: &str) -> Option<(u32, u32)> {
    // Strip ANSI codes first
    let clean_line = strip_ansi_codes(line);

    // Look for "Building [" pattern
    if !clean_line.contains("Building [") {
        return None;
    }

    // Find the progress numbers after "] "
    if let Some(bracket_end) = clean_line.find("] ") {
        let after_bracket = &clean_line[bracket_end + 2..];
        // Find the colon that ends the numbers
        if let Some(colon_pos) = after_bracket.find(':') {
            let numbers = &after_bracket[..colon_pos];
            // Parse "current/total"
            let parts: Vec<&str> = numbers.split('/').collect();
            if parts.len() == 2 {
                if let (Ok(current), Ok(total)) = (parts[0].trim().parse(), parts[1].trim().parse())
                {
                    return Some((current, total));
                }
            }
        }
    }

    None
}

/// Parse crate name from "Compiling <crate> v<version>" lines
fn parse_compiling_crate(line: &str) -> Option<String> {
    // Strip ANSI codes first
    let clean_line = strip_ansi_codes(line);
    let trimmed = clean_line.trim();
    if let Some(rest) = trimmed.strip_prefix("Compiling ") {
        // Find the version marker " v"
        if let Some(v_pos) = rest.find(" v") {
            return Some(rest[..v_pos].to_string());
        }
    }
    None
}