Repository navigation
Expand file tree
/
Copy patheconmanager.cpp
More file actions
150 lines (125 loc) · 4.85 KB
/
Copy patheconmanager.cpp
File metadata and controls
150 lines (125 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "econmanager.h"
#include <IGameConfigs.h>
#include <map>
#ifdef PLATFORM_64BITS
#define OFFS_M_ATTRIBUTES 0x250
#else
#define OFFS_M_ATTRIBUTES 0x1BC
#endif
CEconManager g_EconManager;
// pointer to item schema attribute map singleton
using AttributeMap = CUtlMap<int, CEconItemAttributeDefinition, int>;
AttributeMap *g_SchemaAttributes;
size_t g_nAutoAttributeBase = 4000;
std::map<std::string, int> g_AutoNumberedAttributes{};
typedef uintptr_t (*GetEconItemSchema_fn)(void);
GetEconItemSchema_fn fnGetEconItemSchema = nullptr;
// https://www.unknowncheats.me/wiki/Calling_Functions_From_Injected_Library_Using_Function_Pointers_in_C%2B%2B
#ifdef WIN32
typedef bool (__thiscall *CEconItemAttributeInitFromKV_fn)(CEconItemAttributeDefinition* pThis, KeyValues* pAttributeKeys, CUtlVector<CUtlString>* pErrors);
#elif defined(_LINUX)
typedef bool (__cdecl *CEconItemAttributeInitFromKV_fn)(CEconItemAttributeDefinition* pThis, KeyValues* pAttributeKeys, CUtlVector<CUtlString>* pErrors);
#endif
CEconItemAttributeInitFromKV_fn fnItemAttributeInitFromKV = nullptr;
bool CEconManager::Init(char *error, size_t maxlength) {
// get the base address of the server
SourceMod::IGameConfig *conf;
if (!gameconfs->LoadGameConfigFile("tf2.econ_dynamic", &conf, error, maxlength)) {
return false;
}
if (!conf->GetMemSig("GEconItemSchema()", (void**) &fnGetEconItemSchema)) {
snprintf(error, maxlength, "Failed to setup call to GetEconItemSchema()");
return false;
} else if (!conf->GetMemSig("CEconItemAttributeDefinition::BInitFromKV()", (void**) &fnItemAttributeInitFromKV)) {
snprintf(error, maxlength, "Failed to setup call to CEconItemAttributeDefinition::BInitFromKV");
return false;
}
gameconfs->CloseGameConfigFile(conf);
// is this late enough in the MM:S load stage? we might just have to hold the function
g_SchemaAttributes = reinterpret_cast<AttributeMap*>(fnGetEconItemSchema() + OFFS_M_ATTRIBUTES);
return true;
}
/**
* Initializes a CEconItemAttributeDefinition from a KeyValues definition, then inserts or
* replaces the appropriate entry in the schema.
*/
bool CEconManager::InsertOrReplaceAttribute(KeyValues *pAttribKV) {
KeyValues::AutoDelete attribute{pAttribKV->MakeCopy()};
const char* attrID = attribute->GetName();
const char* attrName = attribute->GetString("name");
int attrdef;
if (strcmp(attrID, "auto") == 0) {
/**
* Have the plugin automatically allocate an attribute ID.
* - if the name is already mapped to an ID, then use that
* - otherwise, continue to increment our counter until we find an unused one
*/
auto search = g_AutoNumberedAttributes.find(attrName);
if (search != g_AutoNumberedAttributes.end()) {
attrdef = search->second;
} else {
while (g_SchemaAttributes->Find(g_nAutoAttributeBase) != g_SchemaAttributes->InvalidIndex()) {
g_nAutoAttributeBase++;
}
attrdef = g_nAutoAttributeBase;
g_AutoNumberedAttributes[attrName] = attrdef;
}
} else {
attrdef = atoi(attrID);
if (attrdef <= 0) {
META_CONPRINTF("Attribute '%s' has invalid index string '%s'\n", attrName, attrID);
return false;
}
}
attribute->SetName(std::to_string(attrdef).c_str());
// only replace existing injected attributes; fail on schema attributes
auto existingIndex = g_SchemaAttributes->Find(attrdef);
if (existingIndex != g_SchemaAttributes->InvalidIndex()) {
auto &existingAttr = g_SchemaAttributes->Element(existingIndex);
if (!existingAttr.m_KeyValues->GetBool("injected")) {
META_CONPRINTF("WARN: Not overriding native attribute '%s'\n",
existingAttr.m_pszName);
return false;
}
}
// embed additional custom data into attribute KV; econdata and the like can deal with this
// one could also add this data into the file itself, but this leaves less room for error
attribute->SetBool("injected", true);
CEconItemAttributeDefinition def;
fnItemAttributeInitFromKV(&def, attribute, nullptr);
// TODO verify that this doesn't leak, or just shrug it off
g_SchemaAttributes->InsertOrReplace(attrdef, def);
return true;
}
bool CEconManager::RegisterAttribute(KeyValues* pAttribKV) {
AutoKeyValues kv{pAttribKV};
std::string attrName = kv->GetString("name");
if (attrName.empty()) {
attrName = kv->GetString("attribute_class");
if (attrName.empty()) {
return false;
}
kv->SetString("name", attrName.c_str());
}
this->m_RegisteredAttributes[attrName] = std::move(kv);
return true;
}
void CEconManager::InstallAttributes() {
for (const auto& pair : m_RegisteredAttributes) {
InsertOrReplaceAttribute(pair.second);
}
}
KeyValues *CEconManager::GetAttributeDefinitionKeyValuesByName(const char *name) {
if (!name) {
return nullptr;
}
FOR_EACH_MAP_FAST( (*g_SchemaAttributes), i ) {
if (!(*g_SchemaAttributes)[i].m_pszName) {
continue;
}
if (stricmp((*g_SchemaAttributes)[i].m_pszName, name) == 0) {
return (*g_SchemaAttributes)[i].m_KeyValues;
}
}
return nullptr;
}