providers/episode1/provider_ep1.cpp
branchsourcemm-1.6.0
changeset 357 1319b9ef1c8a
child 358 a4de8673bdbd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/providers/episode1/provider_ep1.cpp	Thu Sep 27 19:28:30 2007 +0000
     1.3 @@ -0,0 +1,264 @@
     1.4 +#include "convar_smm.h"
     1.5 +#include <eiface.h>
     1.6 +#include <tier0/icommandline.h>
     1.7 +#include <sourcehook/sourcehook.h>
     1.8 +#include "provider_util.h"
     1.9 +#include "provider_ep1.h"
    1.10 +#include "console.h"
    1.11 +
    1.12 +/* Types */
    1.13 +typedef void (*CONPRINTF_FUNC)(const char *, ...);
    1.14 +/* Imports */
    1.15 +#undef CommandLine
    1.16 +DLL_IMPORT ICommandLine *CommandLine();
    1.17 +/* Functions */
    1.18 +CONPRINTF_FUNC ExtractRemotePrinter();
    1.19 +/* Variables */
    1.20 +CONPRINTF_FUNC echo_msg_func = NULL;
    1.21 +IVEngineServer *engine = NULL;
    1.22 +IServerGameDLL *server = NULL;
    1.23 +ICvar *icvar = NULL;
    1.24 +
    1.25 +void BaseProvider::ConsolePrint(const char *str)
    1.26 +{
    1.27 +	if (echo_msg_func != NULL)
    1.28 +	{
    1.29 +		echo_msg_func("%s", str);
    1.30 +	}
    1.31 +	else
    1.32 +	{
    1.33 +		Msg("%s", str);
    1.34 +	}
    1.35 +}
    1.36 +
    1.37 +void BaseProvider::Notify_DLLInit_Pre()
    1.38 +{
    1.39 +	echo_msg_func = ExtractRemotePrinter();	
    1.40 +}
    1.41 +
    1.42 +bool BaseProvider::IsRemotePrintingAvailable()
    1.43 +{
    1.44 +	return (echo_msg_func != NULL);
    1.45 +}
    1.46 +
    1.47 +void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
    1.48 +{
    1.49 +	engine->ClientPrintf(client, message);
    1.50 +}
    1.51 +
    1.52 +void BaseProvider::ServerCommand(const char *cmd)
    1.53 +{
    1.54 +	engine->ServerCommand(cmd);
    1.55 +}
    1.56 +
    1.57 +const char *BaseProvider::GetConVarString(ConVar *convar)
    1.58 +{
    1.59 +	return convar->GetString();
    1.60 +}
    1.61 +
    1.62 +bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
    1.63 +{
    1.64 +	return pCommand->IsCommand();
    1.65 +}
    1.66 +
    1.67 +
    1.68 +bool BaseProvider::IsSourceEngineBuildCompatible(int build)
    1.69 +{
    1.70 +	return (build == SOURCE_ENGINE_ORIGINAL
    1.71 +			|| build == SOURCE_ENGINE_EPISODEONE);
    1.72 +}
    1.73 +
    1.74 +const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
    1.75 +{
    1.76 +	if (key[0] == '-' || key[0] == '+')
    1.77 +	{
    1.78 +		return CommandLine()->ParmValue(key, defval);
    1.79 +	}
    1.80 +	else
    1.81 +	{
    1.82 +		const char *val;
    1.83 +		if ((val = icvar->GetCommandLineValue(key)) == NULL)
    1.84 +		{
    1.85 +			return defval;
    1.86 +		}
    1.87 +
    1.88 +		return val;
    1.89 +	}
    1.90 +}
    1.91 +
    1.92 +int BaseProvider::TryServerGameDLL(const char *iface)
    1.93 +{
    1.94 +	if (strncmp(iface, "ServerGameDLL", 13) != 0)
    1.95 +	{
    1.96 +		return 0;
    1.97 +	}
    1.98 +
    1.99 +	return atoi(&iface[13]);
   1.100 +}
   1.101 +
   1.102 +bool BaseProvider::LogMessage(const char *buffer)
   1.103 +{
   1.104 +	if (!engine)
   1.105 +	{
   1.106 +		return false;
   1.107 +	}
   1.108 +
   1.109 +	engine->LogPrint(buffer);
   1.110 +
   1.111 +	return true;
   1.112 +}
   1.113 +
   1.114 +bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
   1.115 +{
   1.116 +	SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
   1.117 +
   1.118 +	if (hook == ProvidedHook_LevelInit)
   1.119 +	{
   1.120 +		SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
   1.121 +	}
   1.122 +	else if (hook == ProvidedHook_LevelShutdown)
   1.123 +	{
   1.124 +		SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
   1.125 +	}
   1.126 +	else if (hook == ProvidedHook_GameInit)
   1.127 +	{
   1.128 +		SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
   1.129 +	}
   1.130 +	else if (hook == ProvidedHook_DLLShutdown)
   1.131 +	{
   1.132 +		SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
   1.133 +	}
   1.134 +	else if (hook == ProvidedHook_DLLInit)
   1.135 +	{
   1.136 +		SourceHook::GetFuncInfo(&IServerGameDLL::DLLInit, mfi);
   1.137 +	}
   1.138 +
   1.139 +	*pInfo = mfi;
   1.140 +
   1.141 +	return (mfi.thisptroffs >= 0);
   1.142 +}
   1.143 +
   1.144 +void BaseProvider::DisplayError(const char *fmt, ...)
   1.145 +{
   1.146 +	va_list ap;
   1.147 +	char buffer[2048];
   1.148 +
   1.149 +	va_start(ap, fmt);
   1.150 +	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
   1.151 +	va_end(ap);
   1.152 +
   1.153 +	Error(buffer);
   1.154 +}
   1.155 +
   1.156 +void BaseProvider::DisplayWarning(const char *fmt, ...)
   1.157 +{
   1.158 +	va_list ap;
   1.159 +	char buffer[2048];
   1.160 +
   1.161 +	va_start(ap, fmt);
   1.162 +	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
   1.163 +	va_end(ap);
   1.164 +
   1.165 +	Warning(buffer);
   1.166 +}
   1.167 +
   1.168 +IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
   1.169 +{
   1.170 +	return &g_SMConVarAccessor;
   1.171 +}
   1.172 +
   1.173 +bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
   1.174 +{
   1.175 +	return g_SMConVarAccessor.Register(pCommand);
   1.176 +}
   1.177 +
   1.178 +void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
   1.179 +{
   1.180 +	return g_SMConVarAccessor.Unregister(pCommand);
   1.181 +}
   1.182 +
   1.183 +//////////////////////////////////////////////////////////////////////////
   1.184 +//THERE BE HAX HERE!!!! DON'T TELL ALFRED, BUT GABE WANTED IT THAT WAY. //
   1.185 +// (note: you can find the offset by looking for the text               //
   1.186 +//   "Echo text to console", you'll find the callback cmd pushed on the //
   1.187 +//   stack.)                                                            //
   1.188 +//////////////////////////////////////////////////////////////////////////
   1.189 +
   1.190 +#define SIGLEN			8
   1.191 +#define ENGINE486_SIG	"\x55\x89\xE5\x53\x83\xEC\x14\xBB"
   1.192 +#define ENGINE486_OFFS	40
   1.193 +#define ENGINE686_SIG	"\x53\x83\xEC\x08\xBB\x01\x00\x00"
   1.194 +#define ENGINE686_OFFS	50
   1.195 +#define	ENGINEAMD_SIG	"\x53\x51\xBB\x01\x00\x00\x00\x51"
   1.196 +#define	ENGINEAMD_OFFS	47
   1.197 +#define ENGINEW32_SIG	"\xA1\x2A\x2A\x2A\x2A\x56\xBE\x01"
   1.198 +#define ENGINEW32_OFFS	38
   1.199 +#define IA32_CALL		0xE8
   1.200 +
   1.201 +bool vcmp(const void *_addr1, const void *_addr2, size_t len)
   1.202 +{
   1.203 +	unsigned char *addr1 = (unsigned char *)_addr1;
   1.204 +	unsigned char *addr2 = (unsigned char *)_addr2;
   1.205 +
   1.206 +	for (size_t i=0; i<len; i++)
   1.207 +	{
   1.208 +		if (addr2[i] == '*')
   1.209 +			continue;
   1.210 +		if (addr1[i] != addr2[i])
   1.211 +			return false;
   1.212 +	}
   1.213 +
   1.214 +	return true;
   1.215 +}
   1.216 +
   1.217 +//Thanks to fysh for the idea of extracting info from "echo" and for
   1.218 +// having the original offsets at hand!
   1.219 +CONPRINTF_FUNC ExtractRemotePrinter()
   1.220 +{
   1.221 +	ConCommandBase *pBase = icvar->GetCommands();
   1.222 +	unsigned char *ptr = NULL;
   1.223 +	FnCommandCallback callback = NULL;
   1.224 +	int offs = 0;
   1.225 +
   1.226 +	while (pBase)
   1.227 +	{
   1.228 +		if ( strcmp(pBase->GetName(), "echo") == 0 )
   1.229 +		{
   1.230 +			callback = ((ConCommand *)pBase)->GetCallback();
   1.231 +			ptr = (unsigned char *)callback;
   1.232 +#ifdef OS_LINUX
   1.233 +			if (vcmp(ptr, ENGINE486_SIG, SIGLEN))
   1.234 +			{
   1.235 +				offs = ENGINE486_OFFS;
   1.236 +			}
   1.237 +			else if (vcmp(ptr, ENGINE686_SIG, SIGLEN))
   1.238 +			{
   1.239 +				offs = ENGINE686_OFFS;
   1.240 +			}
   1.241 +			else if (vcmp(ptr, ENGINEAMD_SIG, SIGLEN))
   1.242 +			{
   1.243 +				offs = ENGINEAMD_OFFS;
   1.244 +			}
   1.245 +#elif defined OS_WIN32 // Only one Windows engine binary so far...
   1.246 +			if (vcmp(ptr, ENGINEW32_SIG, SIGLEN))
   1.247 +			{
   1.248 +				offs = ENGINEW32_OFFS;
   1.249 +			}
   1.250 +#endif
   1.251 +
   1.252 +			if (!offs || ptr[offs - 1] != IA32_CALL)
   1.253 +			{
   1.254 +				return NULL;
   1.255 +			}
   1.256 +			//get the relative offset
   1.257 +			void *addr = *((void **)(ptr + offs));
   1.258 +			//add the base offset, to the ip (which is the address+offset + 4 bytes for next instruction)
   1.259 +			return (CONPRINTF_FUNC)((unsigned long)addr + (unsigned long)(ptr + offs) + 4);
   1.260 +		}
   1.261 +		pBase = const_cast<ConCommandBase *>(pBase->GetNext());
   1.262 +	}
   1.263 +
   1.264 +	return NULL;
   1.265 +}
   1.266 +
   1.267 +