/
dll.h
192 lines (152 loc) · 4.47 KB
/
dll.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once
#include <assert.h>
#include <string>
#include <memory>
#include <type_traits>
#include <functional>
namespace temple {
/*
Utility to validate the size of structures.
*/
template <typename Type, size_t ExpectedSize, size_t ActualSize = 0>
struct validate_size : std::true_type {
static_assert(ActualSize == ExpectedSize, "Structure does not have required size!");
};
template <typename Type, size_t ExpectedSize>
struct validate_size<Type, ExpectedSize, 0> : validate_size<Type, ExpectedSize, sizeof(Type)> {
};
class Dll {
friend class DllImpl;
public:
Dll(Dll&) = delete;
Dll(Dll&&) = delete;
Dll& operator=(const Dll&) = delete;
Dll& operator=(const Dll&&) = delete;
~Dll();
void* GetAddress(uint32_t vanillaAddress) const;
void Load(const std::wstring &installationPath);
void Unload();
bool HasBeenRebased();
/**
* Checks if the DLL has the Co8 save / load hooks.
*/
bool HasCo8Hooks();
std::wstring FindConflictingModule();
void ReserveMemoryRange();
void SetReservedMemory(void*);
bool IsVanillaDll() const;
bool IsExpandedWorldmapDll() const; // Since Co8 5.0.2
static Dll& GetInstance();
static void RegisterAddressPtr(void** ref);
void SetDebugOutputCallback(std::function<void(const std::string &text)> callback);
private:
std::shared_ptr<class DllImpl> mImpl;
void *mReservedMem;
Dll() {}
};
struct AddressTable
{
AddressTable() {}
protected:
template<typename T>
void rebase(T &ref, uint32_t offset) {
ref = reinterpret_cast<T>(offset);
Dll::RegisterAddressPtr(reinterpret_cast<void**>(&ref));
}
AddressTable(const AddressTable &) = delete;
AddressTable(const AddressTable &&) = delete;
AddressTable &operator =(const AddressTable &) = delete;
AddressTable &operator =(AddressTable &&) = delete;
};
template <typename T, uint32_t offsetPreset>
struct GlobalStruct
{
GlobalStruct() : mPtr(reinterpret_cast<T*>(offsetPreset))
{
static_assert(offsetPreset != 0, "This constructor should only be used with a template argument offset");
Dll::RegisterAddressPtr(reinterpret_cast<void**>(&mPtr));
}
operator T*()
{
return mPtr;
}
T* operator ->()
{
return mPtr;
}
T* ptr()
{
return mPtr;
}
private:
T* mPtr;
};
template <typename T, uint32_t offsetPreset>
struct GlobalPrimitive
{
GlobalPrimitive() : mPtr(reinterpret_cast<T*>(offsetPreset))
{
assert(mPtr != nullptr);
Dll::RegisterAddressPtr(reinterpret_cast<void**>(&mPtr));
}
T operator =(T value)
{
return (*mPtr = value);
}
operator T()
{
return *mPtr;
}
T* ptr()
{
return mPtr;
}
GlobalPrimitive & operator =(const GlobalPrimitive &) = delete;
GlobalPrimitive(const GlobalPrimitive &) = delete;
private:
T* mPtr;
};
template<uint32_t offset> using GlobalBool = GlobalPrimitive<bool, offset>;
// As long as we're replacing malloc/free in temple.dll, this struct
// does not need to override operator new/delete
struct TempleAlloc {
};
// Get's a pointer into DLL memory accounting for any offset from vanilla addresses
// that might've occured thanks to DLL rebasing
template <uint32_t address, typename T = void>
inline T* GetPointer()
{
static_assert(address > 0x10000000 && address < 0x20000000,
"address is not within temple.dll address space");
return Dll::GetInstance().GetAddress(address);
}
template <typename T = void>
inline T* GetPointer(uint32_t address)
{
assert(address > 0x10000000 && address < 0x20000000);
return (T*) Dll::GetInstance().GetAddress(address);
}
// Get's a pointer into DLL memory accounting for any offset from vanilla addresses
// that might've occured thanks to DLL rebasing
template <uint32_t address, typename T>
inline T& GetRef()
{
static_assert(address > 0x10000000 && address < 0x20000000,
"address is not within temple.dll address space");
return *(T*)Dll::GetInstance().GetAddress(address);
}
template <typename T>
inline T& GetRef(uint32_t address)
{
assert(address > 0x10000000 && address < 0x20000000);
return *(T*)Dll::GetInstance().GetAddress(address);
}
template<typename T>
inline void WriteMem(uint32_t address, T value) {
GetRef<T>(address) = value;
}
template<uint32_t address, typename T>
inline void WriteMem(T value) {
GetRef<address, T>() = value;
}
}