|
| 1 | +use std::{io::Write, path::PathBuf}; |
| 2 | + |
| 3 | +use sclauncher::util::{ |
| 4 | + admin::{is_admin, run_as_admin}, |
| 5 | + game::GameManager, |
| 6 | + reg::{async_registry_search, get_game_path}, |
| 7 | +}; |
| 8 | + |
| 9 | +use clap::Parser; |
| 10 | +use winreg::enums::HKEY_LOCAL_MACHINE; |
| 11 | + |
| 12 | +use std::io; |
| 13 | + |
| 14 | +use tokio::time::{sleep, Duration}; |
| 15 | + |
| 16 | +#[derive(Parser, Debug)] |
| 17 | +#[command( |
| 18 | + name = "SC1 Mutli Loader", |
| 19 | + version = "1.0", |
| 20 | + author = "Seok Won Choi", |
| 21 | + about = "StarCraft 1 Multi launcher" |
| 22 | +)] |
| 23 | +struct Args { |
| 24 | + /// Performs an asynchronous search in the registry |
| 25 | + #[arg(short, long, action = clap::ArgAction::SetTrue)] |
| 26 | + async_registry_search: bool, |
| 27 | + |
| 28 | + /// Number of times to launch the game |
| 29 | + #[arg(short, long, default_value_t = 2)] |
| 30 | + num_launches: u32, |
| 31 | + |
| 32 | + /// 64bits or 32bits |
| 33 | + #[arg(short, long, default_value_t = false)] |
| 34 | + is_64bit: bool, |
| 35 | +} |
| 36 | + |
| 37 | +#[tokio::main] |
| 38 | +async fn main() { |
| 39 | + if !is_admin() { |
| 40 | + println!("Not running as admin. Attempting to elevate..."); |
| 41 | + if run_as_admin() { |
| 42 | + println!("Please restart the application with admin privileges."); |
| 43 | + return; // Exit the current instance |
| 44 | + } else { |
| 45 | + println!("Failed to elevate privileges."); |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + let args = Args::parse(); |
| 51 | + let game_manager = GameManager::new(); |
| 52 | + |
| 53 | + // Try to get game path from user input or registry if not found initially |
| 54 | + let game_path = match get_game_path_or_search(&args).await { |
| 55 | + Ok(path) => path, |
| 56 | + Err(e) => { |
| 57 | + eprintln!("{}", e); |
| 58 | + return; |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + // Launch the game the specified number of times |
| 63 | + launch_game_multiple_times(&game_manager, &game_path, args.num_launches).await; |
| 64 | + |
| 65 | + println!("Press Enter to kill all games..."); |
| 66 | + let mut pause = String::new(); |
| 67 | + std::io::stdin().read_line(&mut pause).unwrap(); |
| 68 | + |
| 69 | + game_manager.kill_all_games().await; |
| 70 | +} |
| 71 | + |
| 72 | +async fn get_game_path_or_search(args: &Args) -> Result<PathBuf, String> { |
| 73 | + // Try to get the game path directly |
| 74 | + let direct_path = get_game_path(args.is_64bit); |
| 75 | + |
| 76 | + if args.async_registry_search || direct_path.is_none() { |
| 77 | + println!("Attempting to locate StarCraft.exe..."); |
| 78 | + |
| 79 | + // Perform the registry search if direct path is not found or if async search is requested |
| 80 | + let matches = |
| 81 | + async_registry_search(HKEY_LOCAL_MACHINE, "StarCraft", "InstallLocation").await; |
| 82 | + if matches.is_empty() && direct_path.is_none() { |
| 83 | + // If no matches and no direct path, prompt user for manual input |
| 84 | + return prompt_user_for_path(); |
| 85 | + } |
| 86 | + |
| 87 | + // Use the first found path from registry search if available |
| 88 | + matches.first().map_or_else( |
| 89 | + || direct_path.ok_or_else(|| "StarCraft not found.".to_string()), // Use direct path if no registry matches |
| 90 | + |(_, path)| Ok(PathBuf::from(path)), // Convert the registry path to PathBuf |
| 91 | + ) |
| 92 | + } else { |
| 93 | + // If direct path is found, return it |
| 94 | + direct_path.ok_or_else(|| "StarCraft not found.".to_string()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn prompt_user_for_path() -> Result<PathBuf, String> { |
| 99 | + println!("Please enter the full path to StarCraft.exe:"); |
| 100 | + let mut path_input = String::new(); |
| 101 | + io::stdout().flush().expect("Failed to flush stdout"); |
| 102 | + io::stdin() |
| 103 | + .read_line(&mut path_input) |
| 104 | + .map_err(|e| e.to_string())?; |
| 105 | + |
| 106 | + let trimmed_path = path_input.trim(); |
| 107 | + if trimmed_path.is_empty() { |
| 108 | + return Err("No path provided.".to_string()); |
| 109 | + } |
| 110 | + |
| 111 | + let path = PathBuf::from(trimmed_path); |
| 112 | + if !path.exists() { |
| 113 | + return Err("The provided path does not exist.".to_string()); |
| 114 | + } |
| 115 | + Ok(path) |
| 116 | +} |
| 117 | + |
| 118 | +async fn launch_game_multiple_times(game_manager: &GameManager, path: &PathBuf, num_launches: u32) { |
| 119 | + for i in 0..num_launches { |
| 120 | + println!("Launching StarCraft.exe [{}]", i + 1); |
| 121 | + game_manager.launch_game(path.to_path_buf()).await; |
| 122 | + sleep(Duration::from_secs(1)).await; |
| 123 | + } |
| 124 | +} |
0 commit comments