aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobTheBob <32057864+BobTheBob9@users.noreply.github.com>2021-12-03 20:55:19 +0000
committerBobTheBob <32057864+BobTheBob9@users.noreply.github.com>2021-12-03 20:55:19 +0000
commit9e8232e97db1cfe9f0f9a69bdfde5048a6290df1 (patch)
treebe1324c39dd1680335f4bfaf3d1660b5d6e31514
parentf33bab4fb3586fd06896a7730bce8913c2616b78 (diff)
downloadNorthstarLauncher-9e8232e97db1cfe9f0f9a69bdfde5048a6290df1.tar.gz
NorthstarLauncher-9e8232e97db1cfe9f0f9a69bdfde5048a6290df1.zip
slightly awful fix for allocation issues
-rw-r--r--NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj6
-rw-r--r--NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters6
-rw-r--r--NorthstarDedicatedTest/dedicatedmaterialsystem.cpp10
-rw-r--r--NorthstarDedicatedTest/dllmain.cpp4
-rw-r--r--NorthstarDedicatedTest/gameutils.cpp8
-rw-r--r--NorthstarDedicatedTest/gameutils.h19
-rw-r--r--NorthstarDedicatedTest/include/MinHook.x86.dllbin12288 -> 0 bytes
-rw-r--r--NorthstarDedicatedTest/include/MinHook.x86.libbin4238 -> 0 bytes
-rw-r--r--NorthstarDedicatedTest/memalloc.cpp40
-rw-r--r--NorthstarDedicatedTest/memalloc.h6
-rw-r--r--NorthstarDedicatedTest/modmanager.cpp2
-rw-r--r--NorthstarDedicatedTest/playlist.cpp10
12 files changed, 93 insertions, 18 deletions
diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
index 716b306a..5c7689dc 100644
--- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
+++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj
@@ -137,6 +137,8 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>$(ProjectDir)include\MinHook.x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <ForceSymbolReferences>
+ </ForceSymbolReferences>
</Link>
<PreBuildEvent>
<Command>
@@ -163,6 +165,8 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
<AdditionalDependencies>$(ProjectDir)include\MinHook.x64.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <ForceSymbolReferences>
+ </ForceSymbolReferences>
</Link>
<PreBuildEvent>
<Command>
@@ -312,6 +316,7 @@
<ClInclude Include="logging.h" />
<ClInclude Include="main.h" />
<ClInclude Include="masterserver.h" />
+ <ClInclude Include="memalloc.h" />
<ClInclude Include="misccommands.h" />
<ClInclude Include="modlocalisation.h" />
<ClInclude Include="modmanager.h" />
@@ -344,6 +349,7 @@
<ClCompile Include="hooks.cpp" />
<ClCompile Include="hookutils.cpp" />
<ClCompile Include="keyvalues.cpp" />
+ <ClCompile Include="memalloc.cpp" />
<ClCompile Include="misccommands.cpp" />
<ClCompile Include="modlocalisation.cpp" />
<ClCompile Include="logging.cpp" />
diff --git a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
index ac20a53c..4acfac46 100644
--- a/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
+++ b/NorthstarDedicatedTest/NorthstarDedicatedTest.vcxproj.filters
@@ -585,6 +585,9 @@
<ClInclude Include="gameutils.h">
<Filter>Header Files\Shared\Game Functions</Filter>
</ClInclude>
+ <ClInclude Include="memalloc.h">
+ <Filter>Header Files\Shared</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
@@ -683,6 +686,9 @@
<ClCompile Include="scriptbrowserhooks.cpp">
<Filter>Source Files\Client</Filter>
</ClCompile>
+ <ClCompile Include="memalloc.cpp">
+ <Filter>Source Files\Shared</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="include\spdlog\fmt\bundled\LICENSE.rst">
diff --git a/NorthstarDedicatedTest/dedicatedmaterialsystem.cpp b/NorthstarDedicatedTest/dedicatedmaterialsystem.cpp
index 29ccf6f7..758f0527 100644
--- a/NorthstarDedicatedTest/dedicatedmaterialsystem.cpp
+++ b/NorthstarDedicatedTest/dedicatedmaterialsystem.cpp
@@ -129,7 +129,7 @@ void* RegisterRpakTypeHook(RpakTypeDefinition* rpakStruct, unsigned int a1, unsi
spdlog::info("rpak type {} {} registered", magicName, rpakStruct->longName);
// reregister rpak types that aren't registered on a windowless dedi
- if (IsDedicated() && DisableDedicatedWindowCreation() && rpakStruct->magic == 0x64636C72) // rlcd magic, this one is registered last
+ if (rpakStruct->magic == 0x64636C72) // rlcd magic, this one is registered last
((RegisterMaterialSystemRpakTypes)((char*)GetModuleHandleA("materialsystem_dx11.dll") + 0x22A0))(); // slightly hellish call, registers materialsystem rpak types
return RegisterRpakType(rpakStruct, a1, a2);
@@ -137,13 +137,9 @@ void* RegisterRpakTypeHook(RpakTypeDefinition* rpakStruct, unsigned int a1, unsi
void InitialiseDedicatedRtechGame(HMODULE baseAddress)
{
- // potentially do this somewhere other than dedicated stuff if it's going to be used in non-dedi
+ if (!IsDedicated() || !DisableDedicatedWindowCreation())
+ return;
HookEnabler hook;
ENABLER_CREATEHOOK(hook, (char*)GetModuleHandleA("rtech_game.dll") + 0x7BE0, &RegisterRpakTypeHook, reinterpret_cast<LPVOID*>(&RegisterRpakType));
-
- if (!IsDedicated())
- return;
-
-
} \ No newline at end of file
diff --git a/NorthstarDedicatedTest/dllmain.cpp b/NorthstarDedicatedTest/dllmain.cpp
index bdee441f..9d8dc9a7 100644
--- a/NorthstarDedicatedTest/dllmain.cpp
+++ b/NorthstarDedicatedTest/dllmain.cpp
@@ -22,6 +22,7 @@
#include "miscserverscript.h"
#include "clientauthhooks.h"
#include "scriptbrowserhooks.h"
+#include "memalloc.h"
bool initialised = false;
@@ -51,6 +52,9 @@ void WaitForDebugger(HMODULE baseAddress)
// earlier waitfordebugger call than is in vanilla, just so we can debug stuff a little easier
if (CommandLine()->CheckParm("-waitfordebugger"))
{
+ spdlog::info("waiting for debugger...");
+ spdlog::info("{} bytes have been statically allocated", g_iStaticAllocated);
+
while (!IsDebuggerPresent())
Sleep(100);
}
diff --git a/NorthstarDedicatedTest/gameutils.cpp b/NorthstarDedicatedTest/gameutils.cpp
index 714395c6..c0c01aad 100644
--- a/NorthstarDedicatedTest/gameutils.cpp
+++ b/NorthstarDedicatedTest/gameutils.cpp
@@ -3,6 +3,11 @@
#include "convar.h"
#include "concommand.h"
+// memory
+IMemAlloc* g_pMemAllocSingleton;
+typedef IMemAlloc* (*CreateGlobalMemAllocType)();
+CreateGlobalMemAllocType CreateGlobalMemAlloc;
+
// cmd.h
Cbuf_GetCurrentPlayerType Cbuf_GetCurrentPlayer;
Cbuf_AddTextType Cbuf_AddText;
@@ -75,6 +80,9 @@ void InitialiseTier0GameUtilFunctions(HMODULE baseAddress)
{
baseAddress = GetModuleHandleA("tier0.dll");
+ CreateGlobalMemAlloc = (CreateGlobalMemAllocType)GetProcAddress(baseAddress, "CreateGlobalMemAlloc");
+ g_pMemAllocSingleton = CreateGlobalMemAlloc();
+
Error = (ErrorType)GetProcAddress(baseAddress, "Error");
CommandLine = (CommandLineType)GetProcAddress(baseAddress, "CommandLine");
Plat_FloatTime = (Plat_FloatTimeType)GetProcAddress(baseAddress, "Plat_FloatTime");
diff --git a/NorthstarDedicatedTest/gameutils.h b/NorthstarDedicatedTest/gameutils.h
index 69da4325..8def57eb 100644
--- a/NorthstarDedicatedTest/gameutils.h
+++ b/NorthstarDedicatedTest/gameutils.h
@@ -1,6 +1,25 @@
#pragma once
#include "convar.h"
+// memory
+class IMemAlloc
+{
+public:
+ struct VTable
+ {
+ void* unknown[1];
+ void* (*Alloc)(IMemAlloc* memAlloc, size_t nSize);
+ void* unknown2[3];
+ void (*Free)(IMemAlloc* memAlloc, void* pMem);
+ };
+
+ VTable* m_vtable;
+};
+
+extern IMemAlloc* g_pMemAllocSingleton;
+typedef IMemAlloc*(*CreateGlobalMemAllocType)();
+extern CreateGlobalMemAllocType CreateGlobalMemAlloc;
+
// cmd.h
enum class ECommandTarget_t
{
diff --git a/NorthstarDedicatedTest/include/MinHook.x86.dll b/NorthstarDedicatedTest/include/MinHook.x86.dll
deleted file mode 100644
index d6167c83..00000000
--- a/NorthstarDedicatedTest/include/MinHook.x86.dll
+++ /dev/null
Binary files differ
diff --git a/NorthstarDedicatedTest/include/MinHook.x86.lib b/NorthstarDedicatedTest/include/MinHook.x86.lib
deleted file mode 100644
index 02f351c2..00000000
--- a/NorthstarDedicatedTest/include/MinHook.x86.lib
+++ /dev/null
Binary files differ
diff --git a/NorthstarDedicatedTest/memalloc.cpp b/NorthstarDedicatedTest/memalloc.cpp
new file mode 100644
index 00000000..c7cfbb6a
--- /dev/null
+++ b/NorthstarDedicatedTest/memalloc.cpp
@@ -0,0 +1,40 @@
+#include "pch.h"
+#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;
+
+size_t g_iStaticAllocated = 0;
+char pStaticAllocBuf[STATIC_ALLOC_SIZE];
+
+void* operator new(size_t n)
+{
+ // allocate into static buffer if g_pMemAllocSingleton isn't initialised
+ if (g_pMemAllocSingleton)
+ return g_pMemAllocSingleton->m_vtable->Alloc(g_pMemAllocSingleton, n);
+ else
+ {
+ void* ret = pStaticAllocBuf + g_iStaticAllocated;
+ g_iStaticAllocated += n;
+ return ret;
+ }
+}
+
+void operator delete(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)
+ return;
+
+ g_pMemAllocSingleton->m_vtable->Free(g_pMemAllocSingleton, p);
+} \ No newline at end of file
diff --git a/NorthstarDedicatedTest/memalloc.h b/NorthstarDedicatedTest/memalloc.h
new file mode 100644
index 00000000..fe3c5255
--- /dev/null
+++ b/NorthstarDedicatedTest/memalloc.h
@@ -0,0 +1,6 @@
+#pragma once
+
+extern size_t g_iStaticAllocated;
+
+void* operator new(size_t n);
+void operator delete(void* p); \ No newline at end of file
diff --git a/NorthstarDedicatedTest/modmanager.cpp b/NorthstarDedicatedTest/modmanager.cpp
index 66e6d048..e4af294c 100644
--- a/NorthstarDedicatedTest/modmanager.cpp
+++ b/NorthstarDedicatedTest/modmanager.cpp
@@ -425,7 +425,7 @@ void ReloadModsCommand(const CCommand& args)
void InitialiseModManager(HMODULE baseAddress)
{
- g_ModManager = new ModManager();
+ g_ModManager = new ModManager;
RegisterConCommand("reload_mods", ReloadModsCommand, "idk", FCVAR_NONE);
} \ No newline at end of file
diff --git a/NorthstarDedicatedTest/playlist.cpp b/NorthstarDedicatedTest/playlist.cpp
index 1343ed40..20c76cb0 100644
--- a/NorthstarDedicatedTest/playlist.cpp
+++ b/NorthstarDedicatedTest/playlist.cpp
@@ -19,15 +19,6 @@ void SetPlaylistCommand(const CCommand& args)
SetCurrentPlaylist(args.Arg(1));
}
-void SetPlaylistVarOverrideCommand(const CCommand& args)
-{
- // this is broken lol
- if (args.ArgC() < 3)
- return;
-
- SetPlaylistVarOverride(args.Arg(1), args.Arg(2));
-}
-
char Onclc_SetPlaylistVarOverrideHook(void* a1, void* a2)
{
// the private_match playlist is the only situation where there should be any legitimate sending of this netmessage
@@ -41,7 +32,6 @@ char Onclc_SetPlaylistVarOverrideHook(void* a1, void* a2)
void InitialisePlaylistHooks(HMODULE baseAddress)
{
RegisterConCommand("setplaylist", SetPlaylistCommand, "Sets the current playlist", FCVAR_NONE);
- RegisterConCommand("setplaylistvaroverride", SetPlaylistVarOverrideCommand, "Sets a playlist var override", FCVAR_NONE);
// note: clc_SetPlaylistVarOverride is pretty insecure, since it allows for entirely arbitrary playlist var overrides to be sent to the server
// this is somewhat restricted on custom servers to prevent it being done outside of private matches, but ideally it should be disabled altogether, since the custom menus won't use it anyway
// this should only really be accepted if you want vanilla client compatibility