From 221429ff6e12b540644cdebe81224b9fcbf0923d Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Sat, 1 Dec 2007 00:37:47 +0000 Subject: [PATCH] added amb382, KV get/set vector natives --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401740 --- core/smn_keyvalues.cpp | 104 ++++++++++++++++++++++++++++++++++ plugins/include/keyvalues.inc | 23 ++++++++ 2 files changed, 127 insertions(+) diff --git a/core/smn_keyvalues.cpp b/core/smn_keyvalues.cpp index d0313947..025cc5c8 100644 --- a/core/smn_keyvalues.cpp +++ b/core/smn_keyvalues.cpp @@ -218,6 +218,35 @@ static cell_t smn_KvSetColor(IPluginContext *pCtx, const cell_t *params) return 1; } +static cell_t smn_KvSetVector(IPluginContext *pCtx, const cell_t *params) +{ + Handle_t hndl = static_cast(params[1]); + HandleError herr; + HandleSecurity sec; + KeyValueStack *pStk; + + sec.pOwner = NULL; + sec.pIdentity = g_pCoreIdent; + + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) + != HandleError_None) + { + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); + } + + char *key; + char buffer[64]; + cell_t *vector; + pCtx->LocalToStringNULL(params[2], &key); + pCtx->LocalToPhysAddr(params[3], &vector); + + UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(vector[0]), sp_ctof(vector[1]), sp_ctof(vector[2])); + + pStk->pCurRoot.front()->SetString(key, buffer); + + return 1; +} + static cell_t smn_KvGetString(IPluginContext *pCtx, const cell_t *params) { Handle_t hndl = static_cast(params[1]); @@ -358,6 +387,79 @@ static cell_t smn_KvGetUInt64(IPluginContext *pCtx, const cell_t *params) return 1; } +static cell_t smn_KvGetVector(IPluginContext *pCtx, const cell_t *params) +{ + Handle_t hndl = static_cast(params[1]); + HandleError herr; + HandleSecurity sec; + KeyValueStack *pStk; + + sec.pOwner = NULL; + sec.pIdentity = g_pCoreIdent; + + if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk)) + != HandleError_None) + { + return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr); + } + + char *key; + const char *value; + cell_t *defvector, *outvector; + char buffer[64]; + pCtx->LocalToStringNULL(params[2], &key); + pCtx->LocalToPhysAddr(params[3], &outvector); + pCtx->LocalToPhysAddr(params[4], &defvector); + + UTIL_Format(buffer, sizeof(buffer), "%f %f %f", sp_ctof(defvector[0]), sp_ctof(defvector[1]), sp_ctof(defvector[2])); + + value = pStk->pCurRoot.front()->GetString(key, buffer); + + float out; + int components = 0; + while (*value && components < 3) + { + while ((*value) && (*value == ' ')) + { + value++; + } + + out = 0.0f; + bool isnegative; + if (*value == '-') + { + isnegative = true; + value++; + } + else + { + isnegative = false; + } + + for (; *value && isdigit(*value); ++value) + { + out *= 10.0f; + out += *value - '0'; + } + + if (*value == '.') + { + value++; + float factor = 0.1f; + for (; *value && isdigit(*value); ++value) + { + out += (*value - '0') * factor; + factor *= 0.1f; + } + } + + out = (isnegative) ? -out : out; + outvector[components++] = sp_ftoc(out); + } + + return 1; +} + static cell_t smn_CreateKeyValues(IPluginContext *pCtx, const cell_t *params) { KeyValueStack *pStk; @@ -1015,5 +1117,7 @@ REGISTER_NATIVES(keyvaluenatives) {"KvFindKeyById", smn_FindKeyById}, {"KvGetNameSymbol", smn_GetNameSymbol}, {"KvGetSectionSymbol", smn_KvGetSectionSymbol}, + {"KvGetVector", smn_KvGetVector}, + {"KvSetVector", smn_KvSetVector}, {NULL, NULL} }; diff --git a/plugins/include/keyvalues.inc b/plugins/include/keyvalues.inc index deb21e6c..c14b15fa 100644 --- a/plugins/include/keyvalues.inc +++ b/plugins/include/keyvalues.inc @@ -120,6 +120,17 @@ native KvSetFloat(Handle:kv, const String:key[], Float:value); */ native KvSetColor(Handle:kv, const String:key[], r, g, b, a=0); +/** + * Sets a vector value of a KeyValues key. + * + * @param kv KeyValues Handle. + * @param key Name of the key, or NULL_STRING. + * @param vec Vector value. + * @noreturn + * @error Invalid Handle. + */ +native KvSetVector(Handle:kv, const String:key[], const Float:vec[3]); + /** * Retrieves a string value from a KeyValues key. * @@ -181,6 +192,18 @@ native KvGetColor(Handle:kv, const String:key[], &r, &g, &b, &a); */ native KvGetUInt64(Handle:kv, const String:key[], value[2], defvalue[2]={0,0}); +/** + * Retrieves a vector value from a KeyValues key. + * + * @param kv KeyValues Handle. + * @param key Name of the key, or NULL_STRING. + * @param vec Destination vector to store the value in. + * @param defvalue Optional default value to use if the key is not found. + * @noreturn + * @error Invalid Handle. + */ +native KvGetVector(Handle:kv, const String:key[], Float:vec[3], const Float:defvalue[3]={0.0, 0.0, 0.0}); + /** * Sets the current position in the KeyValues tree to the given key. *