diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3db7134..ea770bb 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -14,6 +14,7 @@ use tauri::{async_runtime, Env, State, Window}; #[derive(Default, Clone)] struct Paths { project_root: PathBuf, + data_root: PathBuf, active_generation_pid: Arc>>, cancel_generation_requested: Arc, } @@ -166,6 +167,35 @@ fn generation_log_path(root: &Path, prompt: &str) -> Result { } } +fn seed_output_dir(resource_root: &Path, data_root: &Path) -> Result<(), String> { + let source_dir = resource_root.join("output"); + if !source_dir.exists() { + return Ok(()); + } + + let dest_dir = output_dir(data_root)?; + for entry in fs::read_dir(&source_dir).map_err(|e| format!("Failed to read bundled output dir: {e}"))? { + let entry = entry.map_err(|e| format!("Failed to read bundled output entry: {e}"))?; + let path = entry.path(); + if path + .extension() + .and_then(|s| s.to_str()) + .map(|s| s.eq_ignore_ascii_case("png")) + != Some(true) + { + continue; + } + let filename = path + .file_name() + .ok_or_else(|| "Bundled output entry has no filename".to_string())?; + let dest = dest_dir.join(filename); + if !dest.exists() { + fs::copy(&path, &dest).map_err(|e| format!("Failed to seed bundled map: {e}"))?; + } + } + Ok(()) +} + fn write_log_line(log_file: &mut File, line: impl AsRef) { let _ = writeln!(log_file, "{}", line.as_ref()); let _ = log_file.flush(); @@ -256,7 +286,7 @@ fn read_png_prompt(path: &Path) -> Option { #[tauri::command] fn list_maps(state: State) -> Result, String> { - let dir = output_dir(&state.project_root)?; + let dir = output_dir(&state.data_root)?; let mut maps = Vec::new(); if !dir.exists() { return Ok(maps); @@ -301,7 +331,7 @@ fn list_maps(state: State) -> Result, String> { #[tauri::command] fn delete_map(path: String, state: State) -> Result<(), String> { - let dir = output_dir(&state.project_root)? + let dir = output_dir(&state.data_root)? .canonicalize() .map_err(|e| format!("Failed to resolve output dir: {e}"))?; let candidate = PathBuf::from(path); @@ -376,6 +406,7 @@ async fn generate_map( let prompt_clone = enrich_prompt(&user_prompt); let root = state.project_root.clone(); + let data_root = state.data_root.clone(); let cfg = settings.unwrap_or_default(); let steps = cfg.steps.unwrap_or(25); let width = cfg.width.unwrap_or(1536); @@ -416,10 +447,10 @@ async fn generate_map( async_runtime::spawn_blocking(move || { let result = (|| { - let out_dir = output_dir(&root)?; + let out_dir = output_dir(&data_root)?; let script = script_path(&root)?; let python = python_binary(&root); - let log_path = generation_log_path(&root, &prompt_clone)?; + let log_path = generation_log_path(&data_root, &prompt_clone)?; let log_path_display = log_path.to_string_lossy().to_string(); let mut log_file = File::create(&log_path) .map_err(|e| format!("Failed to create generation log: {e}"))?; @@ -431,7 +462,7 @@ async fn generate_map( "--output-dir".to_string(), out_dir.to_string_lossy().to_string(), "--work-dir".to_string(), - root.to_string_lossy().to_string(), + data_root.to_string_lossy().to_string(), "--upscale".to_string(), upscale, "--steps".to_string(), @@ -641,9 +672,18 @@ fn main() { Ok::(resource_dir) }) .unwrap_or_else(|_| PathBuf::from(".")); + let data_root = if root.join("src-tauri").exists() { + root.clone() + } else { + tauri::api::path::app_data_dir(context.config()).unwrap_or_else(|| root.clone()) + }; + if let Err(e) = seed_output_dir(&root, &data_root) { + eprintln!("Failed to seed bundled maps: {e}"); + } let state = Paths { project_root: root, + data_root, ..Default::default() }; tauri::Builder::default()