From 8f77f1c8c01094d025fa760ac4ca90beed4e1b0a Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 29 Nov 2021 01:34:35 +0100 Subject: [PATCH] Add kick, fix cmakelists pretending to be on linux all the time lol --- CMakeLists.txt | 3 ++- src/TConsole.cpp | 44 ++++++++++++++++++++++++++++++++++++++------ src/TLuaEngine.cpp | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 133b274..b51ee20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ include_directories("${PROJECT_SOURCE_DIR}/deps/sol2/include") include_directories("${PROJECT_SOURCE_DIR}/deps/cpp-httplib") include_directories("${PROJECT_SOURCE_DIR}/deps") -add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT __linux) +add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT) if(APPLE) set(LUA_INCLUDE_DIR /usr/local/Cellar/lua@5.3/5.3.6/include/lua5.3) @@ -24,6 +24,7 @@ if(APPLE) include_directories(/usr/local/opt/openssl@1.1/include) link_directories(/usr/local/Cellar/lua@5.3/5.3.6/lib) link_directories(/usr/local/opt/openssl@1.1/lib) + add_compile_definitions(__linux) endif() if (WIN32) diff --git a/src/TConsole.cpp b/src/TConsole.cpp index 3b91ea7..6ed9f58 100644 --- a/src/TConsole.cpp +++ b/src/TConsole.cpp @@ -2,6 +2,7 @@ #include "Common.h" #include "Compat.h" +#include "Client.h" #include "CustomAssert.h" #include "LuaAPI.h" #include "TLuaEngine.h" @@ -143,15 +144,44 @@ void TConsole::Command_Lua(const std::string& cmd) { void TConsole::Command_Help(const std::string&) { static constexpr const char* sHelpString = R"( Commands: - help displays this help - exit shuts down the server - lua [state id] switches to lua, optionally into a specific state id's lua + help displays this help + exit shuts down the server + kick [reason] kicks specified player with an optional reason + list lists all players and info about them + say sends the message to all players in chat + lua [state id] switches to lua, optionally into a specific state id's lua )"; Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString)); } void TConsole::Command_Kick(const std::string& cmd) { - cmd.compare() + if (cmd.size() > 4) { + auto Name = cmd.substr(5); + std::string Reason = "Kicked by server console"; + auto SpacePos = Name.find(' '); + if (SpacePos != Name.npos) { + Reason = Name.substr(SpacePos + 1); + Name = cmd.substr(5, cmd.size() - Reason.size() - 5 - 1); + } + beammp_trace("attempt to kick '" + Name + "' for '" + Reason + "'"); + bool Kicked = false; + auto NameCompare = [](std::string Name1, std::string Name2) -> bool { + std::for_each(Name1.begin(), Name1.end(), [](char& c) { c = tolower(c); }); + std::for_each(Name2.begin(), Name2.end(), [](char& c) { c = tolower(c); }); + return StringStartsWith(Name1, Name2) || StringStartsWith(Name2, Name1); + }; + mLuaEngine->Server().ForEachClient([&](std::weak_ptr Client) -> bool { + if (!Client.expired()) { + auto locked = Client.lock(); + if (NameCompare(locked->GetName(), Name)) { + mLuaEngine->Network().ClientKick(*locked, Reason); + Kicked = true; + return false; + } + } + return true; + }); + } } void TConsole::Command_Say(const std::string& cmd) { @@ -182,8 +212,10 @@ void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) { } } } - if (NonNilFutures.size() == 0 && !IgnoreNotACommand) { - Application::Console().WriteRaw("Error: Unknown command: '" + cmd + "'"); + if (NonNilFutures.size() == 0) { + if (!IgnoreNotACommand) { + Application::Console().WriteRaw("Error: Unknown command: '" + cmd + "'"); + } } else { std::stringstream Reply; if (NonNilFutures.size() > 1) { diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index a835aae..e0cbd89 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -460,7 +460,7 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi MPTable.set_function("TriggerClientEvent", &LuaAPI::MP::TriggerClientEvent); MPTable.set_function("GetPlayerCount", &LuaAPI::MP::GetPlayerCount); MPTable.set_function("IsPlayerConnected", &LuaAPI::MP::IsPlayerConnected); - MPTable.set_function("GetPlayerIDByName", [&](const std::string& Name) -> std::string { + MPTable.set_function("GetPlayerIDByName", [&](const std::string& Name) -> int { return Lua_GetPlayerIDByName(Name); }); MPTable.set_function("GetPlayerName", [&](int ID) -> std::string {