123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- #pragma semicolon 1
- #include <sourcemod>
- #tryinclude <steamtools>
- #pragma newdecls required
- #include <stocksoup/log_server>
- #include <stocksoup/files>
- #include <stocksoup/version>
- #define PLUGIN_VERSION "1.4.0"
- public Plugin myinfo = {
- name = "[CSRD] SRCDS Automatic Data Dumper",
- author = "nosoop",
- description = "Automatically dumps useful engine data for plugin stuff.",
-
- #if defined _steamtools_included
- version = PLUGIN_VERSION,
- #else
- version = PLUGIN_VERSION ... "-no-steamtools",
- #endif
-
- url = "https://git.csrd.science/"
- }
- #define REDUMP_MARKER_FILE "data/.force-data-dump"
- #define REDUMP_OUTPUT_DIRECTORY "data/datadump"
- #define LOW_ENTITY_MAP "itemtest"
- enum DumpAction {
- DumpAction_None = 0,
- DumpAction_UpdateCheck,
- DumpAction_PendingDump
- };
- DumpAction g_DumpAction;
- StringMap g_ConCommandFlagCache;
- public APLRes AskPluginLoad2(Handle hPluginSelf, bool bLateLoaded, char[] error, int err_max) {
- if (bLateLoaded) {
-
- g_DumpAction = DumpAction_None;
- } else {
- g_DumpAction = DumpAction_UpdateCheck;
-
-
- g_ConCommandFlagCache = new StringMap();
-
- char commandName[128];
- bool bIsCommand;
- int flags;
-
- Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName),
- bIsCommand, flags);
-
- do {
- g_ConCommandFlagCache.SetValue(commandName, flags);
- } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
- bIsCommand, flags) );
- delete hConCommandIter;
- }
-
- return APLRes_Success;
- }
- public void OnPluginStart() {
- RegAdminCmd("csrd_force_data_dump", ForceDataDump, ADMFLAG_ROOT);
- }
- public void OnMapStart() {
- char path[PLATFORM_MAX_PATH];
- BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
-
- switch (g_DumpAction) {
-
- case DumpAction_UpdateCheck: {
- if (FileExists(path) || IsNewServerVersion()) {
- g_DumpAction = DumpAction_PendingDump;
- ForceChangeLevel(LOW_ENTITY_MAP, "Starting server data dump");
- } else {
- g_DumpAction = DumpAction_None;
-
-
- delete g_ConCommandFlagCache;
- g_ConCommandFlagCache = null;
- }
- }
-
-
- case DumpAction_PendingDump: {
- char currentMap[PLATFORM_MAX_PATH];
- GetCurrentMap(currentMap, sizeof(currentMap));
-
- if (StrEqual(LOW_ENTITY_MAP, currentMap)
- && (FileExists(path) || IsNewServerVersion())) {
- DataPack pack = new DataPack();
- pack.WriteString(path);
-
- PerformDataDump(OnDataDumpFinished, pack);
- } else {
- LogError("Failed to change to map %s", LOW_ENTITY_MAP);
- }
- }
- }
- }
- bool IsNewServerVersion() {
- char updatePath[PLATFORM_MAX_PATH];
- BuildPath(Path_SM, updatePath, sizeof(updatePath), "%s/%s", REDUMP_OUTPUT_DIRECTORY,
- ".server-version");
-
- if (FileExists(updatePath)) {
- char versionString[64];
-
- File versionFile = OpenFile(updatePath, "r");
- versionFile.ReadLine(versionString, sizeof(versionString));
- delete versionFile;
-
- return GetNetworkPatchVersion() > StringToInt(versionString);
- }
- return true;
- }
- void PerformDataDump(RequestFrameCallback postDumpCallback = INVALID_FUNCTION, any data = 0) {
- char outputDirectory[PLATFORM_MAX_PATH];
- BuildPath(Path_SM, outputDirectory, sizeof(outputDirectory), REDUMP_OUTPUT_DIRECTORY);
-
- CreateDirectories(outputDirectory, 0b111101000);
-
- int version = GetNetworkPatchVersion();
-
-
- ServerCommand("sm_dump_netprops %s/%d.netprops.txt", outputDirectory, version);
-
- DumpServerCommands("%s/%d.commands.txt", outputDirectory, version);
- DumpServerConVars("%s/%d.convars.txt", outputDirectory, version);
- DumpUserMessageNames("%s/%d.usermessages.txt", outputDirectory, version);
-
-
- ServerCommand("sm_dump_datamaps %s/%d.datamaps.txt", outputDirectory, version);
-
- ServerCommand("sm_dump_teprops %s/%d.tempents.txt", outputDirectory, version);
-
-
- DataPack pack = new DataPack();
- pack.WriteFunction(postDumpCallback);
- pack.WriteCell(data);
-
- RequestFrame(PerformDataDump_NextFrame, pack);
- }
- public void PerformDataDump_NextFrame(DataPack pack) {
-
- char updatePath[PLATFORM_MAX_PATH];
- BuildPath(Path_SM, updatePath, sizeof(updatePath), "%s/%s", REDUMP_OUTPUT_DIRECTORY,
- ".server-version");
-
- File versionFile = OpenFile(updatePath, "w");
- versionFile.WriteLine("%d", GetNetworkPatchVersion());
- delete versionFile;
-
- pack.Reset();
-
- Function postDumpCallback = pack.ReadFunction();
- any data = pack.ReadCell();
-
- delete pack;
-
- if (postDumpCallback != INVALID_FUNCTION) {
- Call_StartFunction(INVALID_HANDLE, postDumpCallback);
- Call_PushCell(data);
- Call_Finish();
- }
- }
- public void OnDataDumpFinished(DataPack pack) {
- pack.Reset();
-
- char path[PLATFORM_MAX_PATH];
- pack.ReadString(path, sizeof(path));
- delete pack;
-
- if (FileExists(path)) {
- DeleteFile(path);
- }
-
- LogMessage("Dumped server data for network patchversion %d", GetNetworkPatchVersion());
-
- ServerCommand("quit");
- }
- #if defined _steamtools_included
- public Action Steam_RestartRequested() {
- PrepareRedump();
- return Plugin_Continue;
- }
- #endif
- public Action ForceDataDump(int client, int argc) {
- PrepareRedump();
- ReplyToCommand(client, "[SM] Prepared dump process. Restart server.");
- return Plugin_Continue;
- }
- void DumpServerCommands(const char[] format, any ...) {
- char outputPath[PLATFORM_MAX_PATH];
- VFormat(outputPath, sizeof(outputPath), format, 2);
-
- File outputFile = OpenFile(outputPath, "w");
-
- char commandName[128], commandDescription[512];
- bool bIsCommand;
- int flags;
-
- outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", "Names", "Flags", "Help Text");
-
- Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
- flags, commandDescription, sizeof(commandDescription));
-
- do {
- if (bIsCommand) {
- if (g_ConCommandFlagCache) {
- g_ConCommandFlagCache.GetValue(commandName, flags);
- }
-
- ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
- ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
-
- outputFile.WriteLine("\"%s\",\"%s\",\"%s\"", commandName,
- GetCommandFlagString(flags), commandDescription);
- }
- } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
- bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
-
- delete hConCommandIter;
-
- delete outputFile;
- }
- void DumpServerConVars(const char[] format, any ...) {
- char outputPath[PLATFORM_MAX_PATH];
- VFormat(outputPath, sizeof(outputPath), format, 2);
-
- File outputFile = OpenFile(outputPath, "w");
-
- char commandName[128], commandDescription[512];
- bool bIsCommand;
- int flags;
-
- outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", "Names", "Defaults", "Flags",
- "Help Text");
-
- Handle hConCommandIter = FindFirstConCommand(commandName, sizeof(commandName), bIsCommand,
- flags, commandDescription, sizeof(commandDescription));
-
- do {
- if (!bIsCommand) {
- if (g_ConCommandFlagCache) {
- g_ConCommandFlagCache.GetValue(commandName, flags);
- }
-
- char defaultValue[128];
- FindConVar(commandName).GetDefault(defaultValue, sizeof(defaultValue));
-
- ReplaceString(commandDescription, sizeof(commandDescription), "\"", "'");
- ReplaceString(commandDescription, sizeof(commandDescription), "\n", " / ");
-
- outputFile.WriteLine("\"%s\",\"%s\",\"%s\",\"%s\"", commandName,
- defaultValue, GetCommandFlagString(flags), commandDescription);
- }
- } while ( FindNextConCommand(hConCommandIter, commandName, sizeof(commandName),
- bIsCommand, flags, commandDescription, sizeof(commandDescription)) );
- delete hConCommandIter;
- delete outputFile;
- }
- void DumpUserMessageNames(const char[] format, any ...) {
- char outputPath[PLATFORM_MAX_PATH];
- VFormat(outputPath, sizeof(outputPath), format, 2);
-
- File outputFile = OpenFile(outputPath, "w");
- outputFile.WriteLine("\"%s\",\"%s\"", "Index", "Name");
-
- char umName[128];
- for (int um = 0; GetUserMessageName(view_as<UserMsg>(um), umName, sizeof(umName)); um++) {
- outputFile.WriteLine("\"%d\",\"%s\"", um, umName);
- }
-
- delete outputFile;
- }
- char[] GetCommandFlagString(int flags) {
-
- static char s_flagStrings[][] = {
- "UNREGISTERED",
- "DEVELOPMENTONLY",
- "GAMEDLL",
- "CLIENTDLL",
- "HIDDEN",
- "PROTECTED",
- "SPONLY",
- "ARCHIVE",
- "NOTIFY",
- "USERINFO",
- "PRINTABLEONLY",
- "UNLOGGED",
- "NEVER_AS_STRING",
- "REPLICATED",
- "CHEAT",
- "SS",
- "DEMO",
- "DONTRECORD",
- "PLUGIN_OR_SS_ADDED",
- "RELEASE",
- "RELOAD_MATERIALS",
- "RELOAD_TEXTURES",
- "NOT_CONNECTED",
- "MATERIAL_SYSTEM_THREAD",
- "ARCHIVE_GAMECONSOLE",
- "ACCESSIBLE_FROM_THREADS",
- "SERVER_CAN_EXECUTE",
- "SERVER_CANNOT_QUERY",
- "CLIENTCMD_CAN_EXECUTE"
- };
-
- char buffer[512];
-
- for (int i = 0; i < sizeof(s_flagStrings); i++) {
- int flagbit = (1 << i);
-
- if (flags & flagbit) {
- if (strlen(buffer)) {
- StrCat(buffer, sizeof(buffer), " ");
- }
- StrCat(buffer, sizeof(buffer), s_flagStrings[i]);
- }
- }
-
- return buffer;
- }
- void PrepareRedump() {
- char path[PLATFORM_MAX_PATH];
- BuildPath(Path_SM, path, sizeof(path), "%s", REDUMP_MARKER_FILE);
-
- TouchFile(path);
- }
- void TouchFile(const char[] file) {
- File f = OpenFile(file, "w");
- delete f;
- }
|