#pragma semicolon 1 #include #undef REQUIRE_PLUGIN #include #include #include #define REQUIRE_PLUGIN #pragma newdecls required ConVar g_cvHostname; ConVar g_cvFormattedHostname; ConVar g_cvHostIP; ConVar g_cvHostPort; ConVar g_cvPassword; ConVar g_cvMaxLength; ConVar g_cvSendToClients; // Dropped in this order while the hostname is longer than sm_hostname_max_length. char g_sFoldOrder[][] = { "LagCompensation", "NoSteam" }; bool g_bCouchLoaded; bool g_bHHLoaded; bool g_bSDLoaded; public Plugin myinfo = { name = "HostnameManager", author = "Neon", description = "", version = "1.0", url = "https://steamcommunity.com/id/n3ontm" }; //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public void OnPluginStart() { g_cvHostname = FindConVar("hostname"); g_cvHostIP = FindConVar("hostip"); g_cvHostPort = FindConVar("hostport"); g_cvPassword = FindConVar("sv_password"); g_cvFormattedHostname = CreateConVar("sm_hostname_format", "{HH}UNLOZE | {GM} | LagCompensation | NoSteam{SD}", "HH = HappyHour, GM = GameMode (Nemesis Zombie Escape > Couch Escape > Zombie Escape), SD = StageDisplay"); g_cvMaxLength = CreateConVar("sm_hostname_max_length", "63", "Fold LagCompensation, then NoSteam, out of the hostname while it is longer than this many bytes (the server browser shows at most 63)", 0, true, 1.0, true, 127.0); g_cvSendToClients = CreateConVar("sm_hostname_send_to_clients", "1", "Send the new hostname to connected clients so their scoreboard updates without a reconnect", 0, true, 0.0, true, 1.0); g_cvFormattedHostname.AddChangeHook(OnFormatChanged); g_cvMaxLength.AddChangeHook(OnFormatChanged); CreateTimer(60.0, TimerRepeat, INVALID_HANDLE, TIMER_REPEAT); HookEvent("round_start", OnRoundStart, EventHookMode_PostNoCopy); } //---------------------------------------------------------------------------------------------------- // Purpose: Apply a new format (e.g. from event_on.cfg) straight away instead of on the next timer. //---------------------------------------------------------------------------------------------------- public void OnFormatChanged(ConVar convar, const char[] oldValue, const char[] newValue) { RefreshHostname(); } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public void OnAllPluginsLoaded() { g_bCouchLoaded = LibraryExists("Couch"); g_bHHLoaded = LibraryExists("HappyHour"); g_bSDLoaded = LibraryExists("StageDisplay"); } //---------------------------------------------------------------------------------------------------- // Purpose: server.cfg resets the hostname every map, and this also runs on a late load. //---------------------------------------------------------------------------------------------------- public void OnConfigsExecuted() { RefreshHostname(); } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public void OnLibraryAdded(const char[] sName) { if (strcmp(sName, "Couch", false) == 0) g_bCouchLoaded = true; if (strcmp(sName, "HappyHour", false) == 0) g_bHHLoaded = true; if (strcmp(sName, "StageDisplay", false) == 0) g_bSDLoaded = true; } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public void OnLibraryRemoved(const char[] sName) { // Unloading Couch doesn't fire Couch_OnCouchRemoved, so drop the couch game mode here. if (strcmp(sName, "Couch", false) == 0) { g_bCouchLoaded = false; RefreshHostname(); } if (strcmp(sName, "HappyHour", false) == 0) g_bHHLoaded = false; if (strcmp(sName, "StageDisplay", false) == 0) g_bSDLoaded = false; } //---------------------------------------------------------------------------------------------------- // Purpose: Refresh now for anything that changed during the restart, then again once StageDisplay // has worked out the new stage (it checks 3s after round_start). //---------------------------------------------------------------------------------------------------- public void OnRoundStart(Event hEvent, const char[] sEvent, bool bDontBroadcast) { RefreshHostname(); CreateTimer(5.0, TimerRoundStart, INVALID_HANDLE, TIMER_FLAG_NO_MAPCHANGE); } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public Action TimerRoundStart(Handle timer) { RefreshHostname(); return Plugin_Stop; } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public Action TimerRepeat(Handle timer) { RefreshHostname(); return Plugin_Continue; } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public void RefreshHostname() { char sFormattedHostname[128]; g_cvFormattedHostname.GetString(sFormattedHostname, sizeof(sFormattedHostname)); if (g_bHHLoaded && HH_IsItHappyHour()) ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{HH}", "[HappyHour] ", true); else ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{HH}", "", true); char sStage[64]; if (g_bSDLoaded && SD_GetStage(sStage, sizeof(sStage))) { Format(sStage, sizeof(sStage), " | Stage: [%s]", sStage); ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{SD}", sStage, true); } else ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{SD}", "", true); if (IsNemesisMap()) ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{GM}", "Nemesis Zombie Escape", true); else if (IsCouchPresent()) ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{GM}", "Couch Escape", true); else ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), "{GM}", "Zombie Escape", true); TidyHostname(sFormattedHostname); for (int i = 0; i < sizeof(g_sFoldOrder) && strlen(sFormattedHostname) > g_cvMaxLength.IntValue; i++) { ReplaceString(sFormattedHostname, sizeof(sFormattedHostname), g_sFoldOrder[i], "", true); TidyHostname(sFormattedHostname); } char sCurrentHostname[128]; g_cvHostname.GetString(sCurrentHostname, sizeof(sCurrentHostname)); if (strcmp(sFormattedHostname, sCurrentHostname, true) == 0) return; g_cvHostname.SetString(sFormattedHostname, false, false); if (g_cvSendToClients.BoolValue) SendHostname(sFormattedHostname); } //---------------------------------------------------------------------------------------------------- // Purpose: The scoreboard only reads the hostname from server_spawn, which the client's engine fires // locally from svc_ServerInfo on connect. Send a copy straight to each client so it never // reaches server-side listeners, and fill every field so other client listeners // (mumble, abuse report, steam share) see the same values they got on connect. //---------------------------------------------------------------------------------------------------- void SendHostname(const char[] sHostname) { Event hEvent = CreateEvent("server_spawn", true); if (hEvent == null) return; int iIP = g_cvHostIP.IntValue; int iPort = g_cvHostPort.IntValue; char sAddress[32]; Format(sAddress, sizeof(sAddress), "%d.%d.%d.%d:%d", (iIP >>> 24) & 0xFF, (iIP >>> 16) & 0xFF, (iIP >>> 8) & 0xFF, iIP & 0xFF, iPort); char sGame[32]; GetGameFolderName(sGame, sizeof(sGame)); char sMap[PLATFORM_MAX_PATH]; GetCurrentMap(sMap, sizeof(sMap)); char sPassword[64]; g_cvPassword.GetString(sPassword, sizeof(sPassword)); hEvent.SetString("hostname", sHostname); hEvent.SetString("address", sAddress); hEvent.SetInt("ip", iIP); hEvent.SetInt("port", iPort); hEvent.SetString("game", sGame); hEvent.SetString("mapname", sMap); hEvent.SetInt("maxplayers", MaxClients); hEvent.SetString("os", "LINUX"); hEvent.SetBool("dedicated", IsDedicatedServer()); hEvent.SetBool("password", sPassword[0] != '\0'); for (int client = 1; client <= MaxClients; client++) { if (!IsClientInGame(client) || IsFakeClient(client)) continue; hEvent.FireToClient(client); } // FireToClient does not free the event. hEvent.Cancel(); } //---------------------------------------------------------------------------------------------------- // Purpose: The couch entity is purged on every round restart and respawned at round start, so a // remembered placement counts too, otherwise the hostname would flicker each round. //---------------------------------------------------------------------------------------------------- bool IsCouchPresent() { if (!g_bCouchLoaded) return false; return Couch_GetCouch() != -1 || Couch_IsPlaced(); } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public void Couch_OnCouchSpawned(int couch) { RefreshHostname(); } //---------------------------------------------------------------------------------------------------- // Purpose: Couch_Remove fires this before it forgets the placement, so wait a frame. //---------------------------------------------------------------------------------------------------- public void Couch_OnCouchRemoved() { RequestFrame(Frame_RefreshHostname); } //---------------------------------------------------------------------------------------------------- // Purpose: //---------------------------------------------------------------------------------------------------- public void Frame_RefreshHostname(any data) { RefreshHostname(); } //---------------------------------------------------------------------------------------------------- // Purpose: Empty tokens and folded tags leave separators and spaces behind. //---------------------------------------------------------------------------------------------------- void TidyHostname(char[] sText) { DropEmptySeparators(sText); CollapseSpaces(sText); } //---------------------------------------------------------------------------------------------------- // Purpose: {SD} brings its own " | ", so a format like "{GM} | {SD}" leaves "| |" with a stage and a // trailing "|" without one. Drop any separator with nothing but spaces on one side. //---------------------------------------------------------------------------------------------------- void DropEmptySeparators(char[] sText) { int iOut = 0; for (int i = 0; sText[i] != '\0'; i++) { if (sText[i] == '|') { int iLast = iOut; while (iLast > 0 && sText[iLast - 1] == ' ') iLast--; if (iLast == 0 || sText[iLast - 1] == '|') continue; } sText[iOut++] = sText[i]; } while (iOut > 0 && (sText[iOut - 1] == ' ' || sText[iOut - 1] == '|')) iOut--; sText[iOut] = '\0'; } //---------------------------------------------------------------------------------------------------- // Purpose: Tokens that resolve to nothing leave their surrounding spaces behind, so squash any run // of spaces down to one and trim the ends. //---------------------------------------------------------------------------------------------------- void CollapseSpaces(char[] sText) { int iOut = 0; for (int i = 0; sText[i] != '\0'; i++) { if (sText[i] == ' ' && (iOut == 0 || sText[iOut - 1] == ' ')) continue; sText[iOut++] = sText[i]; } if (iOut > 0 && sText[iOut - 1] == ' ') iOut--; sText[iOut] = '\0'; } //---------------------------------------------------------------------------------------------------- // Purpose: Looked up every refresh because not every server runs the InfoMessage plugin, and it // may load after us. //---------------------------------------------------------------------------------------------------- bool IsNemesisMap() { ConVar cvInfoMessage = FindConVar("sm_info_message_file"); if (cvInfoMessage == null) return false; char sFile[PLATFORM_MAX_PATH]; cvInfoMessage.GetString(sFile, sizeof(sFile)); return StrContains(sFile, "nemesis", false) != -1; }