1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2024-11-28 10:24:20 +01:00

Don't attempt to load on Source 2 games if neither -dedicated nor -insecure are present

This commit is contained in:
Nick Hastings 2023-10-26 13:15:01 -04:00
parent 6a05a21fd4
commit af1f4f4607
4 changed files with 106 additions and 68 deletions

View File

@ -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;

View File

@ -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

View File

@ -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;
}

View File

@ -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_ */