Skip to content

Commit a0ff438

Browse files
ExtcanaRyExtcanaRy
authored andcommitted
Simplified macro & enhanced std::string operations
Reduce the use of global variables in THOOK macro Support for construct std::string in allocated memory Support use destructor/delete to free std::string object
1 parent 7aa8a91 commit a0ff438

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

include/modloader/cutils.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,24 @@
4343

4444
#define THOOK(name, ret_type, sym, ...) \
4545
typedef ret_type (*_##name##_t)(__VA_ARGS__); \
46-
_##name##_t _original_##name = NULL; \
4746
ret_type _detour_##name(__VA_ARGS__); \
4847
void _install_##name(void) __attribute__((constructor));\
4948
void _destroy_##name(void); \
5049
\
5150
struct _##name { \
52-
_##name##_t hook; \
51+
void *hook; \
5352
_##name##_t detour; \
5453
_##name##_t original; \
5554
void (*install)(void); \
5655
void (*destroy)(void); \
57-
} name = {NULL, NULL, NULL, \
56+
} name = {NULL, _detour_##name, NULL, \
5857
_install_##name, _destroy_##name}; \
5958
\
6059
void _install_##name(void) \
6160
{ \
62-
modloader_hook_t *_hook_##name = \
63-
modloader_hook(SYM(sym), \
64-
_detour_##name, \
65-
(void**)&_original_##name); \
66-
name.hook = _hook_##name; \
67-
name.detour = _detour_##name; \
68-
name.original = _original_##name; \
61+
name.hook = modloader_hook(SYM(sym), \
62+
name.detour, \
63+
(void**)&name.original); \
6964
} \
7065
\
7166
void _destroy_##name(void) \
@@ -80,14 +75,14 @@ extern "C" {
8075
#endif
8176

8277

83-
// std::string::basic_string(const char *c_str)
84-
void *std_string_string(const char *c_str);
78+
// void *sstr = std::string::basic_string(const char *c_str)
79+
void std_string_string(void **sstr, const char *c_str);
8580

8681
// std::string::c_str()
87-
const char *std_string_c_str(void *str);
82+
const char *std_string_c_str(void *sstr);
8883

8984
// std::string::~basic_string()
90-
void std_string_destroy(void *str, bool should_free);
85+
void std_string_destroy(void *sstr, bool should_free);
9186

9287
#ifdef __cplusplus
9388
}

src/cutils.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#include <modloader/cutils.h>
22
#include <string>
33

4-
void *std_string_string(const char *c_str) {
5-
auto str = new std::string;
6-
str->assign(c_str);
7-
return (void*)str;
4+
void std_string_string(void **sstr, const char *c_str) {
5+
if (*sstr != nullptr)
6+
new (*sstr) std::string(c_str);
7+
else
8+
*sstr = new std::string(c_str);
89
}
910

10-
const char *std_string_c_str(void *str) {
11-
return ((std::string*)str)->c_str();
11+
const char *std_string_c_str(void *sstr) {
12+
return ((std::string*)sstr)->c_str();
1213
}
1314

14-
void std_string_destroy(void *str, bool should_free) {
15-
((std::string*)str)->~basic_string();
15+
void std_string_destroy(void *sstr, bool should_free) {
1616
if (should_free)
17-
delete (std::string*)str;
17+
delete (std::string*)sstr;
18+
else
19+
((std::string*)sstr)->~basic_string();
1820
}

0 commit comments

Comments
 (0)