csrd_data_dump.sp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.4.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) || IsNewServerVersion()) {
  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)
  89. && (FileExists(path) || IsNewServerVersion())) {
  90. DataPack pack = new DataPack();
  91. pack.WriteString(path);
  92. PerformDataDump(OnDataDumpFinished, pack);
  93. } else {
  94. LogError("Failed to change to map %s", LOW_ENTITY_MAP);
  95. }
  96. }
  97. }
  98. }
  99. bool IsNewServerVersion() {
  100. char updatePath[PLATFORM_MAX_PATH];
  101. BuildPath(Path_SM, updatePath, sizeof(updatePath), "%s/%s", REDUMP_OUTPUT_DIRECTORY,
  102. ".server-version");
  103. if (FileExists(updatePath)) {
  104. char versionString[64];
  105. File versionFile = OpenFile(updatePath, "r");
  106. versionFile.ReadLine(versionString, sizeof(versionString));
  107. delete versionFile;
  108. return GetNetworkPatchVersion() > StringToInt(versionString);
  109. }
  110. return true;
  111. }
  112. void PerformDataDump(RequestFrameCallback postDumpCallback = INVALID_FUNCTION, any data = 0) {
  113. char outputDirectory[PLATFORM_MAX_PATH];
  114. BuildPath(Path_SM, outputDirectory, sizeof(outputDirectory), REDUMP_OUTPUT_DIRECTORY);
  115. CreateDirectories(outputDirectory, 0b111101000); // u+rwx,g+rx
  116. int version = GetNetworkPatchVersion();
  117. // does not spawn entities
  118. ServerCommand("sm_dump_netprops %s/%d.netprops.txt", outputDirectory, version);
  119. DumpServerCommands("%s/%d.commands.txt", outputDirectory, version);
  120. DumpServerConVars("%s/%d.convars.txt", outputDirectory, version);
  121. DumpUserMessageNames("%s/%d.usermessages.txt", outputDirectory, version);
  122. /**
  123. * Spawns all entities, server will crash on map change -- make sure there are 700-ish
  124. * free edicts.
  125. * No more class dump; we'll just generate it with `grep -P '^\w+ - \w+$'` on datamaps.
  126. */
  127. ServerCommand("sm_dump_datamaps %s/%d.datamaps.txt", outputDirectory, version);
  128. ServerCommand("sm_dump_teprops %s/%d.tempents.txt", outputDirectory, version);
  129. // Defer the rest of the actions by a frame to ensure everything is finished processing.
  130. DataPack pack = new DataPack();
  131. pack.WriteFunction(postDumpCallback);
  132. pack.WriteCell(data);
  133. RequestFrame(PerformDataDump_NextFrame, pack);
  134. }
  135. public void PerformDataDump_NextFrame(DataPack pack) {
  136. // Create and update a .server-version file to notify incron of updates
  137. char updatePath[PLATFORM_MAX_PATH];
  138. BuildPath(Path_SM, updatePath, sizeof(updatePath), "%s/%s", REDUMP_OUTPUT_DIRECTORY,
  139. ".server-version");
  140. File versionFile = OpenFile(updatePath, "w");
  141. versionFile.WriteLine("%d", GetNetworkPatchVersion());
  142. delete versionFile;
  143. pack.Reset();
  144. Function postDumpCallback = pack.ReadFunction();
  145. any data = pack.ReadCell();
  146. delete pack;
  147. if (postDumpCallback != INVALID_FUNCTION) {
  148. Call_StartFunction(INVALID_HANDLE, postDumpCallback);
  149. Call_PushCell(data);
  150. Call_Finish();
  151. }
  152. }
  153. /**
  154. * User-defined call when the dumping process is finished.
  155. */
  156. public void OnDataDumpFinished(DataPack pack) {
  157. pack.Reset();
  158. char path[PLATFORM_MAX_PATH];
  159. pack.ReadString(path, sizeof(path));
  160. delete pack;
  161. if (FileExists(path)) {
  162. DeleteFile(path);
  163. }
  164. LogMessage("Dumped server data for network patchversion %d", GetNetworkPatchVersion());
  165. ServerCommand("quit");
  166. }
  167. #if defined _steamtools_included
  168. /**
  169. * Steam master server has requested a restart; game version probably updated.
  170. * Create the file indicating a data dump should be performed.
  171. */
  172. public Action Steam_RestartRequested() {
  173. PrepareRedump();
  174. return Plugin_Continue;
  175. }
  176. #endif
  177. public Action ForceDataDump(int client, int argc) {
  178. PrepareRedump();
  179. ReplyToCommand(client, "[SM] Prepared dump process. Restart server.");
  180. return Plugin_Continue;
  181. }
  182. /* Dump utilities */
  183. void DumpServerCommands(const char[] format, any ...) {
  184. char outputPath[PLATFORM_MAX_PATH];
  185. VFormat(outputPath, sizeof(outputPath), format, 2);
  186. File outputFile = OpenFile(outputPath, "w");
  187. char commandName[128], commandDescription[512];
  188. bool bIsCommand;
  189. int flags;
  190. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", "Names", "Flags", "Help Text");
  191. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  192. flags, commandDescription, sizeof(commandDescription));
  193. do {
  194. if (bIsCommand) {
  195. if (g_ConCommandFlagCache) {
  196. g_ConCommandFlagCache.GetValue(commandName, flags);
  197. }
  198. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  199. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  200. outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", commandName,
  201. GetCommandFlagString(flags), commandDescription);
  202. }
  203. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  204. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  205. delete hConCommandIter;
  206. delete outputFile;
  207. }
  208. void DumpServerConVars(const char[] format, any ...) {
  209. char outputPath[PLATFORM_MAX_PATH];
  210. VFormat(outputPath, sizeof(outputPath), format, 2);
  211. File outputFile = OpenFile(outputPath, "w");
  212. char commandName[128], commandDescription[512];
  213. bool bIsCommand;
  214. int flags;
  215. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", "Names", "Defaults", "Flags",
  216. "Help Text");
  217. Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
  218. flags, commandDescription, sizeof(commandDescription));
  219. do {
  220. if (!bIsCommand) {
  221. if (g_ConCommandFlagCache) {
  222. g_ConCommandFlagCache.GetValue(commandName, flags);
  223. }
  224. char defaultValue[128];
  225. FindConVar(commandName).GetDefault(defaultValue, sizeof(defaultValue));
  226. ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
  227. ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
  228. outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", commandName,
  229. defaultValue, GetCommandFlagString(flags), commandDescription);
  230. }
  231. } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
  232. bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
  233. delete hConCommandIter;
  234. delete outputFile;
  235. }
  236. /**
  237. * Dumps user message names to a path created by the format string.
  238. */
  239. void DumpUserMessageNames(const char[] format, any ...) {
  240. char outputPath[PLATFORM_MAX_PATH];
  241. VFormat(outputPath, sizeof(outputPath), format, 2);
  242. File outputFile = OpenFile(outputPath, "w");
  243. outputFile.WriteLine("\"%s\",\"%s\"", "Index", "Name");
  244. char umName[128];
  245. for (int um = 0; GetUserMessageName(view_as<UserMsg>(um), umName, sizeof(umName)); um++) {
  246. outputFile.WriteLine("\"%d\",\"%s\"", um, umName);
  247. }
  248. delete outputFile;
  249. }
  250. /**
  251. * Converts bitflags from a ConCommandBase to a space-separated list of flag descriptions.
  252. */
  253. char[] GetCommandFlagString(int flags) {
  254. // static array of flag descriptions, index mapped to corresponding bit
  255. static char s_flagStrings[][] = {
  256. "UNREGISTERED",
  257. "DEVELOPMENTONLY",
  258. "GAMEDLL",
  259. "CLIENTDLL",
  260. "HIDDEN",
  261. "PROTECTED",
  262. "SPONLY",
  263. "ARCHIVE",
  264. "NOTIFY",
  265. "USERINFO",
  266. "PRINTABLEONLY",
  267. "UNLOGGED",
  268. "NEVER_AS_STRING",
  269. "REPLICATED",
  270. "CHEAT",
  271. "SS", // split screen
  272. "DEMO",
  273. "DONTRECORD",
  274. "PLUGIN_OR_SS_ADDED", // split screen; same value as FCVAR_PLUGIN
  275. "RELEASE",
  276. "RELOAD_MATERIALS",
  277. "RELOAD_TEXTURES",
  278. "NOT_CONNECTED",
  279. "MATERIAL_SYSTEM_THREAD",
  280. "ARCHIVE_GAMECONSOLE",
  281. "ACCESSIBLE_FROM_THREADS",
  282. "SERVER_CAN_EXECUTE",
  283. "SERVER_CANNOT_QUERY",
  284. "CLIENTCMD_CAN_EXECUTE"
  285. };
  286. char buffer[512];
  287. for (int i = 0; i < sizeof(s_flagStrings); i++) {
  288. int flagbit = (1 << i);
  289. if (flags & flagbit) {
  290. if (strlen(buffer)) {
  291. StrCat(buffer, sizeof(buffer), " ");
  292. }
  293. StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
  294. }
  295. }
  296. return buffer;
  297. }
  298. /* Dump marker functrions */
  299. void PrepareRedump() {
  300. char path[PLATFORM_MAX_PATH];
  301. BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
  302. TouchFile(path);
  303. }
  304. void TouchFile(const char[] file) {
  305. File f = OpenFile(file, "w");
  306. delete f;
  307. }