econmanager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. META_CONPRINTF("Creating akv from kv %s (%p -> %p)\n", m_pKeyValues->GetString("name"), (void*) pKeyValues, (void *) m_pKeyValues);
  46. }
  47. AutoKeyValues(const AutoKeyValues &other) : m_pKeyValues{other.m_pKeyValues->MakeCopy()} {
  48. META_CONPRINTF("Creating akv from akv %s (%p)\n", m_pKeyValues->GetString("name"), (void *) m_pKeyValues);
  49. }
  50. ~AutoKeyValues() {
  51. if (m_pKeyValues) {
  52. m_pKeyValues->deleteThis();
  53. }
  54. }
  55. KeyValues *operator->() const {
  56. META_CONPRINTF("deref akv (%p)\n", (void*) m_pKeyValues);
  57. return m_pKeyValues;
  58. }
  59. operator KeyValues *() const {
  60. META_CONPRINTF("deref akv (%p)\n", (void*) m_pKeyValues);
  61. return m_pKeyValues;
  62. }
  63. KeyValues *m_pKeyValues;
  64. private:
  65. };
  66. /**
  67. * Keeps a copy of existing schema items to inject.
  68. */
  69. class CEconManager {
  70. public:
  71. CEconManager() : m_RegisteredAttributes{} {}
  72. bool Init(char *error, size_t maxlength);
  73. bool InsertOrReplaceAttribute(KeyValues *pAttribKV);
  74. bool RegisterAttribute(KeyValues *pAttribKV);
  75. void InstallAttributes();
  76. private:
  77. std::map<std::string, AutoKeyValues> m_RegisteredAttributes;
  78. };
  79. // binary refers to 0x58 when iterating over the attribute map, so we'll refer to that value
  80. // we could also do a runtime assertion
  81. static_assert(sizeof(CEconItemAttributeDefinition) + 0x14 == 0x58, "CEconItemAttributeDefinition size mismatch");
  82. extern CEconManager g_EconManager;
  83. #endif // _INCLUDE_DYNATTRIB_ECON_MANAGER_H_