|
| 1 | +use dialoguer::{theme::ColorfulTheme, Select}; |
| 2 | +use rayon::prelude::*; |
| 3 | +use std::fs::{self, OpenOptions}; |
| 4 | +use std::io::{BufRead, BufReader}; |
| 5 | +use std::path::Path; |
| 6 | +use walkdir::WalkDir; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + let options: Vec<&str> = vec!["Patch to CS:GO", "Patch back to CS2"]; |
| 10 | + let selection: usize = Select::with_theme(&ColorfulTheme::default()) |
| 11 | + .with_prompt("Which version would you like to patch to?") |
| 12 | + .default(0) |
| 13 | + .items(&options) |
| 14 | + .interact() |
| 15 | + .expect("Failed to read input"); |
| 16 | + |
| 17 | + let version: &str = if selection == 0 { "2000303" } else { "2000370" }; // 2000303 = CS:GO Legacy, 2000370 = CS2 |
| 18 | + |
| 19 | + let drives: Vec<String> = get_drives(); |
| 20 | + |
| 21 | + println!("Scanning all possible drives on your PC..."); |
| 22 | + |
| 23 | + let found_file = drives.par_iter().find_map_any(|drive: &String| { |
| 24 | + println!("Scanning: {}..", drive); |
| 25 | + find_file(drive, "steam.inf", "csgo") |
| 26 | + }); |
| 27 | + |
| 28 | + match found_file { |
| 29 | + Some(file_path) => { |
| 30 | + // println!("File found at: {}", file_path.display()); // just for debugging |
| 31 | + |
| 32 | + let version_name: &str = match version { |
| 33 | + "2000303" => "CSGO", |
| 34 | + "2000370" => "CS2", |
| 35 | + _ => version, |
| 36 | + }; |
| 37 | + |
| 38 | + if patch_file(&file_path, version) { |
| 39 | + println!("File successfully patched to version: {}.", version_name); |
| 40 | + |
| 41 | + let note: &str = if version == "2000303" { |
| 42 | + "You should now be able to use your skins in CSGO Legacy. You shouldn't play CS2 while this is active." |
| 43 | + } else { |
| 44 | + "You can now play CS2 (again)." |
| 45 | + }; |
| 46 | + |
| 47 | + println!("{}", note); |
| 48 | + // open_in_notepad(&file_path); // This was only used for testing reasons and is not (longer) necessary. |
| 49 | + } else { |
| 50 | + println!("Failed to patch the file."); |
| 51 | + } |
| 52 | + } |
| 53 | + None => { |
| 54 | + println!("File not found on any drive."); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn get_drives() -> Vec<String> { |
| 60 | + let drives: Vec<&str> = vec!["C:\\", "D:\\", "E:\\", "F:\\", "G:\\"]; |
| 61 | + drives |
| 62 | + .into_iter() |
| 63 | + .filter(|d: &&str| fs::metadata(d).is_ok()) |
| 64 | + .map(String::from) |
| 65 | + .collect() |
| 66 | +} |
| 67 | + |
| 68 | +fn find_file(dir: &str, file_name: &str, parent_dir: &str) -> Option<std::path::PathBuf> { |
| 69 | + for entry in WalkDir::new(dir).into_iter().filter_map(Result::ok) { |
| 70 | + if entry |
| 71 | + .file_name() |
| 72 | + .to_str() |
| 73 | + .map_or(false, |name: &str| name.eq_ignore_ascii_case(file_name)) |
| 74 | + { |
| 75 | + if let Some(parent) = entry.path().parent() { |
| 76 | + if parent |
| 77 | + .file_name() |
| 78 | + .map_or(false, |n| n.eq_ignore_ascii_case(parent_dir)) |
| 79 | + { |
| 80 | + return Some(entry.path().to_path_buf()); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + None |
| 86 | +} |
| 87 | + |
| 88 | +fn patch_file(file_path: &Path, version: &str) -> bool { |
| 89 | + let file: fs::File = OpenOptions::new() |
| 90 | + .read(true) |
| 91 | + .write(true) |
| 92 | + .open(file_path) |
| 93 | + .expect("Failed to open file"); |
| 94 | + let reader = BufReader::new(&file); |
| 95 | + let mut contents: Vec<String> = reader.lines().filter_map(Result::ok).collect(); |
| 96 | + |
| 97 | + for line in &mut contents { |
| 98 | + if line.starts_with("ClientVersion=") { |
| 99 | + *line = format!("ClientVersion={}", version); |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + let new_contents: String = contents.join("\n"); |
| 105 | + fs::write(file_path, new_contents).is_ok() |
| 106 | +} |
| 107 | + |
| 108 | +/* This was only used for testing reasons and is not (longer) necessary. */ |
| 109 | + |
| 110 | +// // Function to open the file in Notepad |
| 111 | +// fn open_in_notepad(file_path: &Path) { |
| 112 | +// Command::new("notepad.exe") |
| 113 | +// .arg(file_path) |
| 114 | +// .spawn() |
| 115 | +// .expect("Failed to open Notepad"); |
| 116 | +// } |
0 commit comments