From af1f4f460773b67f90d420467f48dfb90c6d5299 Mon Sep 17 00:00:00 2001 From: Nick Hastings Date: Thu, 26 Oct 2023 13:15:01 -0400 Subject: [PATCH] Don't attempt to load on Source 2 games if neither -dedicated nor -insecure are present --- loader/gamedll.cpp | 9 +++++ loader/loader.cpp | 69 +--------------------------------- loader/utility.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++ loader/utility.h | 4 ++ 4 files changed, 106 insertions(+), 68 deletions(-) diff --git a/loader/gamedll.cpp b/loader/gamedll.cpp index 82d476a5..f2928775 100644 --- a/loader/gamedll.cpp +++ b/loader/gamedll.cpp @@ -758,6 +758,15 @@ mm_PatchConnect(bool patch) void * mm_GameDllRequest(const char *name, int *ret) { + if (strncmp(name, "Source2Server", 13) == 0 && !mm_GetCommandArgument("-dedicated") && !mm_GetCommandArgument("-insecure")) + { + mm_LogFatal("Metamod:Source requires -dedicated or -insecure on the command line to be able to load"); + if (ret != nullptr) + *ret = 1; // IFACE_FAILED + + return nullptr; + } + if (strncmp(name, "Source2ServerConfig", 19) == 0) { g_is_source2 = true; diff --git a/loader/loader.cpp b/loader/loader.cpp index d39c6271..c8a21336 100644 --- a/loader/loader.cpp +++ b/loader/loader.cpp @@ -211,74 +211,7 @@ typedef const char *(*GetGameInfoStringFn)(const char *pszKeyName, const char *p void mm_GetGameName(char *buffer, size_t size) { - buffer[0] = '\0'; - -#if defined _WIN32 - static char game[128]; - - LPWSTR pCmdLine = GetCommandLineW(); - int argc; - LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc); - for (int i = 0; i < argc; ++i) - { - if (wcscmp(wargv[i], L"-game") == 0) - { - if (++i >= argc) - break; - - wcstombs(buffer, wargv[i], size); - buffer[size-1] = '\0'; - } - } - - LocalFree(wargv); - -#elif defined __APPLE__ - int argc = *_NSGetArgc(); - char **argv = *_NSGetArgv(); - for (int i = 0; i < argc; ++i) - { - if (strcmp(argv[i], "-game") == 0) - { - if (++i >= argc) - break; - - strncpy(buffer, argv[i], size); - buffer[size-1] = '\0'; - } - } - -#elif defined __linux__ - FILE *pFile = fopen("/proc/self/cmdline", "rb"); - if (pFile) - { - char *arg = NULL; - size_t argsize = 0; - bool bNextIsGame = false; - - while (getdelim(&arg, &argsize, 0, pFile) != -1) - { - if (bNextIsGame) - { - strncpy(buffer, arg, size); - buffer[size-1] = '\0'; - bNextIsGame = false; - } - - if (strcmp(arg, "-game") == 0) - { - bNextIsGame = true; - } - } - - free(arg); - fclose(pFile); - } -#else -#error unsupported platform -#endif - - if (buffer[0] == 0) + if (!mm_GetCommandArgument("-game", buffer, size)) { char tier0_path[PLATFORM_MAX_PATH]; #ifdef _WIN32 diff --git a/loader/utility.cpp b/loader/utility.cpp index d70cdddd..053afaed 100644 --- a/loader/utility.cpp +++ b/loader/utility.cpp @@ -632,3 +632,95 @@ void *mm_FindPattern(const void *libPtr, const char *pattern, size_t len) return NULL; } + +bool mm_GetCommandArgument(const char* argName, char* buffer, size_t maxlength) +{ + if (buffer) + buffer[0] = '\0'; + +#if defined _WIN32 + wchar_t wargName[256]; + mbstowcs(wargName, argName, sizeof(wargName) / sizeof(wchar_t)); + + LPWSTR pCmdLine = GetCommandLineW(); + int argc; + LPWSTR* wargv = CommandLineToArgvW(pCmdLine, &argc); + + bool found = false; + for (int i = 0; i < argc; ++i) + { + if (wcscmp(wargv[i], wargName) == 0) + { + found = true; + + if ((++i) < argc && buffer && wargv[i][0] != '-') + { + wcstombs(buffer, wargv[i], maxlength); + buffer[maxlength - 1] = '\0'; + } + + break; + } + } + + LocalFree(wargv); + + return found; + +#elif defined __APPLE__ + int argc = *_NSGetArgc(); + char** argv = *_NSGetArgv(); + for (int i = 0; i < argc; ++i) + { + if (strcmp(argv[i], argName) == 0) + { + if ((++i) < argc && buffer && argv[i][0] != '-') + { + strncpy(buffer, argv[i], maxlength); + buffer[maxlength - 1] = '\0'; + } + + return true; + } + } + +#elif defined __linux__ + FILE* pFile = fopen("/proc/self/cmdline", "rb"); + if (pFile) + { + char* arg = nullptr; + size_t argsize = 0; + bool nextIsValue = false; + bool found = false; + + while (getdelim(&arg, &argsize, 0, pFile) != -1) + { + if (nextIsValue) + { + if (buffer && arg[0] != '-') + { + strncpy(buffer, arg, maxlength); + buffer[maxlength - 1] = '\0'; + } + + found = true; + break; + } + + if (strcmp(arg, argName) == 0) + { + nextIsValue = true; + } + } + + free(arg); + fclose(pFile); + + return found || nextIsValue; + } +#else +#error +#endif + + return false; +} diff --git a/loader/utility.h b/loader/utility.h index 25be3e65..74090615 100644 --- a/loader/utility.h +++ b/loader/utility.h @@ -69,5 +69,9 @@ mm_GetFileOfAddress(void *pAddr, char *buffer, size_t maxlength); extern void * mm_FindPattern(const void *libPtr, const char *pattern, size_t len); +// True if arg is present, false if not. If arg has no value, buffer will be set to an empty string. +extern bool +mm_GetCommandArgument(const char *argName, char *buffer = nullptr, size_t maxlength = 0); + #endif /* _INCLUDE_METAMOD_SOURCE_LOADER_UTILITY_H_ */