Skip to content

Commit

Permalink
Add native to get an attribute index from a name
Browse files Browse the repository at this point in the history
  • Loading branch information
nosoop committed Apr 9, 2019
1 parent 636236a commit f6e76f6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
11 changes: 11 additions & 0 deletions gamedata/tf2.econ_data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
"linux" "@_ZN15CEconItemSchema22GetAttributeDefinitionEi"
"windows" "\x55\x8B\xEC\x83\xEC\x2A\x53\x56\x8B\xD9\x8D\x2A\x2A\x57"
}
"CEconItemSchema::GetAttributeDefinitionByName()"
{
"library" "server"
"linux" "@_ZN15CEconItemSchema28GetAttributeDefinitionByNameEPKc"
"windows" "\x55\x8B\xEC\x83\xEC\x14\x53\x8B\x5D\x08\x56\x57\x8B\xF9\x85\xDB"
}
"TranslateWeaponEntForClass()"
{
// get x-ref to structure containing string "saxxy"
Expand Down Expand Up @@ -105,6 +111,11 @@
"linux" "0"
"windows" "0"
}
"CEconItemAttributeDefinition::m_iAttributeDefinitionIndex"
{
"linux" "4"
"windows" "4"
}
"CEconItemAttributeDefinition::m_bHidden"
{
"linux" "12"
Expand Down
6 changes: 6 additions & 0 deletions scripting/include/tf_econ_data.inc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ native bool TF2Econ_GetAttributeClassName(int defindex, char[] buffer, int maxle
native void TF2Econ_GetAttributeDefinitionString(int defindex, const char[] key,
char[] buffer, int maxlen, const char[] defaultValue = "");

/**
* Returns the attribute definition index for a given name, or -1 if the name does not
* correspond to an attribute.
*/
native int TF2Econ_TranslateAttributeNameToDefinitionIndex(const char[] name);

/**
* Returns the address of the singleton CTFItemSchema (subclass of CEconItemSchema).
*
Expand Down
37 changes: 36 additions & 1 deletion scripting/tf_econ_data.sp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <stocksoup/handles>
#include <stocksoup/memory>

#define PLUGIN_VERSION "0.10.0"
#define PLUGIN_VERSION "0.11.0"
public Plugin myinfo = {
name = "[TF2] Econ Data",
author = "nosoop",
Expand All @@ -30,6 +30,7 @@ public Plugin myinfo = {
Handle g_SDKCallGetEconItemSchema;
Handle g_SDKCallSchemaGetItemDefinition;
Handle g_SDKCallSchemaGetAttributeDefinition;
Handle g_SDKCallSchemaGetAttributeDefinitionByName;
Handle g_SDKCallTranslateWeaponEntForClass;

Address offs_CEconItemSchema_ItemList,
Expand Down Expand Up @@ -68,6 +69,8 @@ public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int maxlen) {
CreateNative("TF2Econ_GetAttributeName", Native_GetAttributeName);
CreateNative("TF2Econ_GetAttributeClassName", Native_GetAttributeClassName);
CreateNative("TF2Econ_GetAttributeDefinitionString", Native_GetAttributeDefinitionString);
CreateNative("TF2Econ_TranslateAttributeNameToDefinitionIndex",
Native_TranslateAttributeNameToDefinitionIndex);

// low-level stuff
CreateNative("TF2Econ_GetItemSchemaAddress", Native_GetItemSchemaAddress);
Expand Down Expand Up @@ -105,6 +108,13 @@ public void OnPluginStart() {
PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
g_SDKCallSchemaGetAttributeDefinition = EndPrepSDKCall();

StartPrepSDKCall(SDKCall_Raw);
PrepSDKCall_SetFromConf(hGameConf, SDKConf_Signature,
"CEconItemSchema::GetAttributeDefinitionByName()");
PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
g_SDKCallSchemaGetAttributeDefinitionByName = EndPrepSDKCall();

StartPrepSDKCall(SDKCall_Static);
PrepSDKCall_SetFromConf(hGameConf, SDKConf_Signature, "TranslateWeaponEntForClass()");
PrepSDKCall_SetReturnInfo(SDKType_String, SDKPass_Pointer);
Expand Down Expand Up @@ -151,6 +161,9 @@ public void OnPluginStart() {

offs_CEconItemAttributeDefinition_pKeyValues =
GameConfGetAddressOffset(hGameConf, "CEconItemAttributeDefinition::m_pKeyValues");
offs_CEconItemAttributeDefinition_iAttributeDefinitionIndex =
GameConfGetAddressOffset(hGameConf,
"CEconItemAttributeDefinition::m_iAttributeDefinitionIndex");
offs_CEconItemAttributeDefinition_bHidden =
GameConfGetAddressOffset(hGameConf, "CEconItemAttributeDefinition::m_bHidden");
offs_CEconItemAttributeDefinition_bIsInteger =
Expand Down Expand Up @@ -255,6 +268,28 @@ Address GetEconAttributeDefinition(int defindex) {
SDKCall(g_SDKCallSchemaGetAttributeDefinition, pSchema, defindex) : Address_Null;
}

public int Native_TranslateAttributeNameToDefinitionIndex(Handle hPlugin, int nParams) {
int maxlen;
GetNativeStringLength(1, maxlen);
maxlen++;
char[] attrName = new char[maxlen];
GetNativeString(1, attrName, maxlen);

Address pAttribute = GetEconAttributeDefinitionByName(attrName);
if (pAttribute) {
Address pOffs =
pAttribute + offs_CEconItemAttributeDefinition_iAttributeDefinitionIndex;
return LoadFromAddress(pOffs, NumberType_Int32);
}
return -1;
}

Address GetEconAttributeDefinitionByName(const char[] name) {
Address pSchema = GetEconItemSchema();
return pSchema?
SDKCall(g_SDKCallSchemaGetAttributeDefinitionByName, pSchema, name) : Address_Null;
}

Address GetEconItemSchema() {
return SDKCall(g_SDKCallGetEconItemSchema);
}
Expand Down
1 change: 1 addition & 0 deletions scripting/tf_econ_data/attribute_definition.sp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Address offs_CEconItemAttributeDefinition_pKeyValues,
offs_CEconItemAttributeDefinition_iAttributeDefinitionIndex,
offs_CEconItemAttributeDefinition_bHidden,
offs_CEconItemAttributeDefinition_bIsInteger,
offs_CEconItemAttributeDefinition_pszAttributeName,
Expand Down

0 comments on commit f6e76f6

Please sign in to comment.