Skip to content

Commit bf87d44

Browse files
Merge pull request DK22Pac#262 from ICantReadYourMind/master
Update patch, injector, SpriteLoader, and GTA3 CPad
2 parents e77220b + cf20918 commit bf87d44

6 files changed

Lines changed: 69 additions & 55 deletions

File tree

injector/injector.hpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <windows.h>
2929
#include <cstdint>
3030
#include <cstdio>
31+
#include <type_traits>
3132
#include "gvm/gvm.hpp"
3233
#include "..\shared\DynAddress.h"
3334
/*
@@ -61,7 +62,24 @@
6162

6263
namespace injector
6364
{
65+
/*
66+
* valid_pointer_type
67+
* Concept to check if T is either a pointer or an integer that fits in a pointer
68+
*/
69+
template <class T>
70+
concept valid_pointer_type = std::is_scalar_v<T> && sizeof(T) <= sizeof(uintptr_t) && !std::is_floating_point_v<T>;
6471

72+
/*
73+
* to_void_pointer
74+
* Function to convert a valid_pointer_type to a void*
75+
*/
76+
template <valid_pointer_type T>
77+
inline void* to_void_pointer(T x)
78+
{
79+
void* p = nullptr;
80+
std::memcpy(&p, std::addressof(x), sizeof(T));
81+
return p;
82+
}
6583

6684
/*
6785
* auto_pointer
@@ -79,8 +97,9 @@ union auto_pointer
7997
public:
8098
auto_pointer() : p(0) {}
8199
auto_pointer(const auto_pointer& x) : p(x.p) {}
82-
explicit auto_pointer(void* x) : p(x) {}
83-
explicit auto_pointer(uint32_t x) : a(x) {}
100+
101+
template<valid_pointer_type T>
102+
explicit auto_pointer(T x) : p(to_void_pointer(x)) {}
84103

85104
bool is_null() const { return this->p != nullptr; }
86105

@@ -117,13 +136,10 @@ union basic_memory_pointer
117136
public:
118137
basic_memory_pointer() : p(nullptr) {}
119138
basic_memory_pointer(std::nullptr_t) : p(nullptr) {}
120-
basic_memory_pointer(uintptr_t x) : a(x) {}
139+
template<valid_pointer_type T>
140+
basic_memory_pointer(T x) : p(to_void_pointer(x)) {}
121141
basic_memory_pointer(const auto_pointer& x) : p(x.p) {}
122142
basic_memory_pointer(const basic_memory_pointer& rhs) : p(rhs.p) {}
123-
124-
template<class T>
125-
basic_memory_pointer(T* x) : p((void*)x) {}
126-
127143

128144

129145

@@ -136,8 +152,9 @@ union basic_memory_pointer
136152
// Gets the raw pointer, without translation (casted to T*)
137153
template<class T> T* get_raw() const { return auto_pointer(p); }
138154

139-
// This type can get assigned from void* and uintptr_t
140-
basic_memory_pointer& operator=(void* x) { return p = x, *this; }
155+
// This type can get assigned from any pointer and uintptr_t
156+
template<valid_pointer_type T>
157+
basic_memory_pointer& operator=(T x) { return p = to_void_pointer(x), *this; }
141158
basic_memory_pointer& operator=(uintptr_t x) { return a = x, *this; }
142159

143160
/* Arithmetic */
@@ -222,13 +239,10 @@ union memory_pointer_tr
222239
: p(rhs.p)
223240
{} // Constructs from my own type, copy constructor
224241

225-
memory_pointer_tr(uintptr_t x)
226-
: p(memory_pointer(x).get())
227-
{} // Constructs from a integer, translating the address
228-
229-
memory_pointer_tr(void* x)
230-
: p(memory_pointer(x).get())
231-
{} // Constructs from a void pointer, translating the address
242+
template<valid_pointer_type T>
243+
memory_pointer_tr(T x)
244+
: p(memory_pointer(to_void_pointer(x)).get())
245+
{} // Constructs from any pointer or integer, translating the address
232246

233247
// Just to be method-compatible with basic_memory_pointer ...
234248
auto_pointer get() { return auto_pointer(p); }

plugin_III/game_III/CPad.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,18 @@ class CPad {
1818
CControllerState PCTempKeyState;
1919
CControllerState PCTempJoyState;
2020
CControllerState PCTempMouseState;
21-
short wordD2;
21+
short Phase;
2222
short Mode;
2323
short ShakeDur;
24-
char ShakeFreq;
25-
char byteD9;
26-
char byteDA;
27-
char byteDB;
28-
char byteDC;
29-
char byteDD;
30-
char byteDE;
31-
char DisablePlayerControls;
32-
char byteE0;
33-
char gapE1[15];
34-
int dwordF0;
35-
int dwordF4;
36-
int dwordF8;
24+
unsigned char ShakeFreq;
25+
bool bHornHistory[5];
26+
unsigned char iCurrHornHistory;
27+
unsigned char DisablePlayerControls;
28+
char bApplyBrakes;
29+
char CheatString[12];
30+
int LastTimeTouched;
31+
int AverageWeapon;
32+
int AverageEntries;
3733

3834
//variables
3935
static short &ReconState;

shared/Patch.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ void plugin::patch::Nop(uintptr_t address, size_t size, bool vp) {
4141
injector::MakeNOP(GetGlobalAddress(address), size, vp);
4242
}
4343

44-
void plugin::patch::RedirectCall(uintptr_t address, void *func, bool vp) {
44+
void plugin::patch::RedirectCall(uintptr_t address, injector::memory_pointer_raw func, bool vp) {
4545
injector::MakeCALL(GetGlobalAddress(address), func, vp);
4646
}
4747

48-
void plugin::patch::RedirectJump(uintptr_t address, void *func, bool vp) {
48+
void plugin::patch::RedirectJump(uintptr_t address, injector::memory_pointer_raw func, bool vp) {
4949
injector::MakeJMP(GetGlobalAddress(address), func, vp);
5050
}
5151

@@ -77,7 +77,7 @@ void plugin::patch::SetFloat(uintptr_t address, float value, bool vp) {
7777
injector::WriteMemory(GetGlobalAddress(address), value, vp);
7878
}
7979

80-
void plugin::patch::SetPointer(uintptr_t address, void *value, bool vp) {
80+
void plugin::patch::SetPointer(uintptr_t address, injector::memory_pointer_raw value, bool vp) {
8181
injector::WriteMemory(GetGlobalAddress(address), value, vp);
8282
}
8383

@@ -130,7 +130,7 @@ void plugin::patch::GetRaw(uintptr_t address,void* ret, size_t size, bool vp) {
130130
injector::ReadMemoryRaw(GetGlobalAddress(address), ret, size, vp);
131131
}
132132

133-
void plugin::patch::RedirectShortJump(uintptr_t address, void* dest, bool vp) {
133+
void plugin::patch::RedirectShortJump(uintptr_t address, injector::memory_pointer_raw dest, bool vp) {
134134

135135
uintptr_t GlobalAddress = GetGlobalAddress(address);
136136
injector::WriteMemory<uint8_t>(GlobalAddress, 0xEB, vp);

shared/Patch.h

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ class patch {
2323
public:
2424
static void NopRestore(uintptr_t address, bool vp = true);
2525
static void Nop(uintptr_t address, size_t size, bool vp = true);
26-
static void RedirectCall(uintptr_t address, void *func, bool vp = true);
27-
static void RedirectJump(uintptr_t address, void *func, bool vp = true);
26+
static void RedirectCall(uintptr_t address, injector::memory_pointer_raw func, bool vp = true);
27+
static void RedirectJump(uintptr_t address, injector::memory_pointer_raw func, bool vp = true);
2828
static void SetChar(uintptr_t address, char value, bool vp = true);
2929
static void SetUChar(uintptr_t address, unsigned char value, bool vp = true);
3030
static void SetShort(uintptr_t address, short value, bool vp = true);
3131
static void SetUShort(uintptr_t address, unsigned short value, bool vp = true);
3232
static void SetInt(uintptr_t address, uintptr_t value, bool vp = true);
3333
static void SetUInt(uintptr_t address, uintptr_t value, bool vp = true);
3434
static void SetFloat(uintptr_t address, float value, bool vp = true);
35-
static void SetPointer(uintptr_t address, void *value, bool vp = true);
35+
static void SetPointer(uintptr_t address, injector::memory_pointer_raw value, bool vp = true);
3636
static char GetChar(uintptr_t address, bool vp = true);
3737
static unsigned char GetUChar(uintptr_t address, bool vp = true);
3838
static short GetShort(uintptr_t address, bool vp = true);
@@ -43,23 +43,23 @@ class patch {
4343
static void *GetPointer(uintptr_t address, bool vp = true);
4444
static void SetRaw(uintptr_t address, void* value, size_t size, bool vp = true);
4545
static void GetRaw(uintptr_t address, void* ret, size_t size, bool vp = true);
46-
static void RedirectShortJump(uintptr_t address, void* dest = nullptr, bool vp = true);
46+
static void RedirectShortJump(uintptr_t address, injector::memory_pointer_raw dest = nullptr, bool vp = true);
4747
static void PutRetn(uintptr_t address, unsigned short BytesToPop = 0, bool vp = true);
4848
static void PutRetn0(uintptr_t address, unsigned short BytesToPop = 0, bool vp = true);
4949
static void PutRetn1(uintptr_t address, unsigned short BytesToPop = 0, bool vp = true);
5050

5151
template <typename T>
52-
static void Set(uintptr_t address, T value, bool vp = true) {
52+
static inline void Set(uintptr_t address, T value, bool vp = true) {
5353
injector::WriteMemory(GetGlobalAddress(address), value, vp);
5454
}
5555

5656
template <typename T>
57-
static T Get(uintptr_t address, bool vp = true) {
57+
static inline T Get(uintptr_t address, bool vp = true) {
5858
return injector::ReadMemory<T>(GetGlobalAddress(address), vp);
5959
}
6060

6161
template <typename T>
62-
static T TranslateCallOffset(uintptr_t address) {
62+
static inline T TranslateCallOffset(uintptr_t address) {
6363
if (GetUChar(address) == 0xE8) {
6464
auto offset = GetInt(address + 1);
6565
offset += (GetGlobalAddress(address) + 5);
@@ -69,7 +69,7 @@ class patch {
6969
}
7070

7171
template <typename T>
72-
static T TranslateJumpOffset(uintptr_t address) {
72+
static inline T TranslateJumpOffset(uintptr_t address) {
7373
if (GetUChar(address) == 0xE9) {
7474
auto offset = GetInt(address + 1);
7575
offset += (GetGlobalAddress(address) + 5);
@@ -81,52 +81,52 @@ class patch {
8181
static void ReplaceFunction(uintptr_t address, void *func, bool vp = true);
8282
static void ReplaceFunctionCall(uintptr_t address, void *func, bool vp = true);
8383

84-
static void SetPointer(std::vector<uintptr_t> const& addresses, void* value, bool vp = true) {
84+
static inline void SetPointer(std::vector<uintptr_t> const& addresses, injector::memory_pointer_raw value, bool vp = true) {
8585
for (auto& address : addresses) {
8686
SetPointer(address, value, vp);
8787
}
8888
}
8989

9090
template <typename T>
91-
static void Set(std::vector<uintptr_t> const& addresses, T value, bool vp = true) {
91+
static inline void Set(std::vector<uintptr_t> const& addresses, T value, bool vp = true) {
9292
for (auto& address : addresses) {
9393
Set(address, value, vp);
9494
}
9595
}
9696

97-
static void SetInt(std::vector<uintptr_t> const& addresses, uintptr_t value, bool vp = true) {
97+
static inline void SetInt(std::vector<uintptr_t> const& addresses, uintptr_t value, bool vp = true) {
9898
for (auto& address : addresses) {
9999
SetInt(address, value, vp);
100100
}
101101
}
102102

103-
static void SetFloat(std::vector<uintptr_t> const& addresses, float value, bool vp = true) {
103+
static inline void SetFloat(std::vector<uintptr_t> const& addresses, float value, bool vp = true) {
104104
for (auto& address : addresses) {
105105
SetFloat(address, value, vp);
106106
}
107107
}
108108

109-
static void RedirectJump(std::vector<uintptr_t> const& addresses, void* func, bool vp = true) {
109+
static inline void RedirectJump(std::vector<uintptr_t> const& addresses, injector::memory_pointer_raw func, bool vp = true) {
110110
for (auto& address : addresses) {
111111
RedirectJump(address, func, vp);
112112
}
113113
}
114114

115-
static void RedirectCall(std::vector<uintptr_t> const& addresses, void* func, bool vp = true) {
115+
static inline void RedirectCall(std::vector<uintptr_t> const& addresses, injector::memory_pointer_raw func, bool vp = true) {
116116
for (auto& address : addresses) {
117117
RedirectCall(address, func, vp);
118118
}
119119
}
120120

121-
static void Nop(std::vector<uintptr_t> const& addresses, size_t size, bool vp = true) {
121+
static inline void Nop(std::vector<uintptr_t> const& addresses, size_t size, bool vp = true) {
122122
for (auto& address : addresses) {
123123
Nop(address, size, vp);
124124
}
125125
}
126126

127127
using RegPack = injector::reg_pack;
128128

129-
static void StaticHook(uintptr_t start, uintptr_t end, void (*func)(RegPack&)) {
129+
static inline void StaticHook(uintptr_t start, uintptr_t end, void (*func)(RegPack&)) {
130130
struct staticHook {
131131
void (*func)(RegPack&);
132132

@@ -143,8 +143,12 @@ class patch {
143143
injector::MakeInline(start, end, *ptr);
144144
}
145145

146+
static inline void StaticHook(uintptr_t address, void (*func)(RegPack&)) {
147+
StaticHook(address, address + 5, func);
148+
}
149+
146150
template <typename T>
147-
static void StaticHook(uintptr_t start, uintptr_t end) {
151+
static inline void StaticHook(uintptr_t start, uintptr_t end) {
148152
injector::MakeInline<T>(start, end);
149153
}
150154
};

shared/SpriteLoader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ namespace plugin {
204204
}
205205

206206
CSprite2d SpriteLoader::GetSprite(std::string const& name) {
207-
CSprite2d sprite;
207+
CSprite2d sprite = {};
208208
auto s = spritesMap.find(name);
209209
if (s != spritesMap.end())
210210
sprite.m_pTexture = s->second;
@@ -213,7 +213,7 @@ namespace plugin {
213213
}
214214

215215
CSprite2d SpriteLoader::GetSprite(uint32_t id) {
216-
CSprite2d sprite;
216+
CSprite2d sprite = {};
217217

218218
auto s = spritesMapIndex.find(id);
219219
if (s != spritesMapIndex.end())
@@ -233,7 +233,7 @@ namespace plugin {
233233
mipMap = on;
234234
}
235235

236-
void SpriteLoader::SetExtension(std::string ext) {
236+
void SpriteLoader::SetExtension(std::string const& ext) {
237237
extension = ext;
238238
}
239239

shared/SpriteLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace plugin {
6464
CSprite2d GetSprite(uint32_t id);
6565
texClass* GetTex(std::string const& name);
6666
void SetMipMapOn(bool on);
67-
void SetExtension(std::string ext);
67+
void SetExtension(std::string const& ext);
6868
uint32_t GetMemoryUsed();
6969
};
7070
}

0 commit comments

Comments
 (0)