feat: clean entities

This commit is contained in:
Tsubasa6848
2024-01-05 20:18:40 +08:00
Unverified
parent 47579ff019
commit 57f70fac0c
8 changed files with 110 additions and 211 deletions
+2 -1
View File
@@ -4,4 +4,5 @@
/bin /bin
/build /build
/compile_commands.json /compile_commands.json
/CMakeLists.txt /CMakeLists.txt
/.vscode
-99
View File
@@ -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"
}
}
-82
View File
@@ -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
$<$<COMPILE_LANGUAGE:C>:/EHa>
$<$<COMPILE_LANGUAGE:CXX>:/EHa>
$<$<COMPILE_LANGUAGE:C>:/utf-8>
$<$<COMPILE_LANGUAGE:CXX>:/utf-8>
$<$<COMPILE_LANGUAGE:C>:-DNDEBUG>
$<$<COMPILE_LANGUAGE:CXX>:-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 $<$<CONFIG:Release>:-Ox -fp:fast>)
else()
target_compile_options(Cleaner PRIVATE -O3)
endif()
if(MSVC)
set_property(TARGET Cleaner PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>: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
)
+40 -24
View File
@@ -3,17 +3,21 @@
using namespace ll::schedule; using namespace ll::schedule;
using namespace ll::chrono_literals; using namespace ll::chrono_literals;
GameTimeScheduler scheduler; GameTimeAsyncScheduler scheduler;
bool auto_clean_triggerred = false;
namespace Cleaner { namespace Cleaner {
bool isDeathDrop(ItemActor* itac) { bool isDeathDrop(ItemActor* itac) {
// Todo //
return false; return false;
} }
bool shouldIgnoreMob(Mob* mob) { bool shouldIgnoreMob(Actor* ac) {
// Todo if (ac->isTame() || ac->isTrusting() || ac->getNameTag() != "") {
return true;
}
return false; return false;
} }
@@ -22,10 +26,6 @@ bool ShouldClean(Actor* en) {
if (en->isType(ActorType::Player)) { if (en->isType(ActorType::Player)) {
return false; return false;
} }
// Tag
if (en->hasTag("testtag")) {
// Todo
}
auto type = en->getTypeName(); auto type = en->getTypeName();
// Items // Items
if (en->hasCategory(ActorCategory::Item)) { if (en->hasCategory(ActorCategory::Item)) {
@@ -36,7 +36,6 @@ bool ShouldClean(Actor* en) {
} }
auto itemType = itac->item().getTypeName(); auto itemType = itac->item().getTypeName();
if (true) { // Todo Config WhiteList if (true) { // Todo Config WhiteList
return true;
return false; return false;
} }
return true; return true;
@@ -46,14 +45,15 @@ bool ShouldClean(Actor* en) {
// Mobs // Mobs
else if (en->hasCategory(ActorCategory::Mob)) { else if (en->hasCategory(ActorCategory::Mob)) {
if (true) { // Todo Config Toggle if (true) { // Todo Config Toggle
auto mob = (Mob*)en; if (shouldIgnoreMob(en)) {
if (shouldIgnoreMob(mob)) {
return false; return false;
} }
// Test
return true;
if (true) { // Todo WhiteList if (true) { // Todo WhiteList
return true;
return false; return false;
} }
return true;
} }
return false; return false;
} }
@@ -63,6 +63,7 @@ bool ShouldClean(Actor* en) {
if (true) { // BlackList if (true) { // BlackList
return true; return true;
} }
return false;
} }
return false; return false;
} }
@@ -91,24 +92,39 @@ int CountEntities() {
return clean_count; return clean_count;
} }
using namespace ll::schedule; void CleanTask(int time, int announce_time) {
auto time_1 = std::chrono::seconds::duration(time);
using namespace ll::chrono_literals; auto time_2 = std::chrono::seconds::duration(time - announce_time);
logger.info("clean will in {}", time_1);
void CleanTask() { scheduler.add<DelayTask>(time_2, [announce_time] { logger.info("clean will in {}", announce_time); });
// Todo Schedule scheduler.add<DelayTask>(time_1, [] {
logger.info("clean will in {}", 20s);
scheduler.add<DelayTask>(20s, [] {
auto count = ExecuteClean(); auto count = ExecuteClean();
logger.info("Successfully cleaned {} entities", count); logger.info("Successfully cleaned {} entities", count);
auto_clean_triggerred = false;
}); });
} }
void AutoCleanTask() { void AutoCleanTask(int seconds) {
auto_clean_task->cancel(); auto time = std::chrono::seconds::duration(seconds);
auto_clean_task = scheduler.add<RepeatTask>(1min, [] { auto_clean_task = scheduler.add<RepeatTask>(time, [] { CleanTask(20, 5); });
CleanTask(); }
void CheckCleanTask(int max_entities, float min_tps) {
check_clean_task = scheduler.add<RepeatTask>(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 } // namespace Cleaner
+10 -2
View File
@@ -6,8 +6,16 @@ extern ll::Logger logger;
extern void RegisterCommands(); extern void RegisterCommands();
extern void UnregisterCommands(); extern void UnregisterCommands();
static std::shared_ptr<ll::schedule::task::Task<ll::chrono::GameTimeClock>> auto_clean_task; static std::shared_ptr<ll::schedule::task::Task<ll::chrono::GameTimeClock>> auto_clean_task;
static std::shared_ptr<ll::schedule::task::Task<ll::chrono::GameTimeClock>> check_clean_task;
namespace Cleaner { namespace Cleaner {
extern void CleanTask(); extern void CleanTask(int time, int announce_time);
extern void AutoCleanTask(); 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();
} }
+54 -1
View File
@@ -1 +1,54 @@
#include "Global.h" #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<std::chrono::microseconds>(elapsed).count();
uint ticks = 0;
float average_tps = 0;
double mspt = 0;
bool culculate_mspt = false;
std::list<ushort> 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
+3 -1
View File
@@ -10,7 +10,8 @@ Plugin::Plugin(ll::plugin::NativePlugin& self) : mSelf(self) {
bool Plugin::enable() { bool Plugin::enable() {
RegisterCommands(); RegisterCommands();
Cleaner::AutoCleanTask(); Cleaner::AutoCleanTask(60);
Cleaner::CheckCleanTask(20, 15);
logger.info("Cleaner Loaded!"); logger.info("Cleaner Loaded!");
logger.info("Author: Tsubasa6848"); logger.info("Author: Tsubasa6848");
logger.info("Repository: https://github.com/GroupMountain/Cleaner"); logger.info("Repository: https://github.com/GroupMountain/Cleaner");
@@ -21,6 +22,7 @@ bool Plugin::disable() {
logger.info("Disabling Cleaner..."); logger.info("Disabling Cleaner...");
// Code for disabling the plugin goes here. // Code for disabling the plugin goes here.
auto_clean_task->cancel(); auto_clean_task->cancel();
check_clean_task->cancel();
UnregisterCommands(); UnregisterCommands();
logger.info("Cleaner Disabled!"); logger.info("Cleaner Disabled!");
return true; return true;
+1 -1
View File
@@ -21,7 +21,7 @@ void RegCleanCommand(CommandRegistry& registry) {
CommandOrigin const& origin, CommandOrigin const& origin,
CommandOutput& output, CommandOutput& output,
std::unordered_map<std::string, DynamicCommand::Result>& result) { std::unordered_map<std::string, DynamicCommand::Result>& result) {
Cleaner::CleanTask(); Cleaner::CleanTask(15, 7);
}); });
DynamicCommand::setup(registry, std::move(command)); DynamicCommand::setup(registry, std::move(command));
} }