Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Compress=0

; 等待压缩的最长时间,单位:秒,如果为0则无限等待
MaxWaitForZip=1800

CommandPermissionLevel=1
47 changes: 20 additions & 27 deletions src/OldBackupHelper/Backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down Expand Up @@ -305,17 +298,17 @@ std::vector<std::string> getAllBackup() {
std::string backupPath = backup_helper::getConfig().GetValue("Main", "BackupPath", "backup");
std::filesystem::directory_entry entry(backupPath);
std::regex isBackFile(".*7z");
std::vector<std::string> backupList;
std::vector<std::string> 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) {
Expand Down Expand Up @@ -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;
Expand Down
65 changes: 37 additions & 28 deletions src/OldBackupHelper/BackupCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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<BackupMainCommand>()
.optional("backupOperation")
.execute([&](CommandOrigin const& origin,
CommandOutput& output,
BackupMainCommand const& param,
Command const&) {
switch (param.backupOperation) {
case BackupOperation::reload:
CmdReloadConfig(
origin.getEntity() ? static_cast<Player*>(origin.getEntity())->getUuid() : mce::UUID::EMPTY()
);
break;
case BackupOperation::cancel:
CmdCancel(origin.getEntity() ? static_cast<Player*>(origin.getEntity())->getUuid() : mce::UUID::EMPTY());
break;
case BackupOperation::list:
CmdListBackup(
origin.getEntity() ? static_cast<Player*>(origin.getEntity())->getUuid() : mce::UUID::EMPTY(),
100
);
break;
default:
CmdBackup(origin.getEntity() ? static_cast<Player*>(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<Player*>(origin.getEntity())->getUuid() : mce::UUID::EMPTY()
);
break;
case BackupOperation::cancel:
CmdCancel(
origin.getEntity() ? static_cast<Player*>(origin.getEntity())->getUuid() : mce::UUID::EMPTY()
);
break;
case BackupOperation::list:
CmdListBackup(
origin.getEntity() ? static_cast<Player*>(origin.getEntity())->getUuid() : mce::UUID::EMPTY(),
100
);
break;
default:
CmdBackup(
origin.getEntity() ? static_cast<Player*>(origin.getEntity())->getUuid() : mce::UUID::EMPTY()
);
break;
}
++output.mSuccessCount;
}
++output.mSuccessCount;
});
);
command.overload<BackupRecoverCommand>()
.text("recover")
.required("recoverNumber")
Expand Down
6 changes: 3 additions & 3 deletions src/OldBackupHelper/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
22 changes: 6 additions & 16 deletions src/OldBackupHelper/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,22 @@

template <typename... Args>
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);
}
}
}