From 65159135756d228ced49f5bbe194582ee322c363 Mon Sep 17 00:00:00 2001 From: Vincent Date: Wed, 27 Nov 2024 17:39:14 +0000 Subject: [PATCH] feat: add save and load functionality to player ban list --- conanfile.py | 3 + include/endstone/ban/ban_entry.h | 2 +- include/endstone/detail/ban/player_ban_list.h | 5 +- src/endstone_core/CMakeLists.txt | 19 +++- src/endstone_core/ban/player_ban_list.cpp | 105 +++++++++++++++++- src/endstone_core/player.cpp | 1 - 6 files changed, 127 insertions(+), 8 deletions(-) diff --git a/conanfile.py b/conanfile.py index 15f0da944..78d8fee64 100644 --- a/conanfile.py +++ b/conanfile.py @@ -34,6 +34,7 @@ class EndstoneRecipe(ConanFile): "capstone/*:tms320c64x": False, "capstone/*:m680x": False, "capstone/*:evm": False, + "date/*:header_only": True, } exports_sources = "CMakeLists.txt", "src/*", "include/*", "tests/*" @@ -112,6 +113,7 @@ def requirements(self): self.requires("boost/1.85.0") self.requires("concurrentqueue/1.0.4") self.requires("cpptrace/0.7.1") + self.requires("date/3.0.3") self.requires("entt/3.14.0") self.requires("expected-lite/0.8.0") self.requires("fmt/[~10]", transitive_headers=True, transitive_libs=True) @@ -177,6 +179,7 @@ def package_info(self): "boost::boost", "concurrentqueue::concurrentqueue", "cpptrace::cpptrace", + "date::date", "entt::entt", "glm::glm", "magic_enum::magic_enum", diff --git a/include/endstone/ban/ban_entry.h b/include/endstone/ban/ban_entry.h index 124446fac..933c0e139 100644 --- a/include/endstone/ban/ban_entry.h +++ b/include/endstone/ban/ban_entry.h @@ -54,7 +54,7 @@ class BanEntry { * * @return the source of the ban */ - [[nodiscard]] std::string getSource() + [[nodiscard]] std::string getSource() const { return source_; } diff --git a/include/endstone/detail/ban/player_ban_list.h b/include/endstone/detail/ban/player_ban_list.h index 2d751a0ab..c0b227c01 100644 --- a/include/endstone/detail/ban/player_ban_list.h +++ b/include/endstone/detail/ban/player_ban_list.h @@ -50,12 +50,13 @@ class EndstonePlayerBanList : public PlayerBanList { void removeBan(std::string name) override; void removeBan(std::string name, std::optional uuid, std::optional xuid) override; + void save(); + void load(); + private: static bool match(const PlayerBanEntry &entry, const std::string &name, const std::optional &uuid, const std::optional &xuid); - void save(); - void load(); void removeExpired(); std::vector entries_; diff --git a/src/endstone_core/CMakeLists.txt b/src/endstone_core/CMakeLists.txt index 73ddfb5b3..bf3c7c610 100644 --- a/src/endstone_core/CMakeLists.txt +++ b/src/endstone_core/CMakeLists.txt @@ -5,6 +5,7 @@ find_package(base64 REQUIRED) find_package(Boost REQUIRED) find_package(concurrentqueue REQUIRED) find_package(cpptrace REQUIRED) +find_package(date REQUIRED) find_package(EnTT REQUIRED) find_package(glm REQUIRED) find_package(magic_enum REQUIRED) @@ -80,9 +81,21 @@ add_library(endstone_core server.cpp ) add_library(endstone::core ALIAS endstone_core) -target_link_libraries(endstone_core PUBLIC endstone::headers aklomp::base64 boost::boost concurrentqueue::concurrentqueue - cpptrace::cpptrace EnTT::EnTT glm::glm magic_enum::magic_enum Microsoft.GSL::GSL nlohmann_json::nlohmann_json - pybind11::module spdlog::spdlog tomlplusplus::tomlplusplus) +target_link_libraries(endstone_core PUBLIC + endstone::headers + aklomp::base64 + boost::boost + concurrentqueue::concurrentqueue + cpptrace::cpptrace + date::date + EnTT::EnTT + glm::glm + magic_enum::magic_enum + Microsoft.GSL::GSL + nlohmann_json::nlohmann_json + pybind11::module + spdlog::spdlog + tomlplusplus::tomlplusplus) if (UNIX) target_link_libraries(endstone_core PUBLIC ${CMAKE_DL_LIBS}) target_compile_definitions(endstone_core PUBLIC ENDSTONE_DISABLE_DEVTOOLS) diff --git a/src/endstone_core/ban/player_ban_list.cpp b/src/endstone_core/ban/player_ban_list.cpp index 45768ccda..0a3894ad7 100644 --- a/src/endstone_core/ban/player_ban_list.cpp +++ b/src/endstone_core/ban/player_ban_list.cpp @@ -14,6 +14,16 @@ #include "endstone/detail/ban/player_ban_list.h" +#include + +#include +#include +#include +#include +#include + +#include "endstone/detail/server.h" + namespace endstone::detail { const PlayerBanEntry *EndstonePlayerBanList::getBanEntry(std::string name) const @@ -70,7 +80,7 @@ PlayerBanEntry &EndstonePlayerBanList::addBan(std::string name, std::optional::value(); + std::ofstream writer(file_, std::ofstream::out | std::ofstream::trunc); + try { + writer << array; + } + catch (const std::exception &e) { + server.getLogger().error("Unable to write file '{}': {}", file_, e.what()); + } +} + +void EndstonePlayerBanList::load() +{ + if (!exists(file_)) { + return; + } + + entries_.clear(); + + auto &server = entt::locator::value(); + std::ifstream file(file_, std::ifstream::in); + try { + auto array = nlohmann::json::parse(file); + std::string name = array["name"]; + std::optional uuid; + std::optional xuid = std::nullopt; + if (array.contains("uuid")) { + try { + boost::uuids::string_generator gen; + boost::uuids::uuid u1 = gen(std::string(array["uuid"])); + UUID u2; + std::memcpy(u2.data, u1.data, u1.size()); + uuid = u2; + } + catch (std::exception &) { + uuid = std::nullopt; + } + } + if (array.contains("xuid")) { + xuid = array["xuid"]; + } + PlayerBanEntry entry{name, uuid, xuid}; + if (array.contains("created")) { + std::string created = array["created"]; + std::istringstream in{created}; + BanEntry::Date date; + in >> date::parse("%FT%TZ", date); + if (!in.fail()) { + entry.setCreated(date); + } + } + if (array.contains("source")) { + entry.setSource(array["source"]); + } + if (array.contains("expires")) { + std::string expires = array["expires"]; + std::istringstream in{expires}; + BanEntry::Date date; + in >> date::parse("%FT%TZ", date); + if (!in.fail()) { + entry.setExpiration(date); + } + } + entries_.emplace_back(entry); + } + catch (const std::exception &e) { + server.getLogger().error("Unable to read file '{}': {}", file_, e.what()); + } +} + void EndstonePlayerBanList::removeExpired() { auto it = entries_.begin(); diff --git a/src/endstone_core/player.cpp b/src/endstone_core/player.cpp index 590bffab3..2eb0e5344 100644 --- a/src/endstone_core/player.cpp +++ b/src/endstone_core/player.cpp @@ -14,7 +14,6 @@ #include "endstone/detail/player.h" -#include #include #include "bedrock/certificates/extended_certificate.h"