natives.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "natives.h"
  11. #include "econmanager.h"
  12. HandleType_t g_EconInjectedAttributeType{};
  13. EconInjectedAttributeHandler g_EconInjectedAttributeHandler;
  14. void EconInjectedAttributeHandler::OnHandleDestroy(HandleType_t type, void* object) {
  15. if (type == g_EconInjectedAttributeType) {
  16. delete reinterpret_cast<AutoKeyValues*>(object);
  17. }
  18. }
  19. cell_t sm_EconAttributeCreate(IPluginContext *pContext, const cell_t *params) {
  20. AutoKeyValues *pContainer = new AutoKeyValues();
  21. return g_pHandleSys->CreateHandle(g_EconInjectedAttributeType, pContainer,
  22. pContext->GetIdentity(), myself->GetIdentity(), NULL);
  23. }
  24. // void EconInjectedAttribute.SetClass(const char[] attrClass)
  25. cell_t sm_EconAttributeSetClass(IPluginContext *pContext, const cell_t *params) {
  26. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  27. HandleError err;
  28. Handle_t hndl = static_cast<Handle_t>(params[1]);
  29. AutoKeyValues *pContainer = nullptr;
  30. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  31. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  32. }
  33. char *value;
  34. pContext->LocalToString(params[2], &value);
  35. (*pContainer)->SetString("attribute_class", value);
  36. return 0;
  37. }
  38. // void EconInjectedAttribute.SetName(const char[] attrName)
  39. cell_t sm_EconAttributeSetName(IPluginContext *pContext, const cell_t *params) {
  40. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  41. HandleError err;
  42. Handle_t hndl = static_cast<Handle_t>(params[1]);
  43. AutoKeyValues *pContainer = nullptr;
  44. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  45. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  46. }
  47. char *value;
  48. pContext->LocalToString(params[2], &value);
  49. (*pContainer)->SetString("name", value);
  50. return 0;
  51. }
  52. // void EconInjectedAttribute.SetDescriptionFormat(const char[] attrDescFmt)
  53. cell_t sm_EconAttributeSetDescriptionFormat(IPluginContext *pContext, const cell_t *params) {
  54. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  55. HandleError err;
  56. Handle_t hndl = static_cast<Handle_t>(params[1]);
  57. AutoKeyValues *pContainer = nullptr;
  58. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  59. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  60. }
  61. char *value;
  62. pContext->LocalToString(params[2], &value);
  63. (*pContainer)->SetString("description_format", value);
  64. return 0;
  65. }
  66. // void EconInjectedAttribute.SetCustom(const char[] key, const char[] value)
  67. cell_t sm_EconAttributeSetCustomKeyValue(IPluginContext *pContext, const cell_t *params) {
  68. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  69. HandleError err;
  70. Handle_t hndl = static_cast<Handle_t>(params[1]);
  71. AutoKeyValues *pContainer = nullptr;
  72. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  73. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  74. }
  75. char *key, *value;
  76. pContext->LocalToString(params[2], &key);
  77. pContext->LocalToString(params[3], &value);
  78. (*pContainer)->SetString(key, value);
  79. return 0;
  80. }
  81. // void EconInjectedAttribute.SetDefIndex(int attrdef)
  82. cell_t sm_EconAttributeSetDefIndex(IPluginContext *pContext, const cell_t *params) {
  83. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  84. HandleError err;
  85. Handle_t hndl = static_cast<Handle_t>(params[1]);
  86. AutoKeyValues *pContainer = nullptr;
  87. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  88. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  89. }
  90. (*pContainer)->SetName(std::to_string(params[2]).c_str());
  91. return 0;
  92. }
  93. // void EconInjectedAttribute.ClearDefIndex()
  94. cell_t sm_EconAttributeClearDefIndex(IPluginContext *pContext, const cell_t *params) {
  95. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  96. HandleError err;
  97. Handle_t hndl = static_cast<Handle_t>(params[1]);
  98. AutoKeyValues *pContainer = nullptr;
  99. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  100. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  101. }
  102. (*pContainer)->SetName("auto");
  103. return 0;
  104. }
  105. // bool EconInjectedAttribute.Register()
  106. cell_t sm_EconAttributeRegister(IPluginContext *pContext, const cell_t *params) {
  107. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  108. HandleError err;
  109. Handle_t hndl = static_cast<Handle_t>(params[1]);
  110. AutoKeyValues *pContainer = nullptr;
  111. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  112. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  113. }
  114. if (!g_EconManager.RegisterAttribute(*pContainer) || !g_EconManager.InsertOrReplaceAttribute(*pContainer)) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. // void EconInjectedAttribute.Clear();
  120. cell_t sm_EconAttributeClear(IPluginContext *pContext, const cell_t *params) {
  121. HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
  122. HandleError err;
  123. Handle_t hndl = static_cast<Handle_t>(params[1]);
  124. AutoKeyValues *pContainer = nullptr;
  125. if ((err = g_pHandleSys->ReadHandle(hndl, g_EconInjectedAttributeType, &sec, (void**) &pContainer)) != HandleError_None) {
  126. return pContext->ThrowNativeError("Invalid EconInjectedAttribute handle %x (error: %d)", hndl, err);
  127. }
  128. KeyValues::AutoDelete empty("auto");
  129. pContainer->Assign(empty);
  130. return 0;
  131. }