VFS: ReplaceFileW is redirected #59

Merged
gefydev merged 1 commit from upstream-pr/60 into main 2026-11-23 09:44:41 +00:00
gefydev commented 2026-11-22 23:07:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gefydev on 2026-11-22T23:07:43Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6549013887 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/60). Originally posted by [gefydev](https://github.com/gefydev) on 2026-11-22T23:07:43Z. --- `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.
upstream-sync closed this pull request 2026-11-24 06:56:58 +00:00
ivall commented 2026-11-24 07:00:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T09:45:18Z.


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!

<!-- greyfinch-sync:ivall/xcl:comment:7097053931 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/60#issuecomment-7097053931). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T09:45:18Z. --- 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!
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!59
No description provided.