Skip to content

Add hook macro & std::string api for C #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ set(MODLOADER_VERSION "Preview 3")
add_subdirectory(dep/funchook)

add_library(server_modloader SHARED include/modloader/hook.h include/modloader/statichook.h include/modloader/log.h
include/modloader/loader.h
src/hook.cpp src/log.cpp src/loader.h src/loader.cpp src/elf_helper.cpp src/elf_helper.h src/main.cpp)
include/modloader/loader.h include/modloader/cutils.h
src/hook.cpp src/log.cpp src/loader.h src/loader.cpp src/elf_helper.cpp src/elf_helper.h src/main.cpp
src/cutils.cpp)
target_link_libraries(server_modloader funchook)
target_compile_definitions(server_modloader PRIVATE MODLOADER_VERSION="${MODLOADER_VERSION}")
target_include_directories(server_modloader PUBLIC include/)
Expand Down
91 changes: 91 additions & 0 deletions include/modloader/cutils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#ifndef MODLOADER_CUTILS_H
#define MODLOADER_CUTILS_H

#include <modloader/hook.h>

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#ifndef RTLD_DEFAULT
#define RTLD_DEFAULT NULL
#endif

#define SYM(sym) \
dlsym(RTLD_DEFAULT, sym)


#define SYMCALL(sym, func_proto, ...) \
((func_proto) \
(SYM(sym))) \
(__VA_ARGS__)


#define VIRTUAL_CALL(ptr, func_proto, ...) \
((func_proto) \
((void *)ptr)) \
(__VA_ARGS__)


#define DEREFERENCE(type, ptr, offset) \
(*(type*)((uintptr_t)ptr + offset))


#define REFERENCE(type, ptr, offset) \
(type*)((uintptr_t)ptr + offset)


// for uintptr_t
#define PTR_OFFSET(ptr, offset) \
((uintptr_t)ptr + offset)


#define THOOK(name, ret_type, sym, ...) \
typedef ret_type (*_##name##_t)(__VA_ARGS__); \
ret_type _detour_##name(__VA_ARGS__); \
void _install_##name(void) __attribute__((constructor));\
void _destroy_##name(void); \
\
struct _##name { \
modloader_hook_t *hook; \
_##name##_t detour; \
_##name##_t original; \
void (*install)(void); \
void (*destroy)(void); \
} name = {NULL, _detour_##name, NULL, \
_install_##name, _destroy_##name}; \
\
void _install_##name(void) \
{ \
name.hook = modloader_hook(SYM(sym), \
name.detour, \
(void**)&name.original); \
} \
\
void _destroy_##name(void) \
{ \
modloader_destroy_hook(name.hook); \
} \
\
ret_type _detour_##name(__VA_ARGS__)

#ifdef __cplusplus
extern "C" {
#endif


// void *sstr = std::string::basic_string(const char *c_str)
void std_string_string(void **sstr, const char *c_str);

// std::string::c_str()
const char *std_string_c_str(void *sstr);

// std::string::~basic_string()
void std_string_destroy(void *sstr, bool should_free);

#ifdef __cplusplus
}
#endif

#endif
20 changes: 20 additions & 0 deletions src/cutils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <modloader/cutils.h>
#include <string>

void std_string_string(void **sstr, const char *c_str) {
if (*sstr != nullptr)
new (*sstr) std::string(c_str);
else
*sstr = new std::string(c_str);
}

const char *std_string_c_str(void *sstr) {
return ((std::string*)sstr)->c_str();
}

void std_string_destroy(void *sstr, bool should_free) {
if (should_free)
delete (std::string*)sstr;
else
((std::string*)sstr)->~basic_string();
}