mmsplugin.cpp 3.3 KB

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