sourcemm/sourcemm.cpp
changeset 14 a3f323f6f91d
parent 12 9fd53562f68c
child 15 6c861b24941b
     1.1 --- a/sourcemm/sourcemm.cpp	Sun Apr 17 04:21:02 2005 +0000
     1.2 +++ b/sourcemm/sourcemm.cpp	Sun Apr 17 18:43:21 2005 +0000
     1.3 @@ -24,10 +24,10 @@
     1.4   * @file sourcemm.cpp
     1.5   */
     1.6  
     1.7 -CServerGameDLL *g_TempGameDLL = NULL;
     1.8 -CServerGameEnts *g_TempGameEnts = NULL;
     1.9 -CServerGameClients *g_TempGameClients = NULL;
    1.10 -CHLTVDirector *g_TempDirector = NULL;
    1.11 +CServerGameDLL g_TempGameDLL;
    1.12 +CServerGameEnts g_TempGameEnts;
    1.13 +CServerGameClients g_TempGameClients;
    1.14 +CHLTVDirector g_TempDirector;
    1.15  GameDllInfo g_GameDll = {false, NULL, NULL};
    1.16  EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
    1.17  SourceHook::CSourceHookImpl g_SourceHook;
    1.18 @@ -52,38 +52,29 @@
    1.19  		// Currently, HL2 Engine has loaded Metamod:Source
    1.20  		// It is now asking it to get an interface.  We don't have one,
    1.21  		//  so we're gonna try to give it a fake one to get the information we need.
    1.22 -		//  Then we'll swap the vtables with the real one from the real gamedll.
    1.23 +		//  Then, the the interface will forward the calls to the original interface
    1.24  
    1.25  		if (strcmp(name, INTERFACEVERSION_SERVERGAMEDLL) == 0)
    1.26  		{
    1.27  			//We're in.  Give the server our fake class as bait.
    1.28  			if (ret)
    1.29  				*ret = IFACE_OK;
    1.30 -
    1.31 -			g_TempGameDLL = new CServerGameDLL;
    1.32 -
    1.33 -			return static_cast<void *>(g_TempGameDLL);
    1.34 +			return static_cast<void *>(&g_TempGameDLL);
    1.35  		} else if (strcmp(name, INTERFACEVERSION_SERVERGAMEENTS) == 0) {
    1.36  			if (ret)
    1.37  				*ret = IFACE_OK;
    1.38  
    1.39 -			g_TempGameEnts = new CServerGameEnts;
    1.40 -
    1.41 -			return static_cast<void *>(g_TempGameEnts);
    1.42 +			return static_cast<void *>(&g_TempGameEnts);
    1.43  		} else if (strcmp(name, INTERFACEVERSION_SERVERGAMECLIENTS) == 0) {
    1.44  			if (ret)
    1.45  				*ret = IFACE_OK;
    1.46  
    1.47 -			g_TempGameClients = new CServerGameClients;
    1.48 -
    1.49 -			return static_cast<void *>(g_TempGameClients);
    1.50 +			return static_cast<void *>(&g_TempGameClients);
    1.51  		} else if (strcmp(name, INTERFACEVERSION_HLTVDIRECTOR) == 0) {
    1.52  			if (ret)
    1.53  				*ret = IFACE_OK;
    1.54  
    1.55 -			g_TempDirector = new CHLTVDirector;
    1.56 -
    1.57 -			return static_cast<void *>(g_TempDirector);
    1.58 +			return static_cast<void *>(&g_TempDirector);
    1.59  		} else {
    1.60  			if (ret)
    1.61  				*ret = IFACE_FAILED;
    1.62 @@ -97,6 +88,9 @@
    1.63  
    1.64  bool CServerGameDLL::DLLInit(CreateInterfaceFn engineFactory, CreateInterfaceFn physicsFactory, CreateInterfaceFn fileSystemFactory, CGlobalVars *pGlobals)
    1.65  {
    1.66 +	if (m_pOrig)
    1.67 +		return m_pOrig->DLLInit(engineFactory, physicsFactory, fileSystemFactory, pGlobals);
    1.68 +
    1.69  	if (!g_GameDll.loaded)
    1.70  	{
    1.71  		//Initialize SourceHook
    1.72 @@ -159,7 +153,7 @@
    1.73  				return false;
    1.74  			}
    1.75  
    1.76 -			g_GameDll.serverGameDLL = serverDll;
    1.77 +			g_GameDll.serverGameDLL_CC = SH_GET_CALLCLASS(IServerGameDLL, serverDll);
    1.78  
    1.79  			//Set this information early in case our wrappers are called somehow
    1.80  			g_Engine.engineFactory = engineFactory;
    1.81 @@ -217,35 +211,12 @@
    1.82  				dlclose(g_GameDll.lib);
    1.83  				return false;
    1.84  			}
    1.85 -
    1.86 -			//The GameDLL has given the go - now we'll swap the vtables ( eww! )
    1.87 -			//This ugly hack will patch the pointer we've given the engine
    1.88 -			// so it will just be calling the real GameDLL instead.
    1.89 -			// Unlike Metamod:HL1, this is the effect we want, rather than duplicating
    1.90 -			//  the entire class (because SourceHook doesn't do that).
    1.91 -			void *vtableDst = ((void *)(g_TempGameDLL));
    1.92 -			void *vtableSrc = ((void *)(serverDll));
    1.93 -
    1.94 -			SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameDLL), SH_MEM_READ|SH_MEM_WRITE);
    1.95 -			memcpy(vtableDst, vtableSrc, sizeof(IServerGameDLL));
    1.96 -
    1.97 -			//Now patch IServerGameEnts
    1.98 -			vtableDst = ((void *)(g_TempGameEnts));
    1.99 -			vtableSrc = ((void *)(serverEnts));
   1.100 -			SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameEnts), SH_MEM_READ|SH_MEM_WRITE);
   1.101 -			memcpy(vtableDst, vtableSrc, sizeof(IServerGameEnts));
   1.102 -
   1.103 -			//Now patch IServerGameClients
   1.104 -			vtableDst = ((void *)(g_TempGameClients));
   1.105 -			vtableSrc = ((void *)(serverClients));
   1.106 -			SourceHook::SetMemAccess(vtableDst, sizeof(IServerGameClients), SH_MEM_READ|SH_MEM_WRITE);
   1.107 -			memcpy(vtableDst, vtableSrc, sizeof(IServerGameClients));
   1.108 -
   1.109 -			//Now patch IHLTVDirector
   1.110 -			vtableDst = ((void *)(g_TempDirector));
   1.111 -			vtableSrc = ((void *)(serverHLTV));
   1.112 -			SourceHook::SetMemAccess(vtableDst, sizeof(IHLTVDirector), SH_MEM_READ|SH_MEM_WRITE);
   1.113 -			memcpy(vtableDst, vtableSrc, sizeof(IHLTVDirector));
   1.114 +			
   1.115 +			// Now tell the global temp classes that they can call the original functions
   1.116 +			g_TempDirector.SetOrig(serverHLTV);
   1.117 +			g_TempGameClients.SetOrig(serverClients);
   1.118 +			g_TempGameEnts.SetOrig(serverEnts);
   1.119 +			g_TempGameDLL.SetOrig(serverDll);
   1.120  
   1.121  			//Everything's done.
   1.122  			g_GameDll.loaded = true;
   1.123 @@ -269,8 +240,7 @@
   1.124  		}
   1.125  	}
   1.126  
   1.127 -	//Somehow, the function got here.  This should be impossible, as not only is it 
   1.128 -	// only called once, but the vtables should be overridden.
   1.129 +	//Somehow, the function got here.  This should be impossible.
   1.130  	Error("Metamod:Source fatal error - IServerGameDLL::DLLInit() called inappropriately");
   1.131  
   1.132  	return false;
   1.133 @@ -282,19 +252,15 @@
   1.134  	g_PluginMngr.UnloadAll();
   1.135  
   1.136  	//Call the DLL...
   1.137 -	g_GameDll.serverGameDLL->DLLShutdown();
   1.138 +	g_GameDll.serverGameDLL_CC->DLLShutdown();
   1.139 +
   1.140 +	// Shutdown sourcehook now
   1.141 +	g_SourceHook.CompleteShutdown();
   1.142  
   1.143  	//Unload the DLL forcefully
   1.144  	dlclose(g_GameDll.lib);
   1.145  	memset(&g_GameDll, 0, sizeof(GameDllInfo));
   1.146  
   1.147 -	//For now, I'm not gonna bother freeing the memory allocated above.
   1.148 -	//Why?
   1.149 -	// 1.  We're exiting the application (I should hope!)
   1.150 -	// 2.  If we're not exiting, we just deallocated the gamedll, so we're about to crash out ANYWAY
   1.151 -	// 3.  We never saved the original vtable pointers, and we'd have to copy them back to get our destructors.
   1.152 -	//Soooo... we'll just accept our fate here.
   1.153 -
   1.154  	//DON'T CALL THE GAMEDLL! IT'S GONE!  pinin' for the fjords
   1.155  	RETURN_META(MRES_SUPERCEDE);
   1.156  }