Skip to content

Hooks and code

Same ideas as tweaks, from code.

addresses.resolve(text, names) takes the same address text tweaks do, and gives where it is now. names are extra names the text can use, often none.

import { Wraith } from "wraith";
const addresses = Wraith.addresses();
const takeDamage = addresses.resolve('sig("40 53 48 83 EC 20")', []);
const player = addresses.resolve("[game.exe+0x2F00A0] + 0x10", []);

What your schemas declare doesn’t need any of this. Each struct’s addresses give where its methods and statics are, like GameStateManager.addresses.spendMira(), and your namespace’s give the top-level ones. Hooks take the function itself, see below.

If another mod hooked the function first, its bytes aren’t there anymore. Lookups then try the snapshot of the game’s code from when Wraith started, then the game’s exe on disk, as schemas do. addresses.find is addresses.resolve that also says which it was found in, as an address and a source. Anything but live means something changed those bytes, so a patch expecting them will fail.

addresses.setSources says which copies your plugin’s lookups use, its schemas’ [Sig]s too, with live, snapshot and disk each on or off. Till you call it, Wraith’s Find hooked code setting says.

A hook sends calls to a game function to your handler instead. Declare the function in a schema and hook it by passing the function itself, so the hook is typed as declared. Wraith only needs to know where it is, so there’s no name to spell out. The examples here are from a plugin with the id trails_horizon, so its namespace is TrailsHorizon, with this schema:

schemas/horizon.sal
[Instance("48 8B 0D ?? ?? ?? ?? 3D", Rip)]
struct GameStateManager {
[Sig("E8 ?? ?? ?? ?? 48 8B 0D ?? ?? ?? ?? F3 0F 10 36", Rip)]
u32 spendMira(u32 amount);
}

The handler gets the game’s arguments, with self first for a method. It can also ask for two more things:

  • The hook, whose original() is the function as it was, typed like the declared one. Call it to run the game’s code.
  • The context, a number you set in the options. It’s handy when one handler serves several hooks.

How it asks depends on the language, see each tab. A [Virtual(n)] method works the same, and Wraith swaps its slot in the struct’s [VirtualTable] for you.

hookAt does the same for a function at an address of your own, typed like a declared one. That’s handy for a copy of a function the game has somewhere else.

Set disabled in the options to start a hook off, then enable() once it’s set up. disable() turns it off again and destroy() puts the code back. Wraith puts everything back when the plugin goes.

Pass the function from your namespace, like GameStateManager.spendMira. The handler’s parameters are matched by name. One named hook gets the hook, one named context gets the context, and the rest are the game’s arguments in order. TypeScript can’t type parameters by their names, so the typings put hook and context after the arguments. Write them last, and leave them off when you don’t need them.

import { Wraith, TrailsHorizon } from "wraith";
const { GameStateManager } = TrailsHorizon;
const hooks = Wraith.hooks();
// Shops still take your Mira, just none of it
const freeShopping = hooks.hook(GameStateManager.spendMira, (self, _amount, hook) => hook.original()(self, 0), { disabled: true });
freeShopping.enable();

hook.original() is typed like GameStateManager.spendMira, so it takes the same arguments. The hook also has context, enable(), disable() and destroy().

With a context, one handler can serve several hooks:

// Half price with 2, a third with 3
const discount = (by: number) =>
hooks.hook(GameStateManager.spendMira, (self, amount, hook, context) => hook.original()(self, Math.floor(amount / context)), { context: by, disabled: true });

hooks.hookAt(address, target, handler, options) hooks your own address, typed like target:

const copy = Wraith.addresses().resolve('sig("40 53 48 89 5C 24 08")', []);
hooks.hookAt(copy, GameStateManager.spendMira, (self, amount, hook) => hook.original()(self, amount * 2), {});

The typed hooks above cover what your schemas declare. For a function that isn’t in a schema, or a quick experiment, hooks also take an address and a signature. Prefer the typed ones whenever you can.

  • hooks.detour(target, signature, handler, options) hooks the function at target. The signature is like "fn i32 (i32 damage)".
  • hooks.detourSlot(slot, signature, handler, options) swaps a function pointer, like one in a vtable.
  • hooks.named(name, handler, options) hooks a declared function by its name as text, like "Player.heal".
  • hooks.context() gives the running hook’s context from inside its handler.

