From f59df243ead6f7f3ef600cbf7fea25a8f2f3b2cc Mon Sep 17 00:00:00 2001 From: Kyle Sanderson Date: Thu, 10 Sep 2015 11:47:46 -0700 Subject: [PATCH] Change FindMap to take a const char* for searching instead of char*. --- core/HalfLife2.cpp | 27 ++++++++++++++++++++------- core/HalfLife2.h | 3 ++- core/smn_halflife.cpp | 13 +++++++++---- plugins/include/halflife.inc | 7 ++++--- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/core/HalfLife2.cpp b/core/HalfLife2.cpp index e280b7be..39a51bd2 100644 --- a/core/HalfLife2.cpp +++ b/core/HalfLife2.cpp @@ -1207,8 +1207,15 @@ const char *CHalfLife2::GetEntityClassname(CBaseEntity *pEntity) return *(const char **)(((unsigned char *)pEntity) + offset); } -SMFindMapResult CHalfLife2::FindMap(char *pMapName, int nMapNameMax) +SMFindMapResult CHalfLife2::FindMap(char *pMapName, size_t nMapNameMax) { + return this->FindMap(pMapName, pMapName, nMapNameMax); +} + +SMFindMapResult CHalfLife2::FindMap(const char *pMapName, char *pFoundMap, size_t nMapNameMax) +{ + ke::SafeStrcpy(pFoundMap, nMapNameMax, pMapName); + #if SOURCE_ENGINE >= SE_LEFT4DEAD static char mapNameTmp[PLATFORM_MAX_PATH]; g_SourceMod.Format(mapNameTmp, sizeof(mapNameTmp), "maps%c%s.bsp", PLATFORM_SEP_CHAR, pMapName); @@ -1247,11 +1254,20 @@ SMFindMapResult CHalfLife2::FindMap(char *pMapName, int nMapNameMax) } else { - ke::SafeStrcpy(pMapName, nMapNameMax, &results[0][helperCmdLen + 1]); + ke::SafeStrcpy(pFoundMap, nMapNameMax, &results[0][helperCmdLen + 1]); return SMFindMapResult::FuzzyMatch; } + #elif SOURCE_ENGINE == SE_TF2 - return static_cast(engine->FindMap(pMapName, nMapNameMax)); + static char szTemp[PLATFORM_MAX_PATH]; + if (pFoundMap == NULL) + { + ke::SafeStrcpy(szTemp, SM_ARRAYSIZE(szTemp), pMapName); + pFoundMap = szTemp; + nMapNameMax = 0; + } + + return static_cast(engine->FindMap(pFoundMap, static_cast(nMapNameMax))); #else return engine->IsMapValid(pMapName) == 0 ? SMFindMapResult::NotFound : SMFindMapResult::Found; #endif @@ -1262,10 +1278,7 @@ bool CHalfLife2::IsMapValid(const char *map) if (!map || !map[0]) return false; - static char szTmp[PLATFORM_MAX_PATH]; - ke::SafeStrcpy(szTmp, sizeof(szTmp), map); - - return FindMap(szTmp, sizeof(szTmp)) != SMFindMapResult::NotFound; + return FindMap(map) != SMFindMapResult::NotFound; } // TODO: Add ep1 support for this. (No IServerTools available there) diff --git a/core/HalfLife2.h b/core/HalfLife2.h index 7756db49..96432723 100644 --- a/core/HalfLife2.h +++ b/core/HalfLife2.h @@ -185,7 +185,8 @@ public: //IGameHelpers const char *GetEntityClassname(edict_t *pEdict); const char *GetEntityClassname(CBaseEntity *pEntity); bool IsMapValid(const char *map); - SMFindMapResult FindMap(char *pMapName, int nMapNameMax); + SMFindMapResult FindMap(char *pMapName, size_t nMapNameMax); + SMFindMapResult FindMap(const char *pMapName, char *pFoundMap = NULL, size_t nMapNameMax = 0); #if SOURCE_ENGINE >= SE_ORANGEBOX string_t AllocPooledString(const char *pszValue); #endif diff --git a/core/smn_halflife.cpp b/core/smn_halflife.cpp index b32cb9e2..4a4abc5e 100644 --- a/core/smn_halflife.cpp +++ b/core/smn_halflife.cpp @@ -71,10 +71,15 @@ static cell_t FindMap(IPluginContext *pContext, const cell_t *params) { char *pMapname; pContext->LocalToString(params[1], &pMapname); - - cell_t size = params[2]; - - return static_cast(g_HL2.FindMap(pMapname, size)); + + if (params[0] == 2) + { + return static_cast(g_HL2.FindMap(pMapname, params[2])); + } + + char *pDestMap; + pContext->LocalToString(params[2], &pDestMap); + return static_cast(g_HL2.FindMap(pMapname, pDestMap, params[3])); } static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params) diff --git a/plugins/include/halflife.inc b/plugins/include/halflife.inc index b50a4f84..422a115d 100644 --- a/plugins/include/halflife.inc +++ b/plugins/include/halflife.inc @@ -159,12 +159,13 @@ native bool:IsMapValid(const String:map[]); * Returns whether a full or partial map name is found or can be resolved * * @param map Map name (usually same as map path relative to maps/ dir, - * excluding .bsp extension). If result is FindMap_FuzzyMatch - * or FindMap_NonCanonical, this will be updated to the full path. + * excluding .bsp extension). + * @param foundmap Resolved map name. If the return is FindMap_FuzzyMatch + * or FindMap_NonCanonical the buffer will be the full path. * @param maxlen Maximum length to write to map var. * @return Result of the find operation. Not all result types are supported on all games. */ -native FindMapResult FindMap(char[] map, int maxlen); +native FindMapResult FindMap(const char[] map, char[] foundmap, int maxlen); /** * Returns whether the server is dedicated.