Update game_support.cpp

This commit is contained in:
STAM 2017-07-21 15:34:58 +03:00 committed by GitHub
parent 827113f7ed
commit fb3bfb7c76

View File

@ -1,5 +1,3 @@
#include "precompiled.h"
// Adapted from adminmod h_export.cpp:
//! this structure contains a list of supported mods and their dlls names
//! To add support for another mod add an entry here, and add all the
@ -18,14 +16,14 @@ const game_modinfo_t g_known_games[] = {
{ "asheep", "hl.so", "hl.dll", "Azure Sheep"},
{ "bdef", "../cl_dlls/server.so", "../cl_dlls/server.dll", "Base Defense" }, //workaround
{ "bg", "bg.so", "bg.dll", "The Battle Grounds"},
{ "bhl", "none", "bhl.dll", "Brutal Half-Life" }, //have no linux binary
{ "bhl", "none", "bhl.dll", "Brutal Half-Life" }, //have no linux binary!
{ "bot", "bot.so", "bot.dll", "Bot"},
{ "brainbread", "bb.so", "bb.dll", "Brain Bread"},
{ "bshift", "bshift.so", "hl.dll", "Half-Life: Blue Shift" },
{ "bumpercars", "hl.so", "hl.dll", "Bumper Cars"},
{ "bumpercars", "hl.so", "hl.dll", "none", "Bumper Cars"},
{ "buzzybots", "bb.so", "bb.dll", "BuzzyBots"},
{ "ckf3", "none", "mp.dll", "Chicken Fortress 3" },
{ "ckf3", "none", "mp.dll", "Chicken Fortress 3" }, //have no linux binary!
{ "cs13", "cs.so", "mp.dll", "Counter-Strike 1.3"},
{ "cstrike", "cs.so", "mp.dll", "Counter-Strike" },
{ "csv15", "cs.so", "mp.dll", "Counter-Strike 1.5 (Steam)"},
@ -37,7 +35,7 @@ const game_modinfo_t g_known_games[] = {
{ "dod", "dod.so", "dod.dll", "Day of Defeat" },
{ "dpb", "pb.i386.so", "pb.dll", "Digital Paintball"},
{ "dragonmodz", "hl.so", "mp.dll", "Dragon Mod Z"},
{ "esf", "../linuxdll/hl_i386.so", "hl.dll", "Earth's Special Forces (Steam)" },
{ "esf", "../linuxdll/hl_i386.so", "hl.dll", "Earth's Special Forces (Steam)" }, //workaround
{ "esf", "hl.so", "hl.dll", "Earth's Special Forces (Old)" },
{ "existence", "ex.so", "existence.dll", "Existence"},
{ "firearms", "fa.so", "firearms.dll", "Firearms"},
@ -68,7 +66,7 @@ const game_modinfo_t g_known_games[] = {
{ "ol", "ol.so", "hl.dll", "Outlawsmod"},
{ "ops1942", "spirit.so", "spirit.dll", "Operations 1942"},
{ "osjb", "osjb.so", "jail.dll", "Open-Source Jailbreak"},
{ "outbreak", "none", "hl.dll", "Out Break"},
{ "outbreak", "none", "hl.dll", "Out Break"}, //have no linux binary
{ "oz", "mp.so", "mp.dll", "Oz Deathmatch"},
{ "paintball", "pb.so", "mp.dll", "Paintball"},
{ "penemy", "pe.so", "pe.dll", "Public Enemy"},
@ -121,188 +119,3 @@ const game_modinfo_t g_known_games[] = {
{ nullptr, nullptr, nullptr, nullptr }
};
// Find a modinfo corresponding to the given game name.
static const game_modinfo_t *lookup_game(const char *name)
{
for (auto& known : g_known_games) {
if (known.name && !Q_stricmp(known.name, name))
return &known;
}
// no match found
return nullptr;
}
bool lookup_game_postfixes(gamedll_t *gamedll)
{
char pathname[PATH_MAX];
static char postfix_path[PATH_MAX] = "";
strlcpy(pathname, gamedll->pathname);
// find extensions and skip
char *pos = strrchr(pathname, '.');
if (pos) {
*pos = '\0';
}
for (size_t i = 0; i < arraysize(g_platform_postfixes); i++)
{
postfix_path[0] = '\0';
strlcat(postfix_path, pathname);
strlcat(postfix_path, g_platform_postfixes[i]);
if (is_file_exists_in_gamedir(postfix_path)) {
strlcpy(gamedll->pathname, postfix_path);
strlcpy(gamedll->real_pathname, postfix_path);
gamedll->file = postfix_path;
return true;
}
}
return false;
}
// Installs gamedll from Steam cache
bool install_gamedll(char *from, const char *to)
{
if (!from)
return false;
if (!to)
to = from;
int length_in;
byte *cachefile = LOAD_FILE_FOR_ME(from, &length_in);
// If the file seems to exist in the cache.
if (cachefile) {
int fd = open(to, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (fd < 0) {
META_DEBUG(3, "Installing gamedll from cache: Failed to create file %s: %s", to, strerror(errno));
FREE_FILE(cachefile);
return false;
}
int length_out = Q_write(fd, cachefile, length_in);
FREE_FILE(cachefile);
close(fd);
// Writing the file was not successfull
if (length_out != length_in) {
META_DEBUG(3, "Installing gamedll from chache: Failed to write all %d bytes to file, only %d written: %s", length_in, length_out, strerror(errno));
// Let's not leave a mess but clean up nicely.
if (length_out >= 0)
_unlink(to);
return false;
}
META_LOG("Installed gamedll %s from cache.", to);
}
else {
META_DEBUG(3, "Failed to install gamedll from cache: file %s not found in cache.", from);
return false;
}
return true;
}
// Set all the fields in the gamedll struct, - based either on an entry in
// known_games matching the current gamedir, or on one specified manually
// by the server admin.
//
// meta_errno values:
// - ME_NOTFOUND couldn't recognize game
bool setup_gamedll(gamedll_t *gamedll)
{
bool override = false;
const game_modinfo_t *known;
const char *knownfn = nullptr;
// First, look for a known game, based on gamedir.
if ((known = lookup_game(gamedll->name))) {
#ifdef _WIN32
knownfn = known->win_dll;
#else
knownfn = known->linux_so;
#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) {
strlcpy(gamedll->pathname, g_config->m_gamedll);
// If the path is relative, the gamedll file will be missing and
// it might be found in the cache file.
if (!is_abs_path(gamedll->pathname)) {
char szInstallPath[MAX_PATH];
Q_snprintf(szInstallPath, sizeof(szInstallPath), "%s/%s", gamedll->gamedir, gamedll->pathname);
// If we could successfully install the gamedll from the cache we
// rectify the pathname to be a full pathname.
if (install_gamedll(gamedll->pathname, szInstallPath)) {
strlcpy(gamedll->pathname, szInstallPath);
}
}
override = true;
}
// Else use Known-list dll.
else if (known) {
Q_snprintf(gamedll->pathname, sizeof(gamedll->pathname), "%s/dlls/%s", gamedll->gamedir, knownfn);
}
else {
// Neither override nor known-list found a gamedll.
return false;
}
// get filename from pathname
char *cp = Q_strrchr(gamedll->pathname, '/');
if (cp)
cp++;
else
cp = gamedll->pathname;
gamedll->file = cp;
// If found, store also the supposed "real" dll path based on the
// gamedir, in case it differs from the "override" dll path.
if (known && override) {
Q_snprintf(gamedll->real_pathname, sizeof(gamedll->real_pathname), "%s/dlls/%s", gamedll->gamedir, knownfn);
}
else {
strlcpy(gamedll->real_pathname, gamedll->pathname);
}
if (override) {
// generate a desc
Q_snprintf(gamedll->desc, sizeof(gamedll->desc), "%s (override)", gamedll->file);
// log result
META_LOG("Overriding game '%s' with dllfile '%s'", gamedll->name, gamedll->file);
}
else if (known) {
strlcpy(gamedll->desc, known->desc);
#if !defined(_WIN32)
if (!is_file_exists_in_gamedir(gamedll->pathname))
{
// trying lookup gamedll with postfixes ie _i386.so
if (lookup_game_postfixes(gamedll)) {
META_DEBUG(3, "dll: Trying lookup to gamedll with postfixes was a success. Game '%s'", gamedll->pathname);
}
}
#endif
META_LOG("Recognized game '%s'; using dllfile '%s'", gamedll->name, gamedll->file);
}
return true;
}