2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-03-03 17:15:26 +03:00

Add additional place to check for finding the config.ini or plugins.ini.

Refactoring.
This commit is contained in:
s1lentq 2017-01-23 01:44:52 +07:00
parent 1dcc52fcc0
commit bcb6bd1af5
10 changed files with 76 additions and 42 deletions

View File

@ -195,7 +195,7 @@ void MConfig::set_directory()
}
#endif
normalize_pathname(m_directory);
NormalizePath(m_directory);
// get directory
char *dir = Q_strrchr(m_directory, '/');

View File

@ -116,7 +116,7 @@ bool setup_gamedll(gamedll_t *gamedll)
// If the path is relative, the gamedll file will be missing and
// it might be found in the cache file.
if (!is_absolute_path(gamedll->pathname))
if (!IsAbsolutePath(gamedll->pathname))
{
char szInstallPath[MAX_PATH];
Q_snprintf(szInstallPath, sizeof(szInstallPath), "%s/%s", gamedll->gamedir, gamedll->pathname);

View File

@ -82,7 +82,7 @@ void metamod_startup()
{
META_LOG("Configfile specified via localinfo: %s", cp);
if (valid_gamedir_file(cp))
if (FileExistsInGameDir(cp))
{
Q_strncpy(configFile, cp, sizeof configFile - 1);
configFile[sizeof configFile - 1] = '\0';
@ -91,11 +91,27 @@ void metamod_startup()
META_ERROR("Empty/missing config.ini file: %s; falling back to %s", cp, configFile);
}
if (!FileExistsInGameDir(configFile))
{
Q_strncpy(configFile, g_config->directory(), sizeof configFile - 1);
configFile[sizeof configFile - 1] = '\0';
// Get out of sub directory and check
char *dir = Q_strrchr(configFile, '/');
if (dir) {
*dir = '\0';
}
Q_strcat(configFile, "/" CONFIG_INI);
if (!FileExistsInGameDir(configFile))
{
META_DEBUG(2, "No config.ini file found: %s", CONFIG_INI);
}
}
// Load config file
if (valid_gamedir_file(configFile))
if (FileExistsInGameDir(configFile))
g_config->load(configFile);
else
META_DEBUG(2, "No config.ini file found: %s", CONFIG_INI);
// Now, override config options with localinfo commandline options.
if ((cp = LOCALINFO("mm_debug")) && *cp != '\0')
@ -164,13 +180,30 @@ void metamod_startup()
// gamedll calls engine functions during GiveFnptrsToDll (like hpb_bot
// does) then it needs to be non-null so META_ENGINE_HANDLE won't crash.
//
// However, having replaced valid_file with valid_gamedir_file, we need
// However, having replaced valid_file with FileExistsInGameDir, we need
// to at least initialize the gameDLL to include the gamedir, before
// looking for plugins.ini.
//
// In fact, we need gamedir even earlier, so moved up above.
// Fall back to old plugins filename, if configured one isn't found.
// Load plugins file
if (!FileExistsInGameDir(pluginFile))
{
Q_strncpy(pluginFile, g_config->directory(), sizeof pluginFile - 1);
pluginFile[sizeof pluginFile - 1] = '\0';
// Get out of sub directory and check
char *dir = Q_strrchr(pluginFile, '/');
if (dir) {
*dir = '\0';
}
Q_strcat(pluginFile, "/" PLUGINS_INI);
if (!FileExistsInGameDir(pluginFile))
{
META_DEBUG(2, "No plugins.ini file found: %s", PLUGINS_INI);
}
}
g_plugins = new MPluginList(pluginFile);
@ -200,7 +233,7 @@ void metamod_startup()
execFile[sizeof execFile - 1] = '\0';
}
if (valid_gamedir_file(execFile))
if (FileExistsInGameDir(execFile))
{
if (execFile[0] == '/')
META_ERROR("Cannot exec absolute pathnames: %s", execFile);
@ -225,8 +258,8 @@ bool meta_init_gamedll(void)
Q_memset(&g_GameDLL, 0, sizeof g_GameDLL);
GET_GAME_DIR(gamedir);
normalize_pathname(gamedir);
//
NormalizePath(gamedir);
// As of 1.1.1.1, the engine routine GET_GAME_DIR no longer returns a
// full-pathname, but rather just the path specified as the argument to
// "-game".
@ -237,7 +270,7 @@ bool meta_init_gamedll(void)
// Note: the code has always assumed the server op wouldn't do:
// hlds -game other/firearms
//
if (is_absolute_path(gamedir))
if (IsAbsolutePath(gamedir))
{
// Old style; GET_GAME_DIR returned full pathname. Copy this into
// our gamedir, and truncate to get the game name.

View File

@ -45,7 +45,6 @@ bool rehlds_api_try_init(CSysModule* engineModule, char* failureReason)
return true;
}
bool meta_init_rehlds_api() {
char failReason[2048];

View File

@ -262,7 +262,7 @@ MPlugin* MPluginList::add(MPlugin* padd)
// copy pathname
Q_strncpy(iplug->m_pathname, padd->m_pathname, sizeof iplug->m_pathname - 1);
iplug->m_pathname[sizeof iplug->m_pathname - 1] = '\0';
normalize_pathname(iplug->m_pathname);
NormalizePath(iplug->m_pathname);
iplug->m_source = padd->m_source;
iplug->m_status = padd->m_status;
@ -280,7 +280,7 @@ bool MPluginList::ini_startup()
int n, ln;
MPlugin *pmatch;
if (!valid_gamedir_file(m_inifile))
if (!FileExistsInGameDir(m_inifile))
{
META_ERROR("ini: Metamod plugins file empty or missing: %s", m_inifile);
return false;

View File

@ -53,7 +53,7 @@ bool MPlugin::ini_parseline(char *_line)
Q_strncpy(m_filename, token, sizeof m_filename - 1);
m_filename[sizeof m_filename - 1] = '\0';
normalize_pathname(m_filename);
NormalizePath(m_filename);
// Store name of just the actual _file_, without dir components.
char* cp = Q_strrchr(m_filename, '/');
@ -135,8 +135,7 @@ bool MPlugin::plugin_parseline(const char *fname, int loader_index)
Q_strncpy(m_filename, fname, sizeof m_filename - 1);
m_filename[sizeof m_filename - 1] = '\0';
normalize_pathname(m_filename);
NormalizePath(m_filename);
//store just name of the actual _file, without path
cp = Q_strrchr(m_filename, '/');
@ -181,7 +180,7 @@ bool MPlugin::cmd_parseline(const char *line)
Q_strncpy(m_filename, token, sizeof m_filename - 1);
m_filename[sizeof m_filename - 1] = '\0';
normalize_pathname(m_filename);
NormalizePath(m_filename);
// store name of just the actual _file_, without dir components
cp = Q_strrchr(m_filename, '/');
@ -263,7 +262,7 @@ bool MPlugin::resolve(void)
return false;
}
if (is_absolute_path(m_filename))
if (IsAbsolutePath(m_filename))
found = resolve_prefix(m_filename);
else
found = resolve_dirs(m_filename);

View File

@ -13,18 +13,18 @@ void __declspec(noreturn) do_exit(int exitval)
// Also, formerly named just "valid_file".
//
// Special-case-recognize "/dev/null" as a valid file.
int valid_gamedir_file(const char* path)
bool FileExistsInGameDir(const char *path)
{
struct stat st;
char buf[PATH_MAX];
if (!path)
return FALSE;
return false;
if (!Q_strcmp(path, "/dev/null"))
return TRUE;
return true;
if (is_absolute_path(path))
if (IsAbsolutePath(path))
{
Q_strncpy(buf, path, sizeof buf);
buf[sizeof buf - 1] = '\0';
@ -36,32 +36,32 @@ int valid_gamedir_file(const char* path)
if (ret != 0)
{
META_DEBUG(5, "Unable to stat '%s': %s", buf, strerror(errno));
return FALSE;
return false;
}
int reg = S_ISREG(st.st_mode);
if (!reg)
{
META_DEBUG(5, "Not a regular file: %s", buf);
return FALSE;
return false;
}
if (!st.st_size)
{
META_DEBUG(5, "Empty file: %s", buf);
return FALSE;
return false;
}
if (ret == 0 && reg)
return TRUE;
return true;
return FALSE;
return false;
}
// Turns path into a full path:
// - if not absolute, prepends gamedir
// - calls realpath() to collapse ".." and such
// - calls normalize_pathname() to fix backslashes, etc
// - calls NormalizePath() to fix backslashes, etc
//
// Much like realpath, buffer pointed to by fullpath is assumed to be
// able to store a string of PATH_MAX length.
@ -70,7 +70,7 @@ char* full_gamedir_path(const char* path, char* fullpath)
char buf[PATH_MAX];
// Build pathname from filename, plus gamedir if relative path.
if (is_absolute_path(path))
if (IsAbsolutePath(path))
{
Q_strncpy(buf, path, sizeof buf - 1);
buf[sizeof buf - 1] = '\0';
@ -86,6 +86,6 @@ char* full_gamedir_path(const char* path, char* fullpath)
}
// Replace backslashes, etc.
normalize_pathname(fullpath);
NormalizePath(fullpath);
return fullpath;
}

View File

@ -7,7 +7,7 @@
void __declspec(noreturn) do_exit(int exitval);
int valid_gamedir_file(const char *path);
bool FileExistsInGameDir(const char *path);
char *full_gamedir_path(const char *path, char *fullpath);
// Turn a variable/function name into the corresponding string, optionally

View File

@ -99,7 +99,7 @@ char *mm_strtok_r(char *s, const char *delim, char **ptrptr)
}
#endif // _WIN32
char* trimbuf(char *str)
char *trimbuf(char *str)
{
char *ibuf;
@ -119,19 +119,22 @@ char* trimbuf(char *str)
return str;
}
void normalize_pathname(char *path)
void NormalizePath(char *path)
{
#ifdef _WIN32
char *cp;
for (cp = path; *cp; cp++)
{
if (isupper(*cp))
*cp = tolower(*cp);
for (cp = path; *cp; cp++) {
if (isupper(*cp)) *cp = tolower(*cp);
if (*cp == '\\') *cp = '/';
if (*cp == '\\')
*cp = '/';
}
#endif
}
bool is_absolute_path(const char *path)
bool IsAbsolutePath(const char *path)
{
if (path[0] == '/') return true;
#ifdef _WIN32
@ -161,7 +164,7 @@ char *realpath(const char *file_name, char *resolved_name)
}
FindClose(handle);
normalize_pathname(resolved_name);
NormalizePath(resolved_name);
return resolved_name;
}

View File

@ -54,5 +54,5 @@ char *realpath(const char *file_name, char *resolved_name);
#endif // _WIN32
char* trimbuf(char *str);
void normalize_pathname(char *path);
bool is_absolute_path(const char *path);
void NormalizePath(char *path);
bool IsAbsolutePath(const char *path);