core-legacy/gamedll_bridge.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:26:09 2014 -0400 (2014-05-21)
changeset 917 95639ee45d3d
parent 608 30b546cc4e03
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 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  * Version: $Id$
    26  */
    27 
    28 #include <assert.h>
    29 #include "sourcemm.h"
    30 #include "CPlugin.h"
    31 #include <loader_bridge.h>
    32 
    33 using namespace SourceMM;
    34 
    35 #define	IFACE_MACRO(orig,nam) \
    36 	CPluginManager::CPlugin *pl; \
    37 	SourceHook::List<CPluginEventHandler>::iterator event; \
    38 	IMetamodListener *api; \
    39 	int mret = 0; \
    40 	void *val = NULL; \
    41 	for (PluginIter iter = g_PluginMngr._begin(); iter != g_PluginMngr._end(); iter++) { \
    42 		pl = (*iter); \
    43 		for (event=pl->m_Events.begin(); event!=pl->m_Events.end(); event++) { \
    44 			api = (*event).event; \
    45 			mret = IFACE_FAILED; \
    46 			if ( (val=api->On##nam##Query(iface, &mret)) != NULL ) { \
    47 				if (ret) *ret = mret; \
    48 				return val; \
    49 			} \
    50 		} \
    51 	} \
    52 	return (orig)(iface, ret);
    53 
    54 
    55 class GameDllBridge : public IGameDllBridge
    56 {
    57 public:
    58 	virtual bool DLLInit_Pre(const gamedll_bridge_info *info, char *buffer, size_t maxlength)
    59 	{
    60 		assert(!g_GameDll.loaded && !g_bIsBridgedAsVsp);
    61 		LoadAsGameDLL(info);
    62 		return true;
    63 	}
    64 	virtual void DLLInit_Post(int *isgdUnload)
    65 	{
    66 		SourceHook::MemFuncInfo mfi;
    67 
    68 		mfi.isVirtual = false;
    69 		SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfi);
    70 		assert(mfi.isVirtual);
    71 		assert(mfi.vtbloffs == 0);
    72 		assert(mfi.thisptroffs == 0);
    73 		*isgdUnload = mfi.vtblindex;
    74 
    75 		g_PluginMngr.SetAllLoaded();
    76 	}
    77 	virtual void *QueryInterface(const char *iface, int *ret)
    78 	{
    79 		/* We use this interface for responding to the meta client command */
    80 		if (strncmp(iface, "ServerGameClients", 17) == 0)
    81 		{
    82 			void *ptr = (g_GameDll.factory)(iface, ret);
    83 			g_GameDll.pGameClients = static_cast<IServerGameClients *>(ptr);
    84 			return ptr;
    85 		}
    86 
    87 		/* If we got here, there's definitely a GameDLL */
    88 		IFACE_MACRO(g_GameDll.factory, GameDLL);
    89 	}
    90 	virtual void Unload()
    91 	{
    92 		UnloadMetamod(true);
    93 	}
    94 };
    95 
    96 GameDllBridge mm14_gamedll_bridge;
    97 
    98 SMM_API IGameDllBridge *
    99 GetGameDllBridge()
   100 {
   101 	return &mm14_gamedll_bridge;
   102 }
   103 
   104 void *ServerFactory(const char *name, int *code)
   105 {
   106 	return mm14_gamedll_bridge.QueryInterface(name, code);
   107 }
   108 
   109 /* Wrapper function.  This is called when the GameDLL thinks it's using
   110  * the engine's real engineFactory.
   111  */
   112 void *EngineFactory(const char *iface, int *ret)
   113 {
   114 	IFACE_MACRO(g_Engine.engineFactory, Engine);
   115 }
   116 
   117 /* Wrapper function.  This is called when the GameDLL thinks it's using
   118  * the engine's real physicsFactory.
   119  */
   120 void *PhysicsFactory(const char *iface, int *ret)
   121 {
   122 	IFACE_MACRO(g_Engine.physicsFactory, Physics);
   123 }
   124 
   125 /* Wrapper function.  This is called when the GameDLL thinks it's using
   126  * the engine's real fileSystemFactory.
   127  */
   128 void *FileSystemFactory(const char *iface, int *ret)
   129 {
   130 	IFACE_MACRO(g_Engine.fileSystemFactory, FileSystem);
   131 }
   132 
   133