econmanager.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. */
  41. class AutoKeyValues {
  42. public:
  43. AutoKeyValues() : m_pKeyValues{nullptr} {}
  44. AutoKeyValues(KeyValues *pKeyValues) : m_pKeyValues{pKeyValues->MakeCopy()} {}
  45. AutoKeyValues(const AutoKeyValues &other) : m_pKeyValues{other.m_pKeyValues->MakeCopy()} {}
  46. ~AutoKeyValues() {
  47. // TODO: figure out why doing this blows up the server
  48. // if (m_pKeyValues) {
  49. // m_pKeyValues->deleteThis();
  50. // }
  51. }
  52. KeyValues *operator->() const { return m_pKeyValues; }
  53. operator KeyValues *() const { return m_pKeyValues; }
  54. private:
  55. KeyValues *m_pKeyValues;
  56. };
  57. /**
  58. * Keeps a copy of existing schema items to inject.
  59. */
  60. class CEconManager {
  61. public:
  62. CEconManager() : m_RegisteredAttributes{} {}
  63. bool Init(char *error, size_t maxlength);
  64. bool InsertOrReplaceAttribute(KeyValues *pAttribKV);
  65. bool RegisterAttribute(KeyValues *pAttribKV);
  66. void InstallAttributes();
  67. private:
  68. std::map<std::string, AutoKeyValues> m_RegisteredAttributes;
  69. };
  70. // binary refers to 0x58 when iterating over the attribute map, so we'll refer to that value
  71. // we could also do a runtime assertion
  72. static_assert(sizeof(CEconItemAttributeDefinition) + 0x14 == 0x58, "CEconItemAttributeDefinition size mismatch");
  73. extern CEconManager g_EconManager;
  74. #endif // _INCLUDE_DYNATTRIB_ECON_MANAGER_H_