Memory
The memory area reads and writes the game’s memory. Reads and writes check the pages first, so a bad address gives an error instead of a crash. It all works from any thread.
Read and write
Section titled “Read and write”There’s a read and a write for each kind of number, like memory.readU32(address) and memory.writeF32(address, value), from U8 to I64, F32, F64 and Pointer. readString and readUtf16 read text up to a 0, and readBytes and writeBytes take bytes. isReadable(address, length) checks first.
For game types, schemas are easier, since they read and write fields by name.
import { Wraith } from "wraith";
const memory = Wraith.memory();
const gold = memory.readU32(address);memory.writeU32(address, gold + 100);local Wraith = require("wraith").Wraith
local memory = Wraith.memory()
local gold = memory:readU32(address)memory:writeU32(address, gold + 100)var memory = Memory.Instance;
var gold = memory.ReadU32(address);memory.WriteU32(address, gold + 100);auto memory = wraith::api().memory();
if (auto gold = memory.readU32(address)) memory.writeU32(address, *gold + 100);let memory = wraith::api().memory();
let gold = memory.read_u32(address).ok()?;memory.write_u32(address, gold + 100).ok()?;ctx.wraith.memory() has the same calls, like try ctx.wraith.memory().readU32(address). The SDK’s sdk.Address is a Zig helper on top, for following pointers in one go.
const gold = try sdk.Address.at(address).load(u32);try sdk.Address.at(base).deref().offset(0x10).store(u32, gold + 100);Wraith_Memory memory = Wraith_Api_memory(w);
uint32_t gold;if (Wraith_Memory_readU32(memory, address, &gold) == SAL_OK) Wraith_Memory_writeU32(memory, address, gold + 100);memory.main() is the game’s exe, and findModule(name) and containing(address) find others, each with its name, base and size. memory.find(module, name, pattern) finds a byte pattern like "48 8B ?? C3" that’s there once, and findAll every match.
Scan for a value
Section titled “Scan for a value”Like Cheat Engine. memory.scan(options) looks on a worker and gives a Memory.Scan, then next(compare, value, high) narrows it, on a worker too.
- The options’
typeisu8tou64,i8toi64,f32,f64,string,utf16orbytesfor a pattern like"48 8B ?? C3". valueis text, like100or-1.5.module, orstartandlength, look in less.nexttakesexact,changed,unchanged,increased,decreased,greater,lessorbetween, with a low and a high forbetween. The others leave them empty.
count() says how many are left, and results(offset, limit) gives their addresses. Results are freed when the plugin stops, or with destroy().
const scan = await memory.scan({ type: "i32", value: "100" });// Take a hit, thenawait scan.next("decreased", "", "");await scan.next("exact", "90", "");const [health] = scan.results(0, 1);task.spawn(function() local scan = memory:scan({ type = "i32", value = "100" }):await() -- Take a hit, then scan:next("decreased", "", ""):await() scan:next("exact", "90", ""):await() health = scan:results(0, 1)[1]end)var scan = (await memory.ScanAsync(new Memory.Scan.Options { Type = Memory.Scan.Type.I32, Value = "100" }))!;// Take a hit, thenawait scan.NextAsync(Memory.Scan.Compare.Decreased, "", "");await scan.NextAsync(Memory.Scan.Compare.Exact, "90", "");health = scan.Results(0, 1)[0];C# can’t await inside an unsafe class or method, so keep these in one that isn’t.
static uintptr_t health;
static sal::Future<void> find_health() { using Scan = Wraith::Memory::Scan; auto scan = co_await wraith::api().memory().scan({.type = Scan::Type::i32, .value = sal::String::of("100")}); if (!scan) co_return {}; // Take a hit, then co_await scan->next(Scan::Compare::decreased, "", ""); co_await scan->next(Scan::Compare::exact, "90", ""); auto found = scan->results(0, 1); if (!found.empty()) health = found[0]; co_return {};}use salamander::Str;use wraith::{MemoryScanCompare, MemoryScanOptions, MemoryScanType, ScanError};
fn find_health() { let _ = salamander::spawn(async { let memory = wraith::api().memory(); let scan = memory.scan(MemoryScanOptions { r#type: MemoryScanType::I32, value: Str::new("100"), ..Default::default() }).await?; // Take a hit, then scan.next(MemoryScanCompare::DECREASED, "", "").await?; scan.next(MemoryScanCompare::EXACT, "90", "").await?; let health = scan.results(0, 1).first().copied(); Ok::<(), ScanError>(()) });}ctx.wraith.memory().scan works as in other languages, with sdk.tasks.wait. The SDK’s sdk.mem.Search is the Workbench’s own value search, which runs right where you call it. It can take a while over all of memory, so run it off the render thread.
var search: sdk.mem.Search = .init(.local);defer search.deinit();try search.first(.{ .all = .{ .writable_only = true } }, .four_bytes, "100", .{});// Take a hit, thentry search.next(.decreased, "");try search.next(.exact, "90");if (search.count > 0) log.info("health at 0x{x}", .{search.address(0)});narrow takes your own test instead, for things like unsigned or between.
Each step gives a sal_Promise, and the next starts once it’s settled, like in Waiting.
Wraith_Memory_Scan_Options options = { .type = Wraith_Memory_Scan_Type_i32, .value = SAL_STR("100") };sal_Promise scanning = Wraith_Memory_scan(Wraith_Api_memory(w), options);/* Once it's settled, take the Wraith_Memory_Scan, then after a hit, Wraith_Memory_Scan_next it */Regions and protection
Section titled “Regions and protection”memory.regions(module, all) lists stretches of memory, just in module unless it’s empty. With all it lists memory you can’t read too. memory.protect(address, length, protection) changes the pages under it, and gives what the first one was. Wraith puts protection back when the plugin stops.
import { Wraith } from "wraith";
for (const r of memory.regions("game.exe", false)) Wraith.log().info(`${r.start} ${r.length} ${r.writable}`);
const old = memory.protect(address, 16, "read_write");local Wraith = require("wraith").Wraith
for _, r in ipairs(memory:regions("game.exe", false)) do Wraith.log():info(r.start .. " " .. r.length) end
local old = memory:protect(address, 16, "read_write")foreach (var r in memory.Regions("game.exe", false)) Log.Instance.Info($"{r.Start:X} {r.Length} {r.Writable}");
var old = memory.Protect(address, 16, Memory.Protection.ReadWrite);#include <format>
// in startauto memory = wraith::api().memory();if (auto regions = memory.regions("game.exe", false)) { for (const auto& r : *regions) wraith::api().log().info(std::format("{:x} {} {}", r.start, r.length, r.writable));}
auto old = memory.protect(address, 16, Wraith::Memory::Protection::read_write);use wraith::MemoryProtection;
let memory = wraith::api().memory();for r in memory.regions("game.exe", false).ok()? { wraith::api().log().info(&format!("{:x} {} {}", r.start, r.length, r.writable));}
let old = memory.protect(address, 16, MemoryProtection::READ_WRITE).ok()?;const memory = ctx.wraith.memory();for (try memory.regions("game.exe", false)) |r| log.info("0x{x} {d} {}", .{ r.start, r.length, r.writable });
const old = try memory.protect(address, 16, .read_write);Wraith_Memory memory = Wraith_Api_memory(w);Slice_Wraith_Memory_Region regions;if (Wraith_Memory_regions(memory, SAL_STR("game.exe"), false, ®ions) == SAL_OK) { for (uintptr_t i = 0; i < regions.len; i++) { char line[64]; int n = snprintf(line, sizeof line, "%llx %llu %d", (unsigned long long)regions.ptr[i].start, (unsigned long long)regions.ptr[i].length, regions.ptr[i].writable); Wraith_Log_info(Wraith_Api_log(w), (sal_string){ line, n > 0 ? (uintptr_t)n : 0 }); }}
Wraith_Memory_Protection old;Wraith_Memory_protect(memory, address, 16, Wraith_Memory_Protection_read_write, &old);Find what writes
Section titled “Find what writes”To see which code writes to an address, watch it with watches.add and write. Each hit says where the instruction is, and code.disassemble shows it. See Watches and breakpoints.
wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
