Skip to content

Commit

Permalink
Don't attempt to load on Source 2 games if neither -dedicated nor -in…
Browse files Browse the repository at this point in the history
…secure are present
  • Loading branch information
psychonic committed Oct 26, 2023
1 parent 6a05a21 commit e030118
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 68 deletions.
9 changes: 9 additions & 0 deletions loader/gamedll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
69 changes: 1 addition & 68 deletions loader/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 92 additions & 0 deletions loader/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions loader/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */

0 comments on commit e030118

Please sign in to comment.