Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ xcl shaders D:\data\shaders.rpf --top 50 # pack statistics; --recomp / --compi
xcl rpf D:\data\shaders.rpf --extract NAME --out folder
```

- RPF extraction keeps existing files and duplicate records, shortening long names when adding
collision suffixes to fit Windows filename limits. Temporary filenames stay short.
- **Without a command line** (xcl.exe opened from Explorer) XCL shows a small menu: *Choose a
game…* (pick its exe or `MicrosoftGame.config`) or *Run again* (the last one). On an error the
window stays open until Enter.
Expand Down
15 changes: 12 additions & 3 deletions crates/xcl/src/rpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ fn extraction_path(out: &Path, name: &str, offset: usize, used: &mut BTreeSet<Pa

let mut n = 0usize;
loop {
let suffix = if n == 0 {
let mut suffix = if n == 0 {
format!("{safe}.{offset:08x}")
} else {
format!("{safe}.{offset:08x}.{n}")
};
if suffix.len() > 255 {
// Keep the collision suffix while shortening the original name.
let mut end = safe.len() - (suffix.len() - 255);
while !safe.is_char_boundary(end) {
end -= 1;
}
suffix.replace_range(end..safe.len(), "");
}
let path = out.join(suffix);
if !path.exists() && used.insert(path.clone()) {
return path;
Expand All @@ -66,10 +74,11 @@ fn atomic_write_with(
before_commit: impl FnOnce(&Path) -> io::Result<()>,
) -> io::Result<()> {
let parent = path.parent().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "output has no parent"))?;
let name = path.file_name().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "output has no file name"))?;
path.file_name().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "output has no file name"))?;
let (temporary, mut file) = loop {
let id = TEMP_ID.fetch_add(1, Ordering::Relaxed);
let candidate = parent.join(format!(".{}.tmp.{}.{id}", name.to_string_lossy(), std::process::id()));
// Scanned record names start with a letter or underscore, never this dot prefix.
let candidate = parent.join(format!(".xcl-rpf.tmp.{}.{id}", std::process::id()));
match std::fs::OpenOptions::new().write(true).create_new(true).open(&candidate) {
Ok(file) => break (TempPath(candidate), file),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
Expand Down
48 changes: 48 additions & 0 deletions crates/xcl/tests/rpf_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,51 @@ fn explicit_probe_rejects_conflicting_options() {
assert!(String::from_utf8(output.stderr).unwrap().contains(args[0]));
}
}

#[test]
fn extraction_preserves_long_duplicate_names_and_existing_files() {
let nonce = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
let root = std::env::temp_dir().join(format!("xcl-rpf-names-{}-{nonce}", std::process::id()));
let output_dir = root.join("out");
std::fs::create_dir_all(&output_dir).unwrap();
// Verbatim paths isolate filename-component limits from MAX_PATH handling.
let output_dir = std::fs::canonicalize(output_dir).unwrap();
let name = format!("PS_{}", "x".repeat(251));
std::fs::write(output_dir.join(&name), b"preserve me").unwrap();

let mut data = b"9FPR".to_vec();
data.resize(0x40, 0);
for payload in [b"first payload".as_slice(), b"second payload".as_slice()] {
let compressed = miniz_oxide::deflate::compress_to_vec(payload, 6);
data.push((name.len() + 1) as u8);
data.extend_from_slice(name.as_bytes());
data.push(0);
data.push(50);
for value in [compressed.len() as u32, 0, compressed.len() as u32, payload.len() as u32] {
data.extend_from_slice(&value.to_le_bytes());
}
data.extend_from_slice(&compressed);
}
let archive = root.join("synthetic.rpf");
std::fs::write(&archive, data).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_xcl"))
.arg("rpf")
.arg(&archive)
.arg("--extract")
.arg(&name)
.arg("--out")
.arg(&output_dir)
.output();
let preserved = std::fs::read(output_dir.join(&name));
let files = std::fs::read_dir(&output_dir).unwrap()
.map(|entry| entry.and_then(|entry| std::fs::read(entry.path())))
.collect::<Result<Vec<_>, _>>();
std::fs::remove_dir_all(root).unwrap();

let output = output.unwrap();
assert_eq!(output.status.code(), Some(0), "{}", String::from_utf8_lossy(&output.stderr));
assert_eq!(preserved.unwrap(), b"preserve me");
let mut files = files.unwrap();
files.sort();
assert_eq!(files, vec![b"first payload".to_vec(), b"preserve me".to_vec(), b"second payload".to_vec()]);
}
Loading