VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ModuleSymbols.cpp
Go to the documentation of this file.
2
4
5#include <format>
6
7namespace VoltMod
8{
9
10static std::unexpected<Error> Unbound(std::string reason)
11{
12 return std::unexpected(Error::NotFound(std::move(reason)));
13}
14
16{
17 auto [it, added] = _modules.try_emplace(moduleName);
18 if (added)
19 {
20 FindImage(moduleName, it->second);
21 }
22 if (!it->second.Base)
23 {
24 return Unbound(std::format("module '{}' is not loaded", moduleName));
25 }
26 return &it->second;
27}
28
29Result<void*> ModuleSymbols::Table(const std::string& moduleName, const std::string& className,
30 const std::string& baseName)
31{
32 if (!baseName.empty())
33 {
34 const auto base = Base(moduleName, className, baseName);
35 if (!base)
36 {
37 return std::unexpected(base.error());
38 }
39 if (!base->Table)
40 {
41 return Unbound(std::format("'{}' has no vtable of its own in '{}'", baseName, className));
42 }
43 return base->Table;
44 }
45
46 const auto module = Module(moduleName);
47 if (!module)
48 {
49 return std::unexpected(module.error());
50 }
51 auto [it, added] = _tables.try_emplace({moduleName, className}, nullptr);
52 if (added)
53 {
55 }
56 if (!it->second)
57 {
58 return Unbound(std::format("no vtable for '{}' in '{}'", className, moduleName));
59 }
60 return it->second;
61}
62
63Result<BaseSubobject> ModuleSymbols::Base(const std::string& moduleName, const std::string& className,
64 const std::string& baseName)
65{
66 const auto module = Module(moduleName);
67 if (!module)
68 {
69 return std::unexpected(module.error());
70 }
71 auto key = std::tuple{moduleName, className, baseName};
72 auto it = _bases.find(key);
73 if (it == _bases.end())
74 {
75 it = _bases.emplace(std::move(key), FindBaseIn(**module, className, baseName)).first;
76 }
77 return it->second;
78}
79
80} // namespace VoltMod
Result< BaseSubobject > Base(const std::string &moduleName, const std::string &className, const std::string &baseName)
Result< void * > Table(const std::string &moduleName, const std::string &className, const std::string &baseName={})
Result< const Image * > Module(const std::string &moduleName)
bool FindImage(std::string_view moduleName, Image &module)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
Result< BaseSubobject > FindBaseIn(const Image &module, std::string_view className, std::string_view baseName)
static std::unexpected< Error > Unbound(std::string reason)
std::expected< T, Error > Result
Definition Result.hpp:64
void * FindVirtualTableIn(const Image &module, std::string_view className)
static Error NotFound(std::string detail)
Definition Result.hpp:49