econmanager.cpp 4.3 KB

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