Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
IntriguingTiles committed Nov 30, 2023
2 parents 885e83d + 909ded8 commit 59e493e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Linux/installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ install() {
cp HLFixes.so "$1/sw.fx"

# only make a backup if the current launcher is seemingly okay
if ! [[ -f "$1/hl_linux.bak" ]] && grep -Fxq 'hw.so' "$path/hl_linux"; then
if grep -Fxq 'hw.so' "$path/hl_linux"; then
echo Making a backup of the launcher...
cp "$1/hl_linux" "$1/hl_linux.bak"
fi
Expand Down
89 changes: 81 additions & 8 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 @@ -41,9 +42,13 @@ typedef void *(*_dlopen)(const char *__file, int __mode);
typedef void (*_SetEngineDLL)(const char **ppEngineDLL);
typedef bool (*_Cvar_HookVariable)(char *var_name, cvarhook_t *pHook);
typedef void (*R_Init)();
typedef void (*Cmd_ExecuteStringWithPrivilegeCheck)(const char *text, int bIsPrivileged, int src);
typedef void (*_Cmd_ExecuteString)(char* text, int src);
typedef void (*PlayStartupSequence)(void *_this);

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 @@ -52,6 +57,9 @@ Q_strncmp orig_Q_strncmp = nullptr;
_SetEngineDLL SetEngineDLL = nullptr;
_Cvar_HookVariable Cvar_HookVariable = nullptr;
R_Init orig_R_Init = nullptr;
Cmd_ExecuteStringWithPrivilegeCheck orig_Cmd_ExecuteStringWithPrivilegeCheck = nullptr;
_Cmd_ExecuteString Cmd_ExecuteString = nullptr;
PlayStartupSequence orig_PlayStartupSequence = nullptr;

bool *gl_texsort = nullptr;
void *engine = nullptr;
Expand All @@ -65,19 +73,28 @@ bool fixOverbright = true;
bool fixMusic = true;
bool fixStartupMusic = true;
bool fixSky = true;
bool fixStartupVideoMusic = true;
bool isPreAnniversary = false;
bool finishedStartupVideos = false;

std::unordered_map<std::string_view, u32> engineSymbols;
std::unordered_map<std::string_view, u32> gameuiSymbols;
std::unordered_map<std::string_view, u32> launcherSymbols;

funchook_t *engineFunchook;
funchook_t *dlopenFunchook;
funchook_t *engineExecuteFunchook;

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 +130,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,16 +159,38 @@ 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);
}

void hooked_Cmd_ExecuteStringWithPrivilegeCheck(const char *text, int bIsPrivileged, int src)
{
if (!finishedStartupVideos && strcmp(text, "mp3 loop media/gamestartup.mp3") == 0)
return;
orig_Cmd_ExecuteStringWithPrivilegeCheck(text, bIsPrivileged, src);
}

void hooked_PlayStartupSequence(void *_this)
{
orig_PlayStartupSequence(_this);

if (!finishedStartupVideos)
{
finishedStartupVideos = true;
orig_Cmd_ExecuteStringWithPrivilegeCheck("mp3 loop media/gamestartup.mp3", true, 1);
Cmd_ExecuteString("mp3 loop media/gamestartup.mp3", 1);
funchook_uninstall(engineExecuteFunchook, 0);
funchook_destroy(engineExecuteFunchook);
}
}

extern "C" void *hooked_dlopen(const char *__file, int __mode)
{
auto ret = orig_dlopen(__file, __mode);
Expand Down Expand Up @@ -227,31 +272,58 @@ 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");
orig_Cmd_ExecuteStringWithPrivilegeCheck = (Cmd_ExecuteStringWithPrivilegeCheck)getEngineSymbol("Cmd_ExecuteStringWithPrivilegeCheck.part.3");
Cmd_ExecuteString = (_Cmd_ExecuteString)getEngineSymbol("Cmd_ExecuteString");
orig_PlayStartupSequence = (PlayStartupSequence)getEngineSymbol("_ZN17CVideoMode_Common19PlayStartupSequenceEv");

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)
funchook_prepare(engineFunchook, (void **)&orig_R_Init, (void *)hooked_R_Init);
{
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);
}
}
}

if (!isPreAnniversary && fixStartupVideoMusic)
{
engineExecuteFunchook = funchook_create();
funchook_prepare(engineExecuteFunchook, (void **)&orig_Cmd_ExecuteStringWithPrivilegeCheck, (void *)hooked_Cmd_ExecuteStringWithPrivilegeCheck);
funchook_install(engineExecuteFunchook, 0);

funchook_prepare(engineFunchook, (void **)&orig_PlayStartupSequence, (void *)hooked_PlayStartupSequence);
}

funchook_prepare(engineFunchook, (void **)&orig_Host_Version_f, (void *)hooked_Host_Version_f);
funchook_install(engineFunchook, 0);
}
Expand All @@ -269,6 +341,7 @@ static int init(int argc, char **argv, char **env)
fixSaves = !checkArg("--no-quicksave-fix", argc, argv);
fixOverbright = !checkArg("--no-overbright-fix", argc, argv);
fixSky = !checkArg("--no-sky-fix", argc, argv);
fixStartupVideoMusic = !checkArg("--no-startup-video-music-fix", argc, argv);

return 0;
}
Expand Down

0 comments on commit 59e493e

Please sign in to comment.