Make the handler with fn from "salamander", and make the original callable with native. Both also take a declared function in place of a signature, like native(GameStateManager.spendMira, address), typed as declared.

import { Wraith } from "wraith";
import { fn, native } from "salamander";
const sig = "fn i32 (i32 damage)";
let original: (damage: number) => number;
const hook = Wraith.hooks().detour(takeDamage, sig, fn(sig, (damage) => original(Math.floor(damage / 2))), {});
original = native(sig, hook.original());

hooks.mid(target, options, handler) runs your handler right before an instruction, with the registers. Changes to them go back into the game. It’s for spots in the middle of a function, where there’s no function to hook.

  • expect checks the bytes or asm there first, so it won’t hook a game that changed.
  • size is how many bytes it takes over, ending between instructions. 0 means what expect covers, or else the one instruction there. A mid hook can’t end inside an instruction, so give expect the whole one.
  • replace replaces the code there rather than running before it.
  • disabled starts it off, like other hooks.

The address comes from your schema’s typed addresses, like any other spot. This one keeps part of each change to a gauge, so it fills slower. The game stores the new gauge from xmm7 into the struct rdi points at, so the schema has the spot and the struct:

schemas/horizon.sal
[Sig("F3 0F 5F FE 0F 28 B4 24 ?? ?? ?? ?? F3 0F 11 BF", Offset(12))]
static u8 tailing_gauge_store;
struct TailingState {
[Offset(0xD4)] f32 gauge;
}

cpu.xmm7.f32x4[0] is the low float in xmm7.

import { Wraith, TrailsHorizon } from "wraith";
const { TailingState } = TrailsHorizon;
let speed = 0.25;
const slowGauge = Wraith.hooks().mid(TrailsHorizon.addresses.tailing_gauge_store(), { expect: "F3 0F 11 BF D4 00 00 00", disabled: true }, (cpu) => {
const state = TailingState.at(cpu.rdi);
if (state) cpu.xmm7.f32x4[0] = state.gauge + (cpu.xmm7.f32x4[0] - state.gauge) * speed;
});
slowGauge.enable();

A tweak with a speed field can turn it on and off and set the speed, as the examples that come with Wraith do.

Asm, like in a tweak. code.patch(target, asm, options) replaces code, and code.inject(target, asm, options) runs asm before the instruction there. code.patchBytes writes exact bytes. They start on unless disabled is set, and give a patch with enable, disable and destroy. expect checks what’s there first.

import { Wraith } from "wraith";
const code = Wraith.code();
code.patch(target, "nop", { expect: "mov [rbx+0x98], eax" });
code.inject(target, "mov eax, [rbx+0x98]", {});
code.inject(target, `mov dword ptr [0x${player.toString(16)}], 100`, {});

code.disassemble(address, count) gives the instructions there, with their address, length, bytes and text. It stops at memory it can’t read. code.instructionLength(address) gives just one’s length.

import { Wraith } from "wraith";
const code = Wraith.code(), log = Wraith.log();
for (const i of code.disassemble(target, 5)) log.info(`${i.address} ${i.text}`);
const size = code.instructionLength(target);

watches.add(address, length, kind, handler) uses one of the CPU’s four debug registers. write watches writes, access reads and writes, and execute with a length of 1 is a breakpoint. The game doesn’t stop, and hits come to your handler on the render thread at the start of the next frame, with a copy of the registers. To change the registers, use hooks.mid.

remove() stops it, or Wraith does when the plugin unloads.

import { Wraith } from "wraith";
const watch = Wraith.watches().add(health, 4, "write", (hit, cpu) => {
Wraith.log().info(`written by ${hit.instruction}, rcx was ${cpu.rcx}`);
});
watch.remove();

Hook handlers run on whatever game thread called the function, which waits for them. Keep them short.

A script runs one thing at a time, so a hook that’s hit while the script’s busy is skipped, so it can miss calls.

Made with ♥ by Asgard