Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
refactor: 重构 Motd 文件读写
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Oct 19, 2024
1 parent fdf1866 commit 1ab6661
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
65 changes: 20 additions & 45 deletions src/Motd/Motd.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Motd.h"
#include "Entry/Entry.h"
#include "Utils/Utils.h"
#include "ll/api/chrono/GameChrono.h"
#include "ll/api/schedule/Scheduler.h"
#include "ll/api/schedule/Task.h"
Expand All @@ -25,60 +26,34 @@ std::vector<string> motd_list;
void loadMotd() {
auto& mSelf = tls::entry::getInstance().getSelf();
auto& logger = mSelf.getLogger();
try {
fs::path path = mSelf.getDataDir() / "motd.json";
if (!fs::exists(path)) {
logger.warn("motd file not found!");
// check dir
if (!fs::exists(mSelf.getDataDir())) {
fs::create_directories(mSelf.getDataDir());
}
// create empty file, value []
std::ofstream ofs(path);
ofs << json::array().dump();
ofs.close();
return;
}
std::ifstream ifs(path);
json j = json::parse(ifs);
ifs.close();
motd_list.clear();
for (auto& s : j) {
motd_list.push_back(s);
}
} catch (...) {
logger.error("load motd failed!");

fs::path path = mSelf.getDataDir() / "motd.json";
auto val = Utils::readJsonFromFile(path);
if (!val.has_value()) {
logger.error("Failed to read motd.json!");
return;
}

motd_list.clear();
for (auto& s : val.value()) {
motd_list.push_back(s);
}
}

void saveMotd() {
auto& mSelf = tls::entry::getInstance().getSelf();
auto& logger = mSelf.getLogger();
try {
fs::path path = mSelf.getDataDir() / "motd.json";

if (!fs::exists(path)) {
fs::create_directories(mSelf.getDataDir());
}
fs::path path = mSelf.getDataDir() / "motd.json";

std::ofstream ofs(path);
if (!ofs) {
logger.error("Failed to open motd.json for writing!");
return;
}

json j = json::array();
for (auto& s : motd_list) {
j.push_back(s);
}
ofs << j.dump();
json data = json::array();
for (auto& i : motd_list) {
data.push_back(i);
}

if (!ofs.good()) {
logger.error("Failed to write to motd.json!");
}
ofs.close();
} catch (...) {
logger.error("save motd failed!");
bool ok = Utils::writeJsonToFile(path, data);
if (!ok) {
logger.error("Failed to write to motd.json!");
}
}

Expand Down
43 changes: 43 additions & 0 deletions src/Utils/Utils.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
#pragma once
#include "Entry/PluginInfo.h"
#include "mc/world/actor/player/Player.h"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <numeric>
#include <optional>
#include <string>
#include <type_traits>
#include <vector>


using string = std::string;
namespace fs = std::filesystem;
using json = nlohmann::json;

namespace tls {


class Utils {
public:
Utils() = delete;
Expand Down Expand Up @@ -51,6 +58,42 @@ class Utils {
static void sendMsg(Player& player, std::string const& msg) {
player.sendMessage("§6[§a" + string(PLUGIN_NAME) + "§6]§b " + msg);
}


static bool writeJsonToFile(fs::path const& file_path, json const& data) {
if (!fs::exists(file_path)) {
fs::create_directories(file_path.parent_path());
}

std::ofstream ofs(file_path);
if (!ofs.is_open()) {
return false;
}

ofs << data.dump(4);
ofs.close();
return true;
}
static std::optional<json> readJsonFromFile(fs::path const& file_path) {
if (!fs::exists(file_path)) {
return std::nullopt;
}

std::ifstream ifs(file_path);
if (!ifs.is_open()) {
return std::nullopt;
}

try {
json data = json::parse(ifs);
ifs.close();
return data;
} catch (json::parse_error const& e) {
return std::nullopt;
} catch (...) {
return std::nullopt;
}
}
};


Expand Down

0 comments on commit 1ab6661

Please sign in to comment.