Repository navigation
Expand file tree
/
Copy pathNativeInvoker.h
More file actions
133 lines (93 loc) · 2.4 KB
/
Copy pathNativeInvoker.h
File metadata and controls
133 lines (93 loc) · 2.4 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
#ifndef __NATIVE_INVOKER_H__
#define __NATIVE_INVOKER_H__
#pragma once
class scrNativeCallContext {
protected:
void * m_pReturn;
uint32_t m_nArgCount;
void * m_pArgs;
uint32_t m_nDataCount;
alignas(uintptr_t)uint8_t m_vectorSpace[192];
public:
template<typename T>
inline T GetArgument( int idx ) {
intptr_t * arguments = (intptr_t*)m_pArgs;
return *(T*)&arguments[idx];
}
template<typename T>
inline void SetResult( int idx, T value ) {
intptr_t * returnValues = (intptr_t*)m_pReturn;
*(T*)&returnValues[idx] = value;
}
inline int GetArgumentCount() {
return m_nArgCount;
}
template<typename T>
inline T GetResult( int idx ) {
intptr_t * returnValues = (intptr_t*)m_pReturn;
return *(T*)&returnValues[idx];
}
static void(*SetVectorResults)(scrNativeCallContext*);
};
class NativeContext : public scrNativeCallContext {
private:
// Configuration
enum {
MaxNativeParams = 16,
ArgSize = 8,
};
// Anything temporary that we need
uint8_t m_TempStack[MaxNativeParams * ArgSize];
public:
inline NativeContext() {
m_pArgs = &m_TempStack;
m_pReturn = &m_TempStack; // It's okay to point both args and return at
// the same pointer. The game should handle this.
m_nArgCount = 0;
m_nDataCount = 0;
}
template <typename T>
inline void Push( T value ) {
if ( sizeof( T ) > ArgSize ) {
throw "Argument has an invalid size";
} else if ( sizeof( T ) < ArgSize ) {
// Ensure we don't have any stray data
*reinterpret_cast<uintptr_t*>( m_TempStack + ArgSize * m_nArgCount ) = 0;
}
*reinterpret_cast<T*>( m_TempStack + ArgSize * m_nArgCount ) = value;
m_nArgCount++;
}
inline void Reverse() {
uintptr_t tempValues[MaxNativeParams];
uintptr_t * args = (uintptr_t*)m_pArgs;
for ( uint32_t i = 0; i < m_nArgCount; i++ ) {
int target = m_nArgCount - i - 1;
tempValues[target] = args[i];
}
memcpy( m_TempStack, tempValues, sizeof( m_TempStack ) );
}
template <typename T>
inline T GetResult() {
return *reinterpret_cast<T*>( m_TempStack );
}
};
struct pass {
template<typename ...T> pass( T... ) {}
};
class NativeManagerContext : public NativeContext {
public:
NativeManagerContext()
: NativeContext() {
}
void Reset() {
m_nArgCount = 0;
m_nDataCount = 0;
}
inline void* GetResultPointer() {
return m_pReturn;
}
};
void nativeInit(UINT64 hash);
void nativePush64(UINT64 val);
PUINT64 nativeCall();
#endif // __NATIVE_INVOKER_H__