From d2ee389192aa425ef9c81b2c3367ffb0de6976d0 Mon Sep 17 00:00:00 2001 From: p0358 Date: Thu, 30 Dec 2021 02:58:19 +0100 Subject: Refactor and fix of various issues, add run_northstar.txt support --- NorthstarDedicatedTest/memalloc.cpp | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'NorthstarDedicatedTest/memalloc.cpp') diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp index 113f56b9..cff0ecac 100644 --- a/NorthstarDedicatedTest/memalloc.cpp +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -17,24 +17,48 @@ const int STATIC_ALLOC_SIZE = 100000; // alot more than we need, could reduce to size_t g_iStaticAllocated = 0; char pStaticAllocBuf[STATIC_ALLOC_SIZE]; -void* operator new(size_t n) +// TODO: rename to malloc and free after removing statically compiled .libs + +void* malloc_(size_t n) { // allocate into static buffer if g_pMemAllocSingleton isn't initialised if (g_pMemAllocSingleton) + { + //printf("Northstar malloc (g_pMemAllocSingleton): %llu\n", n); return g_pMemAllocSingleton->m_vtable->Alloc(g_pMemAllocSingleton, n); + } else { + if (g_iStaticAllocated + n > STATIC_ALLOC_SIZE) + { + throw "Ran out of prealloc space"; // we could log, but spdlog probably does use allocations as well... + } + //printf("Northstar malloc (prealloc): %llu\n", n); void* ret = pStaticAllocBuf + g_iStaticAllocated; g_iStaticAllocated += n; return ret; - } + } } -void operator delete(void* p) +void free_(void* p) { // if it was allocated into the static buffer, just do nothing, safest way to deal with it if (p >= pStaticAllocBuf && p <= pStaticAllocBuf + STATIC_ALLOC_SIZE) + { + //printf("Northstar free (prealloc): %p\n", p); return; + } + //printf("Northstar free (g_pMemAllocSingleton): %p\n", p); g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p); +} + +void* operator new(size_t n) +{ + return malloc_(n); +} + +void operator delete(void* p) +{ + free_(p); } \ No newline at end of file -- cgit v1.2.3 From 2404f063433064e90059e6b3153f663e10d1f884 Mon Sep 17 00:00:00 2001 From: p0358 Date: Thu, 30 Dec 2021 04:47:16 +0100 Subject: add realloc too --- LauncherInjector/memalloc.cpp | 25 +++++++++++++++++++++++++ LauncherInjector/memalloc.h | 15 ++++++++++++--- NorthstarDedicatedTest/gameutils.h | 17 +++++++++++++---- NorthstarDedicatedTest/memalloc.cpp | 24 ++++++++++++++++++++++++ loader_launcher_proxy/Memory.cpp | 24 ++++++++++++++++++++++++ loader_launcher_proxy/Memory.h | 15 ++++++++++++--- 6 files changed, 110 insertions(+), 10 deletions(-) (limited to 'NorthstarDedicatedTest/memalloc.cpp') diff --git a/LauncherInjector/memalloc.cpp b/LauncherInjector/memalloc.cpp index 1d0f13e6..936523d7 100644 --- a/LauncherInjector/memalloc.cpp +++ b/LauncherInjector/memalloc.cpp @@ -17,6 +17,7 @@ void LoadTier0Handle() const int STATIC_ALLOC_SIZE = 16384; size_t g_iStaticAllocated = 0; +void* g_pLastAllocated = nullptr; char pStaticAllocBuf[STATIC_ALLOC_SIZE]; // they should never be used here, except in LibraryLoadError // haha not true @@ -29,6 +30,7 @@ void* malloc(size_t n) { void* ret = pStaticAllocBuf + g_iStaticAllocated; g_iStaticAllocated += n; + g_pLastAllocated = ret; return ret; } else @@ -53,6 +55,29 @@ void free(void* p) (*g_ppMemAllocSingleton)->m_vtable->Free(*g_ppMemAllocSingleton, p); } +void* realloc(void* old_ptr, size_t size) { + // it was allocated into the static buffer + if (old_ptr >= pStaticAllocBuf && old_ptr <= pStaticAllocBuf + STATIC_ALLOC_SIZE) + { + if (g_pLastAllocated == old_ptr) + { + // nothing was allocated after this + size_t old_size = g_iStaticAllocated - ((size_t)g_pLastAllocated - (size_t)pStaticAllocBuf); + size_t diff = size - old_size; + if (diff > 0) + g_iStaticAllocated += diff; + return old_ptr; + } + else + { + return malloc(size); + } + } + + if (g_ppMemAllocSingleton && *g_ppMemAllocSingleton) + return (*g_ppMemAllocSingleton)->m_vtable->Realloc(*g_ppMemAllocSingleton, old_ptr, size); +} + void* operator new(size_t n) { return malloc(n); diff --git a/LauncherInjector/memalloc.h b/LauncherInjector/memalloc.h index 928e403c..c983966c 100644 --- a/LauncherInjector/memalloc.h +++ b/LauncherInjector/memalloc.h @@ -5,10 +5,19 @@ class IMemAlloc public: struct VTable { - void* unknown[1]; + void* unknown[1]; // alloc debug void* (*Alloc) (IMemAlloc* memAlloc, size_t nSize); - void* unknown2[3]; - void(*Free) (IMemAlloc* memAlloc, void* pMem); + void* unknown2[1]; // realloc debug + void* (*Realloc)(IMemAlloc* memAlloc, void* pMem, size_t nSize); + void* unknown3[1]; // free #1 + void (*Free) (IMemAlloc* memAlloc, void* pMem); + void* unknown4[2]; // nullsubs, maybe CrtSetDbgFlag + size_t(*GetSize) (IMemAlloc* memAlloc, void* pMem); + void* unknown5[9]; // they all do literally nothing + void (*DumpStats) (IMemAlloc* memAlloc); + void (*DumpStatsFileBase) (IMemAlloc* memAlloc, const char* pchFileBase); + void* unknown6[4]; + int (*heapchk) (IMemAlloc* memAlloc); }; VTable* m_vtable; diff --git a/NorthstarDedicatedTest/gameutils.h b/NorthstarDedicatedTest/gameutils.h index 8def57eb..43f387d1 100644 --- a/NorthstarDedicatedTest/gameutils.h +++ b/NorthstarDedicatedTest/gameutils.h @@ -7,10 +7,19 @@ class IMemAlloc public: struct VTable { - void* unknown[1]; - void* (*Alloc)(IMemAlloc* memAlloc, size_t nSize); - void* unknown2[3]; - void (*Free)(IMemAlloc* memAlloc, void* pMem); + void* unknown[1]; // alloc debug + void* (*Alloc) (IMemAlloc* memAlloc, size_t nSize); + void* unknown2[1]; // realloc debug + void* (*Realloc)(IMemAlloc* memAlloc, void* pMem, size_t nSize); + void* unknown3[1]; // free #1 + void (*Free) (IMemAlloc* memAlloc, void* pMem); + void* unknown4[2]; // nullsubs, maybe CrtSetDbgFlag + size_t(*GetSize) (IMemAlloc* memAlloc, void* pMem); + void* unknown5[9]; // they all do literally nothing + void (*DumpStats) (IMemAlloc* memAlloc); + void (*DumpStatsFileBase) (IMemAlloc* memAlloc, const char* pchFileBase); + void* unknown6[4]; + int (*heapchk) (IMemAlloc* memAlloc); }; VTable* m_vtable; diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp index cff0ecac..d301f1fa 100644 --- a/NorthstarDedicatedTest/memalloc.cpp +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -15,6 +15,7 @@ const int STATIC_ALLOC_SIZE = 100000; // alot more than we need, could reduce to 50k or even 25k later potentially size_t g_iStaticAllocated = 0; +void* g_pLastAllocated = nullptr; char pStaticAllocBuf[STATIC_ALLOC_SIZE]; // TODO: rename to malloc and free after removing statically compiled .libs @@ -53,6 +54,29 @@ void free_(void* p) g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p); } +void* realloc_(void* old_ptr, size_t size) { + // it was allocated into the static buffer + if (old_ptr >= pStaticAllocBuf && old_ptr <= pStaticAllocBuf + STATIC_ALLOC_SIZE) + { + if (g_pLastAllocated == old_ptr) + { + // nothing was allocated after this + size_t old_size = g_iStaticAllocated - ((size_t)g_pLastAllocated - (size_t)pStaticAllocBuf); + size_t diff = size - old_size; + if (diff > 0) + g_iStaticAllocated += diff; + return old_ptr; + } + else + { + return malloc_(size); + } + } + + if (g_pMemAllocSingleton) + return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, old_ptr, size); +} + void* operator new(size_t n) { return malloc_(n); diff --git a/loader_launcher_proxy/Memory.cpp b/loader_launcher_proxy/Memory.cpp index 6c69d80f..bd19502a 100644 --- a/loader_launcher_proxy/Memory.cpp +++ b/loader_launcher_proxy/Memory.cpp @@ -14,6 +14,7 @@ void LoadTier0Handle() const int STATIC_ALLOC_SIZE = 4096; size_t g_iStaticAllocated = 0; +void* g_pLastAllocated = nullptr; char pStaticAllocBuf[STATIC_ALLOC_SIZE]; // they should never be used here, except in LibraryLoadError? @@ -48,6 +49,29 @@ void free(void* p) (*g_ppMemAllocSingleton)->m_vtable->Free(*g_ppMemAllocSingleton, p); } +void* realloc(void* old_ptr, size_t size) { + // it was allocated into the static buffer + if (old_ptr >= pStaticAllocBuf && old_ptr <= pStaticAllocBuf + STATIC_ALLOC_SIZE) + { + if (g_pLastAllocated == old_ptr) + { + // nothing was allocated after this + size_t old_size = g_iStaticAllocated - ((size_t)g_pLastAllocated - (size_t)pStaticAllocBuf); + size_t diff = size - old_size; + if (diff > 0) + g_iStaticAllocated += diff; + return old_ptr; + } + else + { + return malloc(size); + } + } + + if (g_ppMemAllocSingleton && *g_ppMemAllocSingleton) + return (*g_ppMemAllocSingleton)->m_vtable->Realloc(*g_ppMemAllocSingleton, old_ptr, size); +} + void* operator new(size_t n) { return malloc(n); diff --git a/loader_launcher_proxy/Memory.h b/loader_launcher_proxy/Memory.h index 928e403c..c983966c 100644 --- a/loader_launcher_proxy/Memory.h +++ b/loader_launcher_proxy/Memory.h @@ -5,10 +5,19 @@ class IMemAlloc public: struct VTable { - void* unknown[1]; + void* unknown[1]; // alloc debug void* (*Alloc) (IMemAlloc* memAlloc, size_t nSize); - void* unknown2[3]; - void(*Free) (IMemAlloc* memAlloc, void* pMem); + void* unknown2[1]; // realloc debug + void* (*Realloc)(IMemAlloc* memAlloc, void* pMem, size_t nSize); + void* unknown3[1]; // free #1 + void (*Free) (IMemAlloc* memAlloc, void* pMem); + void* unknown4[2]; // nullsubs, maybe CrtSetDbgFlag + size_t(*GetSize) (IMemAlloc* memAlloc, void* pMem); + void* unknown5[9]; // they all do literally nothing + void (*DumpStats) (IMemAlloc* memAlloc); + void (*DumpStatsFileBase) (IMemAlloc* memAlloc, const char* pchFileBase); + void* unknown6[4]; + int (*heapchk) (IMemAlloc* memAlloc); }; VTable* m_vtable; -- cgit v1.2.3 From 4f7c3d02943a38941b79a638c5607b2b7f668956 Mon Sep 17 00:00:00 2001 From: p0358 Date: Thu, 30 Dec 2021 06:26:10 +0100 Subject: actually use custom allocation, override allocators of curl and rapidjson --- LauncherInjector/memalloc.cpp | 1 + NorthstarDedicatedTest/dllmain.cpp | 9 +++++++- NorthstarDedicatedTest/hooks.cpp | 2 +- NorthstarDedicatedTest/masterserver.cpp | 23 +++++++++--------- NorthstarDedicatedTest/memalloc.cpp | 41 +++++++++++++++++++++++++++------ NorthstarDedicatedTest/memalloc.h | 40 +++++++++++++++++++++++++++++++- NorthstarDedicatedTest/modmanager.cpp | 4 ++-- NorthstarDedicatedTest/modmanager.h | 3 ++- NorthstarDedicatedTest/pch.h | 2 ++ loader_launcher_proxy/Memory.cpp | 1 + 10 files changed, 101 insertions(+), 25 deletions(-) (limited to 'NorthstarDedicatedTest/memalloc.cpp') diff --git a/LauncherInjector/memalloc.cpp b/LauncherInjector/memalloc.cpp index 936523d7..af334acf 100644 --- a/LauncherInjector/memalloc.cpp +++ b/LauncherInjector/memalloc.cpp @@ -76,6 +76,7 @@ void* realloc(void* old_ptr, size_t size) { if (g_ppMemAllocSingleton && *g_ppMemAllocSingleton) return (*g_ppMemAllocSingleton)->m_vtable->Realloc(*g_ppMemAllocSingleton, old_ptr, size); + return nullptr; } void* operator new(size_t n) diff --git a/NorthstarDedicatedTest/dllmain.cpp b/NorthstarDedicatedTest/dllmain.cpp index dfc3afe1..691c9bc7 100644 --- a/NorthstarDedicatedTest/dllmain.cpp +++ b/NorthstarDedicatedTest/dllmain.cpp @@ -65,7 +65,8 @@ void WaitForDebugger(HMODULE baseAddress) } } -// in the future this will be called from launcher instead of dllmain +SourceAllocator* g_SourceAllocator; + bool InitialiseNorthstar() { if (initialised) @@ -81,6 +82,8 @@ bool InitialiseNorthstar() InstallInitialHooks(); InitialiseInterfaceCreationHooks(); + g_SourceAllocator = new SourceAllocator; + AddDllLoadCallback("tier0.dll", InitialiseTier0GameUtilFunctions); AddDllLoadCallback("engine.dll", WaitForDebugger); AddDllLoadCallback("engine.dll", InitialiseEngineGameUtilFunctions); @@ -129,5 +132,9 @@ bool InitialiseNorthstar() // mod manager after everything else AddDllLoadCallback("engine.dll", InitialiseModManager); + // TODO: If you wanna make it more flexible and for example injectable with old Icepick injector + // in this place you should iterate over all already loaded DLLs and execute their callbacks and mark them as executed + // (as they will never get called otherwise and stuff will fail) + return true; } \ No newline at end of file diff --git a/NorthstarDedicatedTest/hooks.cpp b/NorthstarDedicatedTest/hooks.cpp index e58b7afc..5723a8ab 100644 --- a/NorthstarDedicatedTest/hooks.cpp +++ b/NorthstarDedicatedTest/hooks.cpp @@ -77,7 +77,7 @@ LPSTR GetCommandLineAHook() } auto len = args.length(); - cmdlineResult = reinterpret_cast(malloc(len + 1)); + cmdlineResult = reinterpret_cast(_malloc_base(len + 1)); if (!cmdlineResult) { spdlog::error("malloc failed for command line"); diff --git a/NorthstarDedicatedTest/masterserver.cpp b/NorthstarDedicatedTest/masterserver.cpp index fa3854d3..2fec6c82 100644 --- a/NorthstarDedicatedTest/masterserver.cpp +++ b/NorthstarDedicatedTest/masterserver.cpp @@ -3,7 +3,6 @@ #include "concommand.h" #include "gameutils.h" #include "hookutils.h" -#include "libcurl/include/curl/curl.h" #include "serverauthentication.h" #include "gameutils.h" #include "rapidjson/document.h" @@ -137,7 +136,7 @@ void MasterServerManager::AuthenticateOriginWithMasterServer(char* uid, char* or { m_successfullyConnected = true; - rapidjson::Document originAuthInfo; + rapidjson_document originAuthInfo; originAuthInfo.Parse(readBuffer.c_str()); if (originAuthInfo.HasParseError()) @@ -209,7 +208,7 @@ void MasterServerManager::RequestServerList() { m_successfullyConnected = true; - rapidjson::Document serverInfoJson; + rapidjson_document serverInfoJson; serverInfoJson.Parse(readBuffer.c_str()); if (serverInfoJson.HasParseError()) @@ -231,7 +230,7 @@ void MasterServerManager::RequestServerList() goto REQUEST_END_CLEANUP; } - rapidjson::GenericArray serverArray = serverInfoJson.GetArray(); + rapidjson::GenericArray serverArray = serverInfoJson.GetArray(); spdlog::info("Got {} servers", serverArray.Size()); @@ -346,7 +345,7 @@ void MasterServerManager::RequestMainMenuPromos() { m_successfullyConnected = true; - rapidjson::Document mainMenuPromoJson; + rapidjson_document mainMenuPromoJson; mainMenuPromoJson.Parse(readBuffer.c_str()); if (mainMenuPromoJson.HasParseError()) @@ -456,7 +455,7 @@ void MasterServerManager::AuthenticateWithOwnServer(char* uid, char* playerToken { m_successfullyConnected = true; - rapidjson::Document authInfoJson; + rapidjson_document authInfoJson; authInfoJson.Parse(readBuffer.c_str()); if (authInfoJson.HasParseError()) @@ -588,7 +587,7 @@ void MasterServerManager::AuthenticateWithServer(char* uid, char* playerToken, c { m_successfullyConnected = true; - rapidjson::Document connectionInfoJson; + rapidjson_document connectionInfoJson; connectionInfoJson.Parse(readBuffer.c_str()); if (connectionInfoJson.HasParseError()) @@ -672,9 +671,9 @@ void MasterServerManager::AddSelfToServerList(int port, int authPort, char* name m_ownServerId[0] = 0; // build modinfo obj - rapidjson::Document modinfoDoc; + rapidjson_document modinfoDoc; modinfoDoc.SetObject(); - modinfoDoc.AddMember("Mods", rapidjson::Value(rapidjson::kArrayType), modinfoDoc.GetAllocator()); + modinfoDoc.AddMember("Mods", rapidjson_document::GenericValue(rapidjson::kArrayType), modinfoDoc.GetAllocator()); int currentModIndex = 0; for (Mod& mod : g_ModManager->m_loadedMods) @@ -682,7 +681,7 @@ void MasterServerManager::AddSelfToServerList(int port, int authPort, char* name if (!mod.Enabled || (!mod.RequiredOnClient && !mod.Pdiff.size())) continue; - modinfoDoc["Mods"].PushBack(rapidjson::Value(rapidjson::kObjectType), modinfoDoc.GetAllocator()); + modinfoDoc["Mods"].PushBack(rapidjson_document::GenericValue(rapidjson::kObjectType), modinfoDoc.GetAllocator()); modinfoDoc["Mods"][currentModIndex].AddMember("Name", rapidjson::StringRef(&mod.Name[0]), modinfoDoc.GetAllocator()); modinfoDoc["Mods"][currentModIndex].AddMember("Version", rapidjson::StringRef(&mod.Version[0]), modinfoDoc.GetAllocator()); modinfoDoc["Mods"][currentModIndex].AddMember("RequiredOnClient", mod.RequiredOnClient, modinfoDoc.GetAllocator()); @@ -738,7 +737,7 @@ void MasterServerManager::AddSelfToServerList(int port, int authPort, char* name { m_successfullyConnected = true; - rapidjson::Document serverAddedJson; + rapidjson_document serverAddedJson; serverAddedJson.Parse(readBuffer.c_str()); if (serverAddedJson.HasParseError()) @@ -1025,7 +1024,7 @@ void CHostState__State_GameShutdownHook(CHostState* hostState) MasterServerManager::MasterServerManager() { - curl_global_init(CURL_GLOBAL_DEFAULT); + curl_global_init_mem(CURL_GLOBAL_DEFAULT, _malloc_base, _free_base, _realloc_base, _strdup_base, _calloc_base); } void InitialiseSharedMasterServer(HMODULE baseAddress) diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp index d301f1fa..c1fb70e7 100644 --- a/NorthstarDedicatedTest/memalloc.cpp +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -20,7 +20,7 @@ char pStaticAllocBuf[STATIC_ALLOC_SIZE]; // TODO: rename to malloc and free after removing statically compiled .libs -void* malloc_(size_t n) +extern "C" void* _malloc_base(size_t n) { // allocate into static buffer if g_pMemAllocSingleton isn't initialised if (g_pMemAllocSingleton) @@ -41,7 +41,12 @@ void* malloc_(size_t n) } } -void free_(void* p) +/*extern "C" void* malloc(size_t n) +{ + return _malloc_base(n); +}*/ + +extern "C" void _free_base(void* p) { // if it was allocated into the static buffer, just do nothing, safest way to deal with it if (p >= pStaticAllocBuf && p <= pStaticAllocBuf + STATIC_ALLOC_SIZE) @@ -54,7 +59,7 @@ void free_(void* p) g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p); } -void* realloc_(void* old_ptr, size_t size) { +extern "C" void* _realloc_base(void* old_ptr, size_t size) { // it was allocated into the static buffer if (old_ptr >= pStaticAllocBuf && old_ptr <= pStaticAllocBuf + STATIC_ALLOC_SIZE) { @@ -69,20 +74,42 @@ void* realloc_(void* old_ptr, size_t size) { } else { - return malloc_(size); + return _malloc_base(size); } } if (g_pMemAllocSingleton) return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, old_ptr, size); + return nullptr; +} + +extern "C" void* _calloc_base(size_t n, size_t size) +{ + return _malloc_base(n * size); +} + +extern "C" char* _strdup_base(const char* src) +{ + char* str; + char* p; + int len = 0; + + while (src[len]) + len++; + str = reinterpret_cast(_malloc_base(len + 1)); + p = str; + while (*src) + *p++ = *src++; + *p = '\0'; + return str; } void* operator new(size_t n) { - return malloc_(n); + return _malloc_base(n); } void operator delete(void* p) { - free_(p); -} \ No newline at end of file + _free_base(p); +}// /FORCE:MULTIPLE \ No newline at end of file diff --git a/NorthstarDedicatedTest/memalloc.h b/NorthstarDedicatedTest/memalloc.h index fe3c5255..d9277694 100644 --- a/NorthstarDedicatedTest/memalloc.h +++ b/NorthstarDedicatedTest/memalloc.h @@ -1,6 +1,44 @@ #pragma once +#include "include/rapidjson/document.h" +//#include "include/rapidjson/allocators.h" + extern size_t g_iStaticAllocated; +extern "C" { + char* _strdup_base(const char* src); +} + void* operator new(size_t n); -void operator delete(void* p); \ No newline at end of file +void operator delete(void* p); + +void* _malloc_base(size_t n); +//void* malloc(size_t n); + +class SourceAllocator { +public: + static const bool kNeedFree = true; + void* Malloc(size_t size) { + if (size) // behavior of malloc(0) is implementation defined. + return _malloc_base(size); + else + return NULL; // standardize to returning NULL. + } + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + (void)originalSize; + if (newSize == 0) { + _free_base(originalPtr); + return NULL; + } + return _realloc_base(originalPtr, newSize); + } + static void Free(void* ptr) { _free_base(ptr); } +}; + +extern SourceAllocator* g_SourceAllocator; + +typedef rapidjson::GenericDocument, rapidjson::MemoryPoolAllocator, SourceAllocator> rapidjson_document; +//typedef rapidjson::GenericDocument, SourceAllocator, SourceAllocator> rapidjson_document; +//typedef rapidjson::Document rapidjson_document; +//using MyDocument = rapidjson::GenericDocument, MemoryAllocator>; +//using rapidjson_document = rapidjson::GenericDocument, SourceAllocator, SourceAllocator>; diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp index 49c69c78..a9119075 100644 --- a/NorthstarDedicatedTest/modmanager.cpp +++ b/NorthstarDedicatedTest/modmanager.cpp @@ -22,7 +22,7 @@ Mod::Mod(fs::path modDir, char* jsonBuf) ModDirectory = modDir; - rapidjson::Document modJson; + rapidjson_document modJson; modJson.Parse(jsonBuf); // fail if parse error @@ -379,7 +379,7 @@ void ModManager::UnloadMods() // should we be doing this here or should scripts be doing this manually? // main issue with doing this here is when we reload mods for connecting to a server, we write enabled mods, which isn't necessarily what we wanna do if (!m_enabledModsCfg.HasMember(mod.Name.c_str())) - m_enabledModsCfg.AddMember(rapidjson::StringRef(mod.Name.c_str()), rapidjson::Value(false), m_enabledModsCfg.GetAllocator()); + m_enabledModsCfg.AddMember(rapidjson_document::StringRefType(mod.Name.c_str()), rapidjson_document::GenericValue(false), m_enabledModsCfg.GetAllocator()); m_enabledModsCfg[mod.Name.c_str()].SetBool(mod.Enabled); } diff --git a/NorthstarDedicatedTest/modmanager.h b/NorthstarDedicatedTest/modmanager.h index 5f2f6441..20cb0a42 100644 --- a/NorthstarDedicatedTest/modmanager.h +++ b/NorthstarDedicatedTest/modmanager.h @@ -4,6 +4,7 @@ #include #include #include "rapidjson/document.h" +#include "memalloc.h" namespace fs = std::filesystem; @@ -100,7 +101,7 @@ class ModManager private: bool m_hasLoadedMods = false; bool m_hasEnabledModsCfg; - rapidjson::Document m_enabledModsCfg; + rapidjson_document m_enabledModsCfg; // precalculated hashes size_t m_hScriptsRsonHash; diff --git a/NorthstarDedicatedTest/pch.h b/NorthstarDedicatedTest/pch.h index 9ac5b8a9..a07d1401 100644 --- a/NorthstarDedicatedTest/pch.h +++ b/NorthstarDedicatedTest/pch.h @@ -11,10 +11,12 @@ // httplib ssl // add headers that you want to pre-compile here +#include "memalloc.h" #include #include "logging.h" #include "include/MinHook.h" #include "spdlog/spdlog.h" +#include "libcurl/include/curl/curl.h" #include "hookutils.h" #endif \ No newline at end of file diff --git a/loader_launcher_proxy/Memory.cpp b/loader_launcher_proxy/Memory.cpp index bd19502a..f00c4d96 100644 --- a/loader_launcher_proxy/Memory.cpp +++ b/loader_launcher_proxy/Memory.cpp @@ -70,6 +70,7 @@ void* realloc(void* old_ptr, size_t size) { if (g_ppMemAllocSingleton && *g_ppMemAllocSingleton) return (*g_ppMemAllocSingleton)->m_vtable->Realloc(*g_ppMemAllocSingleton, old_ptr, size); + return nullptr; } void* operator new(size_t n) -- cgit v1.2.3 From 32b1257cd62ee6ec7f1087355a2d9e181429d165 Mon Sep 17 00:00:00 2001 From: geni Date: Fri, 31 Dec 2021 14:44:15 +0200 Subject: Remove linear allocator --- LauncherInjector/main.cpp | 22 +++++------ NorthstarDedicatedTest/dllmain.cpp | 6 +-- NorthstarDedicatedTest/gameutils.cpp | 2 - NorthstarDedicatedTest/memalloc.cpp | 75 ++++++++---------------------------- NorthstarDedicatedTest/memalloc.h | 12 +++--- 5 files changed, 35 insertions(+), 82 deletions(-) (limited to 'NorthstarDedicatedTest/memalloc.cpp') diff --git a/LauncherInjector/main.cpp b/LauncherInjector/main.cpp index f35a3015..761f443e 100644 --- a/LauncherInjector/main.cpp +++ b/LauncherInjector/main.cpp @@ -192,6 +192,15 @@ int main(int argc, char* argv[]) { PrependPath(); + printf("[*] Loading tier0.dll\n"); + swprintf_s(buffer, L"%s\\bin\\x64_retail\\tier0.dll", exePath); + hTier0Module = LoadLibraryExW(buffer, 0, LOAD_WITH_ALTERED_SEARCH_PATH); + if (!hTier0Module) + { + LibraryLoadError(GetLastError(), L"tier0.dll", buffer); + return 1; + } + bool loadNorthstar = ShouldLoadNorthstar(argc, argv); if (loadNorthstar) { @@ -204,23 +213,12 @@ int main(int argc, char* argv[]) { printf("[*] Loading launcher.dll\n"); swprintf_s(buffer, L"%s\\bin\\x64_retail\\launcher.dll", exePath); - hLauncherModule = LoadLibraryExW(buffer, 0i64, 8u); + hLauncherModule = LoadLibraryExW(buffer, 0, LOAD_WITH_ALTERED_SEARCH_PATH); if (!hLauncherModule) { LibraryLoadError(GetLastError(), L"launcher.dll", buffer); return 1; } - - printf("[*] Loading tier0.dll\n"); - // this makes zero sense given tier0.dll is already loaded via imports on launcher.dll, but we do it for full consistency with original launcher exe - // and to also let load callbacks in Northstar work for tier0.dll - swprintf_s(buffer, L"%s\\bin\\x64_retail\\tier0.dll", exePath); - hTier0Module = LoadLibraryW(buffer); - if (!hTier0Module) - { - LibraryLoadError(GetLastError(), L"tier0.dll", buffer); - return 1; - } } printf("[*] Launching the game...\n"); diff --git a/NorthstarDedicatedTest/dllmain.cpp b/NorthstarDedicatedTest/dllmain.cpp index 4f8a445c..07741801 100644 --- a/NorthstarDedicatedTest/dllmain.cpp +++ b/NorthstarDedicatedTest/dllmain.cpp @@ -58,7 +58,6 @@ void WaitForDebugger(HMODULE baseAddress) if (strstr(GetCommandLineA(), "-waitfordebugger")) { spdlog::info("waiting for debugger..."); - spdlog::info("{} bytes have been statically allocated", g_iStaticAllocated); while (!IsDebuggerPresent()) Sleep(100); @@ -71,7 +70,7 @@ bool InitialiseNorthstar() { if (initialised) { - fprintf(stderr, "[info] Called InitialiseNorthstar more than once!\n"); + spdlog::warn("Called InitialiseNorthstar more than once!"); return false; } initialised = true; @@ -85,7 +84,6 @@ bool InitialiseNorthstar() g_SourceAllocator = new SourceAllocator; curl_global_init(CURL_GLOBAL_DEFAULT); - AddDllLoadCallback("tier0.dll", InitialiseTier0GameUtilFunctions); AddDllLoadCallback("engine.dll", WaitForDebugger); AddDllLoadCallback("engine.dll", InitialiseEngineGameUtilFunctions); AddDllLoadCallback("server.dll", InitialiseServerGameUtilFunctions); @@ -93,8 +91,8 @@ bool InitialiseNorthstar() // dedi patches { + AddDllLoadCallback("launcher.dll", InitialiseDedicatedOrigin); AddDllLoadCallback("engine.dll", InitialiseDedicated); - AddDllLoadCallback("tier0.dll", InitialiseDedicatedOrigin); AddDllLoadCallback("server.dll", InitialiseDedicatedServerGameDLL); AddDllLoadCallback("materialsystem_dx11.dll", InitialiseDedicatedMaterialSystem); // this fucking sucks, but seemingly we somehow load after rtech_game???? unsure how, but because of this we have to apply patches here, not on rtech_game load diff --git a/NorthstarDedicatedTest/gameutils.cpp b/NorthstarDedicatedTest/gameutils.cpp index 3e62037c..b2c88e49 100644 --- a/NorthstarDedicatedTest/gameutils.cpp +++ b/NorthstarDedicatedTest/gameutils.cpp @@ -94,8 +94,6 @@ void InitialiseTier0GameUtilFunctions(HMODULE baseAddress) else { g_pMemAllocSingleton = *ppMemAllocSingleton; - extern size_t g_iStaticAllocated; - spdlog::info("Using existing g_pMemAllocSingleton for memory allocations, preallocated {} bytes beforehand", g_iStaticAllocated); } Error = reinterpret_cast(GetProcAddress(baseAddress, "Error")); diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp index c1fb70e7..c9cf4d60 100644 --- a/NorthstarDedicatedTest/memalloc.cpp +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -2,43 +2,16 @@ #include "memalloc.h" #include "gameutils.h" -// so for anyone reading this code, you may be curious why the fuck i'm overriding new to alloc into a static 100k buffer -// pretty much, the issue here is that we need to use the game's memory allocator (g_pMemAllocSingleton) or risk heap corruptions, but this allocator is defined in tier0 -// as such, it doesn't exist when we inject -// initially i wanted to just call malloc and free until g_pMemAllocSingleton was initialised, but the issue then becomes that we might try to -// call g_pMemAllocSingleton->Free on memory that was allocated with malloc, which will cause game to crash -// so, the best idea i had for this was to just alloc 100k of memory, have all pre-tier0 allocations use that -// (from what i can tell we hit about 12k before tier0 is loaded atm in debug builds, so it's more than enough) -// then just use the game's allocator after that -// yes, this means we leak 100k of memory, idk how else to do this without breaking stuff - -const int STATIC_ALLOC_SIZE = 100000; // alot more than we need, could reduce to 50k or even 25k later potentially - -size_t g_iStaticAllocated = 0; -void* g_pLastAllocated = nullptr; -char pStaticAllocBuf[STATIC_ALLOC_SIZE]; - // TODO: rename to malloc and free after removing statically compiled .libs extern "C" void* _malloc_base(size_t n) { // allocate into static buffer if g_pMemAllocSingleton isn't initialised - if (g_pMemAllocSingleton) - { - //printf("Northstar malloc (g_pMemAllocSingleton): %llu\n", n); - return g_pMemAllocSingleton->m_vtable->Alloc(g_pMemAllocSingleton, n); - } - else + if (!g_pMemAllocSingleton) { - if (g_iStaticAllocated + n > STATIC_ALLOC_SIZE) - { - throw "Ran out of prealloc space"; // we could log, but spdlog probably does use allocations as well... - } - //printf("Northstar malloc (prealloc): %llu\n", n); - void* ret = pStaticAllocBuf + g_iStaticAllocated; - g_iStaticAllocated += n; - return ret; + InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll")); } + return g_pMemAllocSingleton->m_vtable->Alloc(g_pMemAllocSingleton, n); } /*extern "C" void* malloc(size_t n) @@ -48,44 +21,30 @@ extern "C" void* _malloc_base(size_t n) extern "C" void _free_base(void* p) { - // if it was allocated into the static buffer, just do nothing, safest way to deal with it - if (p >= pStaticAllocBuf && p <= pStaticAllocBuf + STATIC_ALLOC_SIZE) + if (!g_pMemAllocSingleton) { - //printf("Northstar free (prealloc): %p\n", p); - return; + InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll")); } - - //printf("Northstar free (g_pMemAllocSingleton): %p\n", p); g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p); } -extern "C" void* _realloc_base(void* old_ptr, size_t size) { - // it was allocated into the static buffer - if (old_ptr >= pStaticAllocBuf && old_ptr <= pStaticAllocBuf + STATIC_ALLOC_SIZE) + +extern "C" void* _realloc_base(void* oldPtr, size_t size) { + if (!g_pMemAllocSingleton) { - if (g_pLastAllocated == old_ptr) - { - // nothing was allocated after this - size_t old_size = g_iStaticAllocated - ((size_t)g_pLastAllocated - (size_t)pStaticAllocBuf); - size_t diff = size - old_size; - if (diff > 0) - g_iStaticAllocated += diff; - return old_ptr; - } - else - { - return _malloc_base(size); - } + InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll")); } - - if (g_pMemAllocSingleton) - return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, old_ptr, size); - return nullptr; + return g_pMemAllocSingleton->m_vtable->Realloc(g_pMemAllocSingleton, oldPtr, size); } extern "C" void* _calloc_base(size_t n, size_t size) { - return _malloc_base(n * size); + size_t bytes = n * size; + void* memory = _malloc_base(bytes); + if (memory) { + memset(memory, 0, bytes); + } + return memory; } extern "C" char* _strdup_base(const char* src) @@ -96,7 +55,7 @@ extern "C" char* _strdup_base(const char* src) while (src[len]) len++; - str = reinterpret_cast(_malloc_base(len + 1)); + str = (char*)(_malloc_base(len + 1)); p = str; while (*src) *p++ = *src++; diff --git a/NorthstarDedicatedTest/memalloc.h b/NorthstarDedicatedTest/memalloc.h index d9277694..86d2ff58 100644 --- a/NorthstarDedicatedTest/memalloc.h +++ b/NorthstarDedicatedTest/memalloc.h @@ -3,16 +3,16 @@ #include "include/rapidjson/document.h" //#include "include/rapidjson/allocators.h" -extern size_t g_iStaticAllocated; - -extern "C" { - char* _strdup_base(const char* src); -} +extern "C" void* _malloc_base(size_t size); +extern "C" void* _calloc_base(size_t const count, size_t const size); +extern "C" void* _realloc_base(void* block, size_t size); +extern "C" void* _recalloc_base(void* const block, size_t const count, size_t const size); +extern "C" void _free_base(void* const block); +extern "C" char* _strdup_base(const char* src); void* operator new(size_t n); void operator delete(void* p); -void* _malloc_base(size_t n); //void* malloc(size_t n); class SourceAllocator { -- cgit v1.2.3 From 6b401003bbc94748b7839a0a22f02578ff09a4ed Mon Sep 17 00:00:00 2001 From: geni Date: Fri, 31 Dec 2021 15:00:39 +0200 Subject: Whoops --- NorthstarDedicatedTest/memalloc.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'NorthstarDedicatedTest/memalloc.cpp') diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp index c9cf4d60..86215e3f 100644 --- a/NorthstarDedicatedTest/memalloc.cpp +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -41,7 +41,8 @@ extern "C" void* _calloc_base(size_t n, size_t size) { size_t bytes = n * size; void* memory = _malloc_base(bytes); - if (memory) { + if (memory) + { memset(memory, 0, bytes); } return memory; -- cgit v1.2.3 From 9b13df7bc6f4c09c3fdab27cd51fe76d30b756b8 Mon Sep 17 00:00:00 2001 From: p0358 Date: Fri, 31 Dec 2021 22:46:45 +0100 Subject: some post-merge changes combined with my local changes --- LauncherInjector/main.cpp | 6 +++--- NorthstarDedicatedTest/dedicated.cpp | 2 +- NorthstarDedicatedTest/dllmain.cpp | 16 ++++++---------- NorthstarDedicatedTest/gameutils.cpp | 9 ++++++++- NorthstarDedicatedTest/hooks.cpp | 26 ++++++++++++++++++++++++-- NorthstarDedicatedTest/hooks.h | 4 +++- NorthstarDedicatedTest/masterserver.cpp | 4 ++-- NorthstarDedicatedTest/memalloc.cpp | 6 ++++-- NorthstarDedicatedTest/memalloc.h | 2 -- 9 files changed, 51 insertions(+), 24 deletions(-) (limited to 'NorthstarDedicatedTest/memalloc.cpp') diff --git a/LauncherInjector/main.cpp b/LauncherInjector/main.cpp index 761f443e..0f70fd4b 100644 --- a/LauncherInjector/main.cpp +++ b/LauncherInjector/main.cpp @@ -81,7 +81,7 @@ void EnsureOriginStarted() HKEY key; if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\WOW6432Node\\Origin", 0, KEY_READ, &key) != ERROR_SUCCESS) { - MessageBoxA(0, "Error: failed reading origin path!", "", MB_OK); + MessageBoxA(0, "Error: failed reading Origin path!", "", MB_OK); return; } @@ -89,7 +89,7 @@ void EnsureOriginStarted() DWORD originPathLength = 520; if (RegQueryValueExA(key, "ClientPath", 0, 0, (LPBYTE)&originPath, &originPathLength) != ERROR_SUCCESS) { - MessageBoxA(0, "Error: failed reading origin path!", "", MB_OK); + MessageBoxA(0, "Error: failed reading Origin path!", "", MB_OK); return; } @@ -122,7 +122,7 @@ void PrependPath() { MessageBoxW(GetForegroundWindow(), L"Warning: could not prepend the current directory to app's PATH environment variable. Something may break because of that.", L"Northstar Launcher Warning", 0); } - //free(pPath); + free(pPath); } else { diff --git a/NorthstarDedicatedTest/dedicated.cpp b/NorthstarDedicatedTest/dedicated.cpp index 0ecc1dba..8dedcdd9 100644 --- a/NorthstarDedicatedTest/dedicated.cpp +++ b/NorthstarDedicatedTest/dedicated.cpp @@ -394,7 +394,7 @@ void InitialiseDedicatedOrigin(HMODULE baseAddress) char* ptr = (char*)GetProcAddress(GetModuleHandleA("tier0.dll"), "Tier0_InitOrigin"); TempReadWrite rw(ptr); - *ptr = (char)0xC3; + *ptr = (char)0xC3; // ret } typedef void(*PrintFatalSquirrelErrorType)(void* sqvm); diff --git a/NorthstarDedicatedTest/dllmain.cpp b/NorthstarDedicatedTest/dllmain.cpp index 81bae847..87fb4d5f 100644 --- a/NorthstarDedicatedTest/dllmain.cpp +++ b/NorthstarDedicatedTest/dllmain.cpp @@ -43,11 +43,6 @@ BOOL APIENTRY DllMain( HMODULE hModule, break; } - // pls no xD - //if (!initialised) - // InitialiseNorthstar(); - //initialised = true; - return TRUE; } @@ -71,9 +66,10 @@ bool InitialiseNorthstar() spdlog::warn("Called InitialiseNorthstar more than once!"); return false; } + initialised = true; - curl_global_init(CURL_GLOBAL_DEFAULT); + curl_global_init_mem(CURL_GLOBAL_DEFAULT, _malloc_base, _free_base, _realloc_base, _strdup_base, _calloc_base); InitialiseLogging(); @@ -81,6 +77,7 @@ bool InitialiseNorthstar() InstallInitialHooks(); InitialiseInterfaceCreationHooks(); + AddDllLoadCallback("tier0.dll", InitialiseTier0GameUtilFunctions); AddDllLoadCallback("engine.dll", WaitForDebugger); AddDllLoadCallback("engine.dll", InitialiseEngineGameUtilFunctions); AddDllLoadCallback("server.dll", InitialiseServerGameUtilFunctions); @@ -88,7 +85,7 @@ bool InitialiseNorthstar() // dedi patches { - AddDllLoadCallback("launcher.dll", InitialiseDedicatedOrigin); + AddDllLoadCallback("tier0.dll", InitialiseDedicatedOrigin); AddDllLoadCallback("engine.dll", InitialiseDedicated); AddDllLoadCallback("server.dll", InitialiseDedicatedServerGameDLL); AddDllLoadCallback("materialsystem_dx11.dll", InitialiseDedicatedMaterialSystem); @@ -128,9 +125,8 @@ bool InitialiseNorthstar() // mod manager after everything else AddDllLoadCallback("engine.dll", InitialiseModManager); - // TODO: If you wanna make it more flexible and for example injectable with old Icepick injector - // in this place you should iterate over all already loaded DLLs and execute their callbacks and mark them as executed - // (as they will never get called otherwise and stuff will fail) + // run callbacks for any libraries that are already loaded by now + CallAllPendingDLLLoadCallbacks(); return true; } \ No newline at end of file diff --git a/NorthstarDedicatedTest/gameutils.cpp b/NorthstarDedicatedTest/gameutils.cpp index b2c88e49..1cbd8648 100644 --- a/NorthstarDedicatedTest/gameutils.cpp +++ b/NorthstarDedicatedTest/gameutils.cpp @@ -78,6 +78,13 @@ void InitialiseServerGameUtilFunctions(HMODULE baseAddress) void InitialiseTier0GameUtilFunctions(HMODULE baseAddress) { + if (!baseAddress) + { + spdlog::critical("tier0 base address is null, but it should be already loaded"); + throw "tier0 base address is null, but it should be already loaded"; + } + if (g_pMemAllocSingleton) + return; // seems this function was already called CreateGlobalMemAlloc = reinterpret_cast(GetProcAddress(baseAddress, "CreateGlobalMemAlloc")); IMemAlloc** ppMemAllocSingleton = reinterpret_cast(GetProcAddress(baseAddress, "g_pMemAllocSingleton")); if (!ppMemAllocSingleton) @@ -89,7 +96,7 @@ void InitialiseTier0GameUtilFunctions(HMODULE baseAddress) { g_pMemAllocSingleton = CreateGlobalMemAlloc(); *ppMemAllocSingleton = g_pMemAllocSingleton; - spdlog::warn("Created new g_pMemAllocSingleton"); + spdlog::info("Created new g_pMemAllocSingleton"); } else { diff --git a/NorthstarDedicatedTest/hooks.cpp b/NorthstarDedicatedTest/hooks.cpp index 19010e83..0e653d4e 100644 --- a/NorthstarDedicatedTest/hooks.cpp +++ b/NorthstarDedicatedTest/hooks.cpp @@ -8,12 +8,11 @@ #include #include #include +#include typedef LPSTR(*GetCommandLineAType)(); LPSTR GetCommandLineAHook(); -// note that these load library callbacks only support explicitly loaded dynamic libraries - typedef HMODULE(*LoadLibraryExAType)(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags); HMODULE LoadLibraryExAHook(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags); @@ -138,6 +137,29 @@ void CallLoadLibraryWCallbacks(LPCWSTR lpLibFileName, HMODULE moduleAddress) } } +void CallAllPendingDLLLoadCallbacks() +{ + HMODULE hMods[1024]; + HANDLE hProcess = GetCurrentProcess(); + DWORD cbNeeded; + unsigned int i; + + // Get a list of all the modules in this process. + if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded)) + { + for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) + { + wchar_t szModName[MAX_PATH]; + + // Get the full path to the module's file. + if (GetModuleFileNameExW(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) + { + CallLoadLibraryWCallbacks(szModName, hMods[i]); + } + } + } +} + HMODULE LoadLibraryExAHook(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags) { HMODULE moduleAddress = LoadLibraryExAOriginal(lpLibFileName, hFile, dwFlags); diff --git a/NorthstarDedicatedTest/hooks.h b/NorthstarDedicatedTest/hooks.h index 972b38a6..10e4d4ba 100644 --- a/NorthstarDedicatedTest/hooks.h +++ b/NorthstarDedicatedTest/hooks.h @@ -4,4 +4,6 @@ void InstallInitialHooks(); typedef void(*DllLoadCallbackFuncType)(HMODULE moduleAddress); -void AddDllLoadCallback(std::string dll, DllLoadCallbackFuncType callback); \ No newline at end of file +void AddDllLoadCallback(std::string dll, DllLoadCallbackFuncType callback); + +void CallAllPendingDLLLoadCallbacks(); \ No newline at end of file diff --git a/NorthstarDedicatedTest/masterserver.cpp b/NorthstarDedicatedTest/masterserver.cpp index 2fec6c82..e25be8ab 100644 --- a/NorthstarDedicatedTest/masterserver.cpp +++ b/NorthstarDedicatedTest/masterserver.cpp @@ -1022,9 +1022,9 @@ void CHostState__State_GameShutdownHook(CHostState* hostState) CHostState__State_GameShutdown(hostState); } -MasterServerManager::MasterServerManager() +MasterServerManager::MasterServerManager() : m_pendingConnectionInfo{}, m_ownServerId{ "" }, m_ownClientAuthToken{ "" } { - curl_global_init_mem(CURL_GLOBAL_DEFAULT, _malloc_base, _free_base, _realloc_base, _strdup_base, _calloc_base); + } void InitialiseSharedMasterServer(HMODULE baseAddress) diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp index 86215e3f..1b9eaae8 100644 --- a/NorthstarDedicatedTest/memalloc.cpp +++ b/NorthstarDedicatedTest/memalloc.cpp @@ -23,13 +23,15 @@ extern "C" void _free_base(void* p) { if (!g_pMemAllocSingleton) { + spdlog::warn("Trying to free something before g_pMemAllocSingleton was ready, this should never happen"); InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll")); } g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p); } -extern "C" void* _realloc_base(void* oldPtr, size_t size) { +extern "C" void* _realloc_base(void* oldPtr, size_t size) +{ if (!g_pMemAllocSingleton) { InitialiseTier0GameUtilFunctions(GetModuleHandleA("tier0.dll")); @@ -56,7 +58,7 @@ extern "C" char* _strdup_base(const char* src) while (src[len]) len++; - str = (char*)(_malloc_base(len + 1)); + str = reinterpret_cast(_malloc_base(len + 1)); p = str; while (*src) *p++ = *src++; diff --git a/NorthstarDedicatedTest/memalloc.h b/NorthstarDedicatedTest/memalloc.h index b98fe3c8..92ab9672 100644 --- a/NorthstarDedicatedTest/memalloc.h +++ b/NorthstarDedicatedTest/memalloc.h @@ -35,8 +35,6 @@ public: static void Free(void* ptr) { _free_base(ptr); } }; -static SourceAllocator g_SourceAllocator; - typedef rapidjson::GenericDocument, rapidjson::MemoryPoolAllocator, SourceAllocator> rapidjson_document; //typedef rapidjson::GenericDocument, SourceAllocator, SourceAllocator> rapidjson_document; //typedef rapidjson::Document rapidjson_document; -- cgit v1.2.3