|
| 1 | +/** |
| 2 | + * ============================================================================= |
| 3 | + * CleanerCS2 |
| 4 | + * Copyright (C) 2024 Poggu |
| 5 | + * ============================================================================= |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify it under |
| 8 | + * the terms of the GNU General Public License, version 3.0, as published by the |
| 9 | + * Free Software Foundation. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 14 | + * details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License along with |
| 17 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <stdio.h> |
| 21 | +#include "cleanercs2.h" |
| 22 | +#include <iserver.h> |
| 23 | +#include <funchook.h> |
| 24 | +#include "utils/module.h" |
| 25 | +#include <iostream> |
| 26 | +#include <fstream> |
| 27 | +#include <filesystem> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +// bruh |
| 31 | +#undef POSIX |
| 32 | +#include <re2/re2.h> |
| 33 | + |
| 34 | +#ifdef _WIN32 |
| 35 | +#define ROOTBIN "/bin/win64/" |
| 36 | +#define GAMEBIN "/csgo/bin/win64/" |
| 37 | +#else |
| 38 | +#define ROOTBIN "/bin/linuxsteamrt64/" |
| 39 | +#define GAMEBIN "/csgo/bin/linuxsteamrt64/" |
| 40 | +#endif |
| 41 | + |
| 42 | +CleanerPlugin g_CleanerPlugin; |
| 43 | +IServerGameDLL *server = NULL; |
| 44 | +IServerGameClients *gameclients = NULL; |
| 45 | +IVEngineServer *engine = NULL; |
| 46 | +IGameEventManager2 *gameevents = NULL; |
| 47 | +ICvar *icvar = NULL; |
| 48 | + |
| 49 | +typedef int (*LogDirect_t)(void* loggingSystem, int channel, int severity, LeafCodeInfo_t*, LoggingMetaData_t*, Color, char const*, va_list*); |
| 50 | +LogDirect_t g_pLogDirect = nullptr; |
| 51 | +funchook_t* g_pHook = nullptr; |
| 52 | + |
| 53 | +std::vector<re2::RE2*> g_RegexList; |
| 54 | + |
| 55 | +int Detour_LogDirect(void* loggingSystem, int channel, int severity, LeafCodeInfo_t* leafCode, LoggingMetaData_t* metaData, Color color, char const* str, va_list* args) |
| 56 | +{ |
| 57 | + char buffer[MAX_LOGGING_MESSAGE_LENGTH]; |
| 58 | + |
| 59 | + if (args) |
| 60 | + { |
| 61 | + va_list args2; |
| 62 | + va_copy(args2, *args); |
| 63 | + V_vsnprintf(buffer, sizeof buffer, str, args2); |
| 64 | + va_end(args2); |
| 65 | + } |
| 66 | + |
| 67 | + for (auto& regex : g_RegexList) |
| 68 | + { |
| 69 | + if (RE2::FullMatch(args ? buffer : str, *regex)) |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + return g_pLogDirect(loggingSystem, channel, severity, leafCode, metaData, color, str, args); |
| 74 | +} |
| 75 | + |
| 76 | +bool SetupHook() |
| 77 | +{ |
| 78 | + auto serverModule = new CModule(ROOTBIN, "tier0"); |
| 79 | + |
| 80 | + int err; |
| 81 | +#ifdef WIN32 |
| 82 | + const byte sig[] = "\x4C\x89\x4C\x24\x20\x44\x89\x44\x24\x18\x89\x54\x24\x10\x55"; |
| 83 | +#else |
| 84 | + const byte sig[] = "\x55\x89\xD0\x48\x89\xE5\x41\x57\x41\x56\x41\x55"; |
| 85 | +#endif |
| 86 | + g_pLogDirect = (LogDirect_t)serverModule->FindSignature((byte*)sig, sizeof(sig) - 1, err); |
| 87 | + |
| 88 | + if (err) |
| 89 | + { |
| 90 | + META_CONPRINTF("[CleanerCS2] Failed to find signature: %i\n", err); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + auto g_pHook = funchook_create(); |
| 95 | + funchook_prepare(g_pHook, (void**)&g_pLogDirect, (void*)Detour_LogDirect); |
| 96 | + funchook_install(g_pHook, 0); |
| 97 | + |
| 98 | + return true; |
| 99 | +} |
| 100 | + |
| 101 | +void LoadConfig() |
| 102 | +{ |
| 103 | + for(auto& regex : g_RegexList) |
| 104 | + delete regex; |
| 105 | + |
| 106 | + g_RegexList.clear(); |
| 107 | + |
| 108 | + CBufferStringGrowable<MAX_PATH> gameDir; |
| 109 | + engine->GetGameDir(gameDir); |
| 110 | + |
| 111 | + std::filesystem::path cfgPath = gameDir.Get(); |
| 112 | + cfgPath /= "addons/cleanercs2/config.cfg"; |
| 113 | + |
| 114 | + if (!std::filesystem::exists(cfgPath)) |
| 115 | + { |
| 116 | + std::ofstream cfgFile(cfgPath); |
| 117 | + cfgFile.close(); |
| 118 | + } |
| 119 | + |
| 120 | + std::ifstream cfgFile(cfgPath); |
| 121 | + |
| 122 | + if (cfgFile.is_open()) |
| 123 | + { |
| 124 | + std::string line; |
| 125 | + while (std::getline(cfgFile, line)) |
| 126 | + { |
| 127 | + if (line[0] == '/' && line[1] == '/') |
| 128 | + continue; |
| 129 | + |
| 130 | + // allow CRLF on linux |
| 131 | + line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); |
| 132 | + |
| 133 | + META_CONPRINTF("Registering regex: %s\n", line.c_str()); |
| 134 | + |
| 135 | + RE2::Options options; |
| 136 | + options.set_dot_nl(true); |
| 137 | + |
| 138 | + RE2* re = new RE2(line, options); |
| 139 | + |
| 140 | + if (re->ok()) |
| 141 | + g_RegexList.push_back(re); |
| 142 | + else |
| 143 | + META_CONPRINTF("[CleanerCS2] Failed to parse regex: '%s': %s\n", line.c_str(), re->error().c_str()); |
| 144 | + } |
| 145 | + cfgFile.close(); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + META_CONPRINTF("[CleanerCS2] Failed to open config file\n"); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +CON_COMMAND_F(conclear_reload, "Reloads the cleaner config", FCVAR_SPONLY | FCVAR_LINKED_CONCOMMAND) |
| 154 | +{ |
| 155 | + LoadConfig(); |
| 156 | +} |
| 157 | + |
| 158 | +PLUGIN_EXPOSE(CleanerPlugin, g_CleanerPlugin); |
| 159 | +bool CleanerPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late) |
| 160 | +{ |
| 161 | + PLUGIN_SAVEVARS(); |
| 162 | + |
| 163 | + GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER); |
| 164 | + GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION); |
| 165 | + GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL); |
| 166 | + GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS); |
| 167 | + GET_V_IFACE_ANY(GetEngineFactory, g_pNetworkServerService, INetworkServerService, NETWORKSERVERSERVICE_INTERFACE_VERSION); |
| 168 | + |
| 169 | + g_SMAPI->AddListener( this, this ); |
| 170 | + |
| 171 | + g_pCVar = icvar; |
| 172 | + ConVar_Register( FCVAR_RELEASE | FCVAR_CLIENT_CAN_EXECUTE | FCVAR_GAMEDLL ); |
| 173 | + |
| 174 | + LoadConfig(); |
| 175 | + |
| 176 | + if (!SetupHook()) |
| 177 | + { |
| 178 | + META_CONPRINTF("[CleanerCS2] Failed to setup hook\n"); |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + return true; |
| 183 | +} |
| 184 | + |
| 185 | +bool CleanerPlugin::Unload(char *error, size_t maxlen) |
| 186 | +{ |
| 187 | + if (g_pHook) |
| 188 | + { |
| 189 | + funchook_uninstall(g_pHook, 0); |
| 190 | + funchook_destroy(g_pHook); |
| 191 | + g_pHook = nullptr; |
| 192 | + } |
| 193 | + |
| 194 | + return true; |
| 195 | +} |
| 196 | + |
| 197 | +void CleanerPlugin::AllPluginsLoaded() |
| 198 | +{ |
| 199 | +} |
| 200 | + |
| 201 | +void CleanerPlugin::OnLevelInit( char const *pMapName, |
| 202 | + char const *pMapEntities, |
| 203 | + char const *pOldLevel, |
| 204 | + char const *pLandmarkName, |
| 205 | + bool loadGame, |
| 206 | + bool background ) |
| 207 | +{ |
| 208 | + META_CONPRINTF("OnLevelInit(%s)\n", pMapName); |
| 209 | +} |
| 210 | + |
| 211 | +void CleanerPlugin::OnLevelShutdown() |
| 212 | +{ |
| 213 | + META_CONPRINTF("OnLevelShutdown()\n"); |
| 214 | +} |
| 215 | + |
| 216 | +bool CleanerPlugin::Pause(char *error, size_t maxlen) |
| 217 | +{ |
| 218 | + return true; |
| 219 | +} |
| 220 | + |
| 221 | +bool CleanerPlugin::Unpause(char *error, size_t maxlen) |
| 222 | +{ |
| 223 | + return true; |
| 224 | +} |
| 225 | + |
| 226 | +const char *CleanerPlugin::GetLicense() |
| 227 | +{ |
| 228 | + return "GPLv3"; |
| 229 | +} |
| 230 | + |
| 231 | +const char *CleanerPlugin::GetVersion() |
| 232 | +{ |
| 233 | + return "1.0.0"; |
| 234 | +} |
| 235 | + |
| 236 | +const char *CleanerPlugin::GetDate() |
| 237 | +{ |
| 238 | + return __DATE__; |
| 239 | +} |
| 240 | + |
| 241 | +const char *CleanerPlugin::GetLogTag() |
| 242 | +{ |
| 243 | + return "SAMPLE"; |
| 244 | +} |
| 245 | + |
| 246 | +const char *CleanerPlugin::GetAuthor() |
| 247 | +{ |
| 248 | + return "AlliedModders LLC"; |
| 249 | +} |
| 250 | + |
| 251 | +const char *CleanerPlugin::GetDescription() |
| 252 | +{ |
| 253 | + return "Sample basic plugin"; |
| 254 | +} |
| 255 | + |
| 256 | +const char *CleanerPlugin::GetName() |
| 257 | +{ |
| 258 | + return "CleanerCS2"; |
| 259 | +} |
| 260 | + |
| 261 | +const char *CleanerPlugin::GetURL() |
| 262 | +{ |
| 263 | + return "https://poggu.me"; |
| 264 | +} |
0 commit comments