-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.h
131 lines (99 loc) · 4.04 KB
/
Utilities.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
#pragma once
void DumpClass(void * theClassPtr, UInt32 nIntsToDump = 512);
const char * GetObjectClassName(void * obj);
const std::string & GetFalloutDirectory(void);
std::string GetFOSEConfigOption(const char * section, const char * key);
bool GetFOSEConfigOption_UInt32(const char * section, const char * key, UInt32 * dataOut);
// this has been tested to work for non-varargs functions
// varargs functions end up with 'this' passed as the last parameter (ie. probably broken)
// do NOT use with classes that have multiple inheritance
// if many member functions are to be declared, use MEMBER_FN_PREFIX to create a type with a known name
// so it doesn't need to be restated throughout the member list
// all of the weirdness with the _GetType function is because you can't declare a static const pointer
// inside the class definition. inlining automatically makes the function call go away since it's a const
#define MEMBER_FN_PREFIX(className) \
typedef className _MEMBER_FN_BASE_TYPE
#define DEFINE_MEMBER_FN_LONG(className, functionName, retnType, address, ...) \
typedef retnType (className::* _##functionName##_type)(__VA_ARGS__); \
\
inline _##functionName##_type * _##functionName##_GetPtr(void) \
{ \
static const UInt32 _address = address; \
return (_##functionName##_type *)&_address; \
}
#define DEFINE_MEMBER_FN(functionName, retnType, address, ...) \
DEFINE_MEMBER_FN_LONG(_MEMBER_FN_BASE_TYPE, functionName, retnType, address, __VA_ARGS__)
#define CALL_MEMBER_FN(obj, fn) \
((*(obj)).*(*((obj)->_##fn##_GetPtr())))
// Macro for debug output to console at runtime
#if RUNTIME
#ifdef _DEBUG
#define DEBUG_PRINT(x, ...) { Console_Print((x), __VA_ARGS__); }
#define DEBUG_MESSAGE(x, ...) { _MESSAGE((x), __VA_ARGS__); }
#else
#define DEBUG_PRINT(x, ...) { }
#define DEBUG_MESSAGE(x, ...) { }
#endif //_DEBUG
#else
#define DEBUG_PRINT(x, ...) { }
#define DEBUG_MESSAGE(x, ...) { }
#endif // RUNTIME
class TESForm;
class FormMatcher
{
public:
virtual bool Matches(TESForm* pForm) const = 0;
};
// alternative to strtok; doesn't modify src string, supports forward/backward iteration
class Tokenizer
{
public:
Tokenizer(const char* src, const char* delims);
~Tokenizer();
// these return the offset of token in src, or -1 if no token
UInt32 NextToken(std::string& outStr);
UInt32 PrevToken(std::string& outStr);
private:
std::string m_delims;
size_t m_offset;
std::string m_data;
};
void CreateTempHook(UInt32 hookAddr, UInt32 jmpAddr);
bool DeleteTempHook(UInt32 hookAddr);
namespace MersenneTwister
{
/* initializes mt[N] with a seed */
void init_genrand(unsigned long s);
/* initialize by an array with array-length */
void init_by_array(unsigned long init_key[], int key_length);
/* generates a random number on [0,0xffffffff]-interval */
unsigned long genrand_int32(void);
/* generates a random number on [0,0x7fffffff]-interval */
long genrand_int31(void);
/* generates a random number on [0,1]-real-interval */
double genrand_real1(void);
/* generates a random number on [0,1)-real-interval */
double genrand_real2(void);
/* generates a random number on (0,1)-real-interval */
double genrand_real3(void);
/* generates a random number on [0,1) with 53-bit resolution*/
double genrand_res53(void);
};
// thread-safe template versions of ThisStdCall()
template <typename T_Ret = UInt32, typename ...Args>
__forceinline T_Ret ThisCall(UInt32 _addr, const void* _this, Args ...args)
{
return ((T_Ret(__thiscall*)(const void*, Args...))_addr)(_this, std::forward<Args>(args)...);
}
template <typename T_Ret = void, typename ...Args>
__forceinline T_Ret StdCall(UInt32 _addr, Args ...args)
{
return ((T_Ret(__stdcall*)(Args...))_addr)(std::forward<Args>(args)...);
}
template <typename T_Ret = void, typename ...Args>
__forceinline T_Ret CdeclCall(UInt32 _addr, Args ...args)
{
return ((T_Ret(__cdecl*)(Args...))_addr)(std::forward<Args>(args)...);
}
#define GameHeapAlloc(size) ThisCall<void*>(0x86B930, (void*)0x1090A78, size)
#define GameHeapFree(ptr) ThisCall<void*>(0x86BA60, (void*)0x1090A78, ptr)