Skip to content

Instantly share code, notes, and snippets.

@Gekkio
Created March 28, 2024 20:45
Show Gist options
  • Save Gekkio/ae7d0e773f2355f4ad8ababfb60166d4 to your computer and use it in GitHub Desktop.
Save Gekkio/ae7d0e773f2355f4ad8ababfb60166d4 to your computer and use it in GitHub Desktop.
use http_req::{request, response::StatusCode};
use std::{
collections::HashMap,
env,
ffi::OsStr,
fs::{self, File},
io::{self, BufReader, BufWriter, Write},
path::PathBuf,
};
use zip::ZipArchive;
const VERSION: &str = "20220522-1522-55c535c";
fn main() {
let out_dir =
PathBuf::from(env::var_os("OUT_DIR").expect("Missing OUT_DIR environment variable"));
let mts_release = format!("mts-{VERSION}");
let zip_path = out_dir.join("mts.zip");
if !zip_path.exists() {
let url =
format!("https://gekkio.fi/files/mooneye-test-suite/{mts_release}/{mts_release}.zip");
let mut file = BufWriter::new(File::create(&zip_path).expect("Failed to create zip file"));
let res = request::get(&url, &mut file).expect("Failed to fetch zip file");
assert_eq!(res.status_code(), StatusCode::new(200));
file.flush().expect("Failed to write zip file");
}
let roms_path = out_dir.join("mts");
let mts_rs_path = out_dir.join("mts.rs");
if !roms_path.exists() || !mts_rs_path.exists() {
fs::create_dir_all(&roms_path).expect("Failed to create ROM directory");
let mut zip = ZipArchive::new(BufReader::new(
File::open(&zip_path).expect("Failed to open ZIP file"),
))
.expect("Failed to open ZIP file");
let mut tests = HashMap::new();
let release_prefix = format!("{mts_release}/");
for i in 0..zip.len() {
let mut file = zip.by_index(i).expect("Failed to get ZIP file entry");
if !file.is_file() {
continue;
}
let Some(path) = file
.enclosed_name()
.and_then(|path| path.strip_prefix(&release_prefix).ok())
.filter(|&path| path.to_str() != Some(""))
else {
continue;
};
if path.extension() == Some(OsStr::new("gb")) {
let test_name = path
.with_extension("")
.to_string_lossy()
.replace(
|c| match c {
'-' | '/' => true,
_ => false,
},
"_",
)
.to_uppercase();
tests.insert(test_name, path.to_string_lossy().to_string());
}
let target_path = roms_path.join(path);
if let Some(parent) = target_path.parent() {
if !parent.exists() {
fs::create_dir_all(parent).expect("Failed to create directory");
}
}
let mut target_file = File::create(target_path).expect("Failed to create file");
io::copy(&mut file, &mut target_file).expect("Failed to extract file");
target_file.flush().expect("Failed to write file");
}
let mut mts_rs =
BufWriter::new(File::create(mts_rs_path).expect("Failed to create mts.rs"));
for (name, path) in tests {
writeln!(&mut mts_rs, "/// {path}").unwrap();
writeln!(
&mut mts_rs,
"pub static {name}: &[u8] = include_bytes!(concat!(env!(\"OUT_DIR\"), \"/mts/{path}\"));"
)
.unwrap();
}
mts_rs.flush().expect("Failed to write mts.rs");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment