diff -r c89148628c7b -r 9fd53562f68c sourcemm/sourcemm.cpp --- a/sourcemm/sourcemm.cpp Sat Apr 16 23:35:37 2005 +0000 +++ b/sourcemm/sourcemm.cpp Sun Apr 17 03:37:50 2005 +0000 @@ -31,13 +31,18 @@ GameDllInfo g_GameDll = {false, NULL, NULL}; EngineInfo g_Engine = {NULL, NULL, NULL, NULL}; SourceHook::CSourceHookImpl g_SourceHook; +SourceHook::ISourceHook *g_SHPtr; std::string g_ModPath; std::string g_BinPath; +PluginId g_PLID = Pl_Console; //Technically, SourceMM is the "Console" plugin... :p /////////////////////////////////// // Main code for HL2 Interaction // /////////////////////////////////// +void DLLShutdown_handler(void); +SH_DECL_HOOK0_void(IServerGameDLL, DLLShutdown, SH_NOATTRIB, 0); + //This is where the magic happens SMM_API void *CreateInterface(const char *name, int *ret) { @@ -94,6 +99,9 @@ { if (!g_GameDll.loaded) { + //Initialize SourceHook + g_SHPtr = static_cast(&g_SourceHook); + //The gamedll isn't loaded yet. We need to find out where it's hiding. IVEngineServer *ive = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL)); if (!ive) @@ -151,6 +159,8 @@ return false; } + g_GameDll.serverGameDLL = serverDll; + //Set this information early in case our wrappers are called somehow g_Engine.engineFactory = engineFactory; g_Engine.icvar = (ICvar *)(g_Engine.engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL); @@ -240,6 +250,9 @@ //Everything's done. g_GameDll.loaded = true; + //Initialize our shutdown hook + SH_ADD_HOOK_STATICFUNC(IServerGameDLL, DLLShutdown, serverDll, DLLShutdown_handler, false); + //Initialize our console hooks ConCommandBaseMgr::OneTimeInit(static_cast(&g_SMConVarAccessor)); @@ -263,6 +276,29 @@ return false; } +void DLLShutdown_handler(void) +{ + //It's time for us to shut down too! + g_PluginMngr.UnloadAll(); + + //Call the DLL... + g_GameDll.serverGameDLL->DLLShutdown(); + + //Unload the DLL forcefully + dlclose(g_GameDll.lib); + memset(&g_GameDll, 0, sizeof(GameDllInfo)); + + //For now, I'm not gonna bother freeing the memory allocated above. + //Why? + // 1. We're exiting the application (I should hope!) + // 2. If we're not exiting, we just deallocated the gamedll, so we're about to crash out ANYWAY + // 3. We never saved the original vtable pointers, and we'd have to copy them back to get our destructors. + //Soooo... we'll just accept our fate here. + + //DON'T CALL THE GAMEDLL! IT'S GONE! pinin' for the fjords + RETURN_META(MRES_SUPERCEDE); +} + int LoadPluginsFromFile(const char *file) { FILE *fp;