From 0b6097999e158e5fb36b246827f9bdb1e2ae133b Mon Sep 17 00:00:00 2001 From: OEOTYAN Date: Fri, 12 Jul 2024 19:36:02 +0800 Subject: [PATCH] refactor: add config to preloader --- CHANGELOG.md | 61 ++++++++++++++++++-------------- src/pl/Config.cpp | 46 ++++++++++++++++++++++++ src/pl/Config.h | 17 +++++++++ src/pl/PreLoader.cpp | 11 +++--- src/pl/PreLoader.h | 2 -- src/pl/internal/Logger.h | 19 ++-------- src/pl/internal/WindowsUtils.cpp | 6 +++- src/pl/internal/WindowsUtils.h | 1 + tooth.json | 4 +-- 9 files changed, 115 insertions(+), 52 deletions(-) create mode 100644 src/pl/Config.cpp create mode 100644 src/pl/Config.h diff --git a/CHANGELOG.md b/CHANGELOG.md index be2be35..9cd5b94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.9.0] - 2024-07-12 + +### Changed + +- Add config to Preloader. + ## [1.8.0] - 2024-06-28 ### Changed @@ -18,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Add new symbol data format. -- Load Levilamina from manifest. +- Load LeviLamina from manifest. ## [1.6.3] - 2024-06-17 @@ -68,30 +74,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Preload native plugins - Clean up code -[Unreleased]: https://github.com/LiteLDev/LeviLamina/compare/v1.8.0...HEAD -[1.8.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.7.0...v1.8.0 -[1.7.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.3...v1.7.0 -[1.6.3]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.2...v1.6.3 -[1.6.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.1...v1.6.2 -[1.6.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.0...v1.6.1 -[1.6.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.5.2...v1.6.0 -[1.5.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.5.1...v1.5.2 -[1.5.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.5.0...v1.5.1 -[1.5.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.3...v1.5.0 -[1.4.3]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.2...v1.4.3 -[1.4.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.1...v1.4.2 -[1.4.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.0...v1.4.1 -[1.4.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.3.1...v1.4.0 -[1.3.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.3.0...v1.3.1 -[1.3.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.2.0...v1.3.0 -[1.2.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.1.0...v1.2.0 -[1.1.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.3...v1.1.0 -[1.0.3]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.2...v1.0.3 -[1.0.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.1...v1.0.2 -[1.0.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.0...v1.0.1 -[1.0.0]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.3...v1.0.0 -[0.2.3]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.2...v0.2.3 -[0.2.2]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.1...v0.2.2 -[0.2.1]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.0...v0.2.1 -[0.2.0]: https://github.com/LiteLDev/LeviLamina/compare/v0.1.0...v0.2.0 +[Unreleased]: https://github.com/LiteLDev/PreLoader/compare/v1.9.0...HEAD +[1.8.0]: https://github.com/LiteLDev/PreLoader/compare/v1.8.0...v1.9.0 +[1.8.0]: https://github.com/LiteLDev/PreLoader/compare/v1.7.0...v1.8.0 +[1.7.0]: https://github.com/LiteLDev/PreLoader/compare/v1.6.3...v1.7.0 +[1.6.3]: https://github.com/LiteLDev/PreLoader/compare/v1.6.2...v1.6.3 +[1.6.2]: https://github.com/LiteLDev/PreLoader/compare/v1.6.1...v1.6.2 +[1.6.1]: https://github.com/LiteLDev/PreLoader/compare/v1.6.0...v1.6.1 +[1.6.0]: https://github.com/LiteLDev/PreLoader/compare/v1.5.2...v1.6.0 +[1.5.2]: https://github.com/LiteLDev/PreLoader/compare/v1.5.1...v1.5.2 +[1.5.1]: https://github.com/LiteLDev/PreLoader/compare/v1.5.0...v1.5.1 +[1.5.0]: https://github.com/LiteLDev/PreLoader/compare/v1.4.3...v1.5.0 +[1.4.3]: https://github.com/LiteLDev/PreLoader/compare/v1.4.2...v1.4.3 +[1.4.2]: https://github.com/LiteLDev/PreLoader/compare/v1.4.1...v1.4.2 +[1.4.1]: https://github.com/LiteLDev/PreLoader/compare/v1.4.0...v1.4.1 +[1.4.0]: https://github.com/LiteLDev/PreLoader/compare/v1.3.1...v1.4.0 +[1.3.1]: https://github.com/LiteLDev/PreLoader/compare/v1.3.0...v1.3.1 +[1.3.0]: https://github.com/LiteLDev/PreLoader/compare/v1.2.0...v1.3.0 +[1.2.0]: https://github.com/LiteLDev/PreLoader/compare/v1.1.0...v1.2.0 +[1.1.0]: https://github.com/LiteLDev/PreLoader/compare/v1.0.3...v1.1.0 +[1.0.3]: https://github.com/LiteLDev/PreLoader/compare/v1.0.2...v1.0.3 +[1.0.2]: https://github.com/LiteLDev/PreLoader/compare/v1.0.1...v1.0.2 +[1.0.1]: https://github.com/LiteLDev/PreLoader/compare/v1.0.0...v1.0.1 +[1.0.0]: https://github.com/LiteLDev/PreLoader/compare/v0.2.3...v1.0.0 +[0.2.3]: https://github.com/LiteLDev/PreLoader/compare/v0.2.2...v0.2.3 +[0.2.2]: https://github.com/LiteLDev/PreLoader/compare/v0.2.1...v0.2.2 +[0.2.1]: https://github.com/LiteLDev/PreLoader/compare/v0.2.0...v0.2.1 +[0.2.0]: https://github.com/LiteLDev/PreLoader/compare/v0.1.0...v0.2.0 [0.1.0]: https://github.com/LiteLDev/PreLoader/releases/tag/v0.1.0 diff --git a/src/pl/Config.cpp b/src/pl/Config.cpp new file mode 100644 index 0000000..d865b45 --- /dev/null +++ b/src/pl/Config.cpp @@ -0,0 +1,46 @@ +#include "pl/Config.h" + +#include +#include +#include +#include + +#include "pl/internal/WindowsUtils.h" + +#include "nlohmann/json.hpp" + +namespace pl { +char const* pl_mod_manager_name = "preload-native"; +char pl_color_log = utils::isStdoutSupportAnsi(); +int pl_log_level = 4; +char const* pl_log_path = "./logs/server-latest.log"; +char const* pl_mods_path = "./plugins/"; // TODO: change to mods + +nlohmann::json config; + +void loadConfig() try { + auto configPath = std::filesystem::path{u8"PreloaderConfig.json"}; + + try { + if (std::filesystem::exists("./mods/")) { pl_mods_path = "./mods/"; } // TODO: remove when release + if (std::filesystem::exists(configPath)) { + std::ifstream{configPath} >> config; + if (config["version"] == 1) { + pl_color_log = (bool)config["colorLog"]; + pl_log_level = config["logLevel"]; + pl_log_path = config["logPath"].get_ref().c_str(); + pl_mods_path = config["modsPath"].get_ref().c_str(); + return; + } + } + } catch (...) {} + config.clear(); + config["version"] = 1; + config["colorLog"] = pl_color_log; + config["logLevel"] = pl_log_level; + config["logPath"] = pl_log_path; + config["modsPath"] = pl_mods_path; + std::ofstream{configPath} << config.dump(4); +} catch (...) {} + +} // namespace pl diff --git a/src/pl/Config.h b/src/pl/Config.h new file mode 100644 index 0000000..7afcef3 --- /dev/null +++ b/src/pl/Config.h @@ -0,0 +1,17 @@ +#pragma once + +#include "pl/internal/Macro.h" // IWYU pragma: export + +#ifdef __cplusplus +namespace pl { +#endif + +PLCAPI char const* pl_mod_manager_name; +PLCAPI char pl_color_log; +PLCAPI int pl_log_level; +PLCAPI char const* pl_log_path; +PLCAPI char const* pl_mods_path; + +#ifdef __cplusplus +} +#endif diff --git a/src/pl/PreLoader.cpp b/src/pl/PreLoader.cpp index efbe040..6c87b6a 100644 --- a/src/pl/PreLoader.cpp +++ b/src/pl/PreLoader.cpp @@ -8,11 +8,12 @@ #include #include +#include "pl/Config.h" #include "pl/dependency/DependencyWalker.h" #include "pl/internal/Logger.h" #include "pl/internal/StringUtils.h" -#include "nlohmann/json_fwd.hpp" +#include "nlohmann/json.hpp" #include @@ -61,7 +62,7 @@ bool loadLibrary(std::string const& libName, bool showFailInfo = true) { } void loadPreloadNativeMods() { namespace fs = std::filesystem; - fs::path modsDir = ".\\mods"; + fs::path modsDir = (char8_t const*)(pl_mods_path); try { for (const auto& entry : fs::directory_iterator(modsDir)) { if (!entry.is_directory()) { continue; } @@ -73,7 +74,7 @@ void loadPreloadNativeMods() { nlohmann::json manifestJson; manifestFile >> manifestJson; std::string type = manifestJson["type"]; - if (type == preloadModManagerName) { + if (type == pl_mod_manager_name) { std::string modName = manifestJson["name"]; std::string modEntry = manifestJson["entry"]; Info("Preloading: {} <{}>", modName, modEntry); @@ -89,8 +90,10 @@ void loadPreloadNativeMods() { } } catch (...) {} } +void loadConfig(); + void init() { - loadLoggerConfig(); + loadConfig(); pl::symbol_provider::init(); loadPreloadNativeMods(); } diff --git a/src/pl/PreLoader.h b/src/pl/PreLoader.h index b9a8fd8..5294bd6 100644 --- a/src/pl/PreLoader.h +++ b/src/pl/PreLoader.h @@ -3,5 +3,3 @@ #include "pl/Hook.h" // IWYU pragma: export #include "pl/SymbolProvider.h" // IWYU pragma: export #include "pl/internal/Macro.h" // IWYU pragma: export - -[[maybe_unused]] constexpr char const* preloadModManagerName = "preload-native"; diff --git a/src/pl/internal/Logger.h b/src/pl/internal/Logger.h index c6c9ee8..ba3b6de 100644 --- a/src/pl/internal/Logger.h +++ b/src/pl/internal/Logger.h @@ -11,26 +11,13 @@ #include "fmt/format.h" #include "fmt/os.h" // IWYU pragma: keep -#include "nlohmann/json.hpp" -#include "nlohmann/json_fwd.hpp" +#include "pl/Config.h" #include "pl/internal/StringUtils.h" #include "pl/internal/WindowsUtils.h" namespace fs = std::filesystem; -inline bool shouldLogColor = true; - -inline void loadLoggerConfig() { - try { - std::ifstream file(fs::path{u8"mods/LeviLamina/config/config.json"}); - nlohmann::json json; - file >> json; - file.close(); - shouldLogColor = json["logger"]["colorLog"]; - } catch (...) {} -} - #define COLOR_TIME fmt::color::light_blue #define COLOR_INFO_PREFIX fmt::color::light_sea_green #define COLOR_INFO_TEXT fmt::terminal_color::white @@ -46,8 +33,8 @@ inline void loadLoggerConfig() { #define LOG_PREFIX(prefix, color1, color2) \ auto [time, ms] = ::pl::utils::getLocalTime(); \ - fmt::print(shouldLogColor ? fmt::fg(color1) : fmt::text_style(), fmt::format("{:%H:%M:%S}.{:0>3}", time, ms)); \ - fmt::print(shouldLogColor ? fmt::fg(color2) : fmt::text_style(), prefix); + fmt::print(pl::pl_color_log ? fmt::fg(color1) : fmt::text_style(), fmt::format("{:%H:%M:%S}.{:0>3}", time, ms)); \ + fmt::print(pl::pl_color_log ? fmt::fg(color2) : fmt::text_style(), prefix); #define LOG(color1, color2, prefix) \ LOG_PREFIX(prefix, color1, color2); \ diff --git a/src/pl/internal/WindowsUtils.cpp b/src/pl/internal/WindowsUtils.cpp index 11e4e8a..7172a20 100644 --- a/src/pl/internal/WindowsUtils.cpp +++ b/src/pl/internal/WindowsUtils.cpp @@ -113,5 +113,9 @@ std::filesystem::path getSystemRoot() { std::transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower); return buffer; } - +bool isStdoutSupportAnsi() { + DWORD mode; + if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode)) { return mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING; } + return false; +} } // namespace pl::utils diff --git a/src/pl/internal/WindowsUtils.h b/src/pl/internal/WindowsUtils.h index b6f7db6..5b609f8 100644 --- a/src/pl/internal/WindowsUtils.h +++ b/src/pl/internal/WindowsUtils.h @@ -22,4 +22,5 @@ std::pair getLocalTime(); std::filesystem::path getSystemRoot(); +bool isStdoutSupportAnsi(); } // namespace pl::utils diff --git a/tooth.json b/tooth.json index 1ce0bf8..efcec6a 100644 --- a/tooth.json +++ b/tooth.json @@ -1,14 +1,14 @@ { "format_version": 2, "tooth": "github.com/LiteLDev/PreLoader", - "version": "1.8.0", + "version": "1.9.0", "info": { "name": "PreLoader", "description": "A library preloader for loading LeviLamina", "author": "LiteLDev", "tags": [] }, - "asset_url": "https://github.com/LiteLDev/PreLoader/releases/download/v1.8.0/preloader-windows-x64.zip", + "asset_url": "https://github.com/LiteLDev/PreLoader/releases/download/v1.9.0/preloader-windows-x64.zip", "files": { "place": [ {