core/gamedll_bridge.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Dec 10 00:59:50 2009 -0600 (2009-12-10)
changeset 689 4065ba5e9155
parent 683 b507295429f9
permissions -rw-r--r--
Rewrote user message caching code to always use IServerGameDLL::GetUserMessageInfo (bug 4117, r=dvander).

Games where the above function can crash are handled by detouring tier0's Error() function and jumping back to our own code. So in theory this should work on all games now.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2009 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  */
    25 
    26 #include <assert.h>
    27 #include "metamod.h"
    28 #include "metamod_plugins.h"
    29 #include "metamod_util.h"
    30 #include <loader_bridge.h>
    31 #include "provider/provider_ep2.h"
    32 
    33 using namespace SourceMM;
    34 
    35 class GameDllBridge : public IGameDllBridge
    36 {
    37 public:
    38 	virtual bool DLLInit_Pre(const gamedll_bridge_info *info, char *buffer, size_t maxlength)
    39 	{
    40 		if (!mm_DetectGameInformation())
    41 		{
    42 			UTIL_Format(buffer, maxlength, "Metamod:Source failed to detect game paths; cannot load.");
    43 			return false;
    44 		}
    45 
    46 		server = (IServerGameDLL *)info->isgd;
    47 		g_Metamod.SetGameDLLInfo((CreateInterfaceFn)info->gsFactory,
    48 								 info->dllVersion,
    49 								 true);
    50 		g_Metamod.SetVSPListener(info->vsp_listener_path);
    51 		mm_InitializeGlobals((CreateInterfaceFn)info->engineFactory,
    52 							 (CreateInterfaceFn)info->physicsFactory,
    53 							 (CreateInterfaceFn)info->fsFactory,
    54 							 (CGlobalVars*)info->pGlobals);
    55 		mm_InitializeForLoad();
    56 		mm_StartupMetamod(false);
    57 
    58 		return true;
    59 	}
    60 	virtual void DLLInit_Post(int *isgdUnload)
    61 	{
    62 		SourceHook::MemFuncInfo mfi;
    63 
    64 		mfi.isVirtual = false;
    65 		SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
    66 		assert(mfi.isVirtual);
    67 		assert(mfi.vtbloffs == 0);
    68 		assert(mfi.thisptroffs == 0);
    69 		*isgdUnload = mfi.vtblindex;
    70 
    71 		g_PluginMngr.SetAllLoaded();
    72 	}
    73 	virtual void *QueryInterface(const char *iface, int *ret)
    74 	{
    75 		return g_Metamod.GetServerFactory(true)(iface, ret);
    76 	}
    77 	virtual void Unload()
    78 	{
    79 		mm_UnloadMetamod();
    80 	}
    81 };
    82 
    83 GameDllBridge mm16_gamedll_bridge;
    84 
    85 SMM_API IGameDllBridge *
    86 GetGameDllBridge()
    87 {
    88 	return &mm16_gamedll_bridge;
    89 }
    90