Skip to content

Commit

Permalink
refactor: rename std::string functions
Browse files Browse the repository at this point in the history
  • Loading branch information
WillowSauceR authored and WillowSauceR committed Apr 19, 2023
1 parent dc1462a commit 7d89319
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 32 deletions.
12 changes: 7 additions & 5 deletions include/mediaplayer/cpp_string.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once
#include <littlehooker/littlehooker.h>

struct string {
struct string *(*string)(const char *c_str);
const char *(*c_str)(struct string *cpp_str);
};
struct string;


// std::string::string(const char *c_str)
struct string *std_string_string(const char *c_str);
// std::string::c_str()
const char *std_string_c_str(struct string *std_str);

extern struct string string;
1 change: 1 addition & 0 deletions include/mediaplayer/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ bool check_ll_preloader(void);
bool init_hooks(void);
void create_plugin_dir(void);
bool load_plugin(void);
bool unload_plugin(void);
19 changes: 4 additions & 15 deletions src/cpp_string.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
#include <mediaplayer/cpp_string.h>

// std::string::string(const char *c_str)
struct string *cpp_string__string(const char *c_str);
// std::string::c_str()
const char *cpp_string__c_str(struct string *cpp_str);

struct string string = {
.string = cpp_string__string,
.c_str = cpp_string__c_str,
};


struct string *cpp_string__string(const char *c_str)
struct string *std_string_string(const char *c_str)
{
size_t extra_size = 32;
struct string *cpp_str = calloc(strnlen(c_str, 9223372036854775807ULL) + extra_size, 1);
Expand All @@ -21,11 +10,11 @@ struct string *cpp_string__string(const char *c_str)
return cpp_str;
}

const char *cpp_string__c_str(struct string *cpp_str)
const char *std_string_c_str(struct string *std_str)
{
const char *c_str =
TLCALL("?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEBAPEBDXZ",
const char *(*)(struct string *cpp_str),
cpp_str);
const char *(*)(struct string *),
std_str);
return c_str;
}
2 changes: 1 addition & 1 deletion src/dllmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserv
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
lh_uninit();
unload_plugin();
break;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mc/actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const char *get_name_tag(struct actor *actor)
TLCALL("?getNameTag@Actor@@UEBAAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
struct string *(*)(struct actor *actor),
actor);
return string.c_str(cpp_str);
return std_string_c_str(cpp_str);
}

unsigned get_entity_type_id(struct actor *actor)
Expand Down
2 changes: 1 addition & 1 deletion src/mc/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct level *g_level = NULL;

struct player *get_player_by_xuid(struct level *level, const char *xuid)
{
struct string *xuid_cpp_str = string.string(xuid);
struct string *xuid_cpp_str = std_string_string(xuid);
struct player *player =
TLCALL("?getPlayerByXuid@Level@@UEBAPEAVPlayer@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z",
struct player *(*)(struct level *level, struct string *xuid),
Expand Down
8 changes: 4 additions & 4 deletions src/mc/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void send_play_sound_packet(struct player *player, const char *sound_name,

// Never dereference struct string.
// Please use functions like memcpy to manipulate data
struct string *sound_name_cpp_str = string.string(sound_name);
struct string *sound_name_cpp_str = std_string_string(sound_name);
// TLCALL("??0PlaySoundPacket@@QEAA@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEBVVec3@@MM@Z",
// uintptr_t (*)(uintptr_t pkt, struct string *sound_name,
// struct vec3 *pos, float volume, float pitch),
Expand All @@ -46,8 +46,8 @@ void send_text_packet(struct player *player, int mode, const char *msg)
{
uintptr_t pkt = create_packet(9);
const char *player_name = get_name_tag((struct actor *)player);
struct string *player_name_cpp_str = string.string(player_name);
struct string *msg_cpp_str = string.string(msg);
struct string *player_name_cpp_str = std_string_string(player_name);
struct string *msg_cpp_str = std_string_string(msg);
DEREFERENCE(int, pkt, 48) = mode;
memcpy((void *)(pkt + 56), player_name_cpp_str, 32);
memcpy((void *)(pkt + 88), msg_cpp_str, 32);
Expand All @@ -61,7 +61,7 @@ void send_boss_event_packet(struct player *player, const char *name,
{
uintptr_t pkt = create_packet(74);
uintptr_t unique_id = DEREFERENCE(uintptr_t, get_unique_id((struct actor *)player), 0);
struct string *name_cpp_str = string.string(name);
struct string *name_cpp_str = std_string_string(name);
DEREFERENCE(uintptr_t, pkt, 56) = unique_id;
DEREFERENCE(int, pkt, 72) = type;
memcpy((void *)(pkt + 80), name_cpp_str, 32);
Expand Down
4 changes: 2 additions & 2 deletions src/mc/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ struct player *get_server_player(struct server_network_handler *handler, uintptr

const char *get_player_xuid(struct player *player)
{
struct string *xuid_cpp_str = string.string("0000000000000000");
struct string *xuid_cpp_str = std_string_string("0000000000000000");
TLCALL("?getXuid@Player@@UEBA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ",
struct string *(*)(struct player *, struct string *),
player, xuid_cpp_str);
const char *xuid = string.c_str(xuid_cpp_str);
const char *xuid = std_string_c_str(xuid_cpp_str);
free(xuid_cpp_str);
return xuid;
}
Expand Down
13 changes: 10 additions & 3 deletions src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ TLHOOK(change_setting_command_setup, void,
"?setup@ChangeSettingCommand@@SAXAEAVCommandRegistry@@@Z",
uintptr_t _this)
{
struct string *cmd_music = string.string("mpm");
struct string *cmd_video = string.string("mpv");
struct string *cmd_music = std_string_string("mpm");
struct string *cmd_video = std_string_string("mpv");
TLCALL("?registerCommand@CommandRegistry@@QEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEBDW4CommandPermissionLevel@@UCommandFlag@@3@Z",
void (*)(uintptr_t, struct string *, const char *, char, short, short),
_this, cmd_music, "mediaplayer music", 0, 0, 0x80);
Expand All @@ -39,7 +39,7 @@ TLHOOK(on_player_cmd, void,
struct server_network_handler* _this, uintptr_t id, uintptr_t pkt)
{
struct player* player = get_server_player(_this, id, pkt);
const char *cmd = string.c_str(REFERENCE(struct string, pkt, 48));
const char *cmd = std_string_c_str(REFERENCE(struct string, pkt, 48));
if (player && !process_cmd(player, cmd))
return;

Expand Down Expand Up @@ -123,3 +123,10 @@ bool load_plugin(void)

return true;
}

bool unload_plugin(void)
{
if (!using_ll_preloader_api)
lh_uninit();
return true;
}

0 comments on commit 7d89319

Please sign in to comment.