csrd_data_dump.sp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.0"
  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_FileCheck, // check if dump indicator is present
  32. DumpAction_PendingDump // pending dump on current map
  33. };
  34. DumpAction g_DumpAction;
  35. public APLRes AskPluginLoad2(Handle hPluginSelf, bool bLateLoaded, char[] error, int err_max) {
  36. if (bLateLoaded) {
  37. g_DumpAction = DumpAction_None;
  38. } else {
  39. g_DumpAction = DumpAction_FileCheck;
  40. }
  41. return APLRes_Success;
  42. }
  43. public void OnPluginStart() {
  44. RegAdminCmd("csrd_force_data_dump", ForceDataDump, ADMFLAG_ROOT);
  45. }
  46. public void OnMapStart() {
  47. char path[PLATFORM_MAX_PATH];
  48. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  49. switch (g_DumpAction) {
  50. /**
  51. * Stage 1: Check if the marker file exists; change map if necessary.
  52. */
  53. case DumpAction_FileCheck: {
  54. if (FileExists(path, false)) {
  55. g_DumpAction = DumpAction_PendingDump;
  56. ForceChangeLevel(LOW_ENTITY_MAP, "Starting server data dump");
  57. } else {
  58. g_DumpAction = DumpAction_None;
  59. }
  60. }
  61. /**
  62. * Stage 2: Validate map change and ensure dump is pending; start dumping if so.
  63. */
  64. case DumpAction_PendingDump: {
  65. char currentMap[PLATFORM_MAX_PATH];
  66. GetCurrentMap(currentMap, sizeof(currentMap));
  67. if (StrEqual(LOW_ENTITY_MAP, currentMap) && FileExists(path, false)) {
  68. PerformDataDump();
  69. DeleteFile(path);
  70. int version = GetNetworkPatchVersion();
  71. LogMessage("Dumped server data for network patchversion %d", version);
  72. RequestFrame(ReloadServer);
  73. } else {
  74. LogError("Failed to change to map %s", LOW_ENTITY_MAP);
  75. }
  76. }
  77. }
  78. }
  79. void PerformDataDump() {
  80. char outputDirectory[PLATFORM_MAX_PATH];
  81. BuildPath(Path_SM, outputDirectory, sizeof(outputDirectory), REDUMP_OUTPUT_DIRECTORY);
  82. CreateDirectories(outputDirectory, 0b111101000); // u+rwx,g+rx
  83. int version = GetNetworkPatchVersion();
  84. // does not spawn entities
  85. ServerCommand("sm_dump_netprops %s/%d.netprops.txt", outputDirectory, version);
  86. DumpServerCommands("%s/%d.commands.txt", outputDirectory, version);
  87. DumpServerConVars("%s/%d.convars.txt", outputDirectory, version);
  88. /**
  89. * spawns all entities, server will crash on map change -- make sure there are 1400-ish
  90. * free edicts
  91. */
  92. ServerCommand("sm_dump_datamaps %s/%d.datamaps.txt", outputDirectory, version);
  93. // TODO replace this with just filtering the appropriate lines from datamaps?
  94. // match expr /^[A-Za-z0-9_]+ - [A-Za-z0-9_]+$/
  95. ServerCommand("sm_dump_classes %s/%d.classes.txt", outputDirectory, version);
  96. // Create and update a .server-version file to notify incron of updates
  97. char updatePath[PLATFORM_MAX_PATH];
  98. BuildPath(Path_SM, updatePath, sizeof(updatePath), "%s/%s", REDUMP_OUTPUT_DIRECTORY,
  99. ".server-version");
  100. File versionFile = OpenFile(updatePath, "w");
  101. versionFile.WriteLine("%d", version);
  102. delete versionFile;
  103. }
  104. #if defined _steamtools_included
  105. /**
  106. * Steam master server has requested a restart; game version probably updated.
  107. * Create the file indicating a data dump should be performed.
  108. */
  109. public Action Steam_RestartRequested() {
  110. PrepareRedump();
  111. return Plugin_Continue;
  112. }
  113. #endif
  114. public void ReloadServer(any trash_professor_abacus) {
  115. ServerCommand("quit");
  116. }
  117. public Action ForceDataDump(int client, int argc) {
  118. PrepareRedump();
  119. ReplyToCommand(client, "[SM] Prepared dump process. Reload plugin or restart server.");
  120. return Plugin_Continue;
  121. }
  122. /* Dump utilities */
  123. void DumpServerCommands(const char[] format, any ...) {
  124. char outputPath[PLATFORM_MAX_PATH];
  125. VFormat(outputPath, sizeof(outputPath), format, 2);
  126. File outputFile = OpenFile(outputPath, "w");
  127. char commandName[128], commandDescription[512];
  128. bool bIsCommand;
  129. int flags;
  130. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", "Names", "Flags", "Help Text");
  131. Handle hConCommand = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  132. flags, commandDescription, sizeof(commandDescription));
  133. do {
  134. if (bIsCommand) {
  135. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  136. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", commandName,
  137. GetCommandFlagString(flags), commandDescription);
  138. }
  139. } while ( FindNextConCommand(hConCommand, commandName, sizeof(commandName),
  140. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  141. delete outputFile;
  142. }
  143. void DumpServerConVars(const char[] format, any ...) {
  144. char outputPath[PLATFORM_MAX_PATH];
  145. VFormat(outputPath, sizeof(outputPath), format, 2);
  146. File outputFile = OpenFile(outputPath, "w");
  147. char commandName[128], commandDescription[512];
  148. bool bIsCommand;
  149. int flags;
  150. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", "Names", "Defaults", "Flags",
  151. "Help Text");
  152. Handle hConCommand = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  153. flags, commandDescription, sizeof(commandDescription));
  154. do {
  155. if (!bIsCommand) {
  156. char defaultValue[128];
  157. FindConVar(commandName).GetDefault(defaultValue, sizeof(defaultValue));
  158. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  159. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", commandName,
  160. defaultValue, GetCommandFlagString(flags), commandDescription);
  161. }
  162. } while ( FindNextConCommand(hConCommand, commandName, sizeof(commandName),
  163. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  164. delete outputFile;
  165. }
  166. char[] GetCommandFlagString(int flags) {
  167. static char s_flagStrings[][] = {
  168. "UNREGISTERED",
  169. "DEVELOPMENTONLY",
  170. "GAMEDLL",
  171. "CLIENTDLL",
  172. "HIDDEN",
  173. "PROTECTED",
  174. "SPONLY",
  175. "ARCHIVE",
  176. "NOTIFY",
  177. "USERINFO",
  178. "PRINTABLEONLY",
  179. "UNLOGGED",
  180. "NEVER_AS_STRING",
  181. "REPLICATED",
  182. "CHEAT",
  183. "SS", // split screen
  184. "DEMO",
  185. "DONTRECORD",
  186. "PLUGIN_OR_SS_ADDED", // split screen; same value as FCVAR_PLUGIN
  187. "RELEASE",
  188. "RELOAD_MATERIALS",
  189. "RELOAD_TEXTURES",
  190. "NOT_CONNECTED",
  191. "MATERIAL_SYSTEM_THREAD",
  192. "ARCHIVE_GAMECONSOLE",
  193. "ACCESSIBLE_FROM_THREADS",
  194. "SERVER_CAN_EXECUTE",
  195. "SERVER_CANNOT_QUERY",
  196. "CLIENTCMD_CAN_EXECUTE"
  197. };
  198. char buffer[512];
  199. for (int i = 0; i < sizeof(s_flagStrings); i++) {
  200. int flagbit = (1 << i);
  201. if (flags & flagbit) {
  202. if (strlen(buffer)) {
  203. StrCat(buffer, sizeof(buffer), " ");
  204. }
  205. StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
  206. }
  207. }
  208. return buffer;
  209. }
  210. /* Dump marker functrions */
  211. void PrepareRedump() {
  212. char path[PLATFORM_MAX_PATH];
  213. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  214. TouchFile(path);
  215. }
  216. void TouchFile(const char[] file) {
  217. File f = OpenFile(file, "w");
  218. delete f;
  219. }