1
0

csrd_data_dump.sp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.1"
  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. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  137. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", commandName,
  138. GetCommandFlagString(flags), commandDescription);
  139. }
  140. } while ( FindNextConCommand(hConCommand, commandName, sizeof(commandName),
  141. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  142. delete outputFile;
  143. }
  144. void DumpServerConVars(const char[] format, any ...) {
  145. char outputPath[PLATFORM_MAX_PATH];
  146. VFormat(outputPath, sizeof(outputPath), format, 2);
  147. File outputFile = OpenFile(outputPath, "w");
  148. char commandName[128], commandDescription[512];
  149. bool bIsCommand;
  150. int flags;
  151. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", "Names", "Defaults", "Flags",
  152. "Help Text");
  153. Handle hConCommand = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  154. flags, commandDescription, sizeof(commandDescription));
  155. do {
  156. if (!bIsCommand) {
  157. char defaultValue[128];
  158. FindConVar(commandName).GetDefault(defaultValue, sizeof(defaultValue));
  159. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  160. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  161. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", commandName,
  162. defaultValue, GetCommandFlagString(flags), commandDescription);
  163. }
  164. } while ( FindNextConCommand(hConCommand, commandName, sizeof(commandName),
  165. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  166. delete outputFile;
  167. }
  168. char[] GetCommandFlagString(int flags) {
  169. static char s_flagStrings[][] = {
  170. "UNREGISTERED",
  171. "DEVELOPMENTONLY",
  172. "GAMEDLL",
  173. "CLIENTDLL",
  174. "HIDDEN",
  175. "PROTECTED",
  176. "SPONLY",
  177. "ARCHIVE",
  178. "NOTIFY",
  179. "USERINFO",
  180. "PRINTABLEONLY",
  181. "UNLOGGED",
  182. "NEVER_AS_STRING",
  183. "REPLICATED",
  184. "CHEAT",
  185. "SS", // split screen
  186. "DEMO",
  187. "DONTRECORD",
  188. "PLUGIN_OR_SS_ADDED", // split screen; same value as FCVAR_PLUGIN
  189. "RELEASE",
  190. "RELOAD_MATERIALS",
  191. "RELOAD_TEXTURES",
  192. "NOT_CONNECTED",
  193. "MATERIAL_SYSTEM_THREAD",
  194. "ARCHIVE_GAMECONSOLE",
  195. "ACCESSIBLE_FROM_THREADS",
  196. "SERVER_CAN_EXECUTE",
  197. "SERVER_CANNOT_QUERY",
  198. "CLIENTCMD_CAN_EXECUTE"
  199. };
  200. char buffer[512];
  201. for (int i = 0; i < sizeof(s_flagStrings); i++) {
  202. int flagbit = (1 << i);
  203. if (flags & flagbit) {
  204. if (strlen(buffer)) {
  205. StrCat(buffer, sizeof(buffer), " ");
  206. }
  207. StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
  208. }
  209. }
  210. return buffer;
  211. }
  212. /* Dump marker functrions */
  213. void PrepareRedump() {
  214. char path[PLATFORM_MAX_PATH];
  215. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  216. TouchFile(path);
  217. }
  218. void TouchFile(const char[] file) {
  219. File f = OpenFile(file, "w");
  220. delete f;
  221. }