sourcemm/sourcemm.cpp
changeset 12 9fd53562f68c
parent 9 01b8b7435e67
child 14 a3f323f6f91d
     1.1 --- a/sourcemm/sourcemm.cpp	Sat Apr 16 23:35:37 2005 +0000
     1.2 +++ b/sourcemm/sourcemm.cpp	Sun Apr 17 03:37:50 2005 +0000
     1.3 @@ -31,13 +31,18 @@
     1.4  GameDllInfo g_GameDll = {false, NULL, NULL};
     1.5  EngineInfo g_Engine = {NULL, NULL, NULL, NULL};
     1.6  SourceHook::CSourceHookImpl g_SourceHook;
     1.7 +SourceHook::ISourceHook *g_SHPtr;
     1.8  std::string g_ModPath;
     1.9  std::string g_BinPath;
    1.10 +PluginId g_PLID = Pl_Console;		//Technically, SourceMM is the "Console" plugin... :p
    1.11  
    1.12  ///////////////////////////////////
    1.13  // Main code for HL2 Interaction //
    1.14  ///////////////////////////////////
    1.15  
    1.16 +void DLLShutdown_handler(void);
    1.17 +SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, 0);
    1.18 +
    1.19  //This is where the magic happens
    1.20  SMM_API void *CreateInterface(const char *name, int *ret)
    1.21  {
    1.22 @@ -94,6 +99,9 @@
    1.23  {
    1.24  	if (!g_GameDll.loaded)
    1.25  	{
    1.26 +		//Initialize SourceHook
    1.27 +		g_SHPtr = static_cast<SourceHook::ISourceHook *>(&g_SourceHook);
    1.28 +
    1.29  		//The gamedll isn't loaded yet.  We need to find out where it's hiding.
    1.30  		IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
    1.31  		if (!ive)
    1.32 @@ -151,6 +159,8 @@
    1.33  				return false;
    1.34  			}
    1.35  
    1.36 +			g_GameDll.serverGameDLL = serverDll;
    1.37 +
    1.38  			//Set this information early in case our wrappers are called somehow
    1.39  			g_Engine.engineFactory = engineFactory;
    1.40  			g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL);
    1.41 @@ -240,6 +250,9 @@
    1.42  			//Everything's done.
    1.43  			g_GameDll.loaded = true;
    1.44  
    1.45 +			//Initialize our shutdown hook
    1.46 +			SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, serverDll, DLLShutdown_handler, false);
    1.47 +
    1.48  			//Initialize our console hooks
    1.49  			ConCommandBaseMgr::OneTimeInit(static_cast<IConCommandBaseAccessor *>(&g_SMConVarAccessor));
    1.50              
    1.51 @@ -263,6 +276,29 @@
    1.52  	return false;
    1.53  }
    1.54  
    1.55 +void DLLShutdown_handler(void)
    1.56 +{
    1.57 +	//It's time for us to shut down too!
    1.58 +	g_PluginMngr.UnloadAll();
    1.59 +
    1.60 +	//Call the DLL...
    1.61 +	g_GameDll.serverGameDLL->DLLShutdown();
    1.62 +
    1.63 +	//Unload the DLL forcefully
    1.64 +	dlclose(g_GameDll.lib);
    1.65 +	memset(&g_GameDll, 0, sizeof(GameDllInfo));
    1.66 +
    1.67 +	//For now, I'm not gonna bother freeing the memory allocated above.
    1.68 +	//Why?
    1.69 +	// 1.  We're exiting the application (I should hope!)
    1.70 +	// 2.  If we're not exiting, we just deallocated the gamedll, so we're about to crash out ANYWAY
    1.71 +	// 3.  We never saved the original vtable pointers, and we'd have to copy them back to get our destructors.
    1.72 +	//Soooo... we'll just accept our fate here.
    1.73 +
    1.74 +	//DON'T CALL THE GAMEDLL! IT'S GONE!  pinin' for the fjords
    1.75 +	RETURN_META(MRES_SUPERCEDE);
    1.76 +}
    1.77 +
    1.78  int LoadPluginsFromFile(const char *file)
    1.79  {
    1.80  	FILE *fp;