econmanager.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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{new KeyValues("auto")} {}
  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. void Assign(KeyValues *pKeyValues) {
  53. if (m_pKeyValues) {
  54. m_pKeyValues->deleteThis();
  55. }
  56. m_pKeyValues = pKeyValues->MakeCopy();
  57. }
  58. KeyValues *operator->() const { return m_pKeyValues; }
  59. operator KeyValues *() const { return m_pKeyValues; }
  60. private:
  61. KeyValues *m_pKeyValues;
  62. };
  63. /**
  64. * Keeps a copy of existing schema items to inject.
  65. */
  66. class CEconManager {
  67. public:
  68. CEconManager() : m_RegisteredAttributes{} {}
  69. bool Init(char *error, size_t maxlength);
  70. bool InsertOrReplaceAttribute(KeyValues *pAttribKV);
  71. bool RegisterAttribute(KeyValues *pAttribKV);
  72. void InstallAttributes();
  73. private:
  74. std::map<std::string, AutoKeyValues> m_RegisteredAttributes;
  75. };
  76. // binary refers to 0x58 when iterating over the attribute map, so we'll refer to that value
  77. // we could also do a runtime assertion
  78. static_assert(sizeof(CEconItemAttributeDefinition) + 0x14 == 0x58, "CEconItemAttributeDefinition size mismatch");
  79. extern CEconManager g_EconManager;
  80. #endif // _INCLUDE_DYNATTRIB_ECON_MANAGER_H_