csrd_data_dump.sp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.4"
  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. * This is performed on first map load.
  69. */
  70. case DumpAction_UpdateCheck: {
  71. if (FileExists(path, false)) {
  72. g_DumpAction = DumpAction_PendingDump;
  73. ForceChangeLevel(LOW_ENTITY_MAP, "Starting server data dump");
  74. } else {
  75. g_DumpAction = DumpAction_None;
  76. // we're not dumping, so there's no point in keeping the cache
  77. delete g_ConCommandFlagCache;
  78. g_ConCommandFlagCache = null;
  79. }
  80. }
  81. /**
  82. * Stage 2: Validate map change and ensure dump is pending; start dumping if so.
  83. */
  84. case DumpAction_PendingDump: {
  85. char currentMap[PLATFORM_MAX_PATH];
  86. GetCurrentMap(currentMap, sizeof(currentMap));
  87. if (StrEqual(LOW_ENTITY_MAP, currentMap) && FileExists(path, false)) {
  88. DataPack pack = new DataPack();
  89. pack.WriteString(path);
  90. PerformDataDump(OnDataDumpFinished, pack);
  91. } else {
  92. LogError("Failed to change to map %s", LOW_ENTITY_MAP);
  93. }
  94. }
  95. }
  96. }
  97. void PerformDataDump(RequestFrameCallback postDumpCallback = INVALID_FUNCTION, any data = 0) {
  98. char outputDirectory[PLATFORM_MAX_PATH];
  99. BuildPath(Path_SM, outputDirectory, sizeof(outputDirectory), REDUMP_OUTPUT_DIRECTORY);
  100. CreateDirectories(outputDirectory, 0b111101000); // u+rwx,g+rx
  101. int version = GetNetworkPatchVersion();
  102. // does not spawn entities
  103. ServerCommand("sm_dump_netprops %s/%d.netprops.txt", outputDirectory, version);
  104. DumpServerCommands("%s/%d.commands.txt", outputDirectory, version);
  105. DumpServerConVars("%s/%d.convars.txt", outputDirectory, version);
  106. /**
  107. * spawns all entities, server will crash on map change -- make sure there are 1400-ish
  108. * free edicts
  109. */
  110. ServerCommand("sm_dump_datamaps %s/%d.datamaps.txt", outputDirectory, version);
  111. // TODO replace this with just filtering the appropriate lines from datamaps?
  112. // match expr /^[A-Za-z0-9_]+ - [A-Za-z0-9_]+$/
  113. ServerCommand("sm_dump_classes %s/%d.classes.txt", outputDirectory, version);
  114. ServerCommand("sm_dump_teprops %s/%d.tempents.txt", outputDirectory, version);
  115. // Defer the rest of the actions by a frame to ensure everything is finished processing.
  116. DataPack pack = new DataPack();
  117. pack.WriteFunction(postDumpCallback);
  118. pack.WriteCell(data);
  119. RequestFrame(PerformDataDump_NextFrame, pack);
  120. }
  121. public void PerformDataDump_NextFrame(DataPack pack) {
  122. // Create and update a .server-version file to notify incron of updates
  123. char updatePath[PLATFORM_MAX_PATH];
  124. BuildPath(Path_SM, updatePath, sizeof(updatePath), "%s/%s", REDUMP_OUTPUT_DIRECTORY,
  125. ".server-version");
  126. File versionFile = OpenFile(updatePath, "w");
  127. versionFile.WriteLine("%d", GetNetworkPatchVersion());
  128. delete versionFile;
  129. pack.Reset();
  130. Function postDumpCallback = pack.ReadFunction();
  131. any data = pack.ReadCell();
  132. delete pack;
  133. if (postDumpCallback != INVALID_FUNCTION) {
  134. Call_StartFunction(INVALID_HANDLE, postDumpCallback);
  135. Call_PushCell(data);
  136. Call_Finish();
  137. }
  138. }
  139. /**
  140. * User-defined call when the dumping process is finished.
  141. */
  142. public void OnDataDumpFinished(DataPack pack) {
  143. pack.Reset();
  144. char path[PLATFORM_MAX_PATH];
  145. pack.ReadString(path, sizeof(path));
  146. delete pack;
  147. DeleteFile(path);
  148. LogMessage("Dumped server data for network patchversion %d", GetNetworkPatchVersion());
  149. ServerCommand("quit");
  150. }
  151. #if defined _steamtools_included
  152. /**
  153. * Steam master server has requested a restart; game version probably updated.
  154. * Create the file indicating a data dump should be performed.
  155. */
  156. public Action Steam_RestartRequested() {
  157. PrepareRedump();
  158. return Plugin_Continue;
  159. }
  160. #endif
  161. public Action ForceDataDump(int client, int argc) {
  162. PrepareRedump();
  163. ReplyToCommand(client, "[SM] Prepared dump process. Reload plugin or restart server.");
  164. return Plugin_Continue;
  165. }
  166. /* Dump utilities */
  167. void DumpServerCommands(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\"", "Names", "Flags", "Help Text");
  175. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  176. flags, commandDescription, sizeof(commandDescription));
  177. do {
  178. if (bIsCommand) {
  179. if (g_ConCommandFlagCache) {
  180. g_ConCommandFlagCache.GetValue(commandName, flags);
  181. }
  182. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  183. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  184. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", commandName,
  185. GetCommandFlagString(flags), commandDescription);
  186. }
  187. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  188. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  189. delete hConCommandIter;
  190. delete outputFile;
  191. }
  192. void DumpServerConVars(const char[] format, any ...) {
  193. char outputPath[PLATFORM_MAX_PATH];
  194. VFormat(outputPath, sizeof(outputPath), format, 2);
  195. File outputFile = OpenFile(outputPath, "w");
  196. char commandName[128], commandDescription[512];
  197. bool bIsCommand;
  198. int flags;
  199. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", "Names", "Defaults", "Flags",
  200. "Help Text");
  201. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  202. flags, commandDescription, sizeof(commandDescription));
  203. do {
  204. if (!bIsCommand) {
  205. if (g_ConCommandFlagCache) {
  206. g_ConCommandFlagCache.GetValue(commandName, flags);
  207. }
  208. char defaultValue[128];
  209. FindConVar(commandName).GetDefault(defaultValue, sizeof(defaultValue));
  210. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  211. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  212. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", commandName,
  213. defaultValue, GetCommandFlagString(flags), commandDescription);
  214. }
  215. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  216. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  217. delete hConCommandIter;
  218. delete outputFile;
  219. }
  220. char[] GetCommandFlagString(int flags) {
  221. static char s_flagStrings[][] = {
  222. "UNREGISTERED",
  223. "DEVELOPMENTONLY",
  224. "GAMEDLL",
  225. "CLIENTDLL",
  226. "HIDDEN",
  227. "PROTECTED",
  228. "SPONLY",
  229. "ARCHIVE",
  230. "NOTIFY",
  231. "USERINFO",
  232. "PRINTABLEONLY",
  233. "UNLOGGED",
  234. "NEVER_AS_STRING",
  235. "REPLICATED",
  236. "CHEAT",
  237. "SS", // split screen
  238. "DEMO",
  239. "DONTRECORD",
  240. "PLUGIN_OR_SS_ADDED", // split screen; same value as FCVAR_PLUGIN
  241. "RELEASE",
  242. "RELOAD_MATERIALS",
  243. "RELOAD_TEXTURES",
  244. "NOT_CONNECTED",
  245. "MATERIAL_SYSTEM_THREAD",
  246. "ARCHIVE_GAMECONSOLE",
  247. "ACCESSIBLE_FROM_THREADS",
  248. "SERVER_CAN_EXECUTE",
  249. "SERVER_CANNOT_QUERY",
  250. "CLIENTCMD_CAN_EXECUTE"
  251. };
  252. char buffer[512];
  253. for (int i = 0; i < sizeof(s_flagStrings); i++) {
  254. int flagbit = (1 << i);
  255. if (flags & flagbit) {
  256. if (strlen(buffer)) {
  257. StrCat(buffer, sizeof(buffer), " ");
  258. }
  259. StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
  260. }
  261. }
  262. return buffer;
  263. }
  264. /* Dump marker functrions */
  265. void PrepareRedump() {
  266. char path[PLATFORM_MAX_PATH];
  267. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  268. TouchFile(path);
  269. }
  270. void TouchFile(const char[] file) {
  271. File f = OpenFile(file, "w");
  272. delete f;
  273. }