forked from CasualCoderGuy/FO4-ModSwitchFramework-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSF_Addresses.h
97 lines (80 loc) · 1.98 KB
/
MSF_Addresses.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#include <unordered_map>
class IDDatabase
{
private:
IDDatabase();
uintptr_t _base;
std::unordered_map<int, ptrdiff_t> id_database;
public:
[[nodiscard]] static IDDatabase& get()
{
static IDDatabase singleton;
return singleton;
}
[[nodiscard]] ptrdiff_t id2offset(int a_id) const
{
auto itAddr = id_database.find(a_id);
if (itAddr != id_database.end())
return itAddr->second;
else
return 0;
}
[[nodiscard]] ptrdiff_t id2addr(int a_id) const
{
auto itAddr = id_database.find(a_id);
if (itAddr != id_database.end())
return _base + itAddr->second;
else
return 0;
}
[[nodiscard]] uintptr_t base() const
{
return _base;
}
};
class Offset
{
public:
constexpr Offset() noexcept = default;
explicit constexpr Offset(std::size_t a_offset, std::size_t a_offset_ng = 0) noexcept :
#ifndef NEXTGEN
_offset(a_offset)
#else
_offset((a_offset_ng == 0) ? a_offset : a_offset_ng)
#endif
{}
constexpr Offset& operator=(std::size_t a_offset) noexcept
{
_offset = a_offset;
return *this;
}
[[nodiscard]] std::uintptr_t address() const { return base() + offset(); }
[[nodiscard]] constexpr std::size_t offset() const noexcept { return _offset; }
private:
[[nodiscard]] static std::uintptr_t base() { return IDDatabase::get().base(); }
std::size_t _offset{ 0 };
};
class ID
{
public:
constexpr ID() noexcept = default;
explicit constexpr ID(std::uint64_t a_id, std::uint64_t a_id_ng = 0) noexcept :
#ifndef NEXTGEN
_id(a_id)
#else
_id((a_id_ng == 0) ? a_id : a_id_ng)
#endif
{}
constexpr ID& operator=(std::uint64_t a_id) noexcept
{
_id = a_id;
return *this;
}
[[nodiscard]] std::uintptr_t address() const { return base() + offset(); }
[[nodiscard]] constexpr std::uint64_t id() const noexcept { return _id; }
[[nodiscard]] std::size_t offset() const { return IDDatabase::get().id2offset(_id); }
private:
[[nodiscard]] static std::uintptr_t base() { return IDDatabase::get().base(); }
std::uint64_t _id{ static_cast<std::uint64_t>(-1) };
};