Skip to content

Commit

Permalink
refactor: refactor something
Browse files Browse the repository at this point in the history
feat: add config enable_commands
  • Loading branch information
ShrBox committed Feb 18, 2024
1 parent 79b2f38 commit 5f11db9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 65 deletions.
26 changes: 0 additions & 26 deletions src/DllMain.cpp

This file was deleted.

67 changes: 49 additions & 18 deletions src/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
#include "nlohmann/json_fwd.hpp"
#include <string>


ll::Logger* logger;

#define JSON1(key, val) \
if (json.find(key) != json.end()) { \
const nlohmann::json& out = json.at(key); \
Expand All @@ -43,6 +40,7 @@ nlohmann::json globaljson() {
json["def_money"] = def_money;
json["pay_tax"] = pay_tax;
json["enable_ranking"] = enable_ranking;
json["enable_commands"] = enable_commands;
json["currency_symbol"] = currency_symbol;
return json;
}
Expand All @@ -52,13 +50,14 @@ void initjson(nlohmann::json json) {
JSON1("def_money", def_money);
JSON1("pay_tax", pay_tax);
JSON1("enable_ranking", enable_ranking);
JSON1("enable_commands", enable_commands);
JSON1("currency_symbol", currency_symbol);
}
void WriteDefaultConfig(const std::string& fileName) {

std::ofstream file(fileName);
if (!file.is_open()) {
logger->error("Can't open file {}", fileName);
legacymoney::getSelfPluginInstance().getLogger().error("Can't open file {}", fileName);
return;
}
auto json = globaljson();
Expand All @@ -69,7 +68,7 @@ void WriteDefaultConfig(const std::string& fileName) {
void LoadConfigFromJson(const std::string& fileName) {
std::ifstream file(fileName);
if (!file.is_open()) {
logger->error("Can't open file {}", fileName);
legacymoney::getSelfPluginInstance().getLogger().error("Can't open file {}", fileName);
return;
}
nlohmann::json json;
Expand All @@ -83,7 +82,7 @@ void reloadJson(const std::string& fileName) {
if (file) {
file << globaljson().dump(4);
} else {
logger->error("Configuration File Creation failed!");
legacymoney::getSelfPluginInstance().getLogger().error("Configuration File Creation failed!");
}
file.close();
}
Expand All @@ -102,9 +101,12 @@ void loadCfg() {
try {
Settings::LoadConfigFromJson("plugins/LegacyMoney/money.json");
} catch (std::exception& e) {
logger->error("Configuration file is Invalid, Error: {}", e.what());
legacymoney::getSelfPluginInstance().getLogger().error(
"Configuration file is Invalid, Error: {}",
e.what()
);
} catch (...) {
logger->error("Configuration file is Invalid");
legacymoney::getSelfPluginInstance().getLogger().error("Configuration file is Invalid");
}
} else {
Settings::WriteDefaultConfig("plugins/LegacyMoney/money.json");
Expand Down Expand Up @@ -468,25 +470,54 @@ void RegisterMoneyCommands() {

namespace legacymoney {

Plugin::Plugin(ll::plugin::NativePlugin& self) : mSelf(self) {
logger = &mSelf.getLogger();
mSelf.getLogger().info("Loaded!");
namespace {

std::unique_ptr<std::reference_wrapper<ll::plugin::NativePlugin>>
selfPluginInstance; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)

auto disable(ll::plugin::NativePlugin& /*self*/) -> bool { return true; }

auto enable(ll::plugin::NativePlugin& /*self*/) -> bool {
if (Settings::enable_commands) {
RegisterMoneyCommands();
}
return true;
return true;
}

auto load(ll::plugin::NativePlugin& self) -> bool {
auto& logger = self.getLogger();
selfPluginInstance = std::make_unique<std::reference_wrapper<ll::plugin::NativePlugin>>(self);
logger.info("Loaded!");
loadCfg();
if (!initDB()) {
return;
return false;
}
ll::i18n::getInstance() = std::make_unique<ll::i18n::MultiFileI18N>(
ll::i18n::MultiFileI18N("plugins/LegacyMoney/lang", Settings::language)
);
return true;
}

bool Plugin::enable() {
if (Settings::enable_commands) {
RegisterMoneyCommands();
auto unload(ll::plugin::NativePlugin& self) -> bool { return true; }

} // namespace

auto getSelfPluginInstance() -> ll::plugin::NativePlugin& {
if (!selfPluginInstance) {
throw std::runtime_error("selfPluginInstance is null");
}
return true;
}

bool Plugin::disable() { return true; }
return *selfPluginInstance;
}

} // namespace legacymoney

extern "C" {
_declspec(dllexport) auto ll_plugin_disable(ll::plugin::NativePlugin& self) -> bool {
return legacymoney::disable(self);
}
_declspec(dllexport) auto ll_plugin_enable(ll::plugin::NativePlugin& self) -> bool { return legacymoney::enable(self); }
_declspec(dllexport) auto ll_plugin_load(ll::plugin::NativePlugin& self) -> bool { return legacymoney::load(self); }
_declspec(dllexport) auto ll_plugin_unload(ll::plugin::NativePlugin& self) -> bool { return legacymoney::unload(self); }
}
22 changes: 1 addition & 21 deletions src/Plugin.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
#pragma once

#include <ll/api/base/Concepts.h>
#include <ll/api/plugin/NativePlugin.h>


namespace legacymoney {

class Plugin {
public:
Plugin(ll::plugin::NativePlugin& self);

Plugin(Plugin&&) = delete;
Plugin(const Plugin&) = delete;
Plugin& operator=(Plugin&&) = delete;
Plugin& operator=(const Plugin&) = delete;

~Plugin() = default;

/// @return True if the plugin is enabled successfully.
bool enable();

/// @return True if the plugin is disabled successfully.
bool disable();

private:
ll::plugin::NativePlugin& mSelf;
};
[[nodiscard]] auto getSelfPluginInstance() -> ll::plugin::NativePlugin&;

} // namespace legacymoney

0 comments on commit 5f11db9

Please sign in to comment.