Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions core/HalfLife2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,9 +1208,17 @@ const char *CHalfLife2::GetEntityClassname(CBaseEntity *pEntity)
return *(const char **)(((unsigned char *)pEntity) + offset);
}

#if SOURCE_ENGINE >= SE_LEFT4DEAD
static bool ResolveFuzzyMapName(const char *fuzzyName, char *outFullname, int size)
bool CHalfLife2::ResolveFuzzyMapName(const char *fuzzyName, char *outFullname, int size)
{
#if SOURCE_ENGINE != SE_TF2
// TF2's engine->IsMapValid doesn't seem to work at the moment, don't bother trying to call it
if (engine->IsMapValid(fuzzyName))
{
strncopy(outFullname, fuzzyName, size);
return true;
}
#endif
#if SOURCE_ENGINE >= SE_LEFT4DEAD
static ConCommand *pHelperCmd = g_pCVar->FindCommand("changelevel");
if (!pHelperCmd || !pHelperCmd->CanAutoComplete())
return false;
Expand All @@ -1230,8 +1238,20 @@ static bool ResolveFuzzyMapName(const char *fuzzyName, char *outFullname, int si
strncopy(outFullname, &results[0][helperCmdLen + 1], size);

return true;
}
#elif SOURCE_ENGINE == SE_TF2
char szTmp[PLATFORM_MAX_PATH];
strncopy(szTmp, fuzzyName, sizeof(szTmp));

if (engine->FindMap(szTmp, sizeof(szTmp)) == eFindMap_NotFound)
return false;

strncopy(outFullname, szTmp, size);

return true;
#else
return false;
#endif
}

bool CHalfLife2::IsMapValid(const char *map)
{
Expand Down
1 change: 1 addition & 0 deletions core/HalfLife2.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class CHalfLife2 :
ICommandLine *GetValveCommandLine();
const char *GetEntityClassname(edict_t *pEdict);
const char *GetEntityClassname(CBaseEntity *pEntity);
bool ResolveFuzzyMapName(const char *fuzzyName, char *outFullname, int size);
bool IsMapValid(const char *map);
public:
void AddToFakeCliCmdQueue(int client, int userid, const char *cmd);
Expand Down
17 changes: 17 additions & 0 deletions core/smn_halflife.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
return ::RandomInt(params[1], params[2]);
}

static cell_t ResolveFuzzyMapName(IPluginContext *pContext, const cell_t *params)
{
char *fuzzyName, *outFullname;

pContext->LocalToString(params[1], &fuzzyName);
pContext->LocalToString(params[2], &outFullname);

if (!g_HL2.ResolveFuzzyMapName(fuzzyName, outFullname, params[3]))
{
return false;
}

pContext->StringToLocal(params[2], params[3], outFullname);
return true;
}

static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
{
char *map;
Expand Down Expand Up @@ -625,6 +641,7 @@ REGISTER_NATIVES(halflifeNatives)
{"GetRandomFloat", GetRandomFloat},
{"GetRandomInt", GetRandomInt},
{"IsDedicatedServer", IsDedicatedServer},
{"ResolveFuzzyMapName", ResolveFuzzyMapName},
{"IsMapValid", IsMapValid},
{"SetFakeClientConVar", SetFakeClientConVar},
{"SetRandomSeed", SetRandomSeed},
Expand Down
17 changes: 17 additions & 0 deletions plugins/include/halflife.inc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ native Float:GetRandomFloat(Float:fMin=0.0, Float:fMax=1.0);
*/
native GetRandomInt(nmin, nmax);

/**
* Given a map entry name, find its real name
*
* Will always return false on games that do not support fuzzy names.
*
* NOTE: If this function returns false, outFullname is NOT set, so remember to check
* the return value!
*
* @param fuzzyName Map entry name
* @param outFullname Full map path, excluding .bsp extension
* @param size Length of outFullname
*
* @return True if this map resolves to a real map, false if
* it doesn't exist.
*/
native bool:ResolveFuzzyMapName(const String:fuzzyName[], String:outFullname[], size);

/**
* Returns whether a map is valid or not.
*
Expand Down
15 changes: 14 additions & 1 deletion public/IGameHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/

#define SMINTERFACE_GAMEHELPERS_NAME "IGameHelpers"
#define SMINTERFACE_GAMEHELPERS_VERSION 10
#define SMINTERFACE_GAMEHELPERS_VERSION 11

class CBaseEntity;
class CBaseHandle;
Expand Down Expand Up @@ -315,6 +315,19 @@ namespace SourceMod
*/
virtual const char *GetEntityClassname(CBaseEntity *pEntity) =0;

/**
* @brief Resolves a fuzzy map name to its real map name that works
* with the engine's Changelevel functionality.
* Used internally for some games by IsMapValid.
*
* @param fuzzyName Map entry name
* @param outFullname Full map path, excluding .bsp extension
* @param size Length of outFullname
* @return True if this map resolves to a real map, false if
* it doesn't exist.
*/
virtual bool ResolveFuzzyMapName(const char *fuzzyName, char *outFullname, int size) =0;

/**
* @brief Returns whether or not a map name is valid to use with the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not expose this on the interface unless we have an immediate need or if it's been requested with a use case given.

The more we expose, the higher the maintenance burden.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I was planning on doing something with that in one of the extensions (SDKTools probably), but ended up just making it a stock instead. (See pull request #350 specifically sourcemod.inc in it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, and I forgot to outright say this, IGameHelpers can be reverted without breaking anything.

* engine's Changelevel functionality. It need not be an exact filename on
Expand Down