csrd_data_dump.sp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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.3.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_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. // skip dump on late load because muh consistency
  39. g_DumpAction = DumpAction_None;
  40. } else {
  41. g_DumpAction = DumpAction_UpdateCheck;
  42. /**
  43. * cache flags early in case a plugin overrides flags
  44. * https://github.com/nosoop/SM-ConVarConfigs does this when all plugins are loaded
  45. */
  46. g_ConCommandFlagCache = new StringMap();
  47. char commandName[128];
  48. bool bIsCommand;
  49. int flags;
  50. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName),
  51. bIsCommand, flags);
  52. do {
  53. g_ConCommandFlagCache.SetValue(commandName, flags);
  54. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  55. bIsCommand, flags) );
  56. delete hConCommandIter;
  57. }
  58. return APLRes_Success;
  59. }
  60. public void OnPluginStart() {
  61. RegAdminCmd("csrd_force_data_dump", ForceDataDump, ADMFLAG_ROOT);
  62. }
  63. public void OnMapStart() {
  64. char path[PLATFORM_MAX_PATH];
  65. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  66. switch (g_DumpAction) {
  67. /**
  68. * Stage 1: Check if the marker file exists; change map if necessary.
  69. * This is performed on first map load.
  70. */
  71. case DumpAction_UpdateCheck: {
  72. if (FileExists(path, false)) {
  73. g_DumpAction = DumpAction_PendingDump;
  74. ForceChangeLevel(LOW_ENTITY_MAP, "Starting server data dump");
  75. } else {
  76. g_DumpAction = DumpAction_None;
  77. // we're not dumping, so there's no point in keeping the cache
  78. delete g_ConCommandFlagCache;
  79. g_ConCommandFlagCache = null;
  80. }
  81. }
  82. /**
  83. * Stage 2: Validate map change and ensure dump is pending; start dumping if so.
  84. */
  85. case DumpAction_PendingDump: {
  86. char currentMap[PLATFORM_MAX_PATH];
  87. GetCurrentMap(currentMap, sizeof(currentMap));
  88. if (StrEqual(LOW_ENTITY_MAP, currentMap) && FileExists(path, false)) {
  89. DataPack pack = new DataPack();
  90. pack.WriteString(path);
  91. PerformDataDump(OnDataDumpFinished, pack);
  92. } else {
  93. LogError("Failed to change to map %s", LOW_ENTITY_MAP);
  94. }
  95. }
  96. }
  97. }
  98. void PerformDataDump(RequestFrameCallback postDumpCallback = INVALID_FUNCTION, any data = 0) {
  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. DumpUserMessageNames("%s/%d.usermessages.txt", outputDirectory, version);
  108. /**
  109. * Spawns all entities, server will crash on map change -- make sure there are 700-ish
  110. * free edicts.
  111. * No more class dump; we'll just generate it with `grep -P '^\w+ - \w+$'` on datamaps.
  112. */
  113. ServerCommand("sm_dump_datamaps %s/%d.datamaps.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. 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. /**
  221. * Dumps user message names to a path created by the format string.
  222. */
  223. void DumpUserMessageNames(const char[] format, any ...) {
  224. char outputPath[PLATFORM_MAX_PATH];
  225. VFormat(outputPath, sizeof(outputPath), format, 2);
  226. File outputFile = OpenFile(outputPath, "w");
  227. outputFile.WriteLine("\"%s\",\"%s\"", "Index", "Name");
  228. char umName[128];
  229. for (int um = 0; GetUserMessageName(view_as<UserMsg>(um), umName, sizeof(umName)); um++) {
  230. outputFile.WriteLine("\"%d\",\"%s\"", um, umName);
  231. }
  232. delete outputFile;
  233. }
  234. /**
  235. * Converts bitflags from a ConCommandBase to a space-separated list of flag descriptions.
  236. */
  237. char[] GetCommandFlagString(int flags) {
  238. // static array of flag descriptions, index mapped to corresponding bit
  239. static char s_flagStrings[][] = {
  240. "UNREGISTERED",
  241. "DEVELOPMENTONLY",
  242. "GAMEDLL",
  243. "CLIENTDLL",
  244. "HIDDEN",
  245. "PROTECTED",
  246. "SPONLY",
  247. "ARCHIVE",
  248. "NOTIFY",
  249. "USERINFO",
  250. "PRINTABLEONLY",
  251. "UNLOGGED",
  252. "NEVER_AS_STRING",
  253. "REPLICATED",
  254. "CHEAT",
  255. "SS", // split screen
  256. "DEMO",
  257. "DONTRECORD",
  258. "PLUGIN_OR_SS_ADDED", // split screen; same value as FCVAR_PLUGIN
  259. "RELEASE",
  260. "RELOAD_MATERIALS",
  261. "RELOAD_TEXTURES",
  262. "NOT_CONNECTED",
  263. "MATERIAL_SYSTEM_THREAD",
  264. "ARCHIVE_GAMECONSOLE",
  265. "ACCESSIBLE_FROM_THREADS",
  266. "SERVER_CAN_EXECUTE",
  267. "SERVER_CANNOT_QUERY",
  268. "CLIENTCMD_CAN_EXECUTE"
  269. };
  270. char buffer[512];
  271. for (int i = 0; i < sizeof(s_flagStrings); i++) {
  272. int flagbit = (1 << i);
  273. if (flags & flagbit) {
  274. if (strlen(buffer)) {
  275. StrCat(buffer, sizeof(buffer), " ");
  276. }
  277. StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
  278. }
  279. }
  280. return buffer;
  281. }
  282. /* Dump marker functrions */
  283. void PrepareRedump() {
  284. char path[PLATFORM_MAX_PATH];
  285. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  286. TouchFile(path);
  287. }
  288. void TouchFile(const char[] file) {
  289. File f = OpenFile(file, "w");
  290. delete f;
  291. }