From 3e88c91ad2929cbc8846c083b1eaa005a09b61c0 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 22 Jun 2007 14:59:26 +0000 Subject: [PATCH] added ability to use NULL_STRING in KeyValues natives --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401006 --- core/smn_keyvalues.cpp | 20 ++++++++++---------- core/vm/sp_vm_basecontext.cpp | 25 +++++++++++++++++++++++++ core/vm/sp_vm_basecontext.h | 2 ++ plugins/include/core.inc | 3 ++- plugins/include/keyvalues.inc | 20 ++++++++++---------- public/sourcepawn/sp_vm_api.h | 10 ++++++++++ 6 files changed, 59 insertions(+), 21 deletions(-) diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index 80912cb3..63500830 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -92,7 +92,7 @@ static cell_t smn_KvSetString(IPluginContext *pCtx, const cell_t *params) } char *key, *value; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); pCtx->LocalToString(params[3], &value); pStk->pCurRoot.front()->SetString(key, value); @@ -117,7 +117,7 @@ static cell_t smn_KvSetNum(IPluginContext *pCtx, const cell_t *params) } char *key; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); pStk->pCurRoot.front()->SetInt(key, params[3]); @@ -143,7 +143,7 @@ static cell_t smn_KvSetUInt64(IPluginContext *pCtx, const cell_t *params) char *key; cell_t *addr; uint64 value; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); pCtx->LocalToPhysAddr(params[3], &addr); value = static_cast(*addr); @@ -169,7 +169,7 @@ static cell_t smn_KvSetFloat(IPluginContext *pCtx, const cell_t *params) } char *key; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); pStk->pCurRoot.front()->SetFloat(key, sp_ctof(params[3])); @@ -193,7 +193,7 @@ static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params) } char *key; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); Color color(params[3], params[4], params[5], params[6]); pStk->pCurRoot.front()->SetColor(key, color); @@ -219,7 +219,7 @@ static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params) const char *value; char *key, *defvalue; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); pCtx->LocalToString(params[5], &defvalue); value = pStk->pCurRoot.front()->GetString(key, defvalue); @@ -246,7 +246,7 @@ static cell_t smn_KvGetNum(IPluginContext *pCtx, const cell_t *params) int value; char *key; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); value = pStk->pCurRoot.front()->GetInt(key, params[3]); @@ -271,7 +271,7 @@ static cell_t smn_KvGetFloat(IPluginContext *pCtx, const cell_t *params) float value; char *key; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); value = pStk->pCurRoot.front()->GetFloat(key, sp_ctof(params[3])); @@ -297,7 +297,7 @@ static cell_t smn_KvGetColor(IPluginContext *pCtx, const cell_t *params) Color color; char *key; cell_t *r, *g, *b, *a; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); pCtx->LocalToPhysAddr(params[3], &r); pCtx->LocalToPhysAddr(params[4], &g); pCtx->LocalToPhysAddr(params[5], &b); @@ -331,7 +331,7 @@ static cell_t smn_KvGetUInt64(IPluginContext *pCtx, const cell_t *params) char *key; cell_t *addr, *defvalue; uint64 value; - pCtx->LocalToString(params[2], &key); + pCtx->LocalToStringNULL(params[2], &key); pCtx->LocalToPhysAddr(params[3], &addr); pCtx->LocalToPhysAddr(params[4], &defvalue); diff --git a/core/vm/sp_vm_basecontext.cpp b/core/vm/sp_vm_basecontext.cpp index da987ed5..bc25cafb 100644 --- a/core/vm/sp_vm_basecontext.cpp +++ b/core/vm/sp_vm_basecontext.cpp @@ -83,6 +83,15 @@ BaseContext::BaseContext(sp_context_t *_ctx) } else { m_pNullVec = NULL; } + + if (FindPubvarByName("NULL_STRING", &index) == SP_ERROR_NONE) + { + sp_pubvar_t *pubvar; + GetPubvarByIndex(index, &pubvar); + m_pNullString = pubvar->offs; + } else { + m_pNullString = NULL; + } } void BaseContext::FlushFunctionCache() @@ -992,6 +1001,22 @@ IPluginFunction *BaseContext::GetFunctionByName(const char *public_name) return pFunc; } +int BaseContext::LocalToStringNULL(cell_t local_addr, char **addr) +{ + int err; + if ((err = LocalToString(local_addr, addr)) != SP_ERROR_NONE) + { + return err; + } + + if ((cell_t *)*addr == m_pNullString) + { + *addr = NULL; + } + + return SP_ERROR_NONE; +} + #if defined SOURCEMOD_BUILD SourceMod::IdentityToken_t *BaseContext::GetIdentity() { diff --git a/core/vm/sp_vm_basecontext.h b/core/vm/sp_vm_basecontext.h index 938b9f55..68b77c9a 100644 --- a/core/vm/sp_vm_basecontext.h +++ b/core/vm/sp_vm_basecontext.h @@ -70,6 +70,7 @@ namespace SourcePawn SourceMod::IdentityToken_t *GetIdentity(); void SetIdentity(SourceMod::IdentityToken_t *token); cell_t *GetNullRef(SP_NULL_TYPE type); + int LocalToStringNULL(cell_t local_addr, char **addr); #endif public: //IPluginDebugInfo int LookupFile(ucell_t addr, const char **filename); @@ -86,6 +87,7 @@ namespace SourcePawn #if defined SOURCEMOD_BUILD SourceMod::IdentityToken_t *m_pToken; cell_t *m_pNullVec; + cell_t *m_pNullString; #endif char m_MsgCache[1024]; bool m_CustomMsg; diff --git a/plugins/include/core.inc b/plugins/include/core.inc index 4e56a03f..cf23b906 100644 --- a/plugins/include/core.inc +++ b/plugins/include/core.inc @@ -91,7 +91,8 @@ struct Extension bool:required, /**< Whether or not to require */ }; -public Float:NULL_VECTOR[3]; /**< Pass this into certain functions to act as a C++ NULL */ +public Float:NULL_VECTOR[3]; /**< Pass this into certain functions to act as a C++ NULL */ +public const String:NULL_STRING[1]; /**< pass this into certain functions to act as a C++ NULL */ #define AUTOLOAD_EXTENSIONS #define REQUIRE_EXTENSIONS diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index cb7b283a..ba85a395 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -49,7 +49,7 @@ native Handle:CreateKeyValues(const String:name[], const String:firstkey[]="", c * Sets a string value of a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param value String value. * @noreturn * @error Invalid Handle. @@ -60,7 +60,7 @@ native KvSetString(Handle:kv, const String:key[], const String:value[]); * Sets an integer value of a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param value Value number. * @noreturn * @error Invalid Handle. @@ -71,7 +71,7 @@ native KvSetNum(Handle:kv, const String:key[], value); * Sets a large integer value of a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param value Large integer value (0=High bits, 1=Low bits) * @noreturn * @error Invalid Handle. @@ -82,7 +82,7 @@ native KvSetUInt64(Handle:kv, const String:key[], const value[2]); * Sets a floating point value of a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param value Floating point value. * @noreturn * @error Invalid Handle. @@ -93,7 +93,7 @@ native KvSetFloat(Handle:kv, const String:key[], Float:value); * Sets a set of color values of a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param r Red value. * @param g Green value. * @param b Blue value. @@ -107,7 +107,7 @@ native KvSetColor(Handle:kv, const String:key[], r, g, b, a=0); * Retrieves a string value from a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param value Buffer to store key value in. * @param maxlength Maximum length of the value buffer. * @param defvalue Optional default value to use if the key is not found. @@ -120,7 +120,7 @@ native KvGetString(Handle:kv, const String:key[], String:value[], maxlength, con * Retrieves an integer value from a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param defvalue Optional default value to use if the key is not found. * @return Integer value of the key. * @error Invalid Handle. @@ -131,7 +131,7 @@ native KvGetNum(Handle:kv, const String:key[], defvalue=0); * Retrieves a floating point value from a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param defvalue Optional default value to use if the key is not found. * @return Floating point value of the key. * @error Invalid Handle. @@ -142,7 +142,7 @@ native Float:KvGetFloat(Handle:kv, const String:key[], Float:defvalue=0.0); * Retrieves a set of color values from a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param r Red value, set by reference. * @param g Green value, set by reference. * @param b Blue value, set by reference. @@ -156,7 +156,7 @@ native KvGetColor(Handle:kv, const String:key[], &r, &g, &b, &a); * Retrieves a large integer value from a KeyValues key. * * @param kv KeyValues Handle. - * @param key Name of the key. + * @param key Name of the key, or NULL_STRING. * @param value Array to represent the large integer. * @param defvalue Optional default value to use if the key is not found. * @noreturn diff --git a/public/sourcepawn/sp_vm_api.h b/public/sourcepawn/sp_vm_api.h index 66584910..0d280dce 100644 --- a/public/sourcepawn/sp_vm_api.h +++ b/public/sourcepawn/sp_vm_api.h @@ -56,6 +56,7 @@ namespace SourcePawn enum SP_NULL_TYPE { SP_NULL_VECTOR = 0, /**< Float[3] reference */ + SP_NULL_STRING = 1, /**< const String[1] reference */ }; #endif @@ -555,6 +556,15 @@ namespace SourcePawn * @return cell_t address to compare to. */ virtual cell_t *GetNullRef(SP_NULL_TYPE type) =0; + + /** + * @brief Converts a local address to a physical string, and allows + * for NULL_STRING to be set. + * + * @param local_addr Local address in plugin. + * @param addr Destination output pointer. + */ + virtual int LocalToStringNULL(cell_t local_addr, char **addr) =0; #endif };