csrd_data_dump.sp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * [CSRD] SRCDS Data Dumper
  3. *
  4. * Uses SourceMod dumping commands to dump useful information for plugin developers.
  5. */
  6. #pragma semicolon 1
  7. #include <sourcemod>
  8. #tryinclude <steamtools>
  9. #pragma newdecls required
  10. #include <stocksoup/log_server>
  11. #include <stocksoup/files>
  12. #include <stocksoup/version>
  13. #define PLUGIN_VERSION "1.1.3"
  14. public Plugin myinfo = {
  15. name = "[CSRD] SRCDS Automatic Data Dumper",
  16. author = "nosoop",
  17. description = "Automatically dumps useful engine data for plugin stuff.",
  18. #if defined _steamtools_included
  19. version = PLUGIN_VERSION,
  20. #else
  21. version = PLUGIN_VERSION ... "-no-steamtools",
  22. #endif
  23. url = "https://git.csrd.science/"
  24. }
  25. #define REDUMP_MARKER_FILE "data/.force-data-dump"
  26. #define REDUMP_OUTPUT_DIRECTORY "data/datadump"
  27. // Use a map with as few entities as possible to ensure enough free edicts
  28. #define LOW_ENTITY_MAP "itemtest"
  29. enum DumpAction {
  30. DumpAction_None = 0, // no checks are performed
  31. DumpAction_UpdateCheck, // check if dump indicator is present (TODO change method?)
  32. DumpAction_PendingDump // pending dump on current map
  33. };
  34. DumpAction g_DumpAction;
  35. StringMap g_ConCommandFlagCache;
  36. public APLRes AskPluginLoad2(Handle hPluginSelf, bool bLateLoaded, char[] error, int err_max) {
  37. if (bLateLoaded) {
  38. g_DumpAction = DumpAction_None;
  39. } else {
  40. g_DumpAction = DumpAction_UpdateCheck;
  41. /**
  42. * cache flags early in case a plugin overrides flags
  43. * https://github.com/nosoop/SM-ConVarConfigs does this when all plugins are loaded
  44. */
  45. g_ConCommandFlagCache = new StringMap();
  46. char commandName[128];
  47. bool bIsCommand;
  48. int flags;
  49. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName),
  50. bIsCommand, flags);
  51. do {
  52. g_ConCommandFlagCache.SetValue(commandName, flags);
  53. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  54. bIsCommand, flags) );
  55. delete hConCommandIter;
  56. }
  57. return APLRes_Success;
  58. }
  59. public void OnPluginStart() {
  60. RegAdminCmd("csrd_force_data_dump", ForceDataDump, ADMFLAG_ROOT);
  61. }
  62. public void OnMapStart() {
  63. char path[PLATFORM_MAX_PATH];
  64. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  65. switch (g_DumpAction) {
  66. /**
  67. * Stage 1: Check if the marker file exists; change map if necessary.
  68. */
  69. case DumpAction_UpdateCheck: {
  70. if (FileExists(path, false)) {
  71. g_DumpAction = DumpAction_PendingDump;
  72. ForceChangeLevel(LOW_ENTITY_MAP, "Starting server data dump");
  73. } else {
  74. g_DumpAction = DumpAction_None;
  75. // we're not dumping, so there's no point in keeping the cache
  76. delete g_ConCommandFlagCache;
  77. g_ConCommandFlagCache = null;
  78. }
  79. }
  80. /**
  81. * Stage 2: Validate map change and ensure dump is pending; start dumping if so.
  82. */
  83. case DumpAction_PendingDump: {
  84. char currentMap[PLATFORM_MAX_PATH];
  85. GetCurrentMap(currentMap, sizeof(currentMap));
  86. if (StrEqual(LOW_ENTITY_MAP, currentMap) && FileExists(path, false)) {
  87. PerformDataDump();
  88. DeleteFile(path);
  89. int version = GetNetworkPatchVersion();
  90. LogMessage("Dumped server data for network patchversion %d", version);
  91. RequestFrame(ReloadServer);
  92. } else {
  93. LogError("Failed to change to map %s", LOW_ENTITY_MAP);
  94. }
  95. }
  96. }
  97. }
  98. void PerformDataDump() {
  99. char outputDirectory[PLATFORM_MAX_PATH];
  100. BuildPath(Path_SM, outputDirectory, sizeof(outputDirectory), REDUMP_OUTPUT_DIRECTORY);
  101. CreateDirectories(outputDirectory, 0b111101000); // u+rwx,g+rx
  102. int version = GetNetworkPatchVersion();
  103. // does not spawn entities
  104. ServerCommand("sm_dump_netprops %s/%d.netprops.txt", outputDirectory, version);
  105. DumpServerCommands("%s/%d.commands.txt", outputDirectory, version);
  106. DumpServerConVars("%s/%d.convars.txt", outputDirectory, version);
  107. /**
  108. * spawns all entities, server will crash on map change -- make sure there are 1400-ish
  109. * free edicts
  110. */
  111. ServerCommand("sm_dump_datamaps %s/%d.datamaps.txt", outputDirectory, version);
  112. // TODO replace this with just filtering the appropriate lines from datamaps?
  113. // match expr /^[A-Za-z0-9_]+ - [A-Za-z0-9_]+$/
  114. ServerCommand("sm_dump_classes %s/%d.classes.txt", outputDirectory, version);
  115. // Create and update a .server-version file to notify incron of updates
  116. char updatePath[PLATFORM_MAX_PATH];
  117. BuildPath(Path_SM, updatePath, sizeof(updatePath), "%s/%s", REDUMP_OUTPUT_DIRECTORY,
  118. ".server-version");
  119. File versionFile = OpenFile(updatePath, "w");
  120. versionFile.WriteLine("%d", version);
  121. delete versionFile;
  122. }
  123. #if defined _steamtools_included
  124. /**
  125. * Steam master server has requested a restart; game version probably updated.
  126. * Create the file indicating a data dump should be performed.
  127. */
  128. public Action Steam_RestartRequested() {
  129. PrepareRedump();
  130. return Plugin_Continue;
  131. }
  132. #endif
  133. public void ReloadServer(any trash_professor_abacus) {
  134. ServerCommand("quit");
  135. }
  136. public Action ForceDataDump(int client, int argc) {
  137. PrepareRedump();
  138. ReplyToCommand(client, "[SM] Prepared dump process. Reload plugin or restart server.");
  139. return Plugin_Continue;
  140. }
  141. /* Dump utilities */
  142. void DumpServerCommands(const char[] format, any ...) {
  143. char outputPath[PLATFORM_MAX_PATH];
  144. VFormat(outputPath, sizeof(outputPath), format, 2);
  145. File outputFile = OpenFile(outputPath, "w");
  146. char commandName[128], commandDescription[512];
  147. bool bIsCommand;
  148. int flags;
  149. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", "Names", "Flags", "Help Text");
  150. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  151. flags, commandDescription, sizeof(commandDescription));
  152. do {
  153. if (bIsCommand) {
  154. if (g_ConCommandFlagCache) {
  155. g_ConCommandFlagCache.GetValue(commandName, flags);
  156. }
  157. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  158. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  159. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", commandName,
  160. GetCommandFlagString(flags), commandDescription);
  161. }
  162. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  163. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  164. delete hConCommandIter;
  165. delete outputFile;
  166. }
  167. void DumpServerConVars(const char[] format, any ...) {
  168. char outputPath[PLATFORM_MAX_PATH];
  169. VFormat(outputPath, sizeof(outputPath), format, 2);
  170. File outputFile = OpenFile(outputPath, "w");
  171. char commandName[128], commandDescription[512];
  172. bool bIsCommand;
  173. int flags;
  174. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", "Names", "Defaults", "Flags",
  175. "Help Text");
  176. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  177. flags, commandDescription, sizeof(commandDescription));
  178. do {
  179. if (!bIsCommand) {
  180. if (g_ConCommandFlagCache) {
  181. g_ConCommandFlagCache.GetValue(commandName, flags);
  182. }
  183. char defaultValue[128];
  184. FindConVar(commandName).GetDefault(defaultValue, sizeof(defaultValue));
  185. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  186. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  187. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", commandName,
  188. defaultValue, GetCommandFlagString(flags), commandDescription);
  189. }
  190. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  191. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  192. delete hConCommandIter;
  193. delete outputFile;
  194. }
  195. char[] GetCommandFlagString(int flags) {
  196. static char s_flagStrings[][] = {
  197. "UNREGISTERED",
  198. "DEVELOPMENTONLY",
  199. "GAMEDLL",
  200. "CLIENTDLL",
  201. "HIDDEN",
  202. "PROTECTED",
  203. "SPONLY",
  204. "ARCHIVE",
  205. "NOTIFY",
  206. "USERINFO",
  207. "PRINTABLEONLY",
  208. "UNLOGGED",
  209. "NEVER_AS_STRING",
  210. "REPLICATED",
  211. "CHEAT",
  212. "SS", // split screen
  213. "DEMO",
  214. "DONTRECORD",
  215. "PLUGIN_OR_SS_ADDED", // split screen; same value as FCVAR_PLUGIN
  216. "RELEASE",
  217. "RELOAD_MATERIALS",
  218. "RELOAD_TEXTURES",
  219. "NOT_CONNECTED",
  220. "MATERIAL_SYSTEM_THREAD",
  221. "ARCHIVE_GAMECONSOLE",
  222. "ACCESSIBLE_FROM_THREADS",
  223. "SERVER_CAN_EXECUTE",
  224. "SERVER_CANNOT_QUERY",
  225. "CLIENTCMD_CAN_EXECUTE"
  226. };
  227. char buffer[512];
  228. for (int i = 0; i < sizeof(s_flagStrings); i++) {
  229. int flagbit = (1 << i);
  230. if (flags & flagbit) {
  231. if (strlen(buffer)) {
  232. StrCat(buffer, sizeof(buffer), " ");
  233. }
  234. StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
  235. }
  236. }
  237. return buffer;
  238. }
  239. /* Dump marker functrions */
  240. void PrepareRedump() {
  241. char path[PLATFORM_MAX_PATH];
  242. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  243. TouchFile(path);
  244. }
  245. void TouchFile(const char[] file) {
  246. File f = OpenFile(file, "w");
  247. delete f;
  248. }