initial commit

This commit is contained in:
WeeXnes 2025-01-08 02:28:47 +01:00
commit 9f3384f777
3 changed files with 2946 additions and 0 deletions

2784
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "ps2-games-manager"
version = "0.1.0"
edition = "2021"
[dependencies]
reqwest = { version = "0.12.7", features = ["blocking", "json"] }
tokio = { version = "1.40.0", features = ["full"] }
rfd = "0.15.2"
cdfs = "0.2.3"

151
src/main.rs Normal file
View file

@ -0,0 +1,151 @@
use cdfs::{ISO9660, DirectoryEntry};
use std::{fs::{self, File}, io::{self, Read, Write}, path::{Path, PathBuf}, process::exit};
use reqwest;
use std::io::copy;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().collect();
if args.len() < 5 {
eprintln!("Usage: -dir <directory> -install <iso_file>");
exit(1);
}
let dir_index = args.iter().position(|x| x == "-dir").ok_or("Missing -dir argument")? + 1;
let install_index = args.iter().position(|x| x == "-install").ok_or("Missing -install argument")? + 1;
let game_dir = &args[dir_index];
let iso_file_path = &args[install_index];
if !Path::new(game_dir).exists() {
eprintln!("The specified game directory does not exist.");
exit(1);
}
let serial = get_serial_from_iso(iso_file_path)?;
println!("Serial number found: {}", serial);
print!("Enter the game name: ");
io::stdout().flush()?;
let mut game_name = String::new();
io::stdin().read_line(&mut game_name)?;
let game_name = game_name.trim();
let dvd_dir = Path::new(game_dir).join("DVD");
fs::create_dir_all(&dvd_dir)?;
let new_iso_name = format!("{}.{}.iso", serial, game_name);
let destination_path = dvd_dir.join(new_iso_name);
copy_with_progress(iso_file_path, &destination_path)?;
println!("ISO file copied to: {}", destination_path.display());
let arts_dir = Path::new(game_dir).join("ART");
fs::create_dir_all(&arts_dir)?;
download_image(&serial, &arts_dir).await?;
println!("Game cover image downloaded and saved.");
Ok(())
}
fn get_serial_from_iso(iso_path: &str) -> Result<String, Box<dyn std::error::Error>> {
let file = File::open(iso_path)?;
let iso = ISO9660::new(file)?;
if let Some(DirectoryEntry::File(system_cnf)) = iso.open("/SYSTEM.CNF")? {
let mut file_data = Vec::new();
system_cnf.read().read_to_end(&mut file_data)?;
let content = String::from_utf8_lossy(&file_data);
for line in content.lines() {
if line.contains("BOOT2") {
if let Some(serial) = extract_serial(line) {
return Ok(serial);
}
}
}
Err("Serial number not found in SYSTEM.CNF.".into())
} else {
Err("SYSTEM.CNF file not found in the ISO.".into())
}
}
fn extract_serial(line: &str) -> Option<String> {
if let Some(start) = line.find("cdrom0:\\") {
let serial = &line[start + 8..]; // Skip "cdrom0:\"
if let Some(end) = serial.find(';') {
return Some(serial[..end].to_string());
}
}
None
}
fn copy_with_progress(source_path: &str, destination_path: &Path) -> std::io::Result<()> {
let source_file = File::open(source_path)?;
let metadata = source_file.metadata()?;
let total_size = metadata.len();
let mut source_reader = std::io::BufReader::new(source_file);
let mut destination_file = File::create(destination_path)?;
let mut buffer = [0; 8192];
let mut total_copied = 0u64;
loop {
let bytes_read = source_reader.read(&mut buffer)?;
if bytes_read == 0 {
break;
}
destination_file.write_all(&buffer[..bytes_read])?;
total_copied += bytes_read as u64;
let progress = (total_copied as f64 / total_size as f64) * 100.0;
print!("\rProgress: {:.2}%", progress);
std::io::stdout().flush()?;
}
println!();
Ok(())
}
async fn download_image(serial: &str, arts_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
let original_serial = serial.to_string();
let img_name = serial.replace(".", "").replace("_", "-");
let url = format!(
"https://github.com/xlenore/ps2-covers/blob/main/covers/default/{}.jpg?raw=true",
img_name
);
let response = reqwest::get(&url).await?;
if !response.status().is_success() {
eprintln!("Failed to download image. HTTP Status: {}", response.status());
return Err("Failed to download image".into());
}
fs::create_dir_all(arts_dir)?;
let file_name = format!("{}.jpg", original_serial);
let mut file = File::create(arts_dir.join(file_name))?;
let mut content = response.bytes().await?;
copy(&mut content.as_ref(), &mut file)?;
println!("Image downloaded and saved in ARTS directory.");
Ok(())
}