15static std::unexpected<Error>
Unbound(std::string reason)
20static uint64_t
Rva(
const Image& module,
const void* address)
22 return static_cast<uint64_t
>(
static_cast<const uint8_t*
>(address) -
module.Base);
29 return Unbound(
"empty pattern");
35 return Unbound(std::format(
"module '{}' is not loaded", moduleName));
39 return Unbound(
"pattern not found");
43 return Unbound(
"pattern matched more than once");
49template <
class TEntry>
59 return Unbound(std::format(
"{} {} is negative", what, *value));
66 void** slots =
static_cast<void**
>(table);
67 const void* original = originalSlots ? originalSlots(slots, index) :
nullptr;
68 return original ? original : slots[index];
73 : _file(file), _originalSlots(originalSlots), _scriptBindings(scriptBindings)
75 const auto index = [
this](
const auto& section,
GameDataSection kind, std::string_view name) {
76 for (
const auto& key : section | std::views::keys)
89 for (
auto& [key, entry] : _entries)
91 if (entry.Sections.size() > 1)
94 Error::NotFound(std::format(
"in both '{}' and '{}'", entry.Sections[0], entry.Sections[1])));
101 for (
auto& [key, entry] : _entries)
103 if (!entry.Reason.empty())
109 entry.Address = bound->Address;
110 entry.Value = bound->Value;
114 Fail(key, entry, bound.error());
121 const auto it = _entries.find(key);
122 return it != _entries.end() ? &it->second :
nullptr;
127 Log::Info(
"GameData: {} (server {}, verified {}): {} functions, {} globals, {} vtables, {} offsets.", path,
128 _file.build.server, _file.build.verified, _file.functions.size(), _file.globals.size(),
129 _file.vtables.size(), _file.offsets.size());
135 std::ranges::count_if(_file.vtables, [](
const auto& each) { return each.second.Script.empty(); });
136 const auto numbered =
137 std::ranges::count_if(_file.offsets, [](
const auto& each) { return each.second.Base.empty(); });
138 Log::Warn(
"GameData: verified on server {}, running {}: {} vtable indices and {} offsets are unchecked.",
139 _file.build.server,
GameBuild(), indices, numbered);
145 const auto address = [](
void* found) {
return Bound{.Address = found}; };
149 return FindFunction(key).transform(address);
151 return FindGlobal(key).transform(address);
153 return FindSlot(key).transform(
154 [](VirtualSlot slot) {
return Bound{.Address = slot.Table, .Value = slot.Index}; });
156 return FindOffset(key).transform([](
int value) {
return Bound{.Value = value}; });
158 return Unbound(
"in no section");
161void GameDataScanner::Fail(std::string_view key,
GameDataEntry& entry,
const Error& error)
163 entry.Reason = error.Detail;
164 _failures.push_back(std::format(
"{}: {}", key, entry.Reason));
167Result<void*> GameDataScanner::FindFunction(
const std::string& key)
169 const GameDataDocument::Function& entry = _file.functions.at(key);
170 if (!entry.Script.empty())
172 return FindScriptFunction(key, entry);
181 const auto match =
Scan(entry.Module, *pattern);
184 return std::unexpected(match.error());
187 _record.Functions.emplace(key, GameDataRecord::Location{entry.Module,
Rva(match->Module, match->Address)});
188 return match->Address;
193 const auto module = _symbols.Module(entry.Module);
196 return std::unexpected(module.error());
198 const auto table = _symbols.Table(entry.Module, entry.Class);
201 return std::unexpected(table.error());
203 const auto target = FindScript(*table, entry.Script);
206 return std::unexpected(target.error());
208 if (!(*module)->Contains(target->Address))
210 return Unbound(std::format(
"VScript '{}' is virtual or outside '{}'", entry.Script, entry.Module));
213 _record.Functions.emplace(key, GameDataRecord::Location{entry.Module,
Rva(**module, target->Address)});
214 return target->Address;
217Result<void*> GameDataScanner::FindGlobal(
const std::string& key)
219 const GameDataDocument::Global& entry = _file.globals.at(key);
226 const auto match =
Scan(entry.Module, column->pattern);
229 return std::unexpected(match.error());
232 const uintptr_t target =
234 auto* global =
reinterpret_cast<void*
>(target);
235 if (!target || !match->Module.Contains(global) || !
IsReadableAddress(global,
sizeof(
void*)))
238 std::format(
"the rel32 at +{} does not point at readable memory in '{}'", column->rel32At, entry.Module));
241 _record.Globals.emplace(key, GameDataRecord::Location{entry.Module,
Rva(match->Module, global)});
247 const GameDataDocument::VTable& entry = _file.vtables.at(key);
249 const bool tableOnly = entry.Script.empty() && !entry.Windows && !entry.Linux;
254 return std::unexpected(index.error());
257 const auto module = _symbols.Module(entry.Module);
260 return std::unexpected(module.error());
262 const auto table = _symbols.Table(entry.Module, entry.Class, entry.Base);
265 return std::unexpected(table.error());
269 _record.VTables.emplace(key, GameDataRecord::Slot{entry.Module,
Rva(**module, *table), -1, 0});
270 return VirtualSlot{.Index = -1, .Table = *table};
272 if (!entry.Script.empty())
274 index = ScriptSlot(*table, entry.Script);
277 return std::unexpected(index.error());
282 void** slot =
static_cast<void**
>(*table) + *index;
286 const std::string& owner = entry.Base.empty() ? entry.Class : entry.Base;
287 return Unbound(std::format(
"{}::[{}] does not hold code", owner, *index));
291 const Image& loaded = **
module;
292 const uint64_t codeRva = loaded.Contains(code) ?
Rva(loaded, code) : 0;
293 _record.VTables.emplace(key, GameDataRecord::Slot{entry.Module,
Rva(loaded, *table), *index, codeRva});
294 return VirtualSlot{.Index = *index, .Table = *table};
299 if (!_scriptBindings)
301 return Unbound(
"no VScript lookup");
303 return _scriptBindings(table, name);
306Result<int> GameDataScanner::ScriptSlot(
void* table,
const std::string& name)
const
308 const auto target = FindScript(table, name);
311 return std::unexpected(target.error());
313 if (target->Index < 0)
315 return Unbound(std::format(
"VScript '{}' is not virtual", name));
317 return target->Index;
320Result<int> GameDataScanner::FindOffset(
const std::string& key)
322 const GameDataDocument::Offset& entry = _file.offsets.at(key);
326 _record.Offsets.emplace(key, *offset);
333 if (entry.Class.empty())
335 return Unbound(std::format(
"base '{}' names no class", entry.Base));
337 return _symbols.Base(entry.Module, entry.Class, entry.Base).transform([](
const BaseSubobject& base) {
void LogSummary(std::string_view path) const
GameDataScanner(const GameDataDocument &file, const OriginalSlotLookup &originalSlots, const ScriptBindingLookup &scriptBindings)
const GameDataEntry * Find(std::string_view key) const
void Info(std::format_string< Args... > fmt, Args &&... args)
void Warn(std::format_string< Args... > fmt, Args &&... args)
std::function< const void *(void **vtable, int index)> OriginalSlotLookup
ScanResult FindPatternEx(std::string_view moduleName, const std::string &pattern)
bool IsExecutableAddress(const void *address)
bool IsReadableAddress(const void *address, size_t bytes)
const auto & PlatformColumn(const TEntry &entry)
std::expected< T, Error > Result
uintptr_t ResolveRelativeAddress(const Image &module, uintptr_t matchAddress, int ripOffset, int ripSize)
static std::unexpected< Error > Unbound(std::string reason)
std::function< Result< ScriptTarget >(void *classTable, std::string_view name)> ScriptBindingLookup
std::string_view GameBuild()
static uint64_t Rva(const Image &module, const void *address)
static const void * OriginalSlot(void *table, int index, const OriginalSlotLookup &originalSlots)
static Result< int > ColumnValue(const TEntry &entry, std::string_view what)
constexpr std::string_view PlatformName
static Result< ScanResult > Scan(std::string_view moduleName, const std::string &pattern)
One failure: a code to branch on, text for the log, and an optional translation key.
static Error NotFound(std::string detail)
std::map< std::string, Function > functions
std::map< std::string, Global > globals
std::map< std::string, VTable > vtables
std::map< std::string, Offset > offsets
std::vector< std::string_view > Sections