Skip to content

The game's files

The vfs area sees the game’s files. Serve files that aren’t on disk, send the game somewhere else when it opens one, refuse a write, or just watch what it reads. It all works from any thread.

Paths are full ones with forward slashes, like C:/Game/data.pak. Backslashes work going in too, and case doesn’t matter.

For your plugin’s own files, see Settings and files.

vfs.put(path, bytes) makes a file the game can open, read and list like any other. It hides a real file at the same path. vfs.putDir(path) makes a folder. Files the game creates in it are virtual too, so its saves can live there without touching the disk.

vfs.read(path) gives a copy of a virtual file’s bytes, and fails with NotFound if there’s none. vfs.remove(path) takes a file or folder away. Whatever the game already has open keeps working. Everything you put goes when your plugin stops. In a tweak, it goes when the tweak turns off.

Bytes are an array of numbers going in, and a Uint8Array coming out.

import { Wraith } from "wraith";
const vfs = Wraith.vfs();
const ini = [..."[Game]\nDifficulty=Hard\n"].map((c) => c.charCodeAt(0));
vfs.put("C:/Games/Example/config/difficulty.ini", ini);
vfs.putDir("C:/Games/Example/saves");
// later, a save the game wrote there
const save = vfs.read("C:/Games/Example/saves/slot1.sav");

vfs.addWatch(options, handler) calls your handler for what the game does with files that match the Vfs.WatchOptions’s pattern. * matches anything, slashes too, and ? any one character. Empty is every path. kinds says which you want:

Kind When The reply can
open Before an open redirect it to another path, or deny it
opened After it opened
failed An open that failed, with its status
read After a read Set replace and data to change what it read
write Before a write deny it
closed The last handle to a file closed

The handler gets a Vfs.Event and gives back a Vfs.Reply. Left empty, the game goes ahead. deny is access_denied, not_found, sharing_violation or write_protected. The watch’s cancel() stops it, or it goes with your plugin.

kinds is a number: add 1 for open, 2 for opened, 4 for failed, 8 for read, 16 for write and 32 for closed.

import { Wraith } from "wraith";
const vfs = Wraith.vfs();
const { EventKind } = Wraith.Vfs;
// Use our own textures, wherever the game looks for them
const textures = vfs.addWatch({ pattern: "C:/Games/Example/textures/*.dds", kinds: 1 }, (event) => {
if (event.kind === EventKind.open) return { redirect: event.path!.replace("/textures/", "/mods/textures/") };
return {};
});
// Keep the game from writing its log
vfs.addWatch({ pattern: "*/game.log", kinds: 16 }, () => ({ deny: "access_denied" }));
textures.cancel();

The event has path, where the file really is, and requested, what the game asked for. They differ once something redirects it. file is the same number for every handle to one open file, so you can follow it from opened to closed. read_access and write_access say how it was opened, and a read’s or write’s data has its bytes.

The game’s thread waits for your handler, so keep it quick.

It runs when your script’s free, like a hook.

Existence checks like GetFileAttributes only come as opens when you set queries, and then they have query set. While Windows loads a DLL it holds a lock, so those opens are skipped unless you set loader. If you do, don’t wait on anything in the handler, other threads included.

Diagnostics > IO in the workbench shows what the game opens, reads, writes and closes, every file it has open right now, and your virtual files.

File watching can be turned off with file_io in the settings, for a game that doesn’t like it. Then vfs calls fail with Unavailable.

Made with ♥ by Asgard