Skip to content

Commit

Permalink
Fix #33
Browse files Browse the repository at this point in the history
  • Loading branch information
theAsmodai committed Mar 10, 2018
1 parent e0e3266 commit 35eada4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
21 changes: 15 additions & 6 deletions metamod/src/game_support.cpp
Expand Up @@ -106,9 +106,22 @@ const game_modinfo_t g_known_games[] = {
// Find a modinfo corresponding to the given game name.
static const game_modinfo_t *lookup_game(const char *name)
{
char temp[MAX_PATH];

for (auto& known : g_known_games) {
if (known.name && !Q_stricmp(known.name, name))
return &known;
if (known.name && !Q_stricmp(known.name, name)) {
#ifdef _WIN32
const char* knowndll = known.win_dll;
#else
const char* knowndll = known.linux_so;
#endif
if (!knowndll)
continue;

Q_snprintf(temp, sizeof temp, "dlls/%s", knowndll);
if (is_file_exists_in_gamedir(temp))
return &known;
}
}

// no match found
Expand Down Expand Up @@ -213,10 +226,6 @@ bool setup_gamedll(gamedll_t *gamedll)
#endif
}

// Neither override nor known-list found a gamedll.
if (!known && !g_config->m_gamedll)
return false;

// Use override-dll if specified.
if (g_config->m_gamedll) {
Q_strlcpy(gamedll->pathname, g_config->m_gamedll);
Expand Down
46 changes: 24 additions & 22 deletions metamod/src/utils.cpp
Expand Up @@ -114,6 +114,29 @@ char* realpath(const char* file_name, char* resolved_name)
}
#endif // _WIN32

bool is_file_exists(const char* file)
{
struct stat64 st;
int ret = stat64(file, &st);
if (ret != 0) {
META_DEBUG(5, "Unable to stat '%s': %s", file, strerror(errno));
return false;
}

int reg = S_ISREG(st.st_mode);
if (!reg) {
META_DEBUG(5, "Not a regular file: %s", file);
return false;
}

if (!st.st_size) {
META_DEBUG(5, "Empty file: %s", file);
return false;
}

return true;
}

// Checks for a non-empty file, relative to the gamedir if necessary.
// Formerly used LOAD_FILE_FOR_ME, which provided a simple way to check for
// a file under the gamedir, but which would _also_ look in the sibling
Expand All @@ -138,28 +161,7 @@ bool is_file_exists_in_gamedir(const char* path)
Q_snprintf(buf, sizeof buf, "%s/%s", g_GameDLL.gamedir, path);
}

struct stat64 st;
int ret = stat64(buf, &st);
if (ret != 0) {
META_DEBUG(5, "Unable to stat '%s': %s", buf, strerror(errno));
return false;
}

int reg = S_ISREG(st.st_mode);
if (!reg) {
META_DEBUG(5, "Not a regular file: %s", buf);
return false;
}

if (!st.st_size) {
META_DEBUG(5, "Empty file: %s", buf);
return false;
}

if (ret == 0 && reg)
return true;

return false;
return is_file_exists(buf);
}

// Turns path into a full path:
Expand Down
1 change: 1 addition & 0 deletions metamod/src/utils.h
Expand Up @@ -31,6 +31,7 @@ void normalize_path(char *path);
bool is_abs_path(const char *path);
bool is_valid_path(const char *path);
bool is_platform_postfix(const char *pf);
bool is_file_exists(const char *path);
bool is_file_exists_in_gamedir(const char *path);
char *full_gamedir_path(const char *path, char (&fullpath)[MAX_PATH]);
bool mem_compare(const char* addr, const char* pattern, size_t len);
Expand Down

0 comments on commit 35eada4

Please sign in to comment.