VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Image.windows.cpp
Go to the documentation of this file.
2
3#include <windows.h>
4
5#include <cstring>
6#include <psapi.h>
7#include <string>
8#include <string_view>
9#include <utility>
10#include <vector>
11
12namespace VoltMod
13{
14
15static std::string_view BaseName(std::string_view path)
16{
17 const size_t slash = path.find_last_of("\\/");
18 return slash == std::string_view::npos ? path : path.substr(slash + 1);
19}
20
21// Module file names are case-insensitive on Windows, so the match must be too.
22static bool SameFileName(std::string_view left, std::string_view right)
23{
24 return left.size() == right.size() && _strnicmp(left.data(), right.data(), left.size()) == 0;
25}
26
27bool FindModuleAndRanges(std::string_view fileName, Image& module, std::vector<ScanRange>& ranges)
28{
30 HMODULE modules[1024];
31 DWORD needed = 0;
32
34 {
35 return false;
36 }
37
38 Image best;
39 for (DWORD i = 0; i < needed / sizeof(HMODULE); ++i)
40 {
41 char buffer[MAX_PATH];
43 if (length == 0)
44 {
45 continue;
46 }
47
48 const std::string_view path(buffer, length);
49 if (!SameFileName(BaseName(path), fileName))
50 {
51 continue;
52 }
53
55 if (GetModuleInformation(process, modules[i], &info, sizeof(info)) && info.SizeOfImage > best.Size)
56 {
57 best = {static_cast<const uint8_t*>(info.lpBaseOfDll), info.SizeOfImage, std::string(path)};
58 }
59 }
60
61 if (!best.Base)
62 {
63 return false;
64 }
65
66 module = std::move(best);
67 ranges.assign(1, ScanRange{module.Base, module.Size});
68 return true;
69}
70
71} // namespace VoltMod
bool FindModuleAndRanges(std::string_view fileName, Image &module, std::vector< ScanRange > &ranges)
static bool SameFileName(std::string_view left, std::string_view right)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
static std::string_view BaseName(std::string_view path)