From b8718b941f1a959a356b457651a6d7e4a78e0c5e Mon Sep 17 00:00:00 2001 From: Tsubasa6848 <116721335+Tsubasa6848@users.noreply.github.com> Date: Sun, 26 May 2024 06:40:52 +0800 Subject: [PATCH] adapt: adapt to GMLIB v0.12.6 --- SDK-GMLIB | 2 +- manifest.json | 2 +- src/Config.h | 36 ++++++++++++---------- src/DeathMessage.cpp | 8 +++-- src/Entry.cpp | 53 ++++++++++++++++++++++++++++++-- src/Entry.h | 21 ++++++++++--- src/Global.h | 13 ++------ src/Initialization.cpp | 69 ------------------------------------------ tooth.json | 6 ++-- 9 files changed, 100 insertions(+), 110 deletions(-) delete mode 100644 src/Initialization.cpp diff --git a/SDK-GMLIB b/SDK-GMLIB index 68df499..710cc6e 160000 --- a/SDK-GMLIB +++ b/SDK-GMLIB @@ -1 +1 @@ -Subproject commit 68df499fb59036e8cf8d4b743da4ec4751cd67a6 +Subproject commit 710cc6e4c3b0e85fd793fba807d8fffd6c4b607f diff --git a/manifest.json b/manifest.json index c5247fe..5462da1 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "name": "${pluginName}", "entry": "${pluginFile}", "type": "native", - "version": "0.12.0", + "version": "0.12.1", "author": "GroupMountain", "description": "Better Death Messages", "dependencies": [ diff --git a/src/Config.h b/src/Config.h index ccdbb99..b180332 100644 --- a/src/Config.h +++ b/src/Config.h @@ -1,18 +1,22 @@ #pragma once -#include "Global.h" +#include -std::string defaultConfig = R"({ - "ConsoleLog": { - "JoinMessage": true, - "LeftMessage": true, - "DeathMessage": true - }, - "FileLog": { - "Enabled": false, - "Path": "./logs/DeathLog.log" - }, - "ServerSideTranslation": { - "Enabled": false, - "Language": "en_US" - } -})"; \ No newline at end of file +struct Config { + int version = 1; + + struct consoleLog { + bool JoinMessage = true; + bool LeftMessage = true; + bool DeathMessage = true; + } ConsoleLog; + + struct fileLog { + bool Enabled = false; + std::string Path = "./logs/DeathLog.log"; + } FileLog; + + struct serverSideTranslation { + bool Enabled = false; + std::string Language = "en_US"; + } ServerSideTranslation; +}; \ No newline at end of file diff --git a/src/DeathMessage.cpp b/src/DeathMessage.cpp index 850fb93..e1083f3 100644 --- a/src/DeathMessage.cpp +++ b/src/DeathMessage.cpp @@ -1,3 +1,4 @@ +#include "Entry.h" #include "Global.h" void RegisterDamageDefinition() { @@ -101,19 +102,20 @@ void RegisterDamageDefinition() { } void ListenEvents() { + auto& config = DeathMessages::Entry::getInstance()->getConfig(); auto& eventBus = ll::event::EventBus::getInstance(); - if (ConfigData::mJoinMessage) { + if (config.ConsoleLog.JoinMessage) { eventBus.emplaceListener([](ll::event::player::PlayerConnectEvent& ev) { infoLogger.info(fmt::format(fg(fmt::color::yellow), tr("multiplayer.player.joined", {ev.self().getName()})) ); }); } - if (ConfigData::mLeftMessage) { + if (config.ConsoleLog.LeftMessage) { eventBus.emplaceListener([](ll::event::player::PlayerLeaveEvent& ev) { infoLogger.info(fmt::format(fg(fmt::color::yellow), tr("multiplayer.player.left", {ev.self().getName()}))); }); } - if (ConfigData::mDeathMessage) { + if (config.ConsoleLog.DeathMessage) { eventBus.emplaceListener( [](GMLIB::Event::EntityEvent::DeathMessageAfterEvent& ev) { auto msg = ev.getDeathMessage(); diff --git a/src/Entry.cpp b/src/Entry.cpp index aea670c..5a3ad78 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -1,7 +1,10 @@ #include "Entry.h" #include "Global.h" +#include "Language.h" ll::Logger logger(PLUGIN_NAME); +ll::Logger deathLogger("Death"); +ll::Logger infoLogger("Server"); namespace DeathMessages { @@ -11,7 +14,19 @@ std::unique_ptr& Entry::getInstance() { } bool Entry::load() { - initPlugin(); + mConfig.emplace(); + if (!ll::config::loadConfig(*mConfig, getSelf().getConfigDir() / u8"config.json")) { + ll::config::saveConfig(*mConfig, getSelf().getConfigDir() / u8"config.json"); + } + if (getConfig().ServerSideTranslation.Enabled) { + loadI18n(); + } else { + loadResourcePack(); + } + if (getConfig().FileLog.Enabled) { + auto& path = getConfig().FileLog.Path; + deathLogger.setFile(path); + } return true; } @@ -23,6 +38,40 @@ bool Entry::enable() { bool Entry::disable() { return true; } +bool Entry::unload() { + mConfig.reset(); + mI18n.reset(); + getInstance().reset(); + return true; +} + +Config& Entry::getConfig() { return mConfig.value(); } + +std::optional Entry::getI18n() { return mI18n; } + +void Entry::loadI18n() { + mI18n.emplace(getSelf().getLangDir(), getConfig().ServerSideTranslation.Language); + mI18n->updateOrCreateLanguage("en_US", en_US); + mI18n->updateOrCreateLanguage("zh_CN", zh_CN); + mI18n->loadAllLanguages(); +} + +void Entry::loadResourcePack() { + GMLIB::Mod::VanillaFix::setFixI18nEnabled(); + std::string resourcePath = "./plugins/DeathMessages/resource/"; + auto resource = GMLIB::Files::ResourceLanguage::ResourceLanguage(resourcePath, PLUGIN_NAME, 0, 8, 0); + resource.addLanguage("en_US", en_US); + resource.addLanguage("zh_CN", zh_CN); + resource.initLanguage(); +} + } // namespace DeathMessages -LL_REGISTER_PLUGIN(DeathMessages::Entry, DeathMessages::Entry::getInstance()); \ No newline at end of file +LL_REGISTER_PLUGIN(DeathMessages::Entry, DeathMessages::Entry::getInstance()); + +std::string tr(std::string const& key, std::vector const& params) { + if (auto i18n = DeathMessages::Entry::getInstance()->getI18n()) { + return i18n->get(key, params); + } + return I18nAPI::get(key, params); +} \ No newline at end of file diff --git a/src/Entry.h b/src/Entry.h index b0137d0..f0d3587 100644 --- a/src/Entry.h +++ b/src/Entry.h @@ -1,8 +1,11 @@ #pragma once -#include +#include "Config.h" +#include "Global.h" namespace DeathMessages { +using namespace GMLIB::Files::I18n; + class Entry { public: @@ -21,13 +24,21 @@ public: /// @return True if the plugin is disabled successfully. bool disable(); - // TODO: Implement this method if you need to unload the plugin. - // /// @return True if the plugin is unloaded successfully. - // bool unload(); + /// @return True if the plugin is unloaded successfully. + bool unload(); + + Config& getConfig(); + + std::optional getI18n(); + + void loadI18n(); + + void loadResourcePack(); private: ll::plugin::NativePlugin& mSelf; + std::optional mConfig; + std::optional mI18n; }; } // namespace DeathMessages - diff --git a/src/Global.h b/src/Global.h index 44d8030..347a08a 100644 --- a/src/Global.h +++ b/src/Global.h @@ -7,15 +7,8 @@ extern ll::Logger logger; extern ll::Logger deathLogger; extern ll::Logger infoLogger; -namespace ConfigData { -extern bool mServerSideLang; -extern bool mJoinMessage; -extern bool mLeftMessage; -extern bool mDeathMessage; -} // namespace ConfigData +extern std::string tr(std::string const& key, std::vector const& data = {}); -extern std::string tr(std::string key, std::vector data = {}); - -extern void initPlugin(); extern void RegisterDamageDefinition(); -extern void ListenEvents(); \ No newline at end of file +extern void ListenEvents(); +extern void initPlugin(); \ No newline at end of file diff --git a/src/Initialization.cpp b/src/Initialization.cpp deleted file mode 100644 index d8de7ac..0000000 --- a/src/Initialization.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "Config.h" -#include "Global.h" -#include "Language.h" - -ll::Logger deathLogger("Death"); -ll::Logger infoLogger("Server"); - -GMLIB::Files::JsonConfig* Config = nullptr; -GMLIB::Files::I18n::LangI18n* Language = nullptr; - -namespace ConfigData { -bool mServerSideLang = false; -bool mJoinMessage = false; -bool mLeftMessage = false; -bool mDeathMessage = false; -} // namespace ConfigData - - -void initServerSideTranslation() { - std::string langPath = "./plugins/DeathMessages/language/"; - std::string languageCode = Config->getValue({"ServerSideTranslation", "Language"}, "en_US"); - Language = new GMLIB::Files::I18n::LangI18n(langPath); - Language->updateOrCreateLanguage("en_US", en_US); - Language->updateOrCreateLanguage("zh_CN", zh_CN); - Language->loadAllLanguages(); - Language->chooseLanguage(languageCode); -} - -void initResourcePackTranslation() { - GMLIB::Mod::VanillaFix::setFixI18nEnabled(); - std::string resourcePath = "./plugins/DeathMessages/resource/"; - auto resource = GMLIB::Files::ResourceLanguage::ResourceLanguage(resourcePath, PLUGIN_NAME, 0, 8, 0); - resource.addLanguage("en_US", en_US); - resource.addLanguage("zh_CN", zh_CN); - resource.initLanguage(); -} - -std::string tr(std::string key, std::vector data) { - if (ConfigData::mServerSideLang) { - for (auto& info : data) { - ll::utils::string_utils::replaceAll(info, "%", ""); - info = Language->translate(info); - } - return Language->translate(key, data); - } - return I18nAPI::get(key, data); -} - -void initConfig() { - Config = new GMLIB::Files::JsonConfig("./plugins/DeathMessages/config/config.json", defaultConfig); - Config->init(); - ConfigData::mDeathMessage = Config->getValue({"ConsoleLog", "DeathMessage"}, true); - ConfigData::mJoinMessage = Config->getValue({"ConsoleLog", "JoinMessage"}, true); - ConfigData::mLeftMessage = Config->getValue({"ConsoleLog", "LeftMessage"}, true); - ConfigData::mServerSideLang = Config->getValue({"ServerSideTranslation", "Enabled"}, true); -} - -void initPlugin() { - initConfig(); - if (ConfigData::mServerSideLang) { - initServerSideTranslation(); - } else { - initResourcePackTranslation(); - } - if (Config->getValue({"FileLog", "Enabled"}, false)) { - auto path = Config->getValue({"FileLog", "Path"}, "./logs/DeathLog.log"); - deathLogger.setFile(path); - } -} diff --git a/tooth.json b/tooth.json index 28c309b..c3d63ad 100644 --- a/tooth.json +++ b/tooth.json @@ -1,7 +1,7 @@ { "format_version": 2, "tooth": "github.com/GroupMountain/DeathMessages", - "version": "0.12.0", + "version": "0.12.1", "info": { "name": "DeathMessages", "description": "Better Death Messages on MCBE", @@ -11,9 +11,9 @@ "DeathMessages" ] }, - "asset_url": "https://github.com/GroupMountain/DeathMessages/releases/download/v0.12.0/DeathMessages-windows-x64.zip", + "asset_url": "https://github.com/GroupMountain/DeathMessages/releases/download/v0.12.1/DeathMessages-windows-x64.zip", "dependencies": { - "github.com/GroupMountain/GMLIB": ">=0.12.3" + "github.com/GroupMountain/GMLIB": ">=0.12.6" }, "files": { "place": [