VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
LoadConfig.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <VoltMod/Runtime.hpp>
7#include <format>
8#include <string>
9#include <string_view>
10#include <utility>
11
12namespace VoltMod
13{
14
15/** @brief Paths and translation behavior for LoadConfig. */
17{
18 std::string_view SettingsFile = "configs/settings.jsonc";
19 /** Whether to load the translations directory. */
20 bool Translations = true;
21};
22
23/**
24 * @brief Load the plugin's settings, then its translations, and return @p config.
25 *
26 * Meant for the App's `Config` member initializer, so every member below it is built with settings:
27 * `ConfigManager Config = VoltMod::LoadConfig<ConfigManager>(Runtime);`.
28 *
29 * The required "Configuration" step reads the plugin's @ref LoadConfigOptions::SettingsFile
30 * through TConfig::LoadSettings when available, otherwise Options::Load. A failure keeps the
31 * defaults, and the framework refuses the plugin before `Load` runs. Translation loading then
32 * applies `plugin.locale` and reads the plugin's `translations` directory when enabled.
33 */
34template <class TConfig>
35TConfig LoadConfig(Runtime& runtime, TConfig config = {}, const LoadConfigOptions& options = {})
36{
37 const std::string path = runtime.PluginFile(options.SettingsFile);
38 Status status = [&] {
39 if constexpr (requires { config.LoadSettings(path); })
40 {
41 return config.LoadSettings(path);
42 }
43 else
44 {
45 return config.Load(path);
46 }
47 }();
48 // A parse error already names the file.
49 if (!status && !status.error().Detail.starts_with(path))
50 {
51 status.error().Detail = std::format("{}: {}", path, status.error().Detail);
52 }
53 if (!runtime.LoadReport.Required("Configuration", std::move(status)))
54 {
55 return config;
56 }
57
58 if (options.Translations)
59 {
60 auto& translations = runtime.Translations;
61 if constexpr (requires { translations.SetLanguage(config.Get().plugin.locale); })
62 {
63 translations.SetLanguage(config.Get().plugin.locale);
64 }
65 translations.Load(runtime.PluginFile("translations"));
66 }
67 return config;
68}
69
70} // namespace VoltMod
Framework services for one load and unload cycle.
Definition Runtime.hpp:57
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
bool Load(std::string_view dirPath)
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
TConfig LoadConfig(Runtime &runtime, TConfig config={}, const LoadConfigOptions &options={})
Load the plugin's settings, then its translations, and return config.
std::expected< void, Error > Status
Definition Result.hpp:67
Paths and translation behavior for LoadConfig.
std::string_view SettingsFile