forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_tools.h
More file actions
223 lines (188 loc) · 6.48 KB
/
Copy pathmemory_tools.h
File metadata and controls
223 lines (188 loc) · 6.48 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* =============================================================================
* Source Python
* Copyright (C) 2012 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
#ifndef _MEMORY_TOOLS_H
#define _MEMORY_TOOLS_H
#include <malloc.h>
#include "memalloc.h"
#include "dyncall.h"
#include "utility/wrap_macros.h"
#include "DynamicHooks.h"
using namespace DynamicHooks;
#include "boost/python.hpp"
using namespace boost::python;
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
inline size_t UTIL_GetMemSize(void* ptr)
{
#ifdef _WIN32
return g_pMemAlloc->GetSize(ptr);
#elif defined(__linux__)
return malloc_usable_size(ptr);
#else
#error "Implement me!"
#endif
}
inline void* UTIL_Alloc(size_t size)
{
#ifdef _WIN32
return g_pMemAlloc->IndirectAlloc(size);
#elif defined(__linux__)
return malloc(size);
#else
#error "Implement me!"
#endif
}
inline void* UTIL_Realloc(void* ptr, size_t size)
{
#ifdef _WIN32
return g_pMemAlloc->Realloc(ptr, size);
#elif defined(__linux__)
return realloc(ptr, size);
#else
#error "Implement me!"
#endif
}
inline void UTIL_Dealloc(void* ptr)
{
#ifdef _WIN32
g_pMemAlloc->Free(ptr);
#elif defined(__linux__)
free(ptr);
#else
#error "Implement me!"
#endif
}
//-----------------------------------------------------------------------------
// Convention enum
//-----------------------------------------------------------------------------
inline int GetDynCallConvention(Convention_t eConv)
{
switch (eConv)
{
case CONV_CDECL: return DC_CALL_C_DEFAULT;
case CONV_THISCALL:
#ifdef _WIN32
return DC_CALL_C_X86_WIN32_THIS_MS;
#else
return DC_CALL_C_X86_WIN32_THIS_GNU;
#endif
case CONV_STDCALL: return DC_CALL_C_X86_WIN32_STD;
}
// TODO: Throw exception
return 0;
}
//-----------------------------------------------------------------------------
// CPointer class
//-----------------------------------------------------------------------------
class CFunction;
class CPointer
{
public:
CPointer(unsigned long ulAddr = 0);
template<class T>
T get(int iOffset = 0)
{
if (!is_valid())
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Pointer is NULL.")
return *(T *) (m_ulAddr + iOffset);
}
template<class T>
void set(T value, int iOffset = 0)
{
if (!is_valid())
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Pointer is NULL.")
unsigned long newAddr = m_ulAddr + iOffset;
*(T *) newAddr = value;
}
const char * get_string(int iOffset = 0, bool bIsPtr = true);
void set_string(char* szText, int iSize = 0, int iOffset = 0, bool bIsPtr = true);
CPointer* get_ptr(int iOffset = 0);
void set_ptr(CPointer* ptr, int iOffset = 0);
unsigned long get_size() { return UTIL_GetMemSize((void *) m_ulAddr); }
unsigned long get_address() { return m_ulAddr; }
CPointer* add(int iValue);
CPointer* sub(int iValue);
bool is_valid() { return m_ulAddr ? true: false; }
CPointer* get_virtual_func(int iIndex, bool bPlatformCheck = true);
void realloc(int iSize) { m_ulAddr = (unsigned long) UTIL_Realloc((void *) m_ulAddr, iSize); }
void dealloc() { UTIL_Dealloc((void *) m_ulAddr); m_ulAddr = 0; }
CFunction* make_function(Convention_t eConv, char* szParams);
void reset_vm();
void set_mode(int iMode);
void set_arg_bool(bool value);
void set_arg_char(char value);
void set_arg_uchar(unsigned char value);
void set_arg_short(short value);
void set_arg_ushort(unsigned short value);
void set_arg_int(int value);
void set_arg_uint(unsigned int value);
void set_arg_long(long value);
void set_arg_ulong(unsigned long value);
void set_arg_long_long(long long value);
void set_arg_ulong_long(unsigned long long value);
void set_arg_float(float value);
void set_arg_double(double value);
void set_arg_pointer(object value);
void set_arg_string(char* value);
void call_void();
bool call_bool();
char call_char();
unsigned char call_uchar();
short call_short();
unsigned short call_ushort();
int call_int();
unsigned int call_uint();
long call_long();
unsigned long call_ulong();
long long call_long_long();
unsigned long long call_ulong_long();
float call_float();
double call_double();
CPointer* call_pointer();
const char* call_string();
protected:
unsigned long m_ulAddr;
};
class CFunction: public CPointer
{
public:
CFunction(unsigned long ulAddr, Convention_t eConv, char* szParams);
object __call__(object args);
object call_trampoline(object args);
void add_hook(DynamicHooks::HookType_t eType, PyObject* pCallable);
void remove_hook(DynamicHooks::HookType_t eType, PyObject* pCallable);
void add_pre_hook(PyObject* pCallable);
void add_post_hook(PyObject* pCallable);
void remove_pre_hook(PyObject* pCallable);
void remove_post_hook(PyObject* pCallable);
private:
char* m_szParams;
Convention_t m_eConv;
};
int get_error();
CPointer* alloc(int iSize);
#endif // _MEMORY_TOOLS_H