1.1 --- a/loader/gamedll.cpp Sun Nov 23 21:25:45 2008 -0600
1.2 +++ b/loader/gamedll.cpp Sun Nov 23 21:50:17 2008 -0600
1.3 @@ -25,6 +25,7 @@
1.4 static IServerGameDLL *gamedll_iface = NULL;
1.5 static QueryValveInterface gamedll_qvi = NULL;
1.6 static int gamedll_version = 0;
1.7 +static int isgd_shutdown_index = -1;
1.8
1.9 #if defined _WIN32
1.10 #define TIER0_NAME "bin\\tier0.dll"
1.11 @@ -213,7 +214,11 @@
1.12 static void
1.13 mm_PatchDllInit(bool patch);
1.14
1.15 -static void *isgd_orig_call = NULL;
1.16 +static void
1.17 +mm_PatchDllShutdown();
1.18 +
1.19 +static void *isgd_orig_init = NULL;
1.20 +static void *isgd_orig_shutdown = NULL;
1.21
1.22 class VEmptyClass
1.23 {
1.24 @@ -286,7 +291,7 @@
1.25 #if defined _WIN32
1.26 void *addr;
1.27 } u;
1.28 - u.addr = isgd_orig_call;
1.29 + u.addr = isgd_orig_init;
1.30 #else
1.31 struct
1.32 {
1.33 @@ -294,7 +299,7 @@
1.34 intptr_t adjustor;
1.35 } s;
1.36 } u;
1.37 - u.s.addr = isgd_orig_call;
1.38 + u.s.addr = isgd_orig_init;
1.39 u.s.adjustor = 0;
1.40 #endif
1.41 result = (((VEmptyClass *)gamedll_iface)->*u.mfpnew)(engineFactory,
1.42 @@ -316,13 +321,47 @@
1.43 }
1.44 else if (gamedll_bridge != NULL)
1.45 {
1.46 - gamedll_bridge->DLLInit_Post();
1.47 + gamedll_bridge->DLLInit_Post(&isgd_shutdown_index);
1.48 + assert(isgd_shutdown_index != -1);
1.49 + mm_PatchDllShutdown();
1.50 }
1.51
1.52 mm_PatchDllInit(false);
1.53
1.54 return result;
1.55 }
1.56 +
1.57 + virtual void DLLShutdown()
1.58 + {
1.59 + gamedll_bridge->Unload();
1.60 + gamedll_bridge = NULL;
1.61 + mm_UnloadMetamodLibrary();
1.62 +
1.63 + /* Call original function */
1.64 + {
1.65 + union
1.66 + {
1.67 + void (VEmptyClass::*mfpnew)();
1.68 +#if defined _WIN32
1.69 + void *addr;
1.70 + } u;
1.71 + u.addr = isgd_orig_shutdown;
1.72 +#else
1.73 + struct
1.74 + {
1.75 + void *addr;
1.76 + intptr_t adjustor;
1.77 + } s;
1.78 + } u;
1.79 + u.s.addr = isgd_orig_shutdown;
1.80 + u.s.adjustor = 0;
1.81 +#endif
1.82 + (((VEmptyClass *)gamedll_iface)->*u.mfpnew)();
1.83 + }
1.84 +
1.85 + mm_UnloadLibrary(gamedll_lib);
1.86 + gamedll_lib = NULL;
1.87 + }
1.88 };
1.89
1.90 static IServerGameDLL isgd_thunk;
1.91 @@ -349,19 +388,39 @@
1.92
1.93 if (patch)
1.94 {
1.95 - assert(isgd_orig_call == NULL);
1.96 - isgd_orig_call = vtable_dest[mfp.vtblindex];
1.97 + assert(isgd_orig_init == NULL);
1.98 + isgd_orig_init = vtable_dest[mfp.vtblindex];
1.99 vtable_dest[mfp.vtblindex] = vtable_src[mfp.vtblindex];
1.100 }
1.101 else
1.102 {
1.103 - assert(isgd_orig_call != NULL);
1.104 - vtable_dest[mfp.vtblindex] = isgd_orig_call;
1.105 - isgd_orig_call = NULL;
1.106 + assert(isgd_orig_init != NULL);
1.107 + vtable_dest[mfp.vtblindex] = isgd_orig_init;
1.108 + isgd_orig_init = NULL;
1.109 }
1.110 }
1.111
1.112 static void
1.113 +mm_PatchDllShutdown()
1.114 +{
1.115 + void **vtable_src;
1.116 + void **vtable_dest;
1.117 + SourceHook::MemFuncInfo mfp;
1.118 +
1.119 + mfp.isVirtual = false;
1.120 + SourceHook::GetFuncInfo(&IServerGameDLL::DLLShutdown, mfp);
1.121 + assert(mfp.isVirtual);
1.122 + assert(mfp.thisptroffs == 0);
1.123 + assert(mfp.vtbloffs == 0);
1.124 +
1.125 + vtable_src = (void **)*(void **)&isgd_thunk;
1.126 + vtable_dest = (void **)*(void **)gamedll_iface;
1.127 +
1.128 + isgd_orig_shutdown = vtable_dest[isgd_shutdown_index];
1.129 + vtable_dest[isgd_shutdown_index] = vtable_src[mfp.vtblindex];
1.130 +}
1.131 +
1.132 +static void
1.133 mm_PrepForGameLoad()
1.134 {
1.135 mm_PatchDllInit(true);