tf_econ_dynamic_compat.sp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * [TF2] Econ Dynamic Compatibility Shim
  3. *
  4. * Implements compatibility shims to load attribute definitions from files.
  5. */
  6. #pragma semicolon 1
  7. #include <sourcemod>
  8. #pragma newdecls required
  9. #include <tf_econ_dynamic>
  10. #define PLUGIN_VERSION "1.0.0"
  11. public Plugin myinfo = {
  12. name = "[TF2] Econ Dynamic Compatibility Shim",
  13. author = "nosoop",
  14. description = "Compatibility shim to install attributes specified by other plugins",
  15. version = PLUGIN_VERSION,
  16. url = "https://github.com/nosoop/SMExt-TFEconDynamic"
  17. }
  18. static TF2EconDynAttribute s_AttributeContext;
  19. public void OnPluginStart() {
  20. s_AttributeContext = new TF2EconDynAttribute();
  21. CreateAttributeConfigParsers();
  22. }
  23. public void OnMapStart() {
  24. // this loads relatively late, but it should be fine
  25. LoadAttributes();
  26. }
  27. enum HiddenAttributeConfigParseState {
  28. HiddenAttributeConfigParseState_Root,
  29. HiddenAttributeConfigParseState_AttributeList,
  30. HiddenAttributeConfigParseState_AttributeProperties,
  31. }
  32. static SMCParser s_HiddenDevAttributeParser;
  33. static HiddenAttributeConfigParseState s_ParseState = HiddenAttributeConfigParseState_Root;
  34. static int s_nParseStateIgnoreNestedSections;
  35. void CreateAttributeConfigParsers() {
  36. s_HiddenDevAttributeParser = new SMCParser();
  37. s_HiddenDevAttributeParser.OnStart = OnAttributeConfigStartParse;
  38. s_HiddenDevAttributeParser.OnEnd = OnAttributeConfigEndParse;
  39. s_HiddenDevAttributeParser.OnEnterSection = OnAttributeConfigEnterSection;
  40. s_HiddenDevAttributeParser.OnLeaveSection = OnAttributeConfigLeaveSection;
  41. s_HiddenDevAttributeParser.OnKeyValue = OnAttributeConfigKeyValue;
  42. }
  43. void OnAttributeConfigStartParse(SMCParser smc) {
  44. s_ParseState = HiddenAttributeConfigParseState_Root;
  45. s_nParseStateIgnoreNestedSections = 0;
  46. }
  47. /**
  48. * Push new parse state depending on the current section.
  49. */
  50. SMCResult OnAttributeConfigEnterSection(SMCParser smc, const char[] name, bool opt_quotes) {
  51. /**
  52. * If we're ignoring a parent section, increment and don't emit a change in parse state.
  53. */
  54. if (s_nParseStateIgnoreNestedSections) {
  55. s_nParseStateIgnoreNestedSections++;
  56. return SMCParse_Continue;
  57. }
  58. switch (s_ParseState) {
  59. case HiddenAttributeConfigParseState_Root: {
  60. if (StrEqual(name, "attributes", .caseSensitive = false)) {
  61. s_ParseState = HiddenAttributeConfigParseState_AttributeList;
  62. } else {
  63. LogError("Entering unexpected section '%s' while in parse state %d", name, s_ParseState);
  64. return SMCParse_HaltFail;
  65. }
  66. }
  67. case HiddenAttributeConfigParseState_AttributeList: {
  68. s_AttributeContext.Clear();
  69. int attrdef;
  70. if (StrEqual(name, "auto", .caseSensitive = false)) {
  71. // do nothing
  72. } else if (StringToIntEx(name, attrdef)) {
  73. s_AttributeContext.SetDefIndex(attrdef);
  74. #if defined DEBUG
  75. LogMessage("Setting attribute context to itemdef %d", attrdef);
  76. #endif
  77. } else {
  78. LogError("Attempting to declare attribute with invalid defindex '%s'", name);
  79. return SMCParse_HaltFail;
  80. }
  81. s_ParseState = HiddenAttributeConfigParseState_AttributeProperties;
  82. }
  83. case HiddenAttributeConfigParseState_AttributeProperties: {
  84. // ignore nested properties for now
  85. LogMessage("Warning: Nested keys are not currently supported");
  86. s_nParseStateIgnoreNestedSections++;
  87. }
  88. default: {
  89. LogError("Entering unexpected section '%s' while in parse state %d", name,
  90. s_ParseState);
  91. return SMCParse_HaltFail;
  92. }
  93. }
  94. return SMCParse_Continue;
  95. }
  96. /**
  97. * Pop parse state and go back to previous one.
  98. */
  99. SMCResult OnAttributeConfigLeaveSection(SMCParser smc) {
  100. /**
  101. * If we're leaving an ignored section, decrement and don't emit a change in parse state.
  102. */
  103. if (s_nParseStateIgnoreNestedSections) {
  104. s_nParseStateIgnoreNestedSections--;
  105. return SMCParse_Continue;
  106. }
  107. switch (s_ParseState) {
  108. case HiddenAttributeConfigParseState_AttributeProperties: {
  109. s_ParseState = HiddenAttributeConfigParseState_AttributeList;
  110. s_AttributeContext.Register();
  111. }
  112. case HiddenAttributeConfigParseState_AttributeList: {
  113. s_ParseState = HiddenAttributeConfigParseState_Root;
  114. }
  115. case HiddenAttributeConfigParseState_Root: {
  116. LogError("Leaving section while in root parse state");
  117. }
  118. default: {
  119. LogError("Leaving section while in parse state %d", s_ParseState);
  120. }
  121. }
  122. return SMCParse_Continue;
  123. }
  124. SMCResult OnAttributeConfigKeyValue(SMCParser smc, const char[] key, const char[] value,
  125. bool key_quotes, bool value_quotes) {
  126. switch (s_ParseState) {
  127. case HiddenAttributeConfigParseState_AttributeProperties: {
  128. s_AttributeContext.SetCustom(key, value);
  129. #if defined DEBUG
  130. LogMessage("Adding attribute context key / value: '%s' = '%s'", key, value);
  131. #endif
  132. }
  133. default: {
  134. LogError("Unexpected key / value pair while in parse state %d", s_ParseState);
  135. }
  136. }
  137. return SMCParse_Continue;
  138. }
  139. void OnAttributeConfigEndParse(SMCParser smc, bool halted, bool failed) {
  140. if (halted || failed) {
  141. return;
  142. }
  143. if (s_ParseState != HiddenAttributeConfigParseState_Root) {
  144. LogError("Parse state not at root on end parse");
  145. }
  146. }
  147. void LoadAttributes() {
  148. char configDir[PLATFORM_MAX_PATH];
  149. BuildPath(Path_SM, configDir, sizeof(configDir), "configs/tf2nativeattribs");
  150. if (!DirExists(configDir, false)) {
  151. return;
  152. }
  153. DirectoryListing list = OpenDirectory(configDir, false);
  154. FileType fileType;
  155. char filename[PLATFORM_MAX_PATH];
  156. while (list.GetNext(filename, sizeof(filename), fileType)) {
  157. if (fileType != FileType_File) {
  158. continue;
  159. }
  160. Format(filename, sizeof(filename), "%s/%s", configDir, filename);
  161. if (strncmp(filename[strlen(filename) - 4], ".txt", 4, false) != 0) {
  162. PrintToServer("[TF2 Econ Dynamic] "
  163. ... "Attributes file %s does not have a txt extension. "
  164. ... "Attributes will not be loaded",
  165. filename);
  166. continue;
  167. }
  168. if (s_HiddenDevAttributeParser.ParseFile(filename) != SMCError_Okay) {
  169. PrintToServer("[TF2 Econ Dynamic] "
  170. ... "Failed to load file %s. Attributes will not be loaded",
  171. filename);
  172. }
  173. }
  174. }