core/smn_keyvalues.cpp
changeset 753 41f5156d0e63
parent 751 a55c2d4bebe4
child 816 f5551f32680b
     1.1 --- a/core/smn_keyvalues.cpp	Mon May 07 00:11:10 2007 +0000
     1.2 +++ b/core/smn_keyvalues.cpp	Mon May 07 02:58:58 2007 +0000
     1.3 @@ -389,7 +389,7 @@
     1.4  	return 1;
     1.5  }
     1.6  
     1.7 -static cell_t smn_KvJumpFirstSubKey(IPluginContext *pCtx, const cell_t *params)
     1.8 +static cell_t smn_KvGotoFirstSubKey(IPluginContext *pCtx, const cell_t *params)
     1.9  {
    1.10  	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.11  	HandleError herr;
    1.12 @@ -406,7 +406,14 @@
    1.13  	}
    1.14  
    1.15  	KeyValues *pSubKey = pStk->pCurRoot.front();
    1.16 -	KeyValues *pFirstSubKey = pSubKey->GetFirstSubKey();
    1.17 +	KeyValues *pFirstSubKey;
    1.18 +	if (params[2])
    1.19 +	{
    1.20 +		pFirstSubKey = pSubKey->GetFirstTrueSubKey();
    1.21 +	} else {
    1.22 +		pFirstSubKey = pSubKey->GetFirstSubKey();
    1.23 +	}
    1.24 +
    1.25  	if (!pFirstSubKey)
    1.26  	{
    1.27  		return 0;
    1.28 @@ -416,6 +423,39 @@
    1.29  	return 1;
    1.30  }
    1.31  
    1.32 +static cell_t smn_KvGotoNextKey(IPluginContext *pCtx, const cell_t *params)
    1.33 +{
    1.34 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.35 +	HandleError herr;
    1.36 +	HandleSecurity sec;
    1.37 +	KeyValueStack *pStk;
    1.38 +
    1.39 +	sec.pOwner = NULL;
    1.40 +	sec.pIdentity = g_pCoreIdent;
    1.41 +
    1.42 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
    1.43 +		!= HandleError_None)
    1.44 +	{
    1.45 +		return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
    1.46 +	}
    1.47 +
    1.48 +	KeyValues *pSubKey = pStk->pCurRoot.front();
    1.49 +	if (params[2])
    1.50 +	{
    1.51 +		pSubKey = pSubKey->GetNextKey();
    1.52 +	} else {
    1.53 +		pSubKey = pSubKey->GetNextTrueSubKey();
    1.54 +	}
    1.55 +	if (!pSubKey)
    1.56 +	{
    1.57 +		return 0;
    1.58 +	}
    1.59 +	pStk->pCurRoot.pop();
    1.60 +	pStk->pCurRoot.push(pSubKey);
    1.61 +
    1.62 +	return 1;	
    1.63 +}
    1.64 +
    1.65  static cell_t smn_KvJumpNextSubKey(IPluginContext *pCtx, const cell_t *params)
    1.66  {
    1.67  	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.68 @@ -679,10 +719,35 @@
    1.69  
    1.70  	KeyValues *pValues = pStk->pCurRoot.front();
    1.71  	pStk->pCurRoot.pop();
    1.72 -	pStk->pCurRoot.front()->RemoveSubKey(pValues);
    1.73 -	pValues->deleteThis();
    1.74 +	KeyValues *pRoot = pStk->pCurRoot.front();
    1.75  
    1.76 -	return 1;
    1.77 +	/* We have to manually verify this since Valve sucks
    1.78 +	 * :TODO: make our own KeyValues.h file and make
    1.79 +	 * the sub stuff private so we can do this ourselves!
    1.80 + 	 */
    1.81 +	KeyValues *sub = pRoot->GetFirstSubKey();
    1.82 +	while (sub)
    1.83 +	{
    1.84 +		if (sub == pValues)
    1.85 +		{
    1.86 +			KeyValues *pNext = pValues->GetNextKey();
    1.87 +			pRoot->RemoveSubKey(pValues);
    1.88 +			pValues->deleteThis();
    1.89 +			if (pNext)
    1.90 +			{
    1.91 +				pStk->pCurRoot.push(pNext);
    1.92 +				return 1;
    1.93 +			} else {
    1.94 +				return -1;
    1.95 +			}
    1.96 +		}
    1.97 +		sub = sub->GetNextKey();
    1.98 +	}
    1.99 +
   1.100 +	/* Push this back on :( */
   1.101 +	pStk->pCurRoot.push(pValues);
   1.102 +
   1.103 +	return 0;
   1.104  }
   1.105  
   1.106  static cell_t smn_KvDeleteKey(IPluginContext *pContext, const cell_t *params)
   1.107 @@ -722,6 +787,33 @@
   1.108  	return 1;
   1.109  }
   1.110  
   1.111 +static cell_t smn_KvSavePosition(IPluginContext *pContext, const cell_t *params)
   1.112 +{
   1.113 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.114 +	HandleError herr;
   1.115 +	HandleSecurity sec;
   1.116 +	KeyValueStack *pStk;
   1.117 +
   1.118 +	sec.pOwner = NULL;
   1.119 +	sec.pIdentity = g_pCoreIdent;
   1.120 +
   1.121 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
   1.122 +		!= HandleError_None)
   1.123 +	{
   1.124 +		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   1.125 +	}
   1.126 +
   1.127 +	if (pStk->pCurRoot.size() < 2)
   1.128 +	{
   1.129 +		return 0;
   1.130 +	}
   1.131 +
   1.132 +	KeyValues *pValues = pStk->pCurRoot.front();
   1.133 +	pStk->pCurRoot.push(pValues);
   1.134 +
   1.135 +	return 1;
   1.136 +}
   1.137 +
   1.138  static KeyValueNatives s_KeyValueNatives;
   1.139  
   1.140  REGISTER_NATIVES(keyvaluenatives)
   1.141 @@ -738,8 +830,10 @@
   1.142  	{"KvGetUint64",				smn_KvGetUint64},
   1.143  	{"CreateKeyValues",			smn_CreateKeyValues},
   1.144  	{"KvJumpToKey",				smn_KvJumpToKey},
   1.145 -	{"KvJumpFirstSubKey",		smn_KvJumpFirstSubKey},
   1.146 -	{"KvJumpNextSubKey",		smn_KvJumpNextSubKey},
   1.147 +	{"KvGotoNextKey",			smn_KvGotoNextKey},
   1.148 +	{"KvJumpFirstSubKey",		smn_KvGotoFirstSubKey},		/* BACKWARDS COMPAT SHIM */
   1.149 +	{"KvGotoFirstSubKey",		smn_KvGotoFirstSubKey},
   1.150 +	{"KvJumpNextSubKey",		smn_KvJumpNextSubKey},		/* BACKWARDS COMPAT SHIM */
   1.151  	{"KvGoBack",				smn_KvGoBack},
   1.152  	{"KvRewind",				smn_KvRewind},
   1.153  	{"KvGetSectionName",		smn_KvGetSectionName},
   1.154 @@ -751,5 +845,6 @@
   1.155  	{"KvDeleteThis",			smn_KvDeleteThis},
   1.156  	{"KvDeleteKey",				smn_KvDeleteKey},
   1.157  	{"KvNodesInStack",			smn_KvNodesInStack},
   1.158 +	{"KvSavePosition",			smn_KvSavePosition},
   1.159  	{NULL,						NULL}
   1.160  };