mmsplugin.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * vim: set ts=4 sw=4 tw=99 noet :
  3. * ======================================================
  4. * TF2 Dynamic Schema Injector
  5. * Written by nosoop
  6. * ======================================================
  7. */
  8. #include <stdio.h>
  9. #include "mmsplugin.h"
  10. #include <utlmap.h>
  11. #include <utlstring.h>
  12. #include <KeyValues.h>
  13. #include <filesystem.h>
  14. #include "memscan.h"
  15. SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int, int);
  16. SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
  17. DynSchema g_Plugin;
  18. IServerGameDLL *server = nullptr;
  19. IVEngineServer *engine = NULL;
  20. IBaseFileSystem *basefilesystem = nullptr;
  21. PLUGIN_EXPOSE(DynSchema, g_Plugin);
  22. class ISchemaAttributeType;
  23. // this may need to be updated in the future
  24. class CEconItemAttributeDefinition
  25. {
  26. public:
  27. /* 0x00 */ KeyValues *m_KeyValues;
  28. /* 0x04 */ unsigned short m_iIndex;
  29. /* 0x08 */ ISchemaAttributeType *m_AttributeType;
  30. /* 0x0c */ bool m_bHidden;
  31. /* 0x0d */ bool m_bForceOutputDescription;
  32. /* 0x0e */ bool m_bStoreAsInteger;
  33. /* 0x0f */ bool m_bInstanceData;
  34. /* 0x10 */ int m_iAssetClassExportType;
  35. /* 0x14 */ int m_iAssetClassBucket;
  36. /* 0x18 */ bool m_bIsSetBonus;
  37. /* 0x1c */ int m_iIsUserGenerated;
  38. /* 0x20 */ int m_iEffectType;
  39. /* 0x24 */ int m_iDescriptionFormat;
  40. /* 0x28 */ char *m_pszDescriptionString;
  41. /* 0x2c */ char *m_pszArmoryDesc;
  42. /* 0x30 */ char *m_pszName;
  43. /* 0x34 */ char *m_pszAttributeClass;
  44. /* 0x38 */ bool m_bCanAffectMarketName;
  45. /* 0x39 */ bool m_bCanAffectRecipeCompName;
  46. /* 0x3c */ int m_nTagHandle;
  47. /* 0x40 */ string_t m_iszAttributeClass;
  48. };
  49. // binary refers to 0x58 when iterating over the attribute map, so we'll refer to that value
  50. // we could also do a runtime assertion
  51. static_assert(sizeof(CEconItemAttributeDefinition) + 0x14 == 0x58, "CEconItemAttributeDefinition size mismatch");
  52. // pointer to item schema attribute map singleton
  53. using AttributeMap = CUtlMap<int, CEconItemAttributeDefinition, int>;
  54. AttributeMap *g_SchemaAttributes;
  55. using GetEconItemSchemaFn_t = uintptr_t();
  56. GetEconItemSchemaFn_t *fnGetEconItemSchema = nullptr;
  57. // https://www.unknowncheats.me/wiki/Calling_Functions_From_Injected_Library_Using_Function_Pointers_in_C%2B%2B
  58. #ifdef WIN32
  59. typedef bool (__thiscall *CEconItemAttributeInitFromKV_fn)(CEconItemAttributeDefinition* pThis, KeyValues* pAttributeKeys, CUtlVector<CUtlString>* pErrors);
  60. #elif defined(_LINUX)
  61. typedef bool (__cdecl *CEconItemAttributeInitFromKV_fn)(CEconItemAttributeDefinition* pThis, KeyValues* pAttributeKeys, CUtlVector<CUtlString>* pErrors);
  62. #endif
  63. CEconItemAttributeInitFromKV_fn fnItemAttributeInitFromKV = nullptr;
  64. bool DynSchema::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
  65. {
  66. PLUGIN_SAVEVARS();
  67. GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
  68. GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
  69. GET_V_IFACE_CURRENT(GetFileSystemFactory, basefilesystem, IBaseFileSystem, BASEFILESYSTEM_INTERFACE_VERSION);
  70. SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &DynSchema::Hook_LevelInitPost, true);
  71. // get the base address of the server
  72. #if WINDOWS
  73. CWinLibInfo lib(server);
  74. lib.LocatePattern("\xE8\x2A\x2A\x2A\x2A\x83\xC0\x04\xC3", 9, (void**) &fnGetEconItemSchema);
  75. lib.LocatePattern("\x55\x8B\xEC\x53\x8B\x5D\x08\x56\x8B\xF1\x8B\xCB\x57\xE8\x2A\x2A\x2A\x2A", 18, (void**) &fnItemAttributeInitFromKV);
  76. #elif _LINUX
  77. CLinuxLibInfo lib(server);
  78. lib.LocateSymbol("_ZN28CEconItemAttributeDefinition11BInitFromKVEP9KeyValuesP10CUtlVectorI10CUtlString10CUtlMemoryIS3_iEE",
  79. (void**) &fnItemAttributeInitFromKV);
  80. lib.LocateSymbol("_Z15GEconItemSchemav", (void**) &fnGetEconItemSchema);
  81. #endif
  82. if (!fnItemAttributeInitFromKV || !fnGetEconItemSchema) {
  83. META_CONPRINTF("Failed to get GEIS or BIFKV\n");
  84. return false;
  85. }
  86. // is this late enough in the MM:S load stage? we might just have to hold the function
  87. g_SchemaAttributes = reinterpret_cast<AttributeMap*>(fnGetEconItemSchema() + 0x1BC);
  88. return true;
  89. }
  90. bool DynSchema::Unload(char *error, size_t maxlen) {
  91. SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &DynSchema::Hook_LevelInitPost, true);
  92. return true;
  93. }
  94. bool AddAttribute(KeyValues *pAttribKV) {
  95. int attrdef = atoi(pAttribKV->GetName());
  96. // TODO add a copy of these tests in native
  97. if (attrdef <= 0) {
  98. return false;
  99. }
  100. if (g_SchemaAttributes->IsValidIndex(g_SchemaAttributes->Find(attrdef))) {
  101. return false;
  102. }
  103. CEconItemAttributeDefinition def;
  104. fnItemAttributeInitFromKV(&def, pAttribKV, nullptr);
  105. g_SchemaAttributes->Insert(attrdef, def);
  106. return true;
  107. }
  108. bool DynSchema::Hook_LevelInitPost(const char *pMapName, char const *pMapEntities,
  109. char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background) {
  110. // this hook should fire shortly after the schema is (re)initialized
  111. // TODO determine if the schema was updated, we can do this by:
  112. // - adding a sentinel attribute that we test the existence of later, or
  113. // - check in LevelInitPre if we have a non-null CEconItemSchema::m_pDelayedSchemaData
  114. // TODO create a map of existing attribute names
  115. char game_path[256];
  116. engine->GetGameDir(game_path, sizeof(game_path));
  117. char buffer[1024];
  118. g_SMAPI->PathFormat(buffer, sizeof(buffer), "%s/%s",
  119. game_path, "addons/dynattrs/items_dynamic.txt");
  120. KeyValues::AutoDelete pItemKV("DynamicSchema");
  121. if (pItemKV->LoadFromFile(basefilesystem, buffer)) {
  122. KeyValues *pKVAttributes = pItemKV->FindKey( "attributes" );
  123. if (pKVAttributes) {
  124. FOR_EACH_TRUE_SUBKEY(pKVAttributes, pKVAttribute) {
  125. AddAttribute(pKVAttribute);
  126. }
  127. }
  128. META_CONPRINTF("Successfully injected custom schema %s\n", buffer);
  129. } else {
  130. META_CONPRINTF("Failed to inject custom schema %s\n", buffer);
  131. }
  132. return true;
  133. }
  134. void DynSchema::AllPluginsLoaded() {
  135. /* This is where we'd do stuff that relies on the mod or other plugins
  136. * being initialized (for example, cvars added and events registered).
  137. */
  138. }
  139. bool DynSchema::Pause(char *error, size_t maxlen) {
  140. return true;
  141. }
  142. bool DynSchema::Unpause(char *error, size_t maxlen) {
  143. return true;
  144. }
  145. const char *DynSchema::GetLicense() {
  146. return "Proprietary";
  147. }
  148. const char *DynSchema::GetVersion() {
  149. return "1.0.2";
  150. }
  151. const char *DynSchema::GetDate() {
  152. return __DATE__;
  153. }
  154. const char *DynSchema::GetLogTag() {
  155. return "dynschema";
  156. }
  157. const char *DynSchema::GetAuthor() {
  158. return "nosoop";
  159. }
  160. const char *DynSchema::GetDescription() {
  161. return "Injects user-defined content into the game schema";
  162. }
  163. const char *DynSchema::GetName() {
  164. return "TF2 Dynamic Schema";
  165. }
  166. const char *DynSchema::GetURL() {
  167. return "https://git.csrd.science/";
  168. }