Skip to content

Commit

Permalink
Merge branch 'main' into fix/mad-cleanup-handles
Browse files Browse the repository at this point in the history
  • Loading branch information
Alystrasz authored Oct 6, 2024
2 parents 5f0be57 + 42d9702 commit a14fd8b
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 93 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ include_directories(primedev/thirdparty)

# Targets
add_subdirectory(primedev)
# Forces Minizip to not use functions that are not available in Win 7 libraries.
if(WIN32)
target_compile_definitions(minizip PUBLIC -D_WIN32_WINNT=0x0601)
endif()
1 change: 0 additions & 1 deletion primedev/Northstar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ add_library(
"core/memalloc.cpp"
"core/memalloc.h"
"core/sourceinterface.cpp"
"core/sourceinterface.h"
"core/tier0.cpp"
"core/tier0.h"
"core/tier1.cpp"
Expand Down
1 change: 0 additions & 1 deletion primedev/core/convar/convar.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "core/sourceinterface.h"
#include "core/math/color.h"
#include "cvar.h"
#include "concommand.h"
Expand Down
4 changes: 2 additions & 2 deletions primedev/core/filesystem/rpakfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,15 @@ void PakLoadManager::UnloadDependentPaks(PakHandle handle)
static void HandlePakAliases(std::string& originalPath)
{
// convert the pak being loaded to its aliased one, e.g. aliasing mp_hub_timeshift => sp_hub_timeshift
for (int64_t i = g_pModManager->m_LoadedMods.size() - 1; i > PakHandle::INVALID; i--)
for (int64_t i = g_pModManager->m_LoadedMods.size() - 1; i > -1; i--)
{
Mod* mod = &g_pModManager->m_LoadedMods[i];
if (!mod->m_bEnabled)
continue;

if (mod->RpakAliases.find(originalPath) != mod->RpakAliases.end())
{
originalPath = (mod->m_ModDirectory / "paks" / mod->RpakAliases[originalPath]).string();
originalPath = mod->RpakAliases[originalPath];
return;
}
}
Expand Down
1 change: 0 additions & 1 deletion primedev/core/sourceinterface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "sourceinterface.h"
#include "logging/sourceconsole.h"

// really wanted to do a modular callback system here but honestly couldn't be bothered so hardcoding stuff for now: todo later
Expand Down
32 changes: 0 additions & 32 deletions primedev/core/sourceinterface.h

This file was deleted.

6 changes: 6 additions & 0 deletions primedev/core/tier1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

#define CREATEINTERFACE_PROCNAME "CreateInterface"

enum class InterfaceStatus : int
{
IFACE_OK = 0,
IFACE_FAILED,
};

typedef void* (*CreateInterfaceFn)(const char* pName, int* pReturnCode);

CMemory Sys_GetFactoryPtr(const std::string& svModuleName, const std::string& svFact);
22 changes: 9 additions & 13 deletions primedev/dedicated/dedicated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "masterserver/masterserver.h"
#include "util/printcommands.h"

AUTOHOOK_INIT()

bool IsDedicatedServer()
{
static bool result = strstr(GetCommandLineA(), "-dedicated");
Expand Down Expand Up @@ -114,10 +112,8 @@ DWORD WINAPI ConsoleInputThread(PVOID pThreadParameter)
return 0;
}

// clang-format off
AUTOHOOK(IsGameActiveWindow, engine.dll + 0x1CDC80,
bool,, ())
// clang-format on
static bool (*o_pIsGameActiveWindow)() = nullptr;
static bool h_IsGameActiveWindow()
{
return true;
}
Expand All @@ -126,7 +122,8 @@ ON_DLL_LOAD_DEDI_RELIESON("engine.dll", DedicatedServer, ServerPresence, (CModul
{
spdlog::info("InitialiseDedicated");

AUTOHOOK_DISPATCH_MODULE(engine.dll)
o_pIsGameActiveWindow = module.Offset(0x1CDC80).RCast<decltype(o_pIsGameActiveWindow)>();
HookAttach(&(PVOID&)o_pIsGameActiveWindow, (PVOID)h_IsGameActiveWindow);

// Host_Init
// prevent a particle init that relies on client dll
Expand Down Expand Up @@ -270,12 +267,10 @@ ON_DLL_LOAD_DEDI("tier0.dll", DedicatedServerOrigin, (CModule module))
module.GetExportedFunction("Tier0_InitOrigin").Patch("C3");
}

// clang-format off
AUTOHOOK(PrintSquirrelError, server.dll + 0x794D0,
void, __fastcall, (void* sqvm))
// clang-format on
static void(__fastcall* o_pPrintSquirrelError)(void* sqvm) = nullptr;
static void __fastcall h_PrintSquirrelError(void* sqvm)
{
PrintSquirrelError(sqvm);
o_pPrintSquirrelError(sqvm);

// close dedicated server if a fatal error is hit
// atm, this will crash if not aborted, so this just closes more gracefully
Expand All @@ -289,7 +284,8 @@ void, __fastcall, (void* sqvm))

ON_DLL_LOAD_DEDI("server.dll", DedicatedServerGameDLL, (CModule module))
{
AUTOHOOK_DISPATCH_MODULE(server.dll)
o_pPrintSquirrelError = module.Offset(0x794D0).RCast<decltype(o_pPrintSquirrelError)>();
HookAttach(&(PVOID&)o_pPrintSquirrelError, (PVOID)h_PrintSquirrelError);

if (CommandLine()->CheckParm("-nopakdedi"))
{
Expand Down
25 changes: 16 additions & 9 deletions primedev/dedicated/dedicatedmaterialsystem.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#include "dedicated.h"
#include "core/tier0.h"

AUTOHOOK_INIT()

// clang-format off
AUTOHOOK(D3D11CreateDevice, materialsystem_dx11.dll + 0xD9A0E,
HRESULT, __stdcall, (
static HRESULT(__stdcall* o_pD3D11CreateDevice)(
void* pAdapter,
int DriverType,
HMODULE Software,
UINT Flags,
int* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
void** ppDevice,
int* pFeatureLevel,
void** ppImmediateContext) = nullptr;
static HRESULT __stdcall h_D3D11CreateDevice(
void* pAdapter,
int DriverType,
HMODULE Software,
Expand All @@ -15,8 +22,7 @@ HRESULT, __stdcall, (
UINT SDKVersion,
void** ppDevice,
int* pFeatureLevel,
void** ppImmediateContext))
// clang-format on
void** ppImmediateContext)
{
// note: this is super duper temp pretty much just messing around with it
// does run surprisingly well on dedi for a software driver tho if you ignore the +1gb ram usage at times, seems like dedi doesn't
Expand All @@ -26,13 +32,14 @@ HRESULT, __stdcall, (
if (CommandLine()->CheckParm("-softwared3d11"))
DriverType = 5; // D3D_DRIVER_TYPE_WARP

return D3D11CreateDevice(
return o_pD3D11CreateDevice(
pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, ppDevice, pFeatureLevel, ppImmediateContext);
}

ON_DLL_LOAD_DEDI("materialsystem_dx11.dll", DedicatedServerMaterialSystem, (CModule module))
{
AUTOHOOK_DISPATCH()
o_pD3D11CreateDevice = module.Offset(0xD9A0E).RCast<decltype(o_pD3D11CreateDevice)>();
HookAttach(&(PVOID&)o_pD3D11CreateDevice, (PVOID)h_D3D11CreateDevice);

// CMaterialSystem::FindMaterial
// make the game always use the error material
Expand Down
64 changes: 34 additions & 30 deletions primedev/engine/hoststate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
#include "squirrel/squirrel.h"
#include "plugins/pluginmanager.h"

AUTOHOOK_INIT()

CHostState* g_pHostState;

std::string sLastMode;

VAR_AT(engine.dll + 0x13FA6070, ConVar*, Cvar_hostport);
FUNCTION_AT(engine.dll + 0x1232C0, void, __fastcall, _Cmd_Exec_f, (const CCommand& arg, bool bOnlyIfExists, bool bUseWhitelists));
static ConVar* Cvar_hostport = nullptr;
static void(__fastcall* _Cmd_Exec_f)(const CCommand& arg, bool bOnlyIfExists, bool bUseWhitelists) = nullptr;

void ServerStartingOrChangingMap()
{
Expand Down Expand Up @@ -53,10 +51,8 @@ void ServerStartingOrChangingMap()
g_pServerAuthentication->m_bStartingLocalSPGame = false;
}

// clang-format off
AUTOHOOK(CHostState__State_NewGame, engine.dll + 0x16E7D0,
void, __fastcall, (CHostState* self))
// clang-format on
static void(__fastcall* o_pCHostState__State_NewGame)(CHostState* self) = nullptr;
static void __fastcall h_CHostState__State_NewGame(CHostState* self)
{
spdlog::info("HostState: NewGame");

Expand All @@ -70,7 +66,7 @@ void, __fastcall, (CHostState* self))
ServerStartingOrChangingMap();

double dStartTime = Plat_FloatTime();
CHostState__State_NewGame(self);
o_pCHostState__State_NewGame(self);
spdlog::info("loading took {}s", Plat_FloatTime() - dStartTime);

// setup server presence
Expand All @@ -82,10 +78,8 @@ void, __fastcall, (CHostState* self))
g_pServerAuthentication->m_bNeedLocalAuthForNewgame = false;
}

// clang-format off
AUTOHOOK(CHostState__State_LoadGame, engine.dll + 0x16E730,
void, __fastcall, (CHostState* self))
// clang-format on
static void(__fastcall* o_pCHostState__State_LoadGame)(CHostState* self) = nullptr;
static void __fastcall h_CHostState__State_LoadGame(CHostState* self)
{
// singleplayer server starting
// useless in 99% of cases but without it things could potentially break very much
Expand All @@ -100,7 +94,7 @@ void, __fastcall, (CHostState* self))
g_pServerAuthentication->m_bStartingLocalSPGame = true;

double dStartTime = Plat_FloatTime();
CHostState__State_LoadGame(self);
o_pCHostState__State_LoadGame(self);
spdlog::info("loading took {}s", Plat_FloatTime() - dStartTime);

// no server presence, can't do it because no map name in hoststate
Expand All @@ -109,32 +103,28 @@ void, __fastcall, (CHostState* self))
g_pServerAuthentication->m_bNeedLocalAuthForNewgame = false;
}

// clang-format off
AUTOHOOK(CHostState__State_ChangeLevelMP, engine.dll + 0x16E520,
void, __fastcall, (CHostState* self))
// clang-format on
static void(__fastcall* o_pCHostState__State_ChangeLevelMP)(CHostState* self) = nullptr;
static void __fastcall h_CHostState__State_ChangeLevelMP(CHostState* self)
{
spdlog::info("HostState: ChangeLevelMP");

ServerStartingOrChangingMap();

double dStartTime = Plat_FloatTime();
CHostState__State_ChangeLevelMP(self);
o_pCHostState__State_ChangeLevelMP(self);
spdlog::info("loading took {}s", Plat_FloatTime() - dStartTime);

g_pServerPresence->SetMap(g_pHostState->m_levelName);
}

// clang-format off
AUTOHOOK(CHostState__State_GameShutdown, engine.dll + 0x16E640,
void, __fastcall, (CHostState* self))
// clang-format on
static void(__fastcall* o_pCHostState__State_GameShutdown)(CHostState* self) = nullptr;
static void __fastcall h_CHostState__State_GameShutdown(CHostState* self)
{
spdlog::info("HostState: GameShutdown");

g_pServerPresence->DestroyPresence();

CHostState__State_GameShutdown(self);
o_pCHostState__State_GameShutdown(self);

// run gamemode cleanup cfg now instead of when we start next map
if (sLastMode.length())
Expand All @@ -153,12 +143,10 @@ void, __fastcall, (CHostState* self))
}
}

// clang-format off
AUTOHOOK(CHostState__FrameUpdate, engine.dll + 0x16DB00,
void, __fastcall, (CHostState* self, double flCurrentTime, float flFrameTime))
// clang-format on
static void(__fastcall* o_pCHostState__FrameUpdate)(CHostState* self, double flCurrentTime, float flFrameTime) = nullptr;
static void __fastcall h_CHostState__FrameUpdate(CHostState* self, double flCurrentTime, float flFrameTime)
{
CHostState__FrameUpdate(self, flCurrentTime, flFrameTime);
o_pCHostState__FrameUpdate(self, flCurrentTime, flFrameTime);

if (*g_pServerState == server_state_t::ss_active)
{
Expand All @@ -184,7 +172,23 @@ void, __fastcall, (CHostState* self, double flCurrentTime, float flFrameTime))

ON_DLL_LOAD_RELIESON("engine.dll", HostState, ConVar, (CModule module))
{
AUTOHOOK_DISPATCH()
o_pCHostState__State_NewGame = module.Offset(0x16E7D0).RCast<decltype(o_pCHostState__State_NewGame)>();
HookAttach(&(PVOID&)o_pCHostState__State_NewGame, (PVOID)h_CHostState__State_NewGame);

o_pCHostState__State_LoadGame = module.Offset(0x16E730).RCast<decltype(o_pCHostState__State_LoadGame)>();
HookAttach(&(PVOID&)o_pCHostState__State_LoadGame, (PVOID)h_CHostState__State_LoadGame);

o_pCHostState__State_ChangeLevelMP = module.Offset(0x16E520).RCast<decltype(o_pCHostState__State_ChangeLevelMP)>();
HookAttach(&(PVOID&)o_pCHostState__State_ChangeLevelMP, (PVOID)h_CHostState__State_ChangeLevelMP);

o_pCHostState__State_GameShutdown = module.Offset(0x16E640).RCast<decltype(o_pCHostState__State_GameShutdown)>();
HookAttach(&(PVOID&)o_pCHostState__State_GameShutdown, (PVOID)h_CHostState__State_GameShutdown);

o_pCHostState__FrameUpdate = module.Offset(0x16DB00).RCast<decltype(o_pCHostState__FrameUpdate)>();
HookAttach(&(PVOID&)o_pCHostState__FrameUpdate, (PVOID)h_CHostState__FrameUpdate);

Cvar_hostport = module.Offset(0x13FA6070).RCast<decltype(Cvar_hostport)>();
_Cmd_Exec_f = module.Offset(0x1232C0).RCast<decltype(_Cmd_Exec_f)>();

g_pHostState = module.Offset(0x7CF180).RCast<CHostState*>();
}
1 change: 0 additions & 1 deletion primedev/logging/sourceconsole.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "core/sourceinterface.h"
#include "spdlog/sinks/base_sink.h"
#include <map>

Expand Down
1 change: 1 addition & 0 deletions primedev/plugins/interfaces/interface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <string.h>
#include "core/tier1.h"
#include "interface.h"

InterfaceReg* s_pInterfaceRegs;
Expand Down
1 change: 1 addition & 0 deletions primedev/plugins/interfaces/sys/ISys.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "core/tier1.h"
#include "plugins/interfaces/interface.h"
#include "ISys.h"
#include "plugins/plugins.h"
Expand Down
2 changes: 0 additions & 2 deletions primedev/plugins/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "pluginmanager.h"
#include "squirrel/squirrel.h"
#include "util/wininfo.h"
#include "core/sourceinterface.h"
#include "logging/logging.h"
#include "dedicated/dedicated.h"

Expand Down Expand Up @@ -219,7 +218,6 @@ void Plugin::OnSqvmCreated(CSquirrelVM* sqvm) const

void Plugin::OnSqvmDestroying(CSquirrelVM* sqvm) const
{
NS::log::PLUGINSYS->info("destroying sqvm {}", sqvm->vmContext);
m_callbacks->OnSqvmDestroying(sqvm);
}

Expand Down
2 changes: 1 addition & 1 deletion primedev/plugins/plugins.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "core/sourceinterface.h"
#include "core/tier1.h"
#include "plugins/interfaces/interface.h"
#include "plugins/interfaces/IPluginId.h"
#include "plugins/interfaces/IPluginCallbacks.h"
Expand Down

0 comments on commit a14fd8b

Please sign in to comment.