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.
Serve a file
Section titled “Serve a file”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 thereconst save = vfs.read("C:/Games/Example/saves/slot1.sav");Bytes are a string.
local Wraith = require("wraith").Wraith
local vfs = Wraith.vfs()
vfs:put("C:/Games/Example/config/difficulty.ini", "[Game]\nDifficulty=Hard\n")vfs:putDir("C:/Games/Example/saves")
-- later, a save the game wrote therelocal save = vfs:read("C:/Games/Example/saves/slot1.sav")Put takes bytes, like a byte[] or a "..."u8 string.
var vfs = Vfs.Instance;
vfs.Put("C:/Games/Example/config/difficulty.ini", "[Game]\nDifficulty=Hard\n"u8);vfs.PutDir("C:/Games/Example/saves");
// later, a save the game wrote thereusing var save = vfs.Read("C:/Games/Example/saves/slot1.sav")!;var bytes = save.Bytes();put takes a std::span<const uint8_t>, and read gives a sal::Blob, bytes kept by a count, which you release() once you’re done.
#include <string_view>
// in startauto vfs = wraith::api().vfs();
std::string_view ini = "[Game]\nDifficulty=Hard\n";vfs.put("C:/Games/Example/config/difficulty.ini", {reinterpret_cast<const uint8_t*>(ini.data()), ini.size()});vfs.putDir("C:/Games/Example/saves");
// later, a save the game wrote thereif (auto save = vfs.read("C:/Games/Example/saves/slot1.sav")) { std::span<const uint8_t> bytes = save->bytes(); save->release();}put takes a &[u8], and read gives a salamander::Blob, bytes kept by a count, which you release() once you’re done.
let vfs = wraith::api().vfs();
vfs.put("C:/Games/Example/config/difficulty.ini", b"[Game]\nDifficulty=Hard\n").ok()?;vfs.put_dir("C:/Games/Example/saves").ok()?;
// later, a save the game wrote thereif let Ok(save) = vfs.read("C:/Games/Example/saves/slot1.sav") { let bytes = save.bytes().to_vec(); save.release();}const vfs = ctx.wraith.vfs();try vfs.put("C:/Games/Example/config/difficulty.ini", "[Game]\nDifficulty=Hard\n");try vfs.putDir("C:/Games/Example/saves");Wraith_Vfs_read gives a sal_Blob, bytes kept by a count, which you sal_Blob_release once you’re done.
Wraith_Vfs vfs = Wraith_Api_vfs(w);
static const char ini[] = "[Game]\nDifficulty=Hard\n";Wraith_Vfs_put(vfs, SAL_STR("C:/Games/Example/config/difficulty.ini"), (Slice_u8){ (const uint8_t *)ini, sizeof ini - 1 });Wraith_Vfs_putDir(vfs, SAL_STR("C:/Games/Example/saves"));
/* later, a save the game wrote there */sal_object got;if (Wraith_Vfs_read(vfs, SAL_STR("C:/Games/Example/saves/slot1.sav"), &got) == SAL_OK) { sal_Blob save = { got.data, got.table }; sal_Slice_u8 bytes = sal_Blob_bytes(save); sal_Blob_release(save);}Watch the game
Section titled “Watch the game”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 themconst 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 logvfs.addWatch({ pattern: "*/game.log", kinds: 16 }, () => ({ deny: "access_denied" }));
textures.cancel();kinds is a number: add 1 for open, 2 for opened, 4 for failed, 8 for read, 16 for write and 32 for closed.
local Wraith = require("wraith").Wraith
local vfs = Wraith.vfs()local EventKind = Wraith.Vfs.EventKind
-- Use our own textures, wherever the game looks for themlocal textures = vfs:addWatch({ pattern = "C:/Games/Example/textures/*.dds", kinds = 1 }, function(event) if event.kind == EventKind.open then return { redirect = (event.path:gsub("/textures/", "/mods/textures/")) } end return {}end)
-- Keep the game from writing its logvfs:addWatch({ pattern = "*/game.log", kinds = 16 }, function() return { deny = "access_denied" } end)
textures:cancel()var vfs = Vfs.Instance;
// Use our own textures, wherever the game looks for themvar textures = vfs.AddWatch(new Vfs.WatchOptions { Pattern = "C:/Games/Example/textures/*.dds", Kinds = new Vfs.Kinds { Open = true } }, e => new Vfs.Reply { Redirect = e.Path.Replace("/textures/", "/mods/textures/") })!;
// Keep the game from writing its logvfs.AddWatch(new Vfs.WatchOptions { Pattern = "*/game.log", Kinds = new Vfs.Kinds { Write = true } }, e => new Vfs.Reply { Deny = Vfs.Denial.AccessDenied });
textures.Cancel();The reply’s text has to last after the handler gives it back, so keep it somewhere that does, like a thread_local.
#include <string>
// in startauto vfs = wraith::api().vfs();
// Use our own textures, wherever the game looks for themauto textures = vfs.addWatch({.kinds = Wraith::Vfs::Kinds{}.with_open(true), .pattern = sal::String::of("C:/Games/Example/textures/*.dds")}, sal::keep<Wraith::Vfs::Handler>([](Wraith::Vfs::Event event) -> Wraith::Vfs::Reply { thread_local std::string redirected; redirected = event.path.view(); redirected.replace(redirected.find("/textures/"), 10, "/mods/textures/"); return {.redirect = sal::String::of(redirected)}; }));
// Keep the game from writing its logvfs.addWatch({.kinds = Wraith::Vfs::Kinds{}.with_write(true), .pattern = sal::String::of("*/game.log")}, sal::keep<Wraith::Vfs::Handler>([](Wraith::Vfs::Event) -> Wraith::Vfs::Reply { return {.deny = Wraith::Vfs::Denial::access_denied}; }));
if (textures) textures->cancel();The reply’s text has to last after the handler gives it back, so keep it somewhere that does, like a thread-local.
use std::cell::RefCell;
use salamander::Str;use wraith::{VfsDenial, VfsEvent, VfsHandler, VfsKinds, VfsReply, VfsWatchOptions};
thread_local! { static REDIRECTED: RefCell<String> = const { RefCell::new(String::new()) };}
fn add_watches() -> Option<()> { let vfs = wraith::api().vfs();
// Use our own textures, wherever the game looks for them let options = VfsWatchOptions { kinds: VfsKinds::default().with_open(true), pattern: Str::new("C:/Games/Example/textures/*.dds"), ..Default::default() }; let textures = vfs .add_watch(options, VfsHandler::keep(|event: VfsEvent| { REDIRECTED.with_borrow_mut(|r| { *r = unsafe { event.path.get() }.replace("/textures/", "/mods/textures/"); VfsReply { redirect: Str::new(r), ..Default::default() } }) })) .ok()?;
// Keep the game from writing its log let options = VfsWatchOptions { kinds: VfsKinds::default().with_write(true), pattern: Str::new("*/game.log"), ..Default::default() }; vfs.add_watch(options, VfsHandler::keep(|_: VfsEvent| VfsReply { deny: VfsDenial::ACCESS_DENIED, ..Default::default() })).ok()?;
textures.cancel(); Some(())}const W = sdk.api.wraith;
fn onFile(self: *Plugin, event: W.Vfs.Event) W.Vfs.Reply { _ = self; if (event.kind == .open and std.mem.endsWith(u8, event.path, "/intro.bik")) return .{ .deny = .not_found }; return .{};}
// in startself.watch = try ctx.wraith.vfs().addWatch(.{ .kinds = .{ .open = true }, .pattern = "C:/Games/Example/movies/*",}, .init(self, onFile));
// in stop, or let it go with the pluginself.watch.cancel();static Wraith_Vfs_Reply deny_log(uintptr_t self, Wraith_Vfs_Event event) { (void)self; (void)event; Wraith_Vfs_Reply reply = { .deny = Wraith_Vfs_Denial_access_denied }; return reply;}
static const Wraith_Vfs_Handler_table deny_log_table = { SAL_TABLE(Wraith_Vfs_Handler), .call = deny_log };
/* in start *//* Keep the game from writing its log */Wraith_Vfs_WatchOptions options = { .kinds = Wraith_Vfs_Kinds_with_write(0, true), .pattern = SAL_STR("*/game.log") };Wraith_Vfs_Watch watch;Wraith_Vfs_addWatch(Wraith_Api_vfs(w), options, (Wraith_Vfs_Handler){ 0, &deny_log_table }, &watch);A redirect’s text has to last after the handler gives it back, so keep it in a buffer of your own.
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.
It runs when your script’s free, like a hook.
It runs on the thread doing the I/O.
It runs on the thread doing the I/O.
It runs on the thread doing the I/O.
It runs on the thread doing the I/O.
It runs on the thread doing the I/O.
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.
See it
Section titled “See it”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.
wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
