VFS: ReplaceFileW is redirected #60

Manually merged
github merged 1 commit from gh-pr/60/head into gh-pr/60/base 2026-11-26 04:07:10 +00:00
Owner

@⁠gefydev on GitHub · 2026-11-22 23:07 UTC · original
gefydev:vfs-replace-file → main · +39 −0 · 1 files · 1 commits · merged 2026-11-23 09:44 UTC

ReplaceFileW is the usual way to save safely: write save.tmp, then replace save.dat with it, optionally keeping save.bak. vfs_hooks.rs doesn't hook it, so:

  • On console paths outside the game folder (T:\, the PLS drive S:\), the call goes to the PC's own drive and fails, leaving the old save and the temporary file.
  • On a real path into the game folder, it replaces the game's own file instead of writing to the overlay.

The new hook:

  • With no game-folder path involved, translates the three paths (source_of for the two existing files, dest_of for the backup) and calls the original.
  • When the game folder is involved, makes the backup with CopyFileW and the replacement with MoveFileExW. Both go through their own hooks (the guard is dropped first), so the write lands in the overlay the same way a move does today.

CopyFileW is only hooked in #59; without it the backup copy is still the right call, it just isn't redirected yet. The two PRs don't conflict.

Validation

The hooks are process-wide and need an initialized VFS (a global OnceLock), so this isn't a unit test. I ran a probe alone, with a VFS that mounts G: (the game) and T: on temp folders and the real hooks installed. Under Wine:

main this branch
ReplaceFileW(T:\save.dat, T:\save.tmp, T:\save.bak) fails; save.dat still old, no backup, save.tmp left save.dat new, save.bak old, save.tmp gone
ReplaceFileW(G:\save.dat, T:\save.tmp, null) fails; G:\save.dat still the original G:\save.dat reads the new data from overlay\save.dat; the file in the game folder is still the original (checked outside Wine)

cargo test -p xcl --lib: 343 passed, 0 failed, on Windows GNU under Wine.

**@⁠gefydev** on GitHub · 2026-11-22 23:07 UTC · [original](https://github.com/xcl-dev/xcl/pull/60) `gefydev:vfs-replace-file` → `main` · +39 −0 · 1 files · 1 commits · merged 2026-11-23 09:44 UTC `ReplaceFileW` is the usual way to save safely: write `save.tmp`, then replace `save.dat` with it, optionally keeping `save.bak`. `vfs_hooks.rs` doesn't hook it, so: - On console paths outside the game folder (`T:\`, the PLS drive `S:\`), the call goes to the PC's own drive and fails, leaving the old save and the temporary file. - On a real path into the game folder, it replaces the game's own file instead of writing to the overlay. The new hook: - With no game-folder path involved, translates the three paths (`source_of` for the two existing files, `dest_of` for the backup) and calls the original. - When the game folder is involved, makes the backup with `CopyFileW` and the replacement with `MoveFileExW`. Both go through their own hooks (the guard is dropped first), so the write lands in the overlay the same way a move does today. `CopyFileW` is only hooked in #59; without it the backup copy is still the right call, it just isn't redirected yet. The two PRs don't conflict. ## Validation The hooks are process-wide and need an initialized VFS (a global `OnceLock`), so this isn't a unit test. I ran a probe alone, with a VFS that mounts `G:` (the game) and `T:` on temp folders and the real hooks installed. Under Wine: | | `main` | this branch | |---|---|---| | `ReplaceFileW(T:\save.dat, T:\save.tmp, T:\save.bak)` | fails; `save.dat` still old, no backup, `save.tmp` left | `save.dat` new, `save.bak` old, `save.tmp` gone | | `ReplaceFileW(G:\save.dat, T:\save.tmp, null)` | fails; `G:\save.dat` still the original | `G:\save.dat` reads the new data from `overlay\save.dat`; the file in the game folder is still the original (checked outside Wine) | `cargo test -p xcl --lib`: 343 passed, 0 failed, on Windows GNU under Wine.
ReplaceFileW (write a temporary file, then replace the save with it)
was not hooked, so it failed on console paths (T:\, the PLS drive) and,
on real paths into the game folder, replaced the game's own file instead
of writing to the overlay.

Console paths outside the game folder are now translated. When the game
folder is involved, the backup and the replacement go through the
CopyFileW and MoveFileExW hooks, so the write goes to the overlay.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:07:10 +00:00
github manually merged commit ab71385310 into gh-pr/60/base 2026-11-26 04:07:10 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 09:45 UTC · original

Merged (533187c). Verified on Windows 11 with the real hooks: ReplaceFileW and ReplaceFileA (which reaches ReplaceFileW) on T: – new data, backup with the old, temp gone; ReplaceFileW(G:\save.dat, T:\save.tmp, G:\save.bak) – G:\save.dat reads the new data from the overlay, the backup is in the overlay, the game folder's file is unchanged (checked from outside the process).

Tested on Windows 11 (RX 9070): cargo test --workspace green (lib 351, e2e), and a 45 s Unicorn Overlord run on the release build (VFS hooks active, ~60 fps, clean exit). Thanks!

**@⁠ivall** on GitHub · 2026-11-23 09:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/60#issuecomment-7097053931) Merged (533187c). Verified on Windows 11 with the real hooks: `ReplaceFileW` and `ReplaceFileA` (which reaches `ReplaceFileW`) on `T:` – new data, backup with the old, temp gone; `ReplaceFileW(G:\save.dat, T:\save.tmp, G:\save.bak)` – `G:\save.dat` reads the new data from the overlay, the backup is in the overlay, the game folder's file is unchanged (checked from outside the process). Tested on Windows 11 (RX 9070): `cargo test --workspace` green (lib 351, e2e), and a 45 s Unicorn Overlord run on the release build (VFS hooks active, ~60 fps, clean exit). Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.