Skip to content

Commit

Permalink
Add KV key existence check
Browse files Browse the repository at this point in the history
Bandaid that fixes #1.
  • Loading branch information
nosoop committed Mar 17, 2019
1 parent a019e66 commit 93c1f2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions gamedata/tf2.econ_data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
"linux" "@_ZN9KeyValues9GetStringEPKcS1_"
"windows" "\x55\x8B\xEC\x81\xEC\x40\x02\x00\x00"
}
"KeyValues::FindKey()"
{
// first call in KeyValues::GetString()
"library" "server"
"linux" "@_ZN9KeyValues7FindKeyEPKcb"
"windows" "\x55\x8B\xEC\x81\xEC\x04\x01\x00\x00\x56\x8B\x75\x08\x57"
}
}

"Offsets"
Expand Down
17 changes: 16 additions & 1 deletion scripting/tf_econ_data.sp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Handle g_SDKCallGetEconItemSchema;
Handle g_SDKCallSchemaGetItemDefinition;
Handle g_SDKCallTranslateWeaponEntForClass;
Handle g_SDKCallGetKeyValuesString;
Handle g_SDKCallGetKeyValuesFindKey;

Address offs_CEconItemDefinition_pKeyValues,
offs_CEconItemDefinition_u8MinLevel,
Expand Down Expand Up @@ -94,6 +95,13 @@ public void OnPluginStart() {
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
g_SDKCallGetKeyValuesString = EndPrepSDKCall();

StartPrepSDKCall(SDKCall_Raw);
PrepSDKCall_SetFromConf(hGameConf, SDKConf_Signature, "KeyValues::FindKey()");
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
PrepSDKCall_AddParameter(SDKType_Bool, SDKPass_Plain);
g_SDKCallGetKeyValuesFindKey = EndPrepSDKCall();

offs_CEconItemDefinition_pKeyValues =
GameConfGetAddressOffset(hGameConf, "CEconItemDefinition::m_pKeyValues");
offs_CEconItemDefinition_u8MinLevel =
Expand Down Expand Up @@ -222,7 +230,7 @@ public int Native_GetItemDefinitionString(Handle hPlugin, int nParams) {
Address pItemDef = GetEconItemDefinition(defindex);
if (pItemDef) {
Address pKeyValues = DereferencePointer(pItemDef + offs_CEconItemDefinition_pKeyValues);
if (pKeyValues) {
if (KeyValuesPtrKeyExists(pKeyValues, key)) {
SDKCall(g_SDKCallGetKeyValuesString, pKeyValues, buffer, maxlen, key, buffer);
}
}
Expand Down Expand Up @@ -320,6 +328,13 @@ static bool LoadEconItemDefinitionString(int defindex, Address offset, char[] bu
return true;
}

static bool KeyValuesPtrKeyExists(Address pKeyValues, const char[] key) {
if (!pKeyValues) {
return false;
}
return !!SDKCall(g_SDKCallGetKeyValuesFindKey, pKeyValues, key, false);
}

static Address GetEconItemSchema() {
return SDKCall(g_SDKCallGetEconItemSchema);
}
Expand Down

0 comments on commit 93c1f2a

Please sign in to comment.