csrd_data_dump.sp 10 KB

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