Skip to content

Commit

Permalink
Replace all code that uses the old method for game directory find wit…
Browse files Browse the repository at this point in the history
…h the new one
  • Loading branch information
SmileyAG committed Aug 11, 2024
1 parent 8c3686d commit 1ce4b8b
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 350 deletions.
338 changes: 141 additions & 197 deletions BunnymodXT/discord_integration.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions BunnymodXT/discord_integration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace discord_integration
{"bschapter3", "Captive Freight"},
{"bschapter4", "Focal Point"},
{"bschapter5", "Power Struggle"},
{"bschapter6", "Captive Freight"},
{"bschapter6", "Captive Freight / A Leap of Faith"},
{"bschapter7", "Outro"},
{"bschapter8", "Hazard Course"},
{"bschapter9", "Living Quarters Outbound"},
Expand Down Expand Up @@ -830,7 +830,7 @@ namespace discord_integration
{"paraschool", "hrpchapter10"},
};

const std::unordered_set<std::string> urbicide_maps {
const std::unordered_set<std::string_view> urbicide_maps {
"urbicide1"s,
"urbicide2"s,
"urbicide3"s,
Expand Down
34 changes: 30 additions & 4 deletions BunnymodXT/helper_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace helper_functions
{
auto &cl = ClientDLL::GetInstance();
auto &sv = ServerDLL::GetInstance();
auto &hw = HwDLL::GetInstance();

void com_fixslashes(std::string &str)
{
// https://github.com/ValveSoftware/halflife/blob/c7240b965743a53a29491dd49320c88eecf6257b/game_shared/bot/nav_file.cpp#L680
Expand Down Expand Up @@ -44,7 +48,6 @@ namespace helper_functions
void disable_vsync()
{
#ifdef _WIN32
auto &hw = HwDLL::GetInstance();
if (hw.check_vsync)
{
const bool bxtDisableVSync = getenv("BXT_DISABLE_VSYNC");
Expand All @@ -63,7 +66,7 @@ namespace helper_functions

void _com_filebase(const char *in, int &len, int &start)
{
int len, start, end;
int end;

len = strlen(in);

Expand Down Expand Up @@ -121,9 +124,27 @@ namespace helper_functions
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return tolower(c); });
}

double ret_bxt_time()
{
const auto& gt = CustomHud::GetTime();
return (gt.hours * 60 * 60) + (gt.minutes * 60) + gt.seconds + (gt.milliseconds / 1000);
}

void reset_gamedir()
{
hw.gamedir.clear();
hw.gamedir_lw.clear();
hw.GameDirMatchID = hw.GameDirStartsWithID = -1;
}

void set_gamedir_starts_with()
{
#define FIND_GAMEDIR_STARTS_WITH(name, id)
#define FIND_GAMEDIR_STARTS_WITH(name, id) \
if (!hw.GetGameDir().compare(0, sizeof(#name) - 1, #name)) \
{ \
hw.GameDirStartsWithID = BXT_CONCAT(GAMEDIR_STARTS_WITH_, id); \
return; \
}

FIND_GAMEDIR_STARTS_WITH(valve, HL)
FIND_GAMEDIR_STARTS_WITH(gearbox, OPFOR)
Expand All @@ -143,7 +164,12 @@ namespace helper_functions

void set_gamedir_match()
{
#define FIND_GAMEDIR_MATCH(name, id)
#define FIND_GAMEDIR_MATCH(name, id) \
if (!hw.GetGameDir().compare(#name)) \
{ \
hw.GameDirMatchID = BXT_CONCAT(GAMEDIR_MATCH_, id); \
return; \
}

FIND_GAMEDIR_MATCH(valve, HL)
FIND_GAMEDIR_MATCH(gearbox, OPFOR)
Expand Down
3 changes: 3 additions & 0 deletions BunnymodXT/helper_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace helper_functions
void convert_to_lowercase(const char *str);
void convert_to_lowercase(std::string &str);

double ret_bxt_time();

void reset_gamedir();
void set_gamedir_starts_with();
void set_gamedir_match();

Expand Down
88 changes: 5 additions & 83 deletions BunnymodXT/modules/ClientDLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,84 +1157,6 @@ void ClientDLL::StudioAdjustViewmodelAttachments(Vector &vOrigin)
vOrigin = last_vieworg + vOut;
}

void ClientDLL::FileBase(const char *in, char *out)
{
int len, start, end;

len = strlen(in);

// scan backward for '.'
end = len - 1;
while (0 != end && in[end] != '.' && in[end] != '/' && in[end] != '\\')
end--;

if (in[end] != '.') // no '.', copy to end
end = len - 1;
else
end--; // Found ',', copy to left of '.'

// Scan backward for '/'
start = len - 1;
while (start >= 0 && in[start] != '/' && in[start] != '\\')
start--;

if (in[start] != '/' && in[start] != '\\')
start = 0;
else
start++;

// Length of new sting
len = end - start + 1;

// Copy partial string
strncpy(out, &in[start], len);
// Terminate it
out[len] = 0;
}

void ClientDLL::ConvertToLowerCase(const char *str)
{
unsigned char *str_lw = (unsigned char *)str;
while (*str_lw) {
*str_lw = tolower(*str_lw);
str_lw++;
}
}

bool ClientDLL::DoesGameDirMatch(const char *game)
{
if (!pEngfuncs)
return false;

const char *gameDir = pEngfuncs->pfnGetGameDirectory();
char gd[1024];

if (gameDir && gameDir[0])
{
FileBase(gameDir, gd);
ConvertToLowerCase(gd);
}

return !std::strcmp(gd, game);
}

bool ClientDLL::DoesGameDirContain(const char *game)
{
if (!pEngfuncs)
return false;

const char *gameDir = pEngfuncs->pfnGetGameDirectory();
char gd[1024];

if (gameDir && gameDir[0])
{
FileBase(gameDir, gd);
ConvertToLowerCase(gd);
}

return std::strstr(gd, game);
}

size_t ClientDLL::GetMapName(char* dest, size_t count)
{
auto map_path = pEngfuncs->pfnGetLevelName();
Expand Down Expand Up @@ -1265,7 +1187,7 @@ bool ClientDLL::DoesMapNameMatch(const char *map)
GetMapName(map_name, ARRAYSIZE_HL(map_name));

if (map_name[0])
ConvertToLowerCase(map_name);
helper_functions::convert_to_lowercase(map_name);

return !std::strcmp(map_name, map);
}
Expand All @@ -1280,9 +1202,9 @@ bool ClientDLL::DoesMapNameContain(const char *map)
GetMapName(map_name, ARRAYSIZE_HL(map_name));

if (map_name[0])
ConvertToLowerCase(map_name);
helper_functions::convert_to_lowercase(map_name);

return std::strstr(map_name, map);
return !std::strncmp(map_name, map, strlen(map));
}

void ClientDLL::SetAngleSpeedCap(bool capped)
Expand Down Expand Up @@ -1861,7 +1783,7 @@ HOOK_DEF_1(ClientDLL, void, __cdecl, CStudioModelRenderer__StudioSetupBones_Linu
{
ptrdiff_t offpCurrentEntity_Linux;
ptrdiff_t offpStudioHeader_Linux;
if (DoesGameDirMatch("dod")) {
if (IsGameDirMatch(MP_DOD)) {
offpCurrentEntity_Linux = 52;
offpStudioHeader_Linux = 72;
} else {
Expand Down Expand Up @@ -1988,7 +1910,7 @@ HOOK_DEF_1(ClientDLL, void, __fastcall, CStudioModelRenderer__StudioRenderModel,
HOOK_DEF_1(ClientDLL, void, __cdecl, CStudioModelRenderer__StudioRenderModel_Linux, void*, thisptr)
{
ptrdiff_t offpCurrentEntity_Linux;
if (DoesGameDirMatch("dod"))
if (IsGameDirMatch(MP_DOD))
offpCurrentEntity_Linux = 52;
else
offpCurrentEntity_Linux = 44;
Expand Down
6 changes: 0 additions & 6 deletions BunnymodXT/modules/ClientDLL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ class ClientDLL : public IHookableNameFilter

void StudioAdjustViewmodelAttachments(Vector &vOrigin);

bool DoesGameDirMatch(const char *game);
bool DoesGameDirContain(const char *game);

size_t GetMapName(char* dest, size_t count);
bool DoesMapNameMatch(const char *map);
bool DoesMapNameContain(const char *map);
Expand Down Expand Up @@ -110,9 +107,6 @@ class ClientDLL : public IHookableNameFilter

void SetSpeedScaling(bool scaled);

void FileBase(const char *in, char *out);
void ConvertToLowerCase(const char *str);

void SetupTraceVectors(float start[3], float end[3]);

private:
Expand Down
48 changes: 30 additions & 18 deletions BunnymodXT/modules/HwDLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,8 @@ void HwDLL::Clear()
pEngStudio = nullptr;
pEngineAPI = nullptr;

helper_functions::reset_gamedir();

registeredVarsAndCmds = false;
autojump = false;
ducktap = false;
Expand Down Expand Up @@ -3136,7 +3138,12 @@ struct HwDLL::Cmd_BXT_Debug_Dump

static void handler(const char *name)
{
if (!strcmp(name, "sizeof"))
if (!strcmp(name, "gamedir"))
{
auto &hw = HwDLL::GetInstance();
hw.ORIG_Con_Printf("%d, %d, %s, %s\n", hw.GameDirMatchID, hw.GameDirStartsWithID, hw.gamedir.c_str(), hw.gamedir_lw.c_str());
}
else if (!strcmp(name, "sizeof"))
{
auto &hw = HwDLL::GetInstance();
std::ostringstream ss;
Expand Down Expand Up @@ -3401,7 +3408,6 @@ struct HwDLL::Cmd_BXT_CH_CheckPoint_Create
static void handler()
{
auto &hw = HwDLL::GetInstance();
auto &cl = ClientDLL::GetInstance();

auto pl = hw.GetPlayerEdict();

Expand Down Expand Up @@ -3506,7 +3512,7 @@ struct HwDLL::Cmd_BXT_CH_CheckPoint_GoTo
pl->v.origin = cp_origin;

// for CS 1.6 stamina reset
if (hw.is_cstrike_dir)
if (IsGameDirMatch(MP_CS))
pl->v.fuser2 = 0;

pl->v.gravity = cp_gravity;
Expand Down Expand Up @@ -5744,7 +5750,7 @@ void HwDLL::SetTASLogging(bool enabled)
return;
}
const int buildNumber = ORIG_build_number ? ORIG_build_number() : -1;
const char *gameDir = ClientDLL::GetInstance().pEngfuncs->pfnGetGameDirectory();
const char *gameDir = GetGameDir(false).c_str();
logWriter.StartLog(tasLogFile, BUNNYMODXT_VERSION, buildNumber, gameDir);
tasLogging = true;
ORIG_Con_Printf("Started TAS logging into %s\n", filename.c_str());
Expand Down Expand Up @@ -6833,7 +6839,7 @@ bool HwDLL::GetNextMovementFrame(HLTAS::Frame& f)
return false;
}

std::string GetGameDir(bool lowercase)
std::string HwDLL::GetGameDir(bool lowercase)
{
if (gamedir.empty())
{
Expand All @@ -6860,14 +6866,25 @@ std::string GetGameDir(bool lowercase)
}
}

if (lowercase) ? return gamedir_lw; : return gamedir;
if (lowercase)
return gamedir_lw;
else
return gamedir;
}

std::string GetGameDir()
std::string HwDLL::GetGameDir()
{
return GetGameDir(true);
}

int HwDLL::GetBuildNumber()
{
if (ORIG_build_number)
return ORIG_build_number();
else
return 0;
}

HLStrafe::PlayerData HwDLL::GetPlayerData()
{
HLStrafe::PlayerData player{};
Expand Down Expand Up @@ -6951,11 +6968,9 @@ HLStrafe::MovementVars HwDLL::GetMovementVars()
vars.Bounce = CVars::sv_bounce.GetFloat();
vars.Bhopcap = CVars::bxt_bhopcap.GetBool();

static bool is_paranoia_dir = cl.DoesGameDirMatch("paranoia");
is_tfc_dir = cl.DoesGameDirMatch("tfc");
is_cstrike_dir = cl.DoesGameDirMatch("cstrike") || cl.DoesGameDirMatch("czero");
bool is_cstrike_dir = IsGameDirMatch(MP_CS) || IsGameDirMatch(MP_CSCZ);

if (is_paranoia_dir)
if (IsGameDirMatch(PARANOIA))
vars.Maxspeed = cl.pEngfuncs->GetClientMaxspeed() * CVars::sv_maxspeed.GetFloat() / 100.0f; // GetMaxSpeed is factor here
else if (cl.pEngfuncs && (cl.pEngfuncs->GetClientMaxspeed() > 0.0f) && (CVars::sv_maxspeed.GetFloat() > cl.pEngfuncs->GetClientMaxspeed()))
vars.Maxspeed = cl.pEngfuncs->GetClientMaxspeed(); // Get true maxspeed in other mods (example: CS 1.6)
Expand All @@ -6972,7 +6987,7 @@ HLStrafe::MovementVars HwDLL::GetMovementVars()
vars.BhopcapMaxspeedScale = 1.7f;
}

if (!is_cstrike_dir && !is_tfc_dir)
if (!is_cstrike_dir && !IsGameDirMatch(MP_TFC))
vars.UseSlow = true;

if (svs->maxclients >= 1) {
Expand Down Expand Up @@ -8054,7 +8069,7 @@ HOOK_DEF_1(HwDLL, void, __cdecl, VGuiWrap_Paint, int, paintAll)

HOOK_DEF_3(HwDLL, int, __cdecl, DispatchDirectUserMsg, char*, pszName, int, iSize, void*, pBuf)
{
if (ClientDLL::GetInstance().DoesGameDirContain("czeror") && !std::strcmp(pszName, "InitHUD"))
if (IsGameDirStartsWith(CSCZDS) && !std::strcmp(pszName, "InitHUD"))
return ORIG_DispatchDirectUserMsg(0, iSize, pBuf);
else
return ORIG_DispatchDirectUserMsg(pszName, iSize, pBuf);
Expand Down Expand Up @@ -8235,7 +8250,7 @@ HOOK_DEF_0(HwDLL, void, __cdecl, R_DrawParticles)

HOOK_DEF_0(HwDLL, int, __cdecl, BUsesSDLInput)
{
if (ClientDLL::GetInstance().DoesGameDirMatch("bshift_cutsceneless") || CVars::bxt_fix_mouse_horizontal_limit.GetBool())
if (IsGameDirStartsWith(BSHIFT) || CVars::bxt_fix_mouse_horizontal_limit.GetBool())
return true;
else
return ORIG_BUsesSDLInput();
Expand Down Expand Up @@ -8435,16 +8450,13 @@ HOOK_DEF_1(HwDLL, void, __cdecl, LoadThisDll, const char*, szDllFilename)
auto oldszDllFilename = szDllFilename;
std::string newszDllFilename;

auto &hw = HwDLL::GetInstance();
hw.is_cstrike_dir = ClientDLL::GetInstance().DoesGameDirMatch("cstrike");

if (boost::ends_with(szDllFilename, "metamod" DLL_EXTENSION))
{
EngineDevMsg("[hw dll] Metamod detected.\n");

bool is_failed = false;

if (hw.is_cstrike_dir)
if (IsGameDirMatch(MP_CS))
{
#ifdef _WIN32
const std::string cs_lib = "dlls\\mp";
Expand Down
Loading

0 comments on commit 1ce4b8b

Please sign in to comment.