econmanager.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef _INCLUDE_DYNATTRIB_ECON_MANAGER_H_
  2. #define _INCLUDE_DYNATTRIB_ECON_MANAGER_H_
  3. #include "mmsplugin.h"
  4. #include <map>
  5. #include <memory>
  6. #include <utlmap.h>
  7. #include <utlstring.h>
  8. #include <KeyValues.h>
  9. class ISchemaAttributeType;
  10. // this may need to be updated in the future
  11. class CEconItemAttributeDefinition
  12. {
  13. public:
  14. // TODO implementing ~CEconItemAttributeDefinition segfaults. not sure what's up.
  15. // ideally we implement it to match the game so InsertOrReplace is sure to work correctly
  16. /* 0x00 */ KeyValues *m_KeyValues;
  17. /* 0x04 */ unsigned short m_iIndex;
  18. /* 0x08 */ ISchemaAttributeType *m_AttributeType;
  19. /* 0x0c */ bool m_bHidden;
  20. /* 0x0d */ bool m_bForceOutputDescription;
  21. /* 0x0e */ bool m_bStoreAsInteger;
  22. /* 0x0f */ bool m_bInstanceData;
  23. /* 0x10 */ int m_iAssetClassExportType;
  24. /* 0x14 */ int m_iAssetClassBucket;
  25. /* 0x18 */ bool m_bIsSetBonus;
  26. /* 0x1c */ int m_iIsUserGenerated;
  27. /* 0x20 */ int m_iEffectType;
  28. /* 0x24 */ int m_iDescriptionFormat;
  29. /* 0x28 */ char *m_pszDescriptionString;
  30. /* 0x2c */ char *m_pszArmoryDesc;
  31. /* 0x30 */ char *m_pszName;
  32. /* 0x34 */ char *m_pszAttributeClass;
  33. /* 0x38 */ bool m_bCanAffectMarketName;
  34. /* 0x39 */ bool m_bCanAffectRecipeCompName;
  35. /* 0x3c */ int m_nTagHandle;
  36. /* 0x40 */ string_t m_iszAttributeClass;
  37. };
  38. /**
  39. * Copied implementation of KeyValues::AutoDelete.
  40. * This implementation creates copies of any KeyValues pointers passed into it.
  41. */
  42. class AutoKeyValues {
  43. public:
  44. AutoKeyValues() : m_pKeyValues{new KeyValues("auto")} {}
  45. AutoKeyValues(KeyValues *pKeyValues) : m_pKeyValues{pKeyValues->MakeCopy()} {}
  46. AutoKeyValues(const AutoKeyValues &other) : m_pKeyValues{other.m_pKeyValues->MakeCopy()} {}
  47. /**
  48. * Copy assignment. The contents of the KeyValues* needs to be copied; the default
  49. * implementation seems to cause a use-after-free.
  50. */
  51. AutoKeyValues& operator =(const AutoKeyValues &other) {
  52. this->Assign(other);
  53. return *this;
  54. }
  55. ~AutoKeyValues() {
  56. if (m_pKeyValues) {
  57. m_pKeyValues->deleteThis();
  58. }
  59. }
  60. void Assign(KeyValues *pKeyValues) {
  61. if (m_pKeyValues) {
  62. m_pKeyValues->deleteThis();
  63. }
  64. m_pKeyValues = pKeyValues->MakeCopy();
  65. }
  66. KeyValues *operator->() const { return m_pKeyValues; }
  67. operator KeyValues *() const { return m_pKeyValues; }
  68. private:
  69. KeyValues *m_pKeyValues;
  70. };
  71. /**
  72. * Keeps a copy of existing schema items to inject.
  73. */
  74. class CEconManager {
  75. public:
  76. CEconManager() : m_RegisteredAttributes{} {}
  77. bool Init(char *error, size_t maxlength);
  78. bool InsertOrReplaceAttribute(KeyValues *pAttribKV);
  79. bool RegisterAttribute(KeyValues *pAttribKV);
  80. void InstallAttributes();
  81. private:
  82. std::map<std::string, AutoKeyValues> m_RegisteredAttributes;
  83. };
  84. // binary refers to 0x58 when iterating over the attribute map, so we'll refer to that value
  85. // we could also do a runtime assertion
  86. static_assert(sizeof(CEconItemAttributeDefinition) + 0x14 == 0x58, "CEconItemAttributeDefinition size mismatch");
  87. extern CEconManager g_EconManager;
  88. #endif // _INCLUDE_DYNATTRIB_ECON_MANAGER_H_