Skip to content

Commit

Permalink
refactor: add config to preloader
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Jul 12, 2024
1 parent da0a383 commit 0b60979
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 52 deletions.
61 changes: 34 additions & 27 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
46 changes: 46 additions & 0 deletions src/pl/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "pl/Config.h"

#include <filesystem>
#include <fstream>
#include <string>
#include <string_view>

#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<std::string const&>().c_str();
pl_mods_path = config["modsPath"].get_ref<std::string const&>().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
17 changes: 17 additions & 0 deletions src/pl/Config.h
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions src/pl/PreLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
#include <string>
#include <string_view>

#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 <windows.h>

Expand Down Expand Up @@ -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; }
Expand All @@ -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);
Expand All @@ -89,8 +90,10 @@ void loadPreloadNativeMods() {
}
} catch (...) {}
}
void loadConfig();

void init() {
loadLoggerConfig();
loadConfig();
pl::symbol_provider::init();
loadPreloadNativeMods();
}
Expand Down
2 changes: 0 additions & 2 deletions src/pl/PreLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
19 changes: 3 additions & 16 deletions src/pl/internal/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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); \
Expand Down
6 changes: 5 additions & 1 deletion src/pl/internal/WindowsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/pl/internal/WindowsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ std::pair<std::tm, int> getLocalTime();

std::filesystem::path getSystemRoot();

bool isStdoutSupportAnsi();
} // namespace pl::utils
4 changes: 2 additions & 2 deletions tooth.json
Original file line number Diff line number Diff line change
@@ -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": [
{
Expand Down

0 comments on commit 0b60979

Please sign in to comment.