Skip to content

Commit 0d98385

Browse files
committed
[FUNCTION_RESOLVER] add minimum support for variadic functions
1 parent d9c091b commit 0d98385

1 file changed

Lines changed: 26 additions & 47 deletions

File tree

src/precomp/FunctionResolver.h

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef FUNCTION_RESOLVER
22
#define FUNCTION_RESOLVER
33

4+
// NOTE: No support for hierarchical structures
5+
// NOTE: Only limited support for variadic functions (...), only resolver, no wrapper or function traits
6+
47
// NOTE: Changed CDECL tp CCALL to avoid window macro issue
58
// TODO: Maybe change naming convention to not run into these issues
69

@@ -90,6 +93,16 @@
9093
#define MACRO_SUPPORTED_PARAMETER_NUMBER 15
9194
#define MACRO_NUMBER_OF_FUNCTIONS_TO_GENERATE 16
9295

96+
template <int gameAddress> __declspec(naked) void trampoline()
97+
{
98+
enum { addr = gameAddress };
99+
__asm
100+
{
101+
mov eax, addr
102+
jmp eax
103+
}
104+
}
105+
93106
// no support for virtual function ptrs
94107
struct FunctionResolver {
95108
private:
@@ -205,10 +218,7 @@ struct FunctionResolver {
205218
#ifdef OPEN_SHC_DLL
206219

207220
template <typename _> struct FunctionPtrUnifier<false, false, _> {
208-
inline static const FuncPtrType get()
209-
{
210-
return reinterpret_cast<FuncPtrType>(&GameCaller<FuncPtrType>::Function::call);
211-
}
221+
inline static const FuncPtrType get() { return GameFunction<FuncPtrType>::get(); }
212222
};
213223
template <bool implemented, typename _> struct FunctionPtrUnifier<implemented, true, _> {
214224
inline static const FuncPtrType get()
@@ -219,7 +229,9 @@ struct FunctionResolver {
219229

220230
typedef FunctionPtrUnifier<isImplemented, false, void> UnifiedFunctionPtrForWrapper;
221231

222-
template <typename FuncPtrType> struct Wrapper;
232+
template <typename FuncPtrType> struct Wrapper {
233+
typedef typename FuncPtrType::NoWrapperSupportForThisFuncPtrType Function;
234+
};
223235

224236
#define MACRO_STREAM_PRINT_PARAMETER(N) << MACRO_PARAMETER(N)
225237
#define MACRO_STREAM_PRINT_PARAMETER_LIST(N) MACRO_INDEX_ITERATE_DEPTH_0(N, MACRO_STREAM_PRINT_PARAMETER, << ", ")
@@ -361,46 +373,13 @@ struct FunctionResolver {
361373
#undef MACRO_MACRO_STREAM_PRINT_PARAMETER_LIST
362374
#undef MACRO_STREAM_PRINT_PARAMETER
363375

364-
template <typename FuncPtrType> struct GameCaller;
376+
template <typename FuncPtrType> struct GameFunction {
377+
inline static FuncPtrType get() { return reinterpret_cast<FuncPtrType>(&trampoline<gameAddress>); }
378+
};
365379

366-
#define MACRO_GAME_CALLER(N) \
367-
MACRO_FUNC_TEMPLATE_HEADER(, Ret, N, ) struct GameCaller<MACRO_FUNC_PTR_TYPE_CCALL(Ret, N)> { \
368-
private: \
369-
template <typename R, typename = void> struct CallHelper { \
370-
__declspec(noinline) static R __cdecl call(MACRO_PARAMETER_TYPE_PARAMETER_LIST(N)) \
371-
{ \
372-
return ((MACRO_FUNC_PTR_TYPE_CCALL(Ret, N))gameAddress)(MACRO_PARAMETER_LIST(N)); \
373-
} \
374-
}; \
375-
template <typename _> struct CallHelper<void, _> { \
376-
__declspec(noinline) static void __cdecl call(MACRO_PARAMETER_TYPE_PARAMETER_LIST(N)) \
377-
{ \
378-
((MACRO_FUNC_PTR_TYPE_CCALL(Ret, N))gameAddress)(MACRO_PARAMETER_LIST(N)); \
379-
} \
380-
}; \
381-
\
382-
public: \
383-
typedef CallHelper<Ret> Function; \
384-
}; \
385-
MACRO_FUNC_TEMPLATE_HEADER(, Ret, N, ) struct GameCaller<MACRO_FUNC_PTR_TYPE_STDCALL(Ret, N)> { \
386-
private: \
387-
template <typename R, typename = void> struct CallHelper { \
388-
__declspec(noinline) static R __stdcall call(MACRO_PARAMETER_TYPE_PARAMETER_LIST(N)) \
389-
{ \
390-
return ((MACRO_FUNC_PTR_TYPE_STDCALL(Ret, N))gameAddress)(MACRO_PARAMETER_LIST(N)); \
391-
} \
392-
}; \
393-
template <typename _> struct CallHelper<void, _> { \
394-
__declspec(noinline) static void __stdcall call(MACRO_PARAMETER_TYPE_PARAMETER_LIST(N)) \
395-
{ \
396-
((MACRO_FUNC_PTR_TYPE_STDCALL(Ret, N))gameAddress)(MACRO_PARAMETER_LIST(N)); \
397-
} \
398-
}; \
399-
\
400-
public: \
401-
typedef CallHelper<Ret> Function; \
402-
}; \
403-
MACRO_CLASS_FUNC_TEMPLATE_HEADER(, Ret, Class, N, ) struct GameCaller<MACRO_FUNC_PTR_TYPE_MEMBER(Ret, Class, N)> { \
380+
#define MACRO_GAME_THIS_FUNCTION(N) \
381+
MACRO_CLASS_FUNC_TEMPLATE_HEADER(, Ret, Class, N, ) \
382+
struct GameFunction<MACRO_FUNC_PTR_TYPE_MEMBER(Ret, Class, N)> { \
404383
private: \
405384
template <typename R, typename = void> struct CallHelper { \
406385
__declspec(noinline) R call(MACRO_PARAMETER_TYPE_PARAMETER_LIST(N)) \
@@ -418,10 +397,10 @@ struct FunctionResolver {
418397
}; \
419398
\
420399
public: \
421-
typedef CallHelper<Ret> Function; \
400+
inline static FuncPtrType get() { return reinterpret_cast<FuncPtrType>(&CallHelper<Ret>::call); } \
422401
};
423-
MACRO_INDEX_ITERATE_DEPTH_1(MACRO_NUMBER_OF_FUNCTIONS_TO_GENERATE, MACRO_GAME_CALLER, M_SPACE)
424-
#undef MACRO_GAME_CALLER
402+
MACRO_INDEX_ITERATE_DEPTH_1(MACRO_NUMBER_OF_FUNCTIONS_TO_GENERATE, MACRO_GAME_THIS_FUNCTION, M_SPACE)
403+
#undef MACRO_GAME_THIS_FUNCTION
425404

426405
typedef FunctionPtrUnifier<isImplemented, Flags::USE_WRAPPER, void> UnifiedFunctionPtr;
427406

0 commit comments

Comments
 (0)