From 2d48b144eeded23e218320f4db831bcce6f8ec34 Mon Sep 17 00:00:00 2001 From: OEOTYAN Date: Sat, 4 Oct 2025 13:44:07 +0800 Subject: [PATCH] feat: Add command permission level configuration and optimize backup command logic --- assets/config.ini | 2 + src/OldBackupHelper/Backup.cpp | 47 +++++++++---------- src/OldBackupHelper/BackupCommand.cpp | 65 +++++++++++++++------------ src/OldBackupHelper/Entry.cpp | 6 +-- src/OldBackupHelper/Tools.h | 22 +++------ 5 files changed, 68 insertions(+), 74 deletions(-) diff --git a/assets/config.ini b/assets/config.ini index 7981d8b..878e93f 100644 --- a/assets/config.ini +++ b/assets/config.ini @@ -11,3 +11,5 @@ Compress=0 ; 等待压缩的最长时间,单位:秒,如果为0则无限等待 MaxWaitForZip=1800 + +CommandPermissionLevel=1 \ No newline at end of file diff --git a/src/OldBackupHelper/Backup.cpp b/src/OldBackupHelper/Backup.cpp index 5473a60..d79e076 100644 --- a/src/OldBackupHelper/Backup.cpp +++ b/src/OldBackupHelper/Backup.cpp @@ -184,21 +184,18 @@ bool ZipFiles(const std::string& worldName) { int level = backup_helper::getConfig().GetLongValue("Main", "Compress", 0); // Prepare command line - char tmpParas[_MAX_PATH * 4] = {0}; - sprintf( - tmpParas, - "a \"%s\\%s_%s.7z\" \"%ls/%s\" -sdel -mx%d -mmt", - backupPath.c_str(), - worldName.c_str(), - timeStr, - (TEMP_DIR).c_str(), - worldName.c_str(), - level + auto paras = ll::string_utils::str2wstr( + fmt::format( + "a \"{}\\{}_{}.7z\" \"{}/{}\" -sdel -mx{} -mmt", + backupPath, + worldName, + timeStr, + ll::string_utils::u8str2str((TEMP_DIR).u8string()), + worldName, + level + ) ); - wchar_t paras[_MAX_PATH * 4] = {0}; - ll::string_utils::str2wstr(tmpParas).copy(paras, strlen(tmpParas), 0); - DWORD maxWait = backup_helper::getConfig().GetLongValue("Main", "MaxWaitForZip", 0); if (maxWait <= 0) maxWait = 0xFFFFFFFF; else maxWait *= 1000; @@ -211,7 +208,7 @@ bool ZipFiles(const std::string& worldName) { sh.lpVerb = L"open"; sh.nShow = SW_HIDE; sh.lpFile = zipPath.c_str(); - sh.lpParameters = paras; + sh.lpParameters = paras.c_str(); if (!ShellExecuteEx(&sh)) { SendFeedback(playerUuid, "Fail to create Zip process!"_tr()); FailEnd(GetLastError()); @@ -246,14 +243,10 @@ bool UnzipFiles(const std::string& fileName) { // Get Name std::string backupPath = backup_helper::getConfig().GetValue("Main", "BackupPath", "backup"); - int level = backup_helper::getConfig().GetLongValue("Main", "Compress", 0); - // Prepare command line - char tmpParas[_MAX_PATH * 4] = {0}; - sprintf(tmpParas, "x \"%s\\%s\" -o%ls", backupPath.c_str(), fileName.c_str(), (TEMP1_DIR).c_str()); - - wchar_t paras[_MAX_PATH * 4] = {0}; - ll::string_utils::str2wstr(tmpParas).copy(paras, strlen(tmpParas), 0); + auto paras = ll::string_utils::str2wstr( + fmt::format("x \"{}\\{}\" -o{}", backupPath, fileName, ll::string_utils::u8str2str((TEMP_DIR).u8string())) + ); std::filesystem::remove_all(TEMP1_DIR); DWORD maxWait = backup_helper::getConfig().GetLongValue("Main", "MaxWaitForZip", 0); @@ -268,7 +261,7 @@ bool UnzipFiles(const std::string& fileName) { sh.lpVerb = L"open"; sh.nShow = SW_HIDE; sh.lpFile = zipPath.c_str(); - sh.lpParameters = paras; + sh.lpParameters = paras.c_str(); if (!ShellExecuteEx(&sh)) { SendFeedback(playerUuid, "Fail to Unzip process!"_tr()); // FailEnd(GetLastError()); @@ -305,17 +298,17 @@ std::vector getAllBackup() { std::string backupPath = backup_helper::getConfig().GetValue("Main", "BackupPath", "backup"); std::filesystem::directory_entry entry(backupPath); std::regex isBackFile(".*7z"); - std::vector backupList; + std::vector result; if (entry.status().type() == std::filesystem::file_type::directory) { for (const auto& iter : std::filesystem::directory_iterator(backupPath)) { std::string str = iter.path().filename().string(); if (std::regex_match(str, isBackFile)) { - backupList.push_back(str); + result.push_back(str); } } } - std::reverse(backupList.begin(), backupList.end()); - return backupList; + std::reverse(result.begin(), result.end()); + return result; } bool CopyRecoverFile(const std::string& worldName) { @@ -428,7 +421,7 @@ void ResumeBackup() { HashedString("save resume"), origin, (CurrentCmdVersion)CommandVersion::CurrentVersion(), - [](std::string const& err) {} + [](std::string const&) {} ); CommandOutput output(CommandOutputType::AllOutput); std::string outputStr; diff --git a/src/OldBackupHelper/BackupCommand.cpp b/src/OldBackupHelper/BackupCommand.cpp index 185d4ec..03f0ba7 100644 --- a/src/OldBackupHelper/BackupCommand.cpp +++ b/src/OldBackupHelper/BackupCommand.cpp @@ -6,8 +6,8 @@ #include "ll/api/command/CommandHandle.h" #include "ll/api/command/CommandRegistrar.h" #include "ll/api/coro/CoroTask.h" -#include "ll/api/thread/ServerThreadExecutor.h" #include "ll/api/i18n/I18n.h" +#include "ll/api/thread/ServerThreadExecutor.h" #include "mc/platform/UUID.h" #include "mc/server/commands/CommandOutput.h" #include "mc/server/commands/CommandPermissionLevel.h" @@ -78,7 +78,7 @@ void CmdListBackup(mce::UUID uuid, int limit) { SendFeedback(uuid, "No Backup Files"_tr()); return; } - int totalSize = backupList.size(); + int totalSize = (int)backupList.size(); int maxNum = totalSize < limit ? totalSize : limit; SendFeedback(uuid, "Select the rollback file using the number before the archive file"_tr()); for (int i = 0; i < maxNum; i++) { @@ -113,36 +113,45 @@ struct BackupRecoverCommand { }; void RegisterCommand() { + CommandPermissionLevel requirement = (CommandPermissionLevel)backup_helper::getConfig().GetLongValue( + "Main", + "CommandPermissionLevel", + (long)CommandPermissionLevel::GameDirectors + ); + using ll::command::CommandRegistrar; - auto& command = ll::command::CommandRegistrar::getInstance() - .getOrCreateCommand("backup", "Create a backup"_tr(), CommandPermissionLevel::GameDirectors); + auto& command = + ll::command::CommandRegistrar::getInstance().getOrCreateCommand("backup", "Create a backup"_tr(), requirement); command.overload() .optional("backupOperation") - .execute([&](CommandOrigin const& origin, - CommandOutput& output, - BackupMainCommand const& param, - Command const&) { - switch (param.backupOperation) { - case BackupOperation::reload: - CmdReloadConfig( - origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY() - ); - break; - case BackupOperation::cancel: - CmdCancel(origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY()); - break; - case BackupOperation::list: - CmdListBackup( - origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY(), - 100 - ); - break; - default: - CmdBackup(origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY()); - break; + .execute( + [&](CommandOrigin const& origin, CommandOutput& output, BackupMainCommand const& param, Command const&) { + switch (param.backupOperation) { + case BackupOperation::reload: + CmdReloadConfig( + origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY() + ); + break; + case BackupOperation::cancel: + CmdCancel( + origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY() + ); + break; + case BackupOperation::list: + CmdListBackup( + origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY(), + 100 + ); + break; + default: + CmdBackup( + origin.getEntity() ? static_cast(origin.getEntity())->getUuid() : mce::UUID::EMPTY() + ); + break; + } + ++output.mSuccessCount; } - ++output.mSuccessCount; - }); + ); command.overload() .text("recover") .required("recoverNumber") diff --git a/src/OldBackupHelper/Entry.cpp b/src/OldBackupHelper/Entry.cpp index 5c14fc7..f60b6f8 100644 --- a/src/OldBackupHelper/Entry.cpp +++ b/src/OldBackupHelper/Entry.cpp @@ -25,10 +25,10 @@ CSimpleIniA ini; std::filesystem::path getConfigPath() { return BackupHelper::getInstance().getSelf().getModDir() / "config.ini"; } CSimpleIniA& getConfig() { return ini; } -bool Raw_IniOpen(const magic_enum::string& path, const std::string& defContent) { +bool Raw_IniOpen(std::filesystem::path const& path, const std::string& defContent) { if (!std::filesystem::exists(path)) { // 创建新的 - std::filesystem::create_directories(std::filesystem::path(path).remove_filename().u8string()); + std::filesystem::create_directories(std::filesystem::path{path}.remove_filename().u8string()); std::ofstream iniFile(path); if (iniFile.is_open() && defContent != "") iniFile << defContent; @@ -53,7 +53,7 @@ BackupHelper& BackupHelper::getInstance() { } bool BackupHelper::load() { - Raw_IniOpen(getConfigPath().string(), ""); + Raw_IniOpen(getConfigPath(), ""); auto& instance = ll::i18n::getInstance(); auto result = instance.load(getSelf().getLangDir()); if (!result) { diff --git a/src/OldBackupHelper/Tools.h b/src/OldBackupHelper/Tools.h index ea1fcf8..ee80ebd 100644 --- a/src/OldBackupHelper/Tools.h +++ b/src/OldBackupHelper/Tools.h @@ -12,32 +12,22 @@ template inline void SendFeedback(mce::UUID uuid, const std::string& msg) { - bool found = false; - auto level = ll::service::getLevel(); - Player* player; + auto level = ll::service::getLevel(); + Player* player = nullptr; if (level.has_value() && uuid != mce::UUID::EMPTY()) { - if ((player = level->getPlayer(uuid))) { - found = true; - } + player = level->getPlayer(uuid); } - if (!found) { + if (!player) { extern mce::UUID playerUuid; playerUuid = uuid; } - if (!found || uuid != mce::UUID::EMPTY()) { + if (!player) { backup_helper::BackupHelper::getInstance().getSelf().getLogger().info(msg); - } else { - try { - // p->sendTextPacket("§e[BackupHelper]§r " + msg, TextType::RAW); + } else try { player->sendMessage("§e[BackupHelper]§r " + msg); - } catch (const std::exception&) { - extern mce::UUID playerUuid; - playerUuid = mce::UUID::EMPTY(); - backup_helper::BackupHelper::getInstance().getSelf().getLogger().info(msg); } catch (...) { extern mce::UUID playerUuid; playerUuid = mce::UUID::EMPTY(); backup_helper::BackupHelper::getInstance().getSelf().getLogger().info(msg); } - } }