Skip to content

Commit 0b60979

Browse files
committed
refactor: add config to preloader
1 parent da0a383 commit 0b60979

File tree

9 files changed

+115
-52
lines changed

9 files changed

+115
-52
lines changed

CHANGELOG.md

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.9.0] - 2024-07-12
11+
12+
### Changed
13+
14+
- Add config to Preloader.
15+
1016
## [1.8.0] - 2024-06-28
1117

1218
### Changed
@@ -18,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1824
### Changed
1925

2026
- Add new symbol data format.
21-
- Load Levilamina from manifest.
27+
- Load LeviLamina from manifest.
2228

2329
## [1.6.3] - 2024-06-17
2430

@@ -68,30 +74,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6874
- Preload native plugins
6975
- Clean up code
7076

71-
[Unreleased]: https://github.com/LiteLDev/LeviLamina/compare/v1.8.0...HEAD
72-
[1.8.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.7.0...v1.8.0
73-
[1.7.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.3...v1.7.0
74-
[1.6.3]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.2...v1.6.3
75-
[1.6.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.1...v1.6.2
76-
[1.6.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.6.0...v1.6.1
77-
[1.6.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.5.2...v1.6.0
78-
[1.5.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.5.1...v1.5.2
79-
[1.5.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.5.0...v1.5.1
80-
[1.5.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.3...v1.5.0
81-
[1.4.3]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.2...v1.4.3
82-
[1.4.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.1...v1.4.2
83-
[1.4.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.4.0...v1.4.1
84-
[1.4.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.3.1...v1.4.0
85-
[1.3.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.3.0...v1.3.1
86-
[1.3.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.2.0...v1.3.0
87-
[1.2.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.1.0...v1.2.0
88-
[1.1.0]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.3...v1.1.0
89-
[1.0.3]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.2...v1.0.3
90-
[1.0.2]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.1...v1.0.2
91-
[1.0.1]: https://github.com/LiteLDev/LeviLamina/compare/v1.0.0...v1.0.1
92-
[1.0.0]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.3...v1.0.0
93-
[0.2.3]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.2...v0.2.3
94-
[0.2.2]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.1...v0.2.2
95-
[0.2.1]: https://github.com/LiteLDev/LeviLamina/compare/v0.2.0...v0.2.1
96-
[0.2.0]: https://github.com/LiteLDev/LeviLamina/compare/v0.1.0...v0.2.0
77+
[Unreleased]: https://github.com/LiteLDev/PreLoader/compare/v1.9.0...HEAD
78+
[1.8.0]: https://github.com/LiteLDev/PreLoader/compare/v1.8.0...v1.9.0
79+
[1.8.0]: https://github.com/LiteLDev/PreLoader/compare/v1.7.0...v1.8.0
80+
[1.7.0]: https://github.com/LiteLDev/PreLoader/compare/v1.6.3...v1.7.0
81+
[1.6.3]: https://github.com/LiteLDev/PreLoader/compare/v1.6.2...v1.6.3
82+
[1.6.2]: https://github.com/LiteLDev/PreLoader/compare/v1.6.1...v1.6.2
83+
[1.6.1]: https://github.com/LiteLDev/PreLoader/compare/v1.6.0...v1.6.1
84+
[1.6.0]: https://github.com/LiteLDev/PreLoader/compare/v1.5.2...v1.6.0
85+
[1.5.2]: https://github.com/LiteLDev/PreLoader/compare/v1.5.1...v1.5.2
86+
[1.5.1]: https://github.com/LiteLDev/PreLoader/compare/v1.5.0...v1.5.1
87+
[1.5.0]: https://github.com/LiteLDev/PreLoader/compare/v1.4.3...v1.5.0
88+
[1.4.3]: https://github.com/LiteLDev/PreLoader/compare/v1.4.2...v1.4.3
89+
[1.4.2]: https://github.com/LiteLDev/PreLoader/compare/v1.4.1...v1.4.2
90+
[1.4.1]: https://github.com/LiteLDev/PreLoader/compare/v1.4.0...v1.4.1
91+
[1.4.0]: https://github.com/LiteLDev/PreLoader/compare/v1.3.1...v1.4.0
92+
[1.3.1]: https://github.com/LiteLDev/PreLoader/compare/v1.3.0...v1.3.1
93+
[1.3.0]: https://github.com/LiteLDev/PreLoader/compare/v1.2.0...v1.3.0
94+
[1.2.0]: https://github.com/LiteLDev/PreLoader/compare/v1.1.0...v1.2.0
95+
[1.1.0]: https://github.com/LiteLDev/PreLoader/compare/v1.0.3...v1.1.0
96+
[1.0.3]: https://github.com/LiteLDev/PreLoader/compare/v1.0.2...v1.0.3
97+
[1.0.2]: https://github.com/LiteLDev/PreLoader/compare/v1.0.1...v1.0.2
98+
[1.0.1]: https://github.com/LiteLDev/PreLoader/compare/v1.0.0...v1.0.1
99+
[1.0.0]: https://github.com/LiteLDev/PreLoader/compare/v0.2.3...v1.0.0
100+
[0.2.3]: https://github.com/LiteLDev/PreLoader/compare/v0.2.2...v0.2.3
101+
[0.2.2]: https://github.com/LiteLDev/PreLoader/compare/v0.2.1...v0.2.2
102+
[0.2.1]: https://github.com/LiteLDev/PreLoader/compare/v0.2.0...v0.2.1
103+
[0.2.0]: https://github.com/LiteLDev/PreLoader/compare/v0.1.0...v0.2.0
97104
[0.1.0]: https://github.com/LiteLDev/PreLoader/releases/tag/v0.1.0

src/pl/Config.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "pl/Config.h"
2+
3+
#include <filesystem>
4+
#include <fstream>
5+
#include <string>
6+
#include <string_view>
7+
8+
#include "pl/internal/WindowsUtils.h"
9+
10+
#include "nlohmann/json.hpp"
11+
12+
namespace pl {
13+
char const* pl_mod_manager_name = "preload-native";
14+
char pl_color_log = utils::isStdoutSupportAnsi();
15+
int pl_log_level = 4;
16+
char const* pl_log_path = "./logs/server-latest.log";
17+
char const* pl_mods_path = "./plugins/"; // TODO: change to mods
18+
19+
nlohmann::json config;
20+
21+
void loadConfig() try {
22+
auto configPath = std::filesystem::path{u8"PreloaderConfig.json"};
23+
24+
try {
25+
if (std::filesystem::exists("./mods/")) { pl_mods_path = "./mods/"; } // TODO: remove when release
26+
if (std::filesystem::exists(configPath)) {
27+
std::ifstream{configPath} >> config;
28+
if (config["version"] == 1) {
29+
pl_color_log = (bool)config["colorLog"];
30+
pl_log_level = config["logLevel"];
31+
pl_log_path = config["logPath"].get_ref<std::string const&>().c_str();
32+
pl_mods_path = config["modsPath"].get_ref<std::string const&>().c_str();
33+
return;
34+
}
35+
}
36+
} catch (...) {}
37+
config.clear();
38+
config["version"] = 1;
39+
config["colorLog"] = pl_color_log;
40+
config["logLevel"] = pl_log_level;
41+
config["logPath"] = pl_log_path;
42+
config["modsPath"] = pl_mods_path;
43+
std::ofstream{configPath} << config.dump(4);
44+
} catch (...) {}
45+
46+
} // namespace pl

src/pl/Config.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "pl/internal/Macro.h" // IWYU pragma: export
4+
5+
#ifdef __cplusplus
6+
namespace pl {
7+
#endif
8+
9+
PLCAPI char const* pl_mod_manager_name;
10+
PLCAPI char pl_color_log;
11+
PLCAPI int pl_log_level;
12+
PLCAPI char const* pl_log_path;
13+
PLCAPI char const* pl_mods_path;
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif

src/pl/PreLoader.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
#include <string>
99
#include <string_view>
1010

11+
#include "pl/Config.h"
1112
#include "pl/dependency/DependencyWalker.h"
1213
#include "pl/internal/Logger.h"
1314
#include "pl/internal/StringUtils.h"
1415

15-
#include "nlohmann/json_fwd.hpp"
16+
#include "nlohmann/json.hpp"
1617

1718
#include <windows.h>
1819

@@ -61,7 +62,7 @@ bool loadLibrary(std::string const& libName, bool showFailInfo = true) {
6162
}
6263
void loadPreloadNativeMods() {
6364
namespace fs = std::filesystem;
64-
fs::path modsDir = ".\\mods";
65+
fs::path modsDir = (char8_t const*)(pl_mods_path);
6566
try {
6667
for (const auto& entry : fs::directory_iterator(modsDir)) {
6768
if (!entry.is_directory()) { continue; }
@@ -73,7 +74,7 @@ void loadPreloadNativeMods() {
7374
nlohmann::json manifestJson;
7475
manifestFile >> manifestJson;
7576
std::string type = manifestJson["type"];
76-
if (type == preloadModManagerName) {
77+
if (type == pl_mod_manager_name) {
7778
std::string modName = manifestJson["name"];
7879
std::string modEntry = manifestJson["entry"];
7980
Info("Preloading: {} <{}>", modName, modEntry);
@@ -89,8 +90,10 @@ void loadPreloadNativeMods() {
8990
}
9091
} catch (...) {}
9192
}
93+
void loadConfig();
94+
9295
void init() {
93-
loadLoggerConfig();
96+
loadConfig();
9497
pl::symbol_provider::init();
9598
loadPreloadNativeMods();
9699
}

src/pl/PreLoader.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
#include "pl/Hook.h" // IWYU pragma: export
44
#include "pl/SymbolProvider.h" // IWYU pragma: export
55
#include "pl/internal/Macro.h" // IWYU pragma: export
6-
7-
[[maybe_unused]] constexpr char const* preloadModManagerName = "preload-native";

src/pl/internal/Logger.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,13 @@
1111
#include "fmt/format.h"
1212
#include "fmt/os.h" // IWYU pragma: keep
1313

14-
#include "nlohmann/json.hpp"
15-
#include "nlohmann/json_fwd.hpp"
1614

15+
#include "pl/Config.h"
1716
#include "pl/internal/StringUtils.h"
1817
#include "pl/internal/WindowsUtils.h"
1918

2019
namespace fs = std::filesystem;
2120

22-
inline bool shouldLogColor = true;
23-
24-
inline void loadLoggerConfig() {
25-
try {
26-
std::ifstream file(fs::path{u8"mods/LeviLamina/config/config.json"});
27-
nlohmann::json json;
28-
file >> json;
29-
file.close();
30-
shouldLogColor = json["logger"]["colorLog"];
31-
} catch (...) {}
32-
}
33-
3421
#define COLOR_TIME fmt::color::light_blue
3522
#define COLOR_INFO_PREFIX fmt::color::light_sea_green
3623
#define COLOR_INFO_TEXT fmt::terminal_color::white
@@ -46,8 +33,8 @@ inline void loadLoggerConfig() {
4633

4734
#define LOG_PREFIX(prefix, color1, color2) \
4835
auto [time, ms] = ::pl::utils::getLocalTime(); \
49-
fmt::print(shouldLogColor ? fmt::fg(color1) : fmt::text_style(), fmt::format("{:%H:%M:%S}.{:0>3}", time, ms)); \
50-
fmt::print(shouldLogColor ? fmt::fg(color2) : fmt::text_style(), prefix);
36+
fmt::print(pl::pl_color_log ? fmt::fg(color1) : fmt::text_style(), fmt::format("{:%H:%M:%S}.{:0>3}", time, ms)); \
37+
fmt::print(pl::pl_color_log ? fmt::fg(color2) : fmt::text_style(), prefix);
5138

5239
#define LOG(color1, color2, prefix) \
5340
LOG_PREFIX(prefix, color1, color2); \

src/pl/internal/WindowsUtils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,9 @@ std::filesystem::path getSystemRoot() {
113113
std::transform(buffer.begin(), buffer.end(), buffer.begin(), ::tolower);
114114
return buffer;
115115
}
116-
116+
bool isStdoutSupportAnsi() {
117+
DWORD mode;
118+
if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode)) { return mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING; }
119+
return false;
120+
}
117121
} // namespace pl::utils

src/pl/internal/WindowsUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ std::pair<std::tm, int> getLocalTime();
2222

2323
std::filesystem::path getSystemRoot();
2424

25+
bool isStdoutSupportAnsi();
2526
} // namespace pl::utils

tooth.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"format_version": 2,
33
"tooth": "github.com/LiteLDev/PreLoader",
4-
"version": "1.8.0",
4+
"version": "1.9.0",
55
"info": {
66
"name": "PreLoader",
77
"description": "A library preloader for loading LeviLamina",
88
"author": "LiteLDev",
99
"tags": []
1010
},
11-
"asset_url": "https://github.com/LiteLDev/PreLoader/releases/download/v1.8.0/preloader-windows-x64.zip",
11+
"asset_url": "https://github.com/LiteLDev/PreLoader/releases/download/v1.9.0/preloader-windows-x64.zip",
1212
"files": {
1313
"place": [
1414
{

0 commit comments

Comments
 (0)