r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
argsHandler.cpp
Go to the documentation of this file.
1#include <fstream>
2#include <functional>
3#include <iostream>
4#include <unordered_map>
5
6#ifdef _WIN32
7#define APP_EXTENSION ".exe"
8#else
9#define APP_EXTENSION ""
10#endif
11
14#include "Utils/Logger.hpp"
15
16static constexpr std::string_view HELP_MESSAGE = "Usage: " PROJECT_NAME APP_EXTENSION " [options]\n\n"
17 "Options:\n"
18 "\t--help, -h Show this help message\n"
19 "\t--version, -v Show version information\n"
20 "\t--config, -c Specify path to config file\n";
21static constexpr std::string_view VERSION_MESSAGE = PROJECT_NAME " version " PROJECT_VERSION "\n"
22 "Build type: " BUILD_TYPE "\n"
23 "Git tag: " GIT_TAG "\n"
24 "Git commit hash: " GIT_COMMIT_HASH "\n";
25
27{
28 ArgsConfig cfg;
29 std::ifstream file(path);
30 if (!file.is_open())
31 {
32 throw std::runtime_error("Cannot open config file: " + path);
33 }
34
35 json j;
36 file >> j;
37
38 if (j.contains("window"))
39 {
40 const auto &w = j["window"];
41 if (w.contains("width"))
42 {
43 cfg.width = w["width"];
44 }
45 if (w.contains("height"))
46 {
47 cfg.height = w["height"];
48 }
49 if (w.contains("frame_limit"))
50 {
51 cfg.frameLimit = w["frame_limit"];
52 }
53 if (w.contains("fullscreen"))
54 {
55 cfg.fullscreen = w["fullscreen"];
56 }
57 const auto &p = j["plugins"];
58 if (p.contains("audio"))
59 {
60 cfg.audio_lib_path = p["audio"];
61 }
62 if (p.contains("network"))
63 {
64 cfg.network_lib_path = p["network"];
65 }
66 if (p.contains("renderer"))
67 {
68 cfg.renderer_lib_path = p["renderer"];
69 }
70 if (p.contains("game_solo"))
71 {
72 cfg.game_solo_lib_path = p["game_solo"];
73 }
74 if (p.contains("game_multi"))
75 {
76 cfg.game_multi_lib_path = p["game_multi"];
77 }
78 const auto &c = j["client"];
79 if (c.contains("host"))
80 {
81 cfg.host = c["host"];
82 }
83 if (c.contains("port"))
84 {
85 cfg.port = c["port"];
86 }
87 if (c.contains("player_name"))
88 {
89 cfg.player_name = c["player_name"];
90 }
91 }
92 return cfg;
93}
94
95cli::ArgsConfig cli::ArgsHandler::ParseArgs(const int argc, const char *const argv[])
96{
97 if (argc <= 1)
98 {
99 return {};
100 }
101
102 using ArgHandler = std::function<void(const char *arg)>;
103 std::unordered_map<std::string_view, ArgHandler> handlers;
104 ArgsConfig config{};
105 for (const auto *const opt : {"-h", "--help"})
106 {
107 handlers[opt] = [&config](const char *)
108 {
109 std::cout << HELP_MESSAGE;
110 config.exit = true;
111 };
112 }
113 for (const auto *const opt : {"-v", "--version"})
114 {
115 handlers[opt] = [&config](const char *)
116 {
117 std::cout << VERSION_MESSAGE;
118 config.exit = true;
119 };
120 }
121
122 for (const auto *const opt : {"-c", "--config"})
123 {
124 handlers[opt] = [&config](const char *arg)
125 {
126 if (!arg)
127 {
128 throw std::runtime_error("Missing config file argument");
129 }
130 config = ArgsConfig::fromFile(arg);
131 utl::Logger::log("Loaded config from file: " + std::string(arg), utl::LogLevel::INFO);
132 std::cout << "\tWidth: " << config.width << '\n'
133 << "\tHeight: " << config.height << '\n'
134 << "\tFrameLimit: " << config.frameLimit << "\n"
135 << "\tFullscreen: " << (config.fullscreen ? "true" : "false") << '\n';
136 };
137 }
138
139 const std::string_view key = argv[1];
140 const char *argValue = (argc > 2) ? argv[2] : nullptr;
141
142 if (const auto it = handlers.find(key); it != handlers.end())
143 {
144 it->second(argValue);
145 return config;
146 }
147
148 throw std::runtime_error("Unknown argument: " + std::string(key));
149}
150
152{
153 (void)env; // Currently unused
154 return {};
155}
This file contains the Logger class.
static ArgsConfig ParseArgs(int argc, const char *const argv[])
static EnvConfig ParseEnv(const char *const env[])
static void log(const std::string &message, const LogLevel &logLevel)
Definition Logger.hpp:51
This file contains the ArgsHandler class declaration.
#define BUILD_TYPE
Definition Version.hpp:15
#define PROJECT_VERSION
Definition Version.hpp:8
#define GIT_TAG
Definition Version.hpp:14
#define PROJECT_NAME
Definition Version.hpp:7
#define GIT_COMMIT_HASH
Definition Version.hpp:13
static constexpr std::string_view VERSION_MESSAGE
#define APP_EXTENSION
static constexpr std::string_view HELP_MESSAGE
nlohmann::json json
std::string game_multi_lib_path
unsigned int frameLimit
std::string host
std::string player_name
static ArgsConfig fromFile(const std::string &path)
std::string audio_lib_path
unsigned int height
unsigned int port
unsigned int width
std::string network_lib_path
std::string game_solo_lib_path
std::string renderer_lib_path