Last active
December 7, 2024 06:10
-
-
Save Absolucy/c201db2dcf56da0eef023b2374175c3e to your computer and use it in GitHub Desktop.
doohickey to output memory stats at runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************************/ | |
/* DON'T USE THIS, I REWROTE IT WITH BETTER CODE AND LINUX SUPPORT: */ | |
/* https://github.com/Absolucy/byond-memorystats */ | |
/********************************************************************************/ | |
#ifndef __clang__ | |
#error use clang to compile this graaaah | |
#endif | |
#include <stdio.h> | |
#include <string.h> | |
#include <windows.h> | |
typedef struct | |
{ | |
unsigned int _idc1; | |
unsigned short _idc2; | |
unsigned char _idc3; | |
char str[]; | |
} DMStringData; | |
typedef struct | |
{ | |
DMStringData *data; | |
} DMString; | |
typedef DMString* (__stdcall *GetServerMemUsage)(DMString* in); | |
typedef DMString* (__thiscall *DMStringNew)(DMString* this); | |
typedef void (__thiscall *DMStringFree)(DMString* this); | |
static GetServerMemUsage get_server_mem_usage = NULL; | |
static DMStringNew dm_string_new = NULL; | |
static DMStringFree dm_string_free = NULL; | |
// ??0DMString@@QAE@XZ | |
// ??1DMString@@QAE@XZ | |
DMString create_dmstring() { | |
DMString ret = {0}; | |
dm_string_new(&ret); | |
return ret; | |
} | |
static void free_dmstring(DMString* str) { | |
dm_string_free(str); | |
} | |
static void cleanup_lib(HMODULE *byondcore) | |
{ | |
if (*byondcore != NULL) | |
FreeLibrary(*byondcore); | |
} | |
__declspec(dllexport) __cdecl char *get_memory_stats(int argc, const char **argv) | |
{ | |
HMODULE byondcore __attribute__((cleanup(cleanup_lib))) = LoadLibrary("byondcore.dll"); | |
if(byondcore == NULL) | |
return "error: failed to load byondcore"; | |
get_server_mem_usage = (GetServerMemUsage)GetProcAddress(byondcore, "?GetServerMemUsage@DungServer@@QAE?AUDMString@@XZ"); | |
if(get_server_mem_usage == NULL) | |
return "error: failed to get address of DungServer::GetServerMemUsage"; | |
dm_string_new = (DMStringNew)GetProcAddress(byondcore, "??0DMString@@QAE@XZ"); | |
if(dm_string_new == NULL) | |
return "error: failed to get address of DMString::DMString(void)"; | |
dm_string_free = (DMStringFree)GetProcAddress(byondcore, "??1DMString@@QAE@XZ"); | |
if(dm_string_free == NULL) | |
return "error: failed to get address of DMString::~DMString()"; | |
DMString string __attribute__((cleanup(free_dmstring))) = create_dmstring(); | |
get_server_mem_usage(&string); | |
return string.data->str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment