mmsplugin.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "econmanager.h"
  11. #include "natives.h"
  12. #include <filesystem.h>
  13. #include <map>
  14. SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, char const *, char const *, char const *, char const *, bool, bool);
  15. DynSchema g_Plugin;
  16. IFileSystem *filesystem = nullptr;
  17. SMEXT_LINK(&g_Plugin);
  18. const char* NATIVE_ATTRIB_DIR = "addons/sourcemod/configs/tf2nativeattribs";
  19. bool DynSchema::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late) {
  20. GET_V_IFACE_CURRENT(GetFileSystemFactory, filesystem, IFileSystem, FILESYSTEM_INTERFACE_VERSION);
  21. return true;
  22. }
  23. bool DynSchema::SDK_OnLoad(char *error, size_t maxlen, bool late)
  24. {
  25. SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &DynSchema::Hook_LevelInitPost, true);
  26. sharesys->AddNatives(myself, g_EconAttributeNatives);
  27. g_EconInjectedAttributeType = g_pHandleSys->CreateType("EconInjectedAttribute", &g_EconInjectedAttributeHandler, 0, NULL, NULL, myself->GetIdentity(), NULL);
  28. /* Prepare our manager */
  29. if (!g_EconManager.Init(error, maxlen)) {
  30. return false;
  31. }
  32. return true;
  33. }
  34. void DynSchema::SDK_OnUnload() {
  35. SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &DynSchema::Hook_LevelInitPost, true);
  36. g_pHandleSys->RemoveType(g_EconInjectedAttributeType, myself->GetIdentity());
  37. }
  38. bool DynSchema::Hook_LevelInitPost(const char *pMapName, char const *pMapEntities,
  39. char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background) {
  40. // this hook should fire shortly after the schema is (re)initialized
  41. char game_path[256];
  42. engine->GetGameDir(game_path, sizeof(game_path));
  43. char buffer[1024];
  44. g_SMAPI->PathFormat(buffer, sizeof(buffer), "%s/%s",
  45. game_path, "addons/dynattrs/items_dynamic.txt");
  46. // always initialize attributes -- it's better than losing attributes on schema reinit
  47. // coughhiddendevattributescough
  48. // read our own dynamic schema file -- this one supports other sections
  49. KeyValues::AutoDelete pKVMainConfig("DynamicSchema");
  50. if (pKVMainConfig->LoadFromFile(filesystem, buffer)) {
  51. KeyValues *pKVAttributes = pKVMainConfig->FindKey( "attributes" );
  52. if (pKVAttributes) {
  53. FOR_EACH_TRUE_SUBKEY(pKVAttributes, kv) {
  54. g_EconManager.RegisterAttribute(kv);
  55. }
  56. META_CONPRINTF("Successfully injected custom schema %s\n", buffer);
  57. } else {
  58. META_CONPRINTF("Failed to inject custom schema %s\n", buffer);
  59. }
  60. }
  61. // iterate over TF2 Hidden Dev Attributes KV format
  62. // https://forums.alliedmods.net/showthread.php?t=326853
  63. g_SMAPI->PathFormat(buffer, sizeof(buffer), "%s/%s/*", game_path, NATIVE_ATTRIB_DIR);
  64. FileFindHandle_t findHandle;
  65. const char *filename = filesystem->FindFirst(buffer, &findHandle);
  66. while (filename) {
  67. char pathbuf[1024];
  68. g_SMAPI->PathFormat(pathbuf, sizeof(pathbuf), "%s/%s/%s", game_path,
  69. NATIVE_ATTRIB_DIR, filename);
  70. if (!filesystem->FindIsDirectory(findHandle)) {
  71. KeyValues::AutoDelete nativeAttribConfig("attributes");
  72. nativeAttribConfig->LoadFromFile(filesystem, pathbuf);
  73. FOR_EACH_TRUE_SUBKEY(nativeAttribConfig, kv) {
  74. g_EconManager.RegisterAttribute(kv);
  75. }
  76. META_CONPRINTF("Discovered custom schema %s\n", pathbuf);
  77. }
  78. filename = filesystem->FindNext(findHandle);
  79. }
  80. filesystem->FindClose(findHandle);
  81. // perhaps add some other validations before we actually process our attributes?
  82. // TODO ensure the name doesn't clash with existing / newly injected attributes
  83. g_EconManager.InstallAttributes();
  84. return true;
  85. }