From bbc83291ef0d287c3b119e380b10be2742e23b08 Mon Sep 17 00:00:00 2001 From: Arkshine Date: Sat, 26 Jul 2014 14:40:54 +0200 Subject: [PATCH 1/2] Add new format parameters. --- amxmodx/amxmodx.h | 2 +- amxmodx/format.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++--- amxmodx/util.cpp | 18 ++++++++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/amxmodx/amxmodx.h b/amxmodx/amxmodx.h index 1abb1fad..bf55dc7d 100755 --- a/amxmodx/amxmodx.h +++ b/amxmodx/amxmodx.h @@ -153,7 +153,7 @@ unsigned int UTIL_GetUTF8CharBytes(const char *stream); unsigned int UTIL_ReplaceAll(char *subject, size_t maxlength, const char *search, const char *replace, bool caseSensitive); char *UTIL_ReplaceEx(char *subject, size_t maxLen, const char *search, size_t searchLen, const char *replace, size_t replaceLen, bool caseSensitive); char *UTIL_VarArgs(const char *fmt, ...); - +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...); #define GET_PLAYER_POINTER(e) (&g_players[ENTINDEX(e)]) //#define GET_PLAYER_POINTER(e) (&g_players[(((int)e-g_edict_point)/sizeof(edict_t))]) diff --git a/amxmodx/format.cpp b/amxmodx/format.cpp index c61a3407..1c8a06ca 100644 --- a/amxmodx/format.cpp +++ b/amxmodx/format.cpp @@ -118,12 +118,12 @@ const char *translate(AMX *amx, cell amxaddr, const char *key) return def; } -template -void AddString(U **buf_p, size_t &maxlen, const cell *string, int width, int prec) +template +void AddString(U **buf_p, size_t &maxlen, const S *string, int width, int prec) { int size = 0; U *buf; - static cell nlstr[] = {'(','n','u','l','l',')','\0'}; + static S nlstr[] = {'(','n','u','l','l',')','\0'}; buf = *buf_p; @@ -654,6 +654,61 @@ reswitch: llen -= written; break; } + case 'N': + { + CHECK_ARGS(0); + cell *addr = get_amxaddr(amx, params[arg]); + char buffer[255]; + if (*addr >= 1 && *addr <= gpGlobals->maxClients) + { + CPlayer *player = GET_PLAYER_POINTER_I(*addr); + if (!player->ingame) + { + LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr); + return 0; + } + + const char *auth = GETPLAYERAUTHID(player->pEdict); + if (!auth || auth[0] == '\0') + { + auth = "STEAM_ID_PENDING"; + } + + int userid = GETPLAYERUSERID(player->pEdict); + UTIL_Format(buffer, sizeof(buffer), "%s<%d><%s><%s>", player->name.c_str(), userid, auth, player->team.c_str()); + } + else + { + UTIL_Format(buffer, sizeof(buffer), "Console<0>"); + } + + AddString(&buf_p, llen, (const char*)&buffer, width, prec); + arg++; + break; + } + case 'n': + { + CHECK_ARGS(0); + cell *addr = get_amxaddr(amx, params[arg]); + char name[32] = "Console"; + + if (*addr >= 1 && *addr <= gpGlobals->maxClients) + { + CPlayer *player = GET_PLAYER_POINTER_I(*addr); + + if (player->ingame) + { + strncopy(name, player->name.c_str(), sizeof(name)); + + AddString(&buf_p, llen, (const char*)&name, width, prec); + arg++; + break; + } + } + + LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr); + return 0; + } case '%': *buf_p++ = static_cast(ch); if (!llen) diff --git a/amxmodx/util.cpp b/amxmodx/util.cpp index 6cf45ecc..eb2e0bba 100755 --- a/amxmodx/util.cpp +++ b/amxmodx/util.cpp @@ -713,4 +713,22 @@ char *UTIL_ReplaceEx(char *subject, size_t maxLen, const char *search, size_t se } return NULL; +} + +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + size_t len = vsnprintf(buffer, maxlength, fmt, ap); + va_end(ap); + + if (len >= maxlength) + { + buffer[maxlength - 1] = '\0'; + return (maxlength - 1); + } + else + { + return len; + } } \ No newline at end of file From 4ab559ceefc66e6cfe6a92397074f54141e667fd Mon Sep 17 00:00:00 2001 From: Arkshine Date: Fri, 1 Aug 2014 22:08:45 +0200 Subject: [PATCH 2/2] Fix various issues. --- amxmodx/format.cpp | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/amxmodx/format.cpp b/amxmodx/format.cpp index 1c8a06ca..1d65f788 100644 --- a/amxmodx/format.cpp +++ b/amxmodx/format.cpp @@ -659,10 +659,16 @@ reswitch: CHECK_ARGS(0); cell *addr = get_amxaddr(amx, params[arg]); char buffer[255]; - if (*addr >= 1 && *addr <= gpGlobals->maxClients) + if (*addr) { - CPlayer *player = GET_PLAYER_POINTER_I(*addr); - if (!player->ingame) + CPlayer *player = NULL; + + if (*addr >= 1 && *addr <= gpGlobals->maxClients) + { + player = GET_PLAYER_POINTER_I(*addr); + } + + if (!player || !player->initialized) { LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr); return 0; @@ -682,7 +688,7 @@ reswitch: UTIL_Format(buffer, sizeof(buffer), "Console<0>"); } - AddString(&buf_p, llen, (const char*)&buffer, width, prec); + AddString(&buf_p, llen, buffer, width, prec); arg++; break; } @@ -690,24 +696,29 @@ reswitch: { CHECK_ARGS(0); cell *addr = get_amxaddr(amx, params[arg]); - char name[32] = "Console"; + const char *name = "Console"; - if (*addr >= 1 && *addr <= gpGlobals->maxClients) + if (*addr) { - CPlayer *player = GET_PLAYER_POINTER_I(*addr); + CPlayer *player = NULL; - if (player->ingame) + if (*addr >= 1 && *addr <= gpGlobals->maxClients) { - strncopy(name, player->name.c_str(), sizeof(name)); - - AddString(&buf_p, llen, (const char*)&name, width, prec); - arg++; - break; + player = GET_PLAYER_POINTER_I(*addr); } - } - LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr); - return 0; + if (!player || !player->initialized) + { + LogError(amx, AMX_ERR_NATIVE, "Client index %d is invalid", *addr); + return 0; + } + + name = player->name.c_str(); + } + + AddString(&buf_p, llen, name, width, prec); + arg++; + break; } case '%': *buf_p++ = static_cast(ch);