csrd_data_dump.sp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.0.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. /**
  97. * TODO touch .server-version file in REDUMP_OUTPUT_DIRECTORY to notify incron of completed
  98. * dump
  99. */
  100. }
  101. #if defined _steamtools_included
  102. /**
  103. * Steam master server has requested a restart; game version probably updated.
  104. * Create the file indicating a data dump should be performed.
  105. */
  106. public Action Steam_RestartRequested() {
  107. PrepareRedump();
  108. return Plugin_Continue;
  109. }
  110. #endif
  111. public void ReloadServer(any trash_professor_abacus) {
  112. ServerCommand("quit");
  113. }
  114. public Action ForceDataDump(int client, int argc) {
  115. PrepareRedump();
  116. ReplyToCommand(client, "[SM] Prepared dump process. Reload plugin or restart server.");
  117. return Plugin_Continue;
  118. }
  119. /* Dump utilities */
  120. void DumpServerCommands(const char[] format, any ...) {
  121. char outputPath[PLATFORM_MAX_PATH];
  122. VFormat(outputPath, sizeof(outputPath), format, 2);
  123. File outputFile = OpenFile(outputPath, "w");
  124. char commandName[128], commandDescription[512];
  125. bool bIsCommand;
  126. int flags;
  127. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", "Names", "Flags", "Help Text");
  128. Handle hConCommand = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  129. flags, commandDescription, sizeof(commandDescription));
  130. do {
  131. if (bIsCommand) {
  132. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  133. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", commandName,
  134. GetCommandFlagString(flags), commandDescription);
  135. }
  136. } while ( FindNextConCommand(hConCommand, commandName, sizeof(commandName),
  137. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  138. delete outputFile;
  139. }
  140. void DumpServerConVars(const char[] format, any ...) {
  141. char outputPath[PLATFORM_MAX_PATH];
  142. VFormat(outputPath, sizeof(outputPath), format, 2);
  143. File outputFile = OpenFile(outputPath, "w");
  144. char commandName[128], commandDescription[512];
  145. bool bIsCommand;
  146. int flags;
  147. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", "Names", "Defaults", "Flags",
  148. "Help Text");
  149. Handle hConCommand = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  150. flags, commandDescription, sizeof(commandDescription));
  151. do {
  152. if (!bIsCommand) {
  153. char defaultValue[128];
  154. FindConVar(commandName).GetDefault(defaultValue, sizeof(defaultValue));
  155. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  156. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", commandName,
  157. defaultValue, GetCommandFlagString(flags), commandDescription);
  158. }
  159. } while ( FindNextConCommand(hConCommand, commandName, sizeof(commandName),
  160. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  161. delete outputFile;
  162. }
  163. char[] GetCommandFlagString(int flags) {
  164. static char s_flagStrings[][] = {
  165. "UNREGISTERED",
  166. "DEVELOPMENTONLY",
  167. "GAMEDLL",
  168. "CLIENTDLL",
  169. "HIDDEN",
  170. "PROTECTED",
  171. "SPONLY",
  172. "ARCHIVE",
  173. "NOTIFY",
  174. "USERINFO",
  175. "PRINTABLEONLY",
  176. "UNLOGGED",
  177. "NEVER_AS_STRING",
  178. "REPLICATED",
  179. "CHEAT",
  180. "SS", // split screen
  181. "DEMO",
  182. "DONTRECORD",
  183. "PLUGIN_OR_SS_ADDED", // split screen; same value as FCVAR_PLUGIN
  184. "RELEASE",
  185. "RELOAD_MATERIALS",
  186. "RELOAD_TEXTURES",
  187. "NOT_CONNECTED",
  188. "MATERIAL_SYSTEM_THREAD",
  189. "ARCHIVE_GAMECONSOLE",
  190. "ACCESSIBLE_FROM_THREADS",
  191. "SERVER_CAN_EXECUTE",
  192. "SERVER_CANNOT_QUERY",
  193. "CLIENTCMD_CAN_EXECUTE"
  194. };
  195. char buffer[512];
  196. for (int i = 0; i < sizeof(s_flagStrings); i++) {
  197. int flagbit = (1 << i);
  198. if (flags & flagbit) {
  199. if (strlen(buffer)) {
  200. StrCat(buffer, sizeof(buffer), " ");
  201. }
  202. StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
  203. }
  204. }
  205. return buffer;
  206. }
  207. /* Dump marker functrions */
  208. void PrepareRedump() {
  209. char path[PLATFORM_MAX_PATH];
  210. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  211. TouchFile(path);
  212. }
  213. void TouchFile(const char[] file) {
  214. File f = OpenFile(file, "w");
  215. delete f;
  216. }