1
0
mirror of https://github.com/alliedmodders/metamod-source.git synced 2024-12-04 16:24:16 +01:00
HLMetaModOfficial/core/provider/provider_ep2.cpp

705 lines
16 KiB
C++
Raw Normal View History

2009-10-29 06:07:23 +01:00
/**
* vim: set ts=4 sw=4 tw=99 noet :
2009-10-29 06:07:23 +01:00
* ======================================================
* Metamod:Source
* Copyright (C) 2004-2009 AlliedModders LLC and authors.
2009-10-29 06:07:23 +01:00
* All rights reserved.
* ======================================================
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <setjmp.h>
2009-10-29 06:07:23 +01:00
#include "../metamod_oslink.h"
#include <sourcehook.h>
#include <convar.h>
#include <eiface.h>
#include <tier0/icommandline.h>
#include <sh_vector.h>
#include <sh_string.h>
#include "../metamod_util.h"
#include "provider_ep2.h"
#include "console.h"
#include "metamod_console.h"
#include <filesystem.h>
#include "metamod.h"
#include <tier1/KeyValues.h>
#if SOURCE_ENGINE == SE_SOURCE2
#include <iserver.h>
#endif
2009-10-29 06:07:23 +01:00
/* Types */
typedef void (*CONPRINTF_FUNC)(const char *, ...);
struct UsrMsgInfo
{
UsrMsgInfo()
{
}
UsrMsgInfo(int s, const char *t) : size(s), name(t)
{
}
int size;
String name;
};
/* Imports */
#if SOURCE_ENGINE == SE_DARKMESSIAH
#undef CommandLine
DLL_IMPORT ICommandLine *CommandLine();
#endif
/* Functions */
void CacheUserMessages();
void Detour_Error(const tchar *pMsg, ...);
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_DOTA || SOURCE_ENGINE == SE_SOURCE2
void ClientCommand(CEntityIndex index, const CCommand &args);
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_DOTA
void LocalCommand_Meta(const CCommandContext &context, const CCommand &args);
2015-07-09 03:39:28 +02:00
#else
void LocalCommand_Meta(const CCommand &args);
#endif
#if SOURCE_ENGINE == SE_SOURCE2
void meta_game_init(const CCommand &args);
void meta_level_init(const CCommand &args);
void meta_level_shutdown(const CCommand &args);
#endif
#elif SOURCE_ENGINE >= SE_ORANGEBOX
2009-10-29 06:07:23 +01:00
void ClientCommand(edict_t *pEdict, const CCommand &args);
void LocalCommand_Meta(const CCommand &args);
#else
void ClientCommand(edict_t *pEdict);
void LocalCommand_Meta();
#endif
void _ServerCommand();
/* Variables */
static BaseProvider g_Ep1Provider;
static List<ConCommandBase *> conbases_unreg;
static CVector<UsrMsgInfo> usermsgs_list;
static jmp_buf usermsg_end;
2009-10-29 06:07:23 +01:00
ICvar *icvar = NULL;
IFileSystem *baseFs = NULL;
IServerGameDLL *server = NULL;
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_SOURCE2
static ISource2ServerConfig *serverconfig = NULL;
INetworkServerService *netservice = NULL;
IEngineServiceMgr *enginesvcmgr = NULL;
2015-07-09 03:39:28 +02:00
#endif
2009-10-29 06:07:23 +01:00
IVEngineServer *engine = NULL;
IServerGameClients *gameclients = NULL;
2015-07-09 03:39:28 +02:00
CGlobalVars *gpGlobals = NULL;
2009-10-29 06:07:23 +01:00
IMetamodSourceProvider *provider = &g_Ep1Provider;
ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_DOTA || SOURCE_ENGINE == SE_SOURCE2
SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CEntityIndex, const CCommand &);
#elif SOURCE_ENGINE >= SE_ORANGEBOX
2009-10-29 06:07:23 +01:00
SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
#else
SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
#endif
void BaseProvider::ConsolePrint(const char *str)
{
#if SOURCE_ENGINE >= SE_ORANGEBOX
ConMsg("%s", str);
#else
Msg("%s", str);
#endif
}
void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory,
CreateInterfaceFn serverFactory)
{
engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
if (!engine)
{
DisplayError("Could not find IVEngineServer! Metamod cannot load.");
return;
}
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_SOURCE2
gpGlobals = engine->GetServerGlobals();
serverconfig = (ISource2ServerConfig *) ((serverFactory) (INTERFACEVERSION_SERVERCONFIG, NULL));
netservice = (INetworkServerService *) ((engineFactory) (NETWORKSERVERSERVICE_INTERFACE_VERSION, NULL));
enginesvcmgr = (IEngineServiceMgr *) ((engineFactory) (ENGINESERVICEMGR_INTERFACE_VERSION, NULL));
2015-07-09 03:39:28 +02:00
#endif
2009-10-29 06:07:23 +01:00
#if SOURCE_ENGINE >= SE_ORANGEBOX
icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
#else
icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL));
#endif
if (!icvar)
{
DisplayError("Could not find ICvar! Metamod cannot load.");
return;
}
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_SOURCE2
gameclients = (IServerGameClients *)(serverFactory(INTERFACEVERSION_SERVERGAMECLIENTS, NULL));
#else
2009-10-29 06:07:23 +01:00
if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
== NULL)
{
gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
}
2015-07-09 03:39:28 +02:00
#endif
2009-10-29 06:07:23 +01:00
baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
if (baseFs == NULL)
{
mm_LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
}
#if SOURCE_ENGINE >= SE_ORANGEBOX
g_pCVar = icvar;
#endif
2015-07-09 12:54:44 +02:00
g_SMConVarAccessor.RegisterConCommandBase(&meta_local_cmd);
2009-10-29 06:07:23 +01:00
CacheUserMessages();
2009-10-29 06:07:23 +01:00
#if SOURCE_ENGINE == SE_DARKMESSIAH
if (!g_SMConVarAccessor.InitConCommandBaseList())
{
/* This is very unlikely considering it's old engine */
mm_LogMessage("[META] Warning: Failed to find ConCommandBase list!");
mm_LogMessage("[META] Warning: ConVars and ConCommands cannot be unregistered properly! Please file a bug report.");
2009-10-29 06:07:23 +01:00
}
#endif
if (gameclients)
{
SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
}
}
void BaseProvider::Notify_DLLShutdown_Pre()
{
2015-07-09 03:39:28 +02:00
2009-10-29 06:07:23 +01:00
g_SMConVarAccessor.RemoveMetamodCommands();
#if SOURCE_ENGINE == SE_DARKMESSIAH
if (g_Metamod.IsLoadedAsGameDLL())
{
icvar->UnlinkVariables(FCVAR_GAMEDLL);
}
#endif
}
bool BaseProvider::IsRemotePrintingAvailable()
{
return true;
}
void BaseProvider::ClientConsolePrint(edict_t *pEdict, const char *message)
2009-10-29 06:07:23 +01:00
{
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_DOTA || SOURCE_ENGINE == SE_SOURCE2
int client = (int)(pEdict - gpGlobals->pEdicts);
2009-10-29 06:07:23 +01:00
engine->ClientPrintf(client, message);
#else
engine->ClientPrintf(pEdict, message);
#endif
2009-10-29 06:07:23 +01:00
}
void BaseProvider::ServerCommand(const char *cmd)
{
engine->ServerCommand(cmd);
}
const char *BaseProvider::GetConVarString(ConVar *convar)
{
if (convar == NULL)
{
return NULL;
}
return convar->GetString();
}
void BaseProvider::SetConVarString(ConVar *convar, const char *str)
{
convar->SetValue(str);
}
bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
{
return pCommand->IsCommand();
}
bool BaseProvider::IsSourceEngineBuildCompatible(int build)
{
return (build == SOURCE_ENGINE_ORIGINAL
|| build == SOURCE_ENGINE_EPISODEONE);
}
const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
{
if (key[0] == '-' || key[0] == '+')
{
return CommandLine()->ParmValue(key, defval);
}
else if (icvar)
{
const char *val;
if ((val = icvar->GetCommandLineValue(key)) == NULL)
{
return defval;
}
return val;
}
return NULL;
}
int BaseProvider::TryServerGameDLL(const char *iface)
{
if (strncmp(iface, "ServerGameDLL", 13) != 0)
{
return 0;
}
return atoi(&iface[13]);
}
bool BaseProvider::LogMessage(const char *buffer)
{
if (!engine)
{
return false;
}
engine->LogPrint(buffer);
return true;
}
bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
{
#if SOURCE_ENGINE == SE_SOURCE2
SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
switch (hook)
{
case ProvidedHook_StartupServer:
SourceHook::GetFuncInfo(&INetworkServerService::StartupServer, mfi);
break;
case ProvidedHook_StartChangeLevel:
SourceHook::GetFuncInfo(&INetworkGameServer::StartChangeLevel, mfi);
break;
case ProvidedHook_Init:
SourceHook::GetFuncInfo(&INetworkGameServer::Init, mfi);
break;
case ProvidedHook_SwitchToLoop:
SourceHook::GetFuncInfo(&IEngineServiceMgr::SwitchToLoop, mfi);
break;
case ProvidedHook_AllocateServer:
SourceHook::GetFuncInfo(&INetworkGameServerFactory::Allocate, mfi);
break;
default:
return false;
}
*pInfo = mfi;
return (mfi.thisptroffs >= 0);
#else
2009-10-29 06:07:23 +01:00
SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
if (hook == ProvidedHook_LevelInit)
{
SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
}
else if (hook == ProvidedHook_LevelShutdown)
{
SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
}
else if (hook == ProvidedHook_GameInit)
{
SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
}
*pInfo = mfi;
return (mfi.thisptroffs >= 0);
2015-07-09 03:39:28 +02:00
#endif
2009-10-29 06:07:23 +01:00
}
void BaseProvider::DisplayError(const char *fmt, ...)
{
va_list ap;
char buffer[2048];
va_start(ap, fmt);
UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_SOURCE2
Msg("ERROR: %s", buffer);
#else
Error("%s", buffer);
2015-07-09 03:39:28 +02:00
#endif
2009-10-29 06:07:23 +01:00
}
void BaseProvider::DisplayWarning(const char *fmt, ...)
{
va_list ap;
char buffer[2048];
va_start(ap, fmt);
UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
Warning("%s", buffer);
2009-10-29 06:07:23 +01:00
}
IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
{
return &g_SMConVarAccessor;
}
bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
{
return g_SMConVarAccessor.Register(pCommand);
}
void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
{
return g_SMConVarAccessor.Unregister(pCommand);
}
int BaseProvider::GetUserMessageCount()
{
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA || SOURCE_ENGINE == SE_SOURCE2
return -1;
#else
2009-10-29 06:07:23 +01:00
return (int)usermsgs_list.size();
#endif
2009-10-29 06:07:23 +01:00
}
int BaseProvider::FindUserMessage(const char *name, int *size)
{
for (size_t i = 0; i < usermsgs_list.size(); i++)
{
if (usermsgs_list[i].name.compare(name) == 0)
{
if (size)
{
*size = usermsgs_list[i].size;
}
return (int)i;
}
}
return -1;
}
const char *BaseProvider::GetUserMessage(int index, int *size)
{
if (index < 0 || index >= (int)usermsgs_list.size())
2009-10-29 06:07:23 +01:00
{
return NULL;
}
if (size)
{
*size = usermsgs_list[index].size;
}
return usermsgs_list[index].name.c_str();
}
void BaseProvider::GetGamePath(char *pszBuffer, int len)
{
engine->GetGameDir(pszBuffer, len);
}
2009-10-29 06:07:23 +01:00
const char *BaseProvider::GetGameDescription()
{
#if SOURCE_ENGINE == SE_SOURCE2
2015-07-09 03:39:28 +02:00
return serverconfig->GetGameDescription();
#else
return server->GetGameDescription();
#endif
2009-10-29 06:07:23 +01:00
}
int BaseProvider::DetermineSourceEngine()
2009-10-29 06:07:23 +01:00
{
#if SOURCE_ENGINE == SE_BLOODYGOODTIME
return SOURCE_ENGINE_BLOODYGOODTIME;
#elif SOURCE_ENGINE == SE_ALIENSWARM
return SOURCE_ENGINE_ALIENSWARM;
#elif SOURCE_ENGINE == SE_LEFT4DEAD2
return SOURCE_ENGINE_LEFT4DEAD2;
#elif SOURCE_ENGINE == SE_NUCLEARDAWN
return SOURCE_ENGINE_NUCLEARDAWN;
#elif SOURCE_ENGINE == SE_CONTAGION
return SOURCE_ENGINE_CONTAGION;
#elif SOURCE_ENGINE == SE_LEFT4DEAD
2009-10-29 06:07:23 +01:00
return SOURCE_ENGINE_LEFT4DEAD;
#elif SOURCE_ENGINE == SE_ORANGEBOX
return SOURCE_ENGINE_ORANGEBOX;
#elif SOURCE_ENGINE == SE_CSS
return SOURCE_ENGINE_CSS;
#elif SOURCE_ENGINE == SE_HL2DM
return SOURCE_ENGINE_HL2DM;
#elif SOURCE_ENGINE == SE_DODS
return SOURCE_ENGINE_DODS;
#elif SOURCE_ENGINE == SE_SDK2013
return SOURCE_ENGINE_SDK2013;
#elif SOURCE_ENGINE == SE_TF2
return SOURCE_ENGINE_TF2;
#elif SOURCE_ENGINE == SE_DARKMESSIAH
2009-10-29 06:07:23 +01:00
return SOURCE_ENGINE_DARKMESSIAH;
#elif SOURCE_ENGINE == SE_EYE
return SOURCE_ENGINE_EYE;
#elif SOURCE_ENGINE == SE_PORTAL2
return SOURCE_ENGINE_PORTAL2;
#elif SOURCE_ENGINE == SE_BLADE
return SOURCE_ENGINE_BLADE;
#elif SOURCE_ENGINE == SE_INSURGENCY
return SOURCE_ENGINE_INSURGENCY;
#elif SOURCE_ENGINE == SE_CSGO
return SOURCE_ENGINE_CSGO;
2013-03-04 16:38:17 +01:00
#elif SOURCE_ENGINE == SE_DOTA
return SOURCE_ENGINE_DOTA;
#elif SOURCE_ENGINE == SE_BMS
return SOURCE_ENGINE_BMS;
2015-07-09 03:39:28 +02:00
#elif SOURCE_ENGINE == SE_SOURCE2
return SOURCE_ENGINE_SOURCE2;
#else
#error "SOURCE_ENGINE not defined to a known value"
2009-10-29 06:07:23 +01:00
#endif
}
ConVar *BaseProvider::CreateConVar(const char *name,
const char *defval,
const char *help,
int flags)
{
int newflags = 0;
if (flags & ConVarFlag_Notify)
{
newflags |= FCVAR_NOTIFY;
}
if (flags & ConVarFlag_SpOnly)
{
newflags |= FCVAR_SPONLY;
}
ConVar *pVar = new ConVar(name, defval, newflags, help);
g_SMConVarAccessor.RegisterConCommandBase(pVar);
return pVar;
}
bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
{
if (baseFs == NULL)
{
return false;
}
KeyValues *pValues;
const char *plugin_file, *p_alias;
pValues = new KeyValues("Metamod Plugin");
if (!pValues->LoadFromFile(baseFs, file))
{
pValues->deleteThis();
return false;
}
if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
{
pValues->deleteThis();
return false;
}
UTIL_Format(path, path_len, "%s", plugin_file);
if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
{
UTIL_Format(alias, alias_len, "%s", p_alias);
}
else
{
UTIL_Format(alias, alias_len, "");
}
pValues->deleteThis();
return true;
}
#if SOURCE_ENGINE >= SE_ORANGEBOX
class GlobCommand : public IMetamodSourceCommandInfo
{
public:
GlobCommand(const CCommand *cmd) : m_cmd(cmd)
{
}
public:
unsigned int GetArgCount()
{
return m_cmd->ArgC() - 1;
}
const char *GetArg(unsigned int num)
{
return m_cmd->Arg(num);
}
const char *GetArgString()
{
return m_cmd->ArgS();
}
private:
const CCommand *m_cmd;
};
#else
class GlobCommand : public IMetamodSourceCommandInfo
{
public:
unsigned int GetArgCount()
{
return engine->Cmd_Argc() - 1;
}
const char *GetArg(unsigned int num)
{
return engine->Cmd_Argv(num);
}
const char *GetArgString()
{
return engine->Cmd_Args();
}
};
#endif
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_DOTA// || SOURCE_ENGINE == SE_SOURCE2
void LocalCommand_Meta(const CCommandContext &context, const CCommand &args)
{
GlobCommand cmd(&args);
#elif SOURCE_ENGINE >= SE_ORANGEBOX
2009-10-29 06:07:23 +01:00
void LocalCommand_Meta(const CCommand &args)
{
GlobCommand cmd(&args);
#else
void LocalCommand_Meta()
{
GlobCommand cmd;
#endif
Command_Meta(&cmd);
}
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_DOTA || SOURCE_ENGINE == SE_SOURCE2
void ClientCommand(CEntityIndex index, const CCommand &_cmd)
{
int client = index.Get();
GlobCommand cmd(&_cmd);
#elif SOURCE_ENGINE >= SE_ORANGEBOX
void ClientCommand(edict_t *client, const CCommand &_cmd)
2009-10-29 06:07:23 +01:00
{
GlobCommand cmd(&_cmd);
#else
void ClientCommand(edict_t *client)
2009-10-29 06:07:23 +01:00
{
GlobCommand cmd;
#endif
if (strcmp(cmd.GetArg(0), "meta") == 0)
{
Command_ClientMeta(client, &cmd);
2009-10-29 06:07:23 +01:00
RETURN_META(MRES_SUPERCEDE);
}
RETURN_META(MRES_IGNORED);
}
2015-07-09 03:39:28 +02:00
#if SOURCE_ENGINE == SE_CSGO || SOURCE_ENGINE == SE_DOTA || SOURCE_ENGINE == SE_SOURCE2
void CacheUserMessages()
{
}
#else
/* This only gets called if IServerGameDLL::GetUserMessageInfo() triggers it */
void Detour_Error(const tchar *pMsg, ...)
2009-10-29 06:07:23 +01:00
{
/* Jump back to setjmp() in CacheUserMessages() */
longjmp(usermsg_end, 1);
}
2009-10-29 06:07:23 +01:00
#define IA32_JMP_IMM32 0xE9
2009-10-29 06:07:23 +01:00
/* IServerGameDLL::GetUserMessageInfo() crashes on games based on the old engine and
* early Orange Box. This is because Error() from tier0 gets called when a bad index is
* passed. This is all due to a bug in CUtlRBTree::IsValidIndex().
*
* So we detour Error() to fix this. Our detour then jumps back into CacheUserMessages()
* to a point before GetUserMessageInfo() is called. The detour is then removed and we
* exit.
*/
void CacheUserMessages()
2009-10-29 06:07:23 +01:00
{
int q, size;
char buffer[256];
unsigned char *target, *detour;
unsigned char orig_bytes[5];
2009-10-29 06:07:23 +01:00
target = (unsigned char *)&Error;
detour = (unsigned char *)&Detour_Error;
2009-10-29 06:07:23 +01:00
/* Save bytes from target function */
memcpy(orig_bytes, target, sizeof(orig_bytes));
2009-10-29 06:07:23 +01:00
/* Patch in relative jump to our Error() detour */
SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
target[0] = IA32_JMP_IMM32;
*(int32_t *)&target[1] = (int32_t)(detour - (target + 5));
2009-10-29 06:07:23 +01:00
/* This is where longjmp() will end up */
if (setjmp(usermsg_end))
2009-10-29 06:07:23 +01:00
{
/* Restore bytes and memory protection */
memcpy(target, orig_bytes, sizeof(orig_bytes));
SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_EXEC);
return;
2009-10-29 06:07:23 +01:00
}
q = 0;
2009-10-29 06:07:23 +01:00
/* If GetUserMessageInfo() calls Error(), we should end up in our detour */
while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
2009-10-29 06:07:23 +01:00
{
usermsgs_list.push_back(UsrMsgInfo(size, buffer));
q++;
2009-10-29 06:07:23 +01:00
}
/* Jump back to setjmp() */
longjmp(usermsg_end, 1);
2009-10-29 06:07:23 +01:00
}
#endif