From 537953200e3b3f2c1b8c0b2f25641f1cc55fc670 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Sun, 5 Nov 2017 08:42:51 -0500 Subject: [PATCH] Fix crash saving KeyValues to file on games using older SDK 2013 version (#38) Fixes https://github.com/alliedmodders/sourcemod/issues/666 The tier1 lib will need to be recompiled for all platforms as well. --- tier1/KeyValues.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/tier1/KeyValues.cpp b/tier1/KeyValues.cpp index 004eb024..89d53c5e 100644 --- a/tier1/KeyValues.cpp +++ b/tier1/KeyValues.cpp @@ -45,6 +45,30 @@ void KVStringDelete(void* pMem) MemAlloc_Free(pMem); } +static bool BKeyValuesSystemSupportsCache() +{ + static bool s_bSupportsCache = false; + static bool s_bCheckedForCacheSupport = false; + + if (!s_bCheckedForCacheSupport) + { + // Use Sys_LoadModule to resolve actual bin name. + CSysModule *pTier0 = Sys_LoadModule("tier0"); + if (pTier0) + { +#ifdef _WIN32 + s_bSupportsCache = !!GetProcAddress(reinterpret_cast(pTier0), "HushAsserts"); +#elif defined(POSIX) + s_bSupportsCache = !!dlsym(reinterpret_cast(pTier0), "HushAsserts"); +#endif + } + + s_bCheckedForCacheSupport = true; + } + + return s_bSupportsCache; +} + static const char * s_LastFileLoadingFrom = "unknown"; // just needed for error messages // Statics for the growable string table @@ -766,8 +790,13 @@ bool KeyValues::SaveToFile( IBaseFileSystem *filesystem, const char *resourceNam return false; } - KeyValuesSystem()->InvalidateCacheForFile( resourceName, pathID ); - if ( bCacheResult ) { + bool bSupportsCache = BKeyValuesSystemSupportsCache(); + if (bSupportsCache) + { + KeyValuesSystem()->InvalidateCacheForFile(resourceName, pathID); + } + + if ( bCacheResult && bSupportsCache ) { KeyValuesSystem()->AddFileKeyValuesToCache( this, resourceName, pathID ); } RecursiveSaveToFile(filesystem, f, NULL, 0, sortKeys, bAllowEmptyString );