Skip to content

Commit

Permalink
Linux: Pre-anniversary support
Browse files Browse the repository at this point in the history
  • Loading branch information
IntriguingTiles committed Nov 30, 2023
1 parent 3ffd459 commit 0a83c39
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions Linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static_assert(sizeof(cvarhook_t) == 0xC, "wrong size for cvarhook_t");
typedef int (*ConnectToServer)(void *_this, const char *game, int b, int c);
typedef bool (*SaveGameSlot)(char *save, char *comment);
typedef void *(*_CreateInterface)(const char *name, u32 *b);
typedef int (*R_BuildLightMap)(int a1, int a2, int a3);
typedef u32 (*_GetInteralCDAudio)();
typedef void (*Host_Version_f)();
typedef void (*_Con_Printf)(const char *format, ...);
Expand All @@ -44,6 +45,7 @@ typedef void (*R_Init)();

ConnectToServer orig_ConnectToServer = nullptr;
SaveGameSlot orig_SaveGameSlot = nullptr;
R_BuildLightMap orig_R_BuildLightMap = nullptr;
_dlopen orig_dlopen = nullptr;
_GetInteralCDAudio GetInteralCDAudio = nullptr; // "Interal" is a typo on valve's part
Host_Version_f orig_Host_Version_f = nullptr;
Expand All @@ -65,6 +67,7 @@ bool fixOverbright = true;
bool fixMusic = true;
bool fixStartupMusic = true;
bool fixSky = true;
bool isPreAnniversary = false;

std::unordered_map<std::string_view, u32> engineSymbols;
std::unordered_map<std::string_view, u32> gameuiSymbols;
Expand All @@ -78,6 +81,11 @@ u32 getEngineSymbol(const char *symbol)
return engineSymbols[symbol] + engineAddr;
}

bool hasEngineSymbol(const char *symbol)
{
return engineSymbols.find(symbol) != engineSymbols.end();
}

u32 getGameUISymbol(const char *symbol)
{
return gameuiSymbols[symbol] + gameuiAddr;
Expand Down Expand Up @@ -113,6 +121,12 @@ bool hooked_SaveGameSlot(char *save, char *comment)
return orig_SaveGameSlot(save, comment);
}

int hooked_R_BuildLightMap(int a1, int a2, int a3)
{
*gl_texsort = true;
return orig_R_BuildLightMap(a1, a2, a3);
}

int hooked_Q_strncmp(const char *s1, const char *s2, int count)
{
if (!strncmp(s1, "skycull", 7) && !strncmp(s2, "sky", 3))
Expand All @@ -136,12 +150,13 @@ void gl_use_shaders_callback(cvar_t *cvar)
}

cvarhook_t gl_use_shaders_hook = {
(void*)gl_use_shaders_callback,
(void *)gl_use_shaders_callback,
nullptr,
nullptr,
nullptr
};

void hooked_R_Init() {
void hooked_R_Init()
{
orig_R_Init();
Cvar_HookVariable("gl_use_shaders", &gl_use_shaders_hook);
}
Expand Down Expand Up @@ -227,27 +242,41 @@ extern "C" void *CreateInterface(const char *name, u32 *b)
orig_Q_strncmp = (Q_strncmp)getEngineSymbol("Q_strncmp");
orig_dlopen = dlopen;
Cvar_HookVariable = (_Cvar_HookVariable)getEngineSymbol("Cvar_HookVariable");
if (isHW)
isPreAnniversary = !hasEngineSymbol("R_UsingShaders");

if (fixMusic)
{
dlopenFunchook = funchook_create();
funchook_prepare(dlopenFunchook, (void **)&orig_dlopen, (void *)hooked_dlopen);
funchook_install(dlopenFunchook, 0);
}

if (fixSaves)
funchook_prepare(engineFunchook, (void **)&orig_SaveGameSlot, (void *)hooked_SaveGameSlot);

if (isHW && fixOverbright)
{
if (isPreAnniversary)
{
funchook_prepare(engineFunchook, (void **)&orig_R_BuildLightMap, (void *)hooked_R_BuildLightMap);
}
else
{
funchook_prepare(engineFunchook, (void **)&orig_R_Init, (void *)hooked_R_Init);
}
}

if (isHW && fixSky)
{
u32 addr_R_NewMap = getEngineSymbol("R_NewMap");
if (orig_Q_strncmp && *(u8 *)(addr_R_NewMap + 0x199) == 0xE8)
u32 offset = isPreAnniversary ? 0x17B : 0x199;
if (orig_Q_strncmp && *(u8 *)(addr_R_NewMap + offset) == 0xE8)
{
if (RelativeToAbsolute(*(u32 *)(addr_R_NewMap + 0x19A), addr_R_NewMap + 0x199 + 5) == (u32)orig_Q_strncmp)
if (RelativeToAbsolute(*(u32 *)(addr_R_NewMap + offset + 1), addr_R_NewMap + offset + 5) == (u32)orig_Q_strncmp)
{
u32 addr = AbsoluteToRelative((u32)hooked_Q_strncmp, addr_R_NewMap + 0x199 + 5);
MakePatch(addr_R_NewMap + 0x19A, (u8 *)&addr, 4);
u32 addr = AbsoluteToRelative((u32)hooked_Q_strncmp, addr_R_NewMap + offset + 5);
MakePatch(addr_R_NewMap + offset + 1, (u8 *)&addr, 4);
}
}
}
Expand Down

0 comments on commit 0a83c39

Please sign in to comment.