From ac054ab2dc1370981258cafb175250142844260d Mon Sep 17 00:00:00 2001 From: p0358 Date: Thu, 30 Dec 2021 04:23:08 +0100 Subject: fix various random warnings --- NorthstarDedicatedTest/modmanager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'NorthstarDedicatedTest/modmanager.cpp') diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp index 4f4a2da0..49c69c78 100644 --- a/NorthstarDedicatedTest/modmanager.cpp +++ b/NorthstarDedicatedTest/modmanager.cpp @@ -333,7 +333,7 @@ void ModManager::LoadMods() } // in a seperate loop because we register mod files in reverse order, since mods loaded later should have their files prioritised - for (int i = m_loadedMods.size() - 1; i > -1; i--) + for (size_t i = m_loadedMods.size() - 1; i > -1; i--) { if (!m_loadedMods[i].Enabled) continue; -- 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/modmanager.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 07fbef738274518dfcc60da3a624d03eb09dc015 Mon Sep 17 00:00:00 2001 From: geni Date: Fri, 31 Dec 2021 14:41:53 +0200 Subject: Clean up --- NorthstarDedicatedTest/convar.cpp | 2 +- NorthstarDedicatedTest/hooks.cpp | 18 +++++++++--------- NorthstarDedicatedTest/hookutils.cpp | 4 ++-- NorthstarDedicatedTest/modmanager.cpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) (limited to 'NorthstarDedicatedTest/modmanager.cpp') diff --git a/NorthstarDedicatedTest/convar.cpp b/NorthstarDedicatedTest/convar.cpp index ed7e8dac..de460662 100644 --- a/NorthstarDedicatedTest/convar.cpp +++ b/NorthstarDedicatedTest/convar.cpp @@ -20,7 +20,7 @@ ConVar* RegisterConVar(const char* name, const char* defaultValue, int flags, co ConVar* newVar = new ConVar; conVarConstructor(newVar, name, defaultValue, flags, helpString); - g_CustomConvars.insert(std::make_pair(name, newVar)); + g_CustomConvars.emplace(name, newVar); return newVar; } diff --git a/NorthstarDedicatedTest/hooks.cpp b/NorthstarDedicatedTest/hooks.cpp index 5723a8ab..19010e83 100644 --- a/NorthstarDedicatedTest/hooks.cpp +++ b/NorthstarDedicatedTest/hooks.cpp @@ -45,12 +45,12 @@ void InstallInitialHooks() ENABLER_CREATEHOOK(hook, &LoadLibraryW, &LoadLibraryWHook, reinterpret_cast(&LoadLibraryWOriginal)); } -char* cmdlineResult; LPSTR GetCommandLineAHook() { + static char* cmdlineModified; static char* cmdlineOrg; - if (cmdlineOrg == nullptr || cmdlineResult == nullptr) + if (cmdlineOrg == nullptr || cmdlineModified == nullptr) { cmdlineOrg = GetCommandLineAOriginal(); bool isDedi = strstr(cmdlineOrg, "-dedicated"); // well, this one has to be a real argument @@ -77,18 +77,18 @@ LPSTR GetCommandLineAHook() } auto len = args.length(); - cmdlineResult = reinterpret_cast(_malloc_base(len + 1)); - if (!cmdlineResult) + cmdlineModified = new char[len + 1]; + if (!cmdlineModified) { spdlog::error("malloc failed for command line"); return cmdlineOrg; } - memcpy(cmdlineResult, args.c_str(), len + 1); + memcpy(cmdlineModified, args.c_str(), len + 1); - spdlog::info("Command line: {}", cmdlineResult); + spdlog::info("Command line: {}", cmdlineModified); } - return cmdlineResult; + return cmdlineModified; } // dll load callback stuff @@ -116,7 +116,7 @@ void CallLoadLibraryACallbacks(LPCSTR lpLibFileName, HMODULE moduleAddress) { for (auto& callbackStruct : dllLoadCallbacks) { - if (!callbackStruct->called && strstr(lpLibFileName + (strlen(lpLibFileName) - strlen(callbackStruct->dll.c_str())), callbackStruct->dll.c_str()) != nullptr) + if (!callbackStruct->called && strstr(lpLibFileName + (strlen(lpLibFileName) - callbackStruct->dll.length()), callbackStruct->dll.c_str()) != nullptr) { callbackStruct->callback(moduleAddress); callbackStruct->called = true; @@ -130,7 +130,7 @@ void CallLoadLibraryWCallbacks(LPCWSTR lpLibFileName, HMODULE moduleAddress) { std::wstring wcharStrDll = std::wstring(callbackStruct->dll.begin(), callbackStruct->dll.end()); const wchar_t* callbackDll = wcharStrDll.c_str(); - if (!callbackStruct->called && wcsstr(lpLibFileName + (wcslen(lpLibFileName) - wcslen(callbackDll)), callbackDll) != nullptr) + if (!callbackStruct->called && wcsstr(lpLibFileName + (wcslen(lpLibFileName) - wcharStrDll.length()), callbackDll) != nullptr) { callbackStruct->callback(moduleAddress); callbackStruct->called = true; diff --git a/NorthstarDedicatedTest/hookutils.cpp b/NorthstarDedicatedTest/hookutils.cpp index e86c671c..8ab24a3b 100644 --- a/NorthstarDedicatedTest/hookutils.cpp +++ b/NorthstarDedicatedTest/hookutils.cpp @@ -38,7 +38,7 @@ void HookEnabler::CreateHook(LPVOID ppTarget, LPVOID ppDetour, LPVOID* ppOrigina else { if (targetName != nullptr) - spdlog::error("MH_CreateHook failed for function %s", targetName); + spdlog::error("MH_CreateHook failed for function {}", targetName); else spdlog::error("MH_CreateHook failed for unknown function"); } @@ -51,7 +51,7 @@ HookEnabler::~HookEnabler() if (MH_EnableHook(hook->targetAddress) != MH_OK) { if (hook->targetName != nullptr) - spdlog::error("MH_EnableHook failed for function %s", hook->targetName); + spdlog::error("MH_EnableHook failed for function {}", hook->targetName); else spdlog::error("MH_EnableHook failed for unknown function"); } diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp index a9119075..92f32a43 100644 --- a/NorthstarDedicatedTest/modmanager.cpp +++ b/NorthstarDedicatedTest/modmanager.cpp @@ -309,7 +309,7 @@ void ModManager::LoadMods() if (fs::is_regular_file(file)) { std::string kvStr = file.path().lexically_relative(mod.ModDirectory / "keyvalues").lexically_normal().string(); - mod.KeyValues.insert(std::make_pair(std::hash{}(kvStr), kvStr)); + mod.KeyValues.emplace(std::hash{}(kvStr), kvStr); } } } -- cgit v1.2.3 From a6a54befbd5a577804a2363e987bbc917abf9ff0 Mon Sep 17 00:00:00 2001 From: geni Date: Fri, 31 Dec 2021 14:43:15 +0200 Subject: Fix loop --- NorthstarDedicatedTest/modmanager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'NorthstarDedicatedTest/modmanager.cpp') diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp index 92f32a43..23dd2d6e 100644 --- a/NorthstarDedicatedTest/modmanager.cpp +++ b/NorthstarDedicatedTest/modmanager.cpp @@ -333,7 +333,7 @@ void ModManager::LoadMods() } // in a seperate loop because we register mod files in reverse order, since mods loaded later should have their files prioritised - for (size_t i = m_loadedMods.size() - 1; i > -1; i--) + for (int64_t i = m_loadedMods.size() - 1; i > -1; i--) { if (!m_loadedMods[i].Enabled) continue; -- cgit v1.2.3