From 57f70fac0c3ce4dc8e1e7b55ba27f62078270524 Mon Sep 17 00:00:00 2001 From: Tsubasa6848 <116721335+Tsubasa6848@users.noreply.github.com> Date: Fri, 5 Jan 2024 20:18:40 +0800 Subject: [PATCH] feat: clean entities --- .gitignore | 3 +- .vscode/settings.json | 99 ------------------------------------------- CMakeLists.txt | 82 ----------------------------------- src/Cleaner.cpp | 64 +++++++++++++++++----------- src/Global.h | 12 +++++- src/Mspt.cpp | 55 +++++++++++++++++++++++- src/Plugin.cpp | 4 +- src/RegCommand.cpp | 2 +- 8 files changed, 110 insertions(+), 211 deletions(-) delete mode 100644 .vscode/settings.json delete mode 100644 CMakeLists.txt diff --git a/.gitignore b/.gitignore index 81a8b0c..6ccf134 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ /bin /build /compile_commands.json -/CMakeLists.txt \ No newline at end of file +/CMakeLists.txt +/.vscode \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 55a7196..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,99 +0,0 @@ -{ - "files.associations": { - "xtr1common": "cpp", - "vector": "cpp", - "unordered_map": "cpp", - "iostream": "cpp", - "pointers": "cpp", - "algorithm": "cpp", - "any": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "exception": "cpp", - "expected": "cpp", - "filesystem": "cpp", - "format": "cpp", - "forward_list": "cpp", - "fstream": "cpp", - "functional": "cpp", - "future": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "istream": "cpp", - "iterator": "cpp", - "limits": "cpp", - "list": "cpp", - "locale": "cpp", - "map": "cpp", - "memory": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "numeric": "cpp", - "optional": "cpp", - "ostream": "cpp", - "queue": "cpp", - "random": "cpp", - "ranges": "cpp", - "ratio": "cpp", - "regex": "cpp", - "set": "cpp", - "shared_mutex": "cpp", - "source_location": "cpp", - "span": "cpp", - "sstream": "cpp", - "stack": "cpp", - "stacktrace": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "string": "cpp", - "system_error": "cpp", - "thread": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "typeinfo": "cpp", - "unordered_set": "cpp", - "utility": "cpp", - "valarray": "cpp", - "variant": "cpp", - "xfacet": "cpp", - "xhash": "cpp", - "xiosbase": "cpp", - "xlocale": "cpp", - "xlocbuf": "cpp", - "xlocinfo": "cpp", - "xlocmes": "cpp", - "xlocmon": "cpp", - "xlocnum": "cpp", - "xloctime": "cpp", - "xmemory": "cpp", - "xstddef": "cpp", - "xstring": "cpp", - "xtree": "cpp", - "xutility": "cpp" - } -} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index ea7ed0c..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,82 +0,0 @@ -# this is the build file for project -# it is autogenerated by the xmake build system. -# do not edit by hand. - -# project -cmake_minimum_required(VERSION 3.15.0) -cmake_policy(SET CMP0091 NEW) -project(Cleaner LANGUAGES CXX C) - -# target -add_library(Cleaner SHARED "") -set_target_properties(Cleaner PROPERTIES OUTPUT_NAME "Cleaner") -set_target_properties(Cleaner PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/windows/x64/release") -set_target_properties(Cleaner PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/windows/x64/release") -target_include_directories(Cleaner PRIVATE - src -) -target_include_directories(Cleaner PRIVATE - C:/Users/yang/AppData/Local/.xmake/packages/l/levilamina/0.3.0/59a288d9257e463883bf6b863a7de723/include - C:/Users/yang/AppData/Local/.xmake/packages/c/ctre/3.8.1/6aca3e546d064397b0970026de72d808/include - C:/Users/yang/AppData/Local/.xmake/packages/e/entt/v3.12.2/5748c075ebf044f9b53ef5be15be7e68/include - C:/Users/yang/AppData/Local/.xmake/packages/f/fmt/10.1.1/adc308aa1dfc472ab92a30abb14e2d6f/include - C:/Users/yang/AppData/Local/.xmake/packages/g/gsl/v4.0.0/b1ec133f21b342ada9530623a4d0880a/include - C:/Users/yang/AppData/Local/.xmake/packages/l/leveldb/1.23/bd4e0156f5854fe28ddb7800bcdf5a2d/include - C:/Users/yang/AppData/Local/.xmake/packages/s/snappy/1.1.10/849445e2a764471789906fbaf94e9a02/include - C:/Users/yang/AppData/Local/.xmake/packages/m/magic_enum/v0.9.0/fe30b4c97c064545940a9e7c58e584e8/include - C:/Users/yang/AppData/Local/.xmake/packages/n/nlohmann_json/v3.11.2/d18ecf552d7148eeb73c26c13078fb10/include - C:/Users/yang/AppData/Local/.xmake/packages/r/rapidjson/v1.1.0/18062f0a9cf74262946aa0534019b51b/include - C:/Users/yang/AppData/Local/.xmake/packages/p/pcg_cpp/v1.0.0/ac3e59677a854a20b6edd5c0d74babf3/include - C:/Users/yang/AppData/Local/.xmake/packages/p/pfr/2.1.1/505ef9ee28ad46fcaf5064a24938026c/include - C:/Users/yang/AppData/Local/.xmake/packages/p/preloader/v1.4.0/e087d05f0e95498c9a4b89f94139b1cd/include - C:/Users/yang/AppData/Local/.xmake/packages/s/symbolprovider/v1.1.0/ca5041352577410f83705b4b2abb93f7/include -) -target_compile_options(Cleaner PRIVATE - $<$:/EHa> - $<$:/EHa> - $<$:/utf-8> - $<$:/utf-8> - $<$:-DNDEBUG> - $<$:-DNDEBUG> -) -string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") -set_target_properties(Cleaner PROPERTIES CXX_EXTENSIONS OFF) -target_compile_features(Cleaner PRIVATE cxx_std_23) -if(MSVC) - target_compile_options(Cleaner PRIVATE $<$:-Ox -fp:fast>) -else() - target_compile_options(Cleaner PRIVATE -O3) -endif() -if(MSVC) - set_property(TARGET Cleaner PROPERTY - MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") -endif() -target_link_libraries(Cleaner PRIVATE - LeviLamina - bedrock_server_api - bedrock_server_var - fmt - leveldb - snappy - PreLoader - SymbolProvider -) -target_link_directories(Cleaner PRIVATE - C:/Users/yang/AppData/Local/.xmake/packages/l/levilamina/0.3.0/59a288d9257e463883bf6b863a7de723/lib - C:/Users/yang/AppData/Local/.xmake/packages/b/bdslibrary/1.20.50.03/ed5b31d060e14f9fa04590d78581d780/lib - C:/Users/yang/AppData/Local/.xmake/packages/f/fmt/10.1.1/adc308aa1dfc472ab92a30abb14e2d6f/lib - C:/Users/yang/AppData/Local/.xmake/packages/l/leveldb/1.23/bd4e0156f5854fe28ddb7800bcdf5a2d/lib - C:/Users/yang/AppData/Local/.xmake/packages/s/snappy/1.1.10/849445e2a764471789906fbaf94e9a02/lib - C:/Users/yang/AppData/Local/.xmake/packages/p/preloader/v1.4.0/e087d05f0e95498c9a4b89f94139b1cd/lib - C:/Users/yang/AppData/Local/.xmake/packages/s/symbolprovider/v1.1.0/ca5041352577410f83705b4b2abb93f7/lib -) -target_link_options(Cleaner PRIVATE - /DELAYLOAD:bedrock_server.dll -) -target_sources(Cleaner PRIVATE - src/Cleaner.cpp - src/dllmain.cpp - src/Plugin.cpp - src/RegCommand.cpp -) - diff --git a/src/Cleaner.cpp b/src/Cleaner.cpp index 1338418..33dcd82 100644 --- a/src/Cleaner.cpp +++ b/src/Cleaner.cpp @@ -3,17 +3,21 @@ using namespace ll::schedule; using namespace ll::chrono_literals; -GameTimeScheduler scheduler; +GameTimeAsyncScheduler scheduler; +bool auto_clean_triggerred = false; namespace Cleaner { bool isDeathDrop(ItemActor* itac) { - // Todo + // return false; } -bool shouldIgnoreMob(Mob* mob) { - // Todo +bool shouldIgnoreMob(Actor* ac) { + if (ac->isTame() || ac->isTrusting() || ac->getNameTag() != "") { + return true; + } + return false; } @@ -22,10 +26,6 @@ bool ShouldClean(Actor* en) { if (en->isType(ActorType::Player)) { return false; } - // Tag - if (en->hasTag("testtag")) { - // Todo - } auto type = en->getTypeName(); // Items if (en->hasCategory(ActorCategory::Item)) { @@ -36,7 +36,6 @@ bool ShouldClean(Actor* en) { } auto itemType = itac->item().getTypeName(); if (true) { // Todo Config WhiteList - return true; return false; } return true; @@ -46,14 +45,15 @@ bool ShouldClean(Actor* en) { // Mobs else if (en->hasCategory(ActorCategory::Mob)) { if (true) { // Todo Config Toggle - auto mob = (Mob*)en; - if (shouldIgnoreMob(mob)) { + if (shouldIgnoreMob(en)) { return false; } + // Test + return true; if (true) { // Todo WhiteList - return true; return false; } + return true; } return false; } @@ -63,6 +63,7 @@ bool ShouldClean(Actor* en) { if (true) { // BlackList return true; } + return false; } return false; } @@ -91,24 +92,39 @@ int CountEntities() { return clean_count; } -using namespace ll::schedule; - -using namespace ll::chrono_literals; - -void CleanTask() { - // Todo Schedule - logger.info("clean will in {}", 20s); - scheduler.add(20s, [] { +void CleanTask(int time, int announce_time) { + auto time_1 = std::chrono::seconds::duration(time); + auto time_2 = std::chrono::seconds::duration(time - announce_time); + logger.info("clean will in {}", time_1); + scheduler.add(time_2, [announce_time] { logger.info("clean will in {}", announce_time); }); + scheduler.add(time_1, [] { auto count = ExecuteClean(); logger.info("Successfully cleaned {} entities", count); + auto_clean_triggerred = false; }); } -void AutoCleanTask() { - auto_clean_task->cancel(); - auto_clean_task = scheduler.add(1min, [] { - CleanTask(); +void AutoCleanTask(int seconds) { + auto time = std::chrono::seconds::duration(seconds); + auto_clean_task = scheduler.add(time, [] { CleanTask(20, 5); }); +} + +void CheckCleanTask(int max_entities, float min_tps) { + check_clean_task = scheduler.add(10s, [max_entities, min_tps] { + auto count = CountEntities(); + if (!auto_clean_triggerred) { + if (count >= max_entities) { + auto_clean_triggerred = true; + logger.warn("Too many entities! Auto started clean task!"); + CleanTask(20, 5); + } else if (TPS::getAverageTps() <= min_tps) { + auto_clean_triggerred = true; + logger.warn("TPS too low! Auto started clean task!"); + CleanTask(20, 5); + } + } }); } + } // namespace Cleaner \ No newline at end of file diff --git a/src/Global.h b/src/Global.h index a40666a..b00812a 100644 --- a/src/Global.h +++ b/src/Global.h @@ -6,8 +6,16 @@ extern ll::Logger logger; extern void RegisterCommands(); extern void UnregisterCommands(); static std::shared_ptr> auto_clean_task; +static std::shared_ptr> check_clean_task; namespace Cleaner { - extern void CleanTask(); - extern void AutoCleanTask(); + extern void CleanTask(int time, int announce_time); + extern void AutoCleanTask(int seconds); + extern void CheckCleanTask(int max_entities, float min_tps); +} + +namespace TPS { + extern void CaculateTPS(); + extern float getCurrentTps(); + extern float getAverageTps(); } \ No newline at end of file diff --git a/src/Mspt.cpp b/src/Mspt.cpp index 61e8f7a..a40b6f0 100644 --- a/src/Mspt.cpp +++ b/src/Mspt.cpp @@ -1 +1,54 @@ -#include "Global.h" \ No newline at end of file +#include "Global.h" + +typedef std::chrono::high_resolution_clock timer_clock; +#define TIMER_START auto start = timer_clock::now(); +#define TIMER_END \ + auto elapsed = timer_clock::now() - start; \ + long long timeReslut = std::chrono::duration_cast(elapsed).count(); + +uint ticks = 0; +float average_tps = 0; +double mspt = 0; +bool culculate_mspt = false; +std::list avrtps = {}; + +LL_AUTO_TYPED_INSTANCE_HOOK(LevelTickHook, ll::memory::HookPriority::Normal, Level, "?tick@Level@@UEAAXXZ", void) { + ticks++; + TIMER_START + origin(); + TIMER_END + culculate_mspt = true; + if (culculate_mspt) { + mspt = (double)timeReslut / 1000; + culculate_mspt = false; + } +} + +namespace TPS { + +void CaculateTPS() { + std::thread([] { + while (true) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + culculate_mspt = true; + avrtps.push_back(ticks); + ticks = 0; + if (avrtps.size() >= 60) { + avrtps.clear(); + continue; + } + uint ticks_minute = 0; + for (auto i : avrtps) { + ticks_minute = ticks_minute + i; + } + float res = (float)ticks_minute / ((float)avrtps.size()); + average_tps = res >= 20 ? 20 : res; + } + }).detach(); +} + +float getCurrentTps() { return mspt <= 50 ? 20 : (float)(1000.0 / mspt); } + +float getAverageTps() { return average_tps; } + +} // namespace TPS diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 0f7dd09..a55e571 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -10,7 +10,8 @@ Plugin::Plugin(ll::plugin::NativePlugin& self) : mSelf(self) { bool Plugin::enable() { RegisterCommands(); - Cleaner::AutoCleanTask(); + Cleaner::AutoCleanTask(60); + Cleaner::CheckCleanTask(20, 15); logger.info("Cleaner Loaded!"); logger.info("Author: Tsubasa6848"); logger.info("Repository: https://github.com/GroupMountain/Cleaner"); @@ -21,6 +22,7 @@ bool Plugin::disable() { logger.info("Disabling Cleaner..."); // Code for disabling the plugin goes here. auto_clean_task->cancel(); + check_clean_task->cancel(); UnregisterCommands(); logger.info("Cleaner Disabled!"); return true; diff --git a/src/RegCommand.cpp b/src/RegCommand.cpp index 85bfe68..b40d071 100644 --- a/src/RegCommand.cpp +++ b/src/RegCommand.cpp @@ -21,7 +21,7 @@ void RegCleanCommand(CommandRegistry& registry) { CommandOrigin const& origin, CommandOutput& output, std::unordered_map& result) { - Cleaner::CleanTask(); + Cleaner::CleanTask(15, 7); }); DynamicCommand::setup(registry, std::move(command)); }