2
0
mirror of https://github.com/rehlds/metamod-r.git synced 2025-01-17 00:58:07 +03:00
metamod-r/metamod/src/metamod.cpp

384 lines
13 KiB
C++
Raw Normal View History

2016-07-26 07:22:47 +07:00
#include "precompiled.h"
cvar_t meta_version = { "metamod_version", APP_VERSION_STRD, FCVAR_SERVER, 0, NULL };
2016-07-04 12:07:29 +06:00
MConfig static_config;
MConfig *Config=&static_config;
option_t global_options[] = {
{ "debuglevel", CF_INT, &Config->debuglevel, "0" },
{ "plugins_file", CF_PATH, &Config->plugins_file, PLUGINS_INI },
2016-07-26 07:22:47 +07:00
{ "exec_cfg", CF_STR, &Config->exec_cfg, EXEC_CFG },
2016-07-04 12:07:29 +06:00
{ "clientmeta", CF_BOOL, &Config->clientmeta, "yes" },
// list terminator
{ NULL, CF_NONE, NULL, NULL }
};
gamedll_t GameDLL;
meta_globals_t PublicMetaGlobals;
meta_globals_t PrivateMetaGlobals;
meta_enginefuncs_t g_plugin_engfuncs;
MPluginList *Plugins;
MRegCmdList *RegCmds;
MRegCvarList *RegCvars;
MRegMsgList *RegMsgs;
2016-07-26 07:22:47 +07:00
MPlayerList g_Players;
2016-07-04 12:07:29 +06:00
int requestid_counter = 0;
DLHANDLE metamod_handle;
int metamod_not_loaded = 0;
// Very first metamod function that's run.
// Do startup operations...
2016-07-26 07:22:47 +07:00
int metamod_startup()
{
char *cp, *mmfile = NULL, *cfile = NULL;
2016-07-04 12:07:29 +06:00
META_CONS(" ");
2016-07-26 07:22:47 +07:00
META_CONS(" Metamod version %s Copyright (c) 2001-2016 Will Day (modification ReHLDS Team)", APP_VERSION_STRD);
META_CONS(" Metamod comes with ABSOLUTELY NO WARRANTY; for details type `meta gpl'.");
2016-07-04 12:07:29 +06:00
META_CONS(" This is free software, and you are welcome to redistribute it");
META_CONS(" under certain conditions; type `meta gpl' for details.");
META_CONS(" ");
2016-07-26 07:22:47 +07:00
META_CONS("Metamod v%s, API (%s)", APP_VERSION_STRD, META_INTERFACE_VERSION);
META_CONS("Metamod build: " __TIME__ " " __DATE__ " (" APP_VERSION_STRD ")");
META_CONS("Metamod from: " APP_COMMITS_URL APP_COMMIT_ID " " APP_COMMIT_AUTHOR "");
2016-07-04 12:07:29 +06:00
// If running with "+developer", allow an opportunity to break in with
// a debugger.
2016-07-26 07:22:47 +07:00
if ((int)CVAR_GET_FLOAT("developer") != 0)
2016-07-04 12:07:29 +06:00
sleep(1);
// Get gamedir, very early on, because it seems we need it all over the
// place here at the start.
2016-07-26 07:22:47 +07:00
if (!meta_init_gamedll())
{
2016-07-04 12:07:29 +06:00
META_ERROR("Failure to init game DLL; exiting...");
2016-07-26 07:22:47 +07:00
return 0;
2016-07-04 12:07:29 +06:00
}
// Register various console commands and cvars.
2016-07-26 07:22:47 +07:00
// Can I do these here, rather than waiting for GameDLLInit() ?
2016-07-04 12:07:29 +06:00
// Looks like it works okay..
meta_register_cmdcvar();
// Set a slight debug level for developer mode, if debug level not
// already set.
2016-07-26 07:22:47 +07:00
if ((int)CVAR_GET_FLOAT("developer") != 0 && (int)meta_debug.value == 0) {
2016-07-04 12:07:29 +06:00
CVAR_SET_FLOAT("meta_debug", (float)(meta_debug_value = 3));
}
// Init default values
Config->init(global_options);
// Find config file
cfile=CONFIG_INI;
2016-07-26 07:22:47 +07:00
if ((cp=LOCALINFO("mm_configfile")) && *cp != '\0') {
2016-07-04 12:07:29 +06:00
META_LOG("Configfile specified via localinfo: %s", cp);
2016-07-26 07:22:47 +07:00
if (valid_gamedir_file(cp))
2016-07-04 12:07:29 +06:00
cfile=cp;
else
META_WARNING("Empty/missing config.ini file: %s; falling back to %s",
cp, cfile);
}
// Load config file
2016-07-26 07:22:47 +07:00
if (valid_gamedir_file(cfile))
2016-07-04 12:07:29 +06:00
Config->load(cfile);
else
META_DEBUG(2, ("No config.ini file found: %s", CONFIG_INI));
// Now, override config options with localinfo commandline options.
2016-07-26 07:22:47 +07:00
if ((cp=LOCALINFO("mm_debug")) && *cp != '\0') {
2016-07-04 12:07:29 +06:00
META_LOG("Debuglevel specified via localinfo: %s", cp);
Config->set("debuglevel", cp);
}
2016-07-26 07:22:47 +07:00
if ((cp=LOCALINFO("mm_gamedll")) && *cp != '\0') {
2016-07-04 12:07:29 +06:00
META_LOG("Gamedll specified via localinfo: %s", cp);
Config->set("gamedll", cp);
}
2016-07-26 07:22:47 +07:00
if ((cp=LOCALINFO("mm_pluginsfile")) && *cp != '\0') {
2016-07-04 12:07:29 +06:00
META_LOG("Pluginsfile specified via localinfo: %s", cp);
Config->set("plugins_file", cp);
}
2016-07-26 07:22:47 +07:00
if ((cp=LOCALINFO("mm_execcfg")) && *cp != '\0') {
2016-07-04 12:07:29 +06:00
META_LOG("Execcfg specified via localinfo: %s", cp);
Config->set("exec_cfg", cp);
}
2016-07-26 07:22:47 +07:00
if ((cp=LOCALINFO("mm_autodetect")) && *cp != '\0') {
2016-07-04 12:07:29 +06:00
META_LOG("Autodetect specified via localinfo: %s", cp);
Config->set("autodetect", cp);
}
2016-07-26 07:22:47 +07:00
if ((cp=LOCALINFO("mm_clientmeta")) && *cp != '\0') {
2016-07-04 12:07:29 +06:00
META_LOG("Clientmeta specified via localinfo: %s", cp);
Config->set("clientmeta", cp);
}
// Check for an initial debug level, since cfg files don't get exec'd
// until later.
2016-07-26 07:22:47 +07:00
if (Config->debuglevel != 0) {
2016-07-04 12:07:29 +06:00
CVAR_SET_FLOAT("meta_debug", (float)(meta_debug_value = Config->debuglevel));
}
// Prepare for registered commands from plugins.
RegCmds = new MRegCmdList();
RegCvars = new MRegCvarList();
// Prepare for registered user messages from gamedll.
RegMsgs = new MRegMsgList();
2016-07-26 07:22:47 +07:00
2016-07-04 12:07:29 +06:00
// Copy, and store pointer in Engine struct. Yes, we could just store
// the actual engine_t struct in Engine, but then it wouldn't be a
// pointer to match the other g_engfuncs.
g_plugin_engfuncs.set_from(Engine.funcs);
Engine.pl_funcs=&g_plugin_engfuncs;
// substitute our special versions of various commands
Engine.pl_funcs->pfnAddServerCommand = meta_AddServerCommand;
Engine.pl_funcs->pfnCVarRegister = meta_CVarRegister;
Engine.pl_funcs->pfnCvar_RegisterVariable = meta_CVarRegister;
Engine.pl_funcs->pfnRegUserMsg = meta_RegUserMsg;
2016-07-26 07:22:47 +07:00
if (IS_VALID_PTR((void*)Engine.pl_funcs->pfnQueryClientCvarValue))
2016-07-04 12:07:29 +06:00
Engine.pl_funcs->pfnQueryClientCvarValue = meta_QueryClientCvarValue;
else
Engine.pl_funcs->pfnQueryClientCvarValue = NULL;
2016-07-26 07:22:47 +07:00
if (!IS_VALID_PTR((void*)Engine.pl_funcs->pfnQueryClientCvarValue2))
2016-07-04 12:07:29 +06:00
Engine.pl_funcs->pfnQueryClientCvarValue2 = NULL;
2016-07-26 07:22:47 +07:00
2016-07-04 12:07:29 +06:00
// Before, we loaded plugins before loading the game DLL, so that if no
// plugins caught engine functions, we could pass engine funcs straight
// to game dll, rather than acting as intermediary. (Should perform
// better, right?)
//
// But since a plugin can be loaded at any time, we have to go ahead
// and catch the engine funcs regardless. Also, we want to give each
// plugin a copy of the gameDLL's api tables, in case they need to call
// API functions directly.
//
// Thus, load gameDLL first, then plugins.
//
// However, we have to init the Plugins object first, because if the
// 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
2016-07-26 07:22:47 +07:00
// to at least initialize the gameDLL to include the gamedir, before
2016-07-04 12:07:29 +06:00
// 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.
mmfile=PLUGINS_INI;
2016-07-26 07:22:47 +07:00
if (!valid_gamedir_file(PLUGINS_INI) && valid_gamedir_file(OLD_PLUGINS_INI))
2016-07-04 12:07:29 +06:00
mmfile=OLD_PLUGINS_INI;
2016-07-26 07:22:47 +07:00
if (valid_gamedir_file(Config->plugins_file))
2016-07-04 12:07:29 +06:00
mmfile=Config->plugins_file;
else
2016-07-26 07:22:47 +07:00
META_WARNING("Plugins file is empty/missing: %s; falling back to %s",
2016-07-04 12:07:29 +06:00
Config->plugins_file, mmfile);
Plugins = new MPluginList(mmfile);
2016-07-26 07:22:47 +07:00
if (!meta_load_gamedll()) {
2016-07-04 12:07:29 +06:00
META_ERROR("Failure to load game DLL; exiting...");
2016-07-26 07:22:47 +07:00
return 0;
2016-07-04 12:07:29 +06:00
}
2016-07-26 07:22:47 +07:00
if (!Plugins->load()) {
2016-07-04 12:07:29 +06:00
META_WARNING("Failure to load plugins...");
// Exit on failure here? Dunno...
}
// Allow for commands to metamod plugins at startup. Autoexec.cfg is
// read too early, and server.cfg is read too late.
//
// Only attempt load if the file appears to exist and be non-empty, to
// avoid confusing users with "couldn't exec exec.cfg" console
// messages.
2016-07-26 07:22:47 +07:00
if (valid_gamedir_file(Config->exec_cfg))
2016-07-04 12:07:29 +06:00
mmfile=Config->exec_cfg;
2016-07-26 07:22:47 +07:00
else if (valid_gamedir_file(OLD_EXEC_CFG))
2016-07-04 12:07:29 +06:00
mmfile=OLD_EXEC_CFG;
else
mmfile=NULL;
2016-07-26 07:22:47 +07:00
if (mmfile) {
if (mmfile[0]=='/')
2016-07-04 12:07:29 +06:00
META_WARNING("Cannot exec absolute pathnames: %s", mmfile);
else {
char cmd[NAME_MAX];
META_LOG("Exec'ing metamod exec.cfg: %s...", mmfile);
safevoid_snprintf(cmd, sizeof(cmd), "exec %s\n", mmfile);
SERVER_COMMAND(cmd);
}
}
2016-07-26 07:22:47 +07:00
return 1;
2016-07-04 12:07:29 +06:00
}
// Set initial GameDLL fields (name, gamedir).
// meta_errno values:
// - ME_NULLRESULT getcwd failed
2016-07-26 07:22:47 +07:00
mBOOL meta_init_gamedll() {
2016-07-04 12:07:29 +06:00
char gamedir[PATH_MAX];
char *cp;
2016-07-04 13:11:20 +06:00
Q_memset(&GameDLL, 0, sizeof(GameDLL));
2016-07-04 12:07:29 +06:00
GET_GAME_DIR(gamedir);
normalize_pathname(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".
//
// However, since we have to work properly under both the new version
// as well as previous versions, we have to support both possibilities.
//
// Note: the code has always assumed the server op wouldn't do:
// hlds -game other/firearms
//
2016-07-26 07:22:47 +07:00
if (is_absolute_path(gamedir))
{
2016-07-04 12:07:29 +06:00
// Old style; GET_GAME_DIR returned full pathname. Copy this into
// our gamedir, and truncate to get the game name.
// (note check for both linux and win32 full pathname.)
2016-07-26 07:22:47 +07:00
Q_strncpy(GameDLL.gamedir, gamedir, sizeof(GameDLL.gamedir) - 1);
GameDLL.gamedir[sizeof(GameDLL.gamedir) - 1] = '\0';
cp = Q_strrchr(gamedir, '/') + 1;
Q_strncpy(GameDLL.name, cp, sizeof(GameDLL.name) - 1);
GameDLL.name[sizeof(GameDLL.name) - 1] = '\0';
2016-07-04 12:07:29 +06:00
}
2016-07-26 07:22:47 +07:00
else
{
2016-07-04 12:07:29 +06:00
// New style; GET_GAME_DIR returned game name. Copy this into our
// game name, and prepend the current working directory.
char buf[PATH_MAX];
2016-07-26 07:22:47 +07:00
if (!getcwd(buf, sizeof(buf)))
{
2016-07-04 12:07:29 +06:00
META_WARNING("dll: Couldn't get cwd; %s", strerror(errno));
RETURN_ERRNO(mFALSE, ME_NULLRESULT);
}
2016-07-26 07:22:47 +07:00
safevoid_snprintf(GameDLL.gamedir, sizeof(GameDLL.gamedir), "%s/%s", buf, gamedir);
Q_strncpy(GameDLL.name, gamedir, sizeof(GameDLL.name) - 1);
GameDLL.name[sizeof(GameDLL.name) - 1] = '\0';
2016-07-04 12:07:29 +06:00
}
META_DEBUG(3, ("Game: %s", GameDLL.name));
2016-07-26 07:22:47 +07:00
return mTRUE;
2016-07-04 12:07:29 +06:00
}
// Load game DLL.
// meta_errno values:
// - ME_DLOPEN couldn't dlopen game dll file
// - ME_DLMISSING couldn't find required routine in game dll
// (GiveFnptrsToDll, GetEntityAPI, GetEntityAPI2)
2016-07-26 07:22:47 +07:00
mBOOL meta_load_gamedll() {
2016-07-04 12:07:29 +06:00
int iface_vers;
int found=0;
GIVE_ENGINE_FUNCTIONS_FN pfn_give_engfuncs;
GETNEWDLLFUNCTIONS_FN pfn_getapinew;
GETENTITYAPI2_FN pfn_getapi2;
GETENTITYAPI_FN pfn_getapi;
2016-07-26 07:22:47 +07:00
if (!setup_gamedll(&GameDLL)) {
2016-07-04 12:07:29 +06:00
META_WARNING("dll: Unrecognized game: %s", GameDLL.name);
// meta_errno should be already set in lookup_game()
2016-07-26 07:22:47 +07:00
return mFALSE;
2016-07-04 12:07:29 +06:00
}
// open the game DLL
2016-07-26 07:22:47 +07:00
if (!(GameDLL.handle=DLOPEN(GameDLL.pathname))) {
META_WARNING("dll: Couldn't load game DLL %s: %s", GameDLL.pathname,
2016-07-04 12:07:29 +06:00
DLERROR());
RETURN_ERRNO(mFALSE, ME_DLOPEN);
}
// Used to only pass our table of engine funcs if a loaded plugin
// wanted to catch one of the functions, but now that plugins are
// dynamically loadable at any time, we have to always pass our table,
// so that any plugin loaded later can catch what they need to.
2016-07-26 07:22:47 +07:00
if ((pfn_give_engfuncs = (GIVE_ENGINE_FUNCTIONS_FN) DLSYM(GameDLL.handle, "GiveFnptrsToDll")))
2016-07-04 12:07:29 +06:00
{
pfn_give_engfuncs(&meta_engfuncs, gpGlobals);
2016-07-26 07:22:47 +07:00
META_DEBUG(3, ("dll: Game '%s': Called GiveFnptrsToDll",
2016-07-04 12:07:29 +06:00
GameDLL.name));
2016-07-26 07:22:47 +07:00
//activate linkent-replacement after give_engfuncs so that if game dll is
//plugin too and uses same method we get combined export table of plugin
2016-07-04 12:07:29 +06:00
//and game dll
2016-07-26 07:22:47 +07:00
if (!init_linkent_replacement(metamod_handle, GameDLL.handle)) {
2016-07-04 12:07:29 +06:00
META_WARNING("dll: Couldn't load linkent replacement for game DLL");
RETURN_ERRNO(mFALSE, ME_DLERROR);
}
}
else {
2016-07-26 07:22:47 +07:00
META_WARNING("dll: Couldn't find GiveFnptrsToDll() in game DLL '%s': %s",
2016-07-04 12:07:29 +06:00
GameDLL.name, DLERROR());
RETURN_ERRNO(mFALSE, ME_DLMISSING);
}
// Yes...another macro.
#define GET_FUNC_TABLE_FROM_GAME(gamedll, pfnGetFuncs, STR_GetFuncs, struct_field, API_TYPE, TABLE_TYPE, vers_pass, vers_int, vers_want, gotit) \
2016-07-26 07:22:47 +07:00
if ((pfnGetFuncs = (API_TYPE) DLSYM(gamedll.handle, STR_GetFuncs))) { \
2016-07-04 12:07:29 +06:00
gamedll.funcs.struct_field = (TABLE_TYPE*) calloc(1, sizeof(TABLE_TYPE)); \
2016-07-26 07:22:47 +07:00
if (!gamedll.funcs.struct_field) {\
2016-07-04 12:07:29 +06:00
META_WARNING("malloc failed for gamedll struct_field: %s", STR_GetFuncs); \
} \
2016-07-26 07:22:47 +07:00
else if (pfnGetFuncs(gamedll.funcs.struct_field, vers_pass)) { \
2016-07-04 12:07:29 +06:00
META_DEBUG(3, ("dll: Game '%s': Found %s", gamedll.name, STR_GetFuncs)); \
gotit=1; \
} \
else { \
META_WARNING("dll: Failure calling %s in game '%s'", STR_GetFuncs, gamedll.name); \
free(gamedll.funcs.struct_field); \
gamedll.funcs.struct_field=NULL; \
2016-07-26 07:22:47 +07:00
if (vers_int != vers_want) { \
2016-07-04 12:07:29 +06:00
META_WARNING("dll: Interface version didn't match; we wanted %d, they had %d", vers_want, vers_int); \
/* reproduce error from engine */ \
META_CONS("=================="); \
META_CONS("Game DLL version mismatch"); \
META_CONS("DLL version is %d, engine version is %d", vers_int, vers_want); \
2016-07-26 07:22:47 +07:00
if (vers_int > vers_want) \
2016-07-04 12:07:29 +06:00
META_CONS("Engine appears to be outdated, check for updates"); \
else \
META_CONS("The game DLL for %s appears to be outdated, check for updates", GameDLL.name); \
META_CONS("=================="); \
ALERT(at_error, "Exiting...\n"); \
} \
} \
} \
else { \
META_DEBUG(5, ("dll: Game '%s': No %s", gamedll.name, STR_GetFuncs)); \
gamedll.funcs.struct_field=NULL; \
}
2016-07-26 07:22:47 +07:00
// Look for API-NEW interface in Game dll. We do this before API2/API, because
2016-07-04 12:07:29 +06:00
// that's what the engine appears to do..
iface_vers=NEW_DLL_FUNCTIONS_VERSION;
2016-07-26 07:22:47 +07:00
GET_FUNC_TABLE_FROM_GAME(GameDLL, pfn_getapinew, "GetNewDLLFunctions", newapi_table, GETNEWDLLFUNCTIONS_FN, meta_new_dll_functions_t, &iface_vers, iface_vers, NEW_DLL_FUNCTIONS_VERSION, found);
2016-07-04 12:07:29 +06:00
// Look for API2 interface in plugin; preferred over API-1.
found=0;
iface_vers=INTERFACE_VERSION;
2016-07-26 07:22:47 +07:00
GET_FUNC_TABLE_FROM_GAME(GameDLL, pfn_getapi2, "GetEntityAPI2", dllapi_table, GETENTITYAPI2_FN, DLL_FUNCTIONS, &iface_vers, iface_vers, INTERFACE_VERSION, found);
2016-07-04 12:07:29 +06:00
// Look for API-1 in plugin, if API2 interface wasn't found.
2016-07-26 07:22:47 +07:00
if (!found)
{
found = 0;
GET_FUNC_TABLE_FROM_GAME(GameDLL, pfn_getapi, "GetEntityAPI", dllapi_table, GETENTITYAPI_FN, DLL_FUNCTIONS, INTERFACE_VERSION, INTERFACE_VERSION, INTERFACE_VERSION, found);
2016-07-04 12:07:29 +06:00
}
// If didn't find either, return failure.
2016-07-26 07:22:47 +07:00
if (!found)
{
2016-07-04 12:07:29 +06:00
META_WARNING("dll: Couldn't find either GetEntityAPI nor GetEntityAPI2 in game DLL '%s'", GameDLL.name);
RETURN_ERRNO(mFALSE, ME_DLMISSING);
}
META_LOG("Game DLL for '%s' loaded successfully", GameDLL.desc);
2016-07-26 07:22:47 +07:00
return mTRUE;
2016-07-04 12:07:29 +06:00
}