R-Type  2
Doom but in better
Loading...
Searching...
No Matches
RealMain.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** RealMain.cpp
6*/
7
14#include "RealMain.hpp"
15
26std::vector<std::string> extract_argument(char *arg)
27{
28 PRETTY_INFO << "Extracting arguments" << std::endl;
29 std::string arg_str(arg);
30 size_t pos = arg_str.find('=');
31 std::string flag = "";
32 std::string value = "";
33 std::vector<std::string> resp;
34
35 if (pos != std::string::npos) {
36 flag = arg_str.substr(0, pos);
37 value = arg_str.substr(pos + 1);
38 } else {
39 flag = arg_str;
40 value = "";
41 }
42
43 if (flag.find("--") == 0) {
44 flag = flag.substr(2);
45 } else if (flag.find("-") == 0) {
46 flag = flag.substr(1);
47 } else if (flag.find("/") == 0) {
48 flag = flag.substr(1);
49 }
50
51 resp.push_back(flag);
52 resp.push_back(value);
53
54 return resp;
55}
56
73void process_given_argument(Main &main, const std::vector<std::string> &args, std::string const &binName)
74{
75 if (args[0] == "help" || args[0] == "h" || args[0] == "?") {
76 DisplayHelp(binName);
78 } else if (args[0] == "version" || args[0] == "v") {
79 DisplayVersion(false);
81 } else if (args[0] == "ip" || args[0] == "i") {
82 PRETTY_SUCCESS << "Ip is provided: '" << args[1] << "'" << std::endl;
83 if (args[1].empty()) {
84 PRETTY_CRITICAL << "Error: Ip value is required" << std::endl;
85 std::cerr << "Error: Ip value is required" << std::endl;
87 }
88 main.setIp(args[1]);
89 } else if (args[0] == "port" || args[0] == "p") {
90 PRETTY_SUCCESS << "Port is provided: '" << args[1] << "'" << std::endl;
91 unsigned int port = 0;
92 if (args[1].empty()) {
93 PRETTY_CRITICAL << "Error: Port number is required" << std::endl;
94 std::cerr << "Error: Port number is required" << std::endl;
96 }
97 try {
98 port = static_cast<unsigned int>(std::stoul(args[1]));
99 }
100 catch (const std::invalid_argument &e) {
101 PRETTY_CRITICAL << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
102 std::cerr << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
103 throw CustomExceptions::InvalidPort(args[1]);
104 }
105 catch (const std::out_of_range &e) {
106 PRETTY_CRITICAL << "Out of range: '" << args[1] << "' is too large for an unsigned int." << std::endl;
107 std::cerr << "Out of range: '" << args[1] << "' is too large for an unsigned int." << std::endl;
108 throw CustomExceptions::InvalidPort(args[1]);
109 }
110 main.setPort(port);
111 } else if (args[0] == "debug" || args[0] == "d") {
112 main.setLog(true);
113 main.setDebug(true);
114 PRETTY_INFO << "Debug is True" << std::endl;
115 } else if (args[0] == "log" || args[0] == "l") {
116 main.setLog(true);
117 PRETTY_INFO << "Debug is True" << std::endl;
118 } else if (args[0] == "full-screen" || args[0] == "fs" || args[0] == "fullscreen") {
119 PRETTY_INFO << "Full screen is activated" << std::endl;
120 main.setWindowFullscreen(true);
121 } else if (args[0] == "position-x" || args[0] == "px" || args[0] == "positionx" || args[0] == "posx" || args[0] == "pos-x") {
122 unsigned int posx = 0;
123 PRETTY_INFO << "Position x is provided: '" << args[1] << "'" << std::endl;
124 if (args[1].empty()) {
125 std::cerr << "Error: position of x is required." << std::endl;
127 }
128 try {
129 posx = static_cast<int>(std::stoul(args[1]));
130 }
131 catch (const std::invalid_argument &e) {
132 PRETTY_CRITICAL << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
133 std::cerr << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
135 }
136 catch (const std::out_of_range &e) {
137 PRETTY_CRITICAL << "Out of range: '" << args[1] << "' is too large for an int." << std::endl;
138 std::cerr << "Out of range: '" << args[1] << "' is too large for an int." << std::endl;
140 }
141 main.setWindowPositionX(posx);
142 } else if (args[0] == "position-y" || args[0] == "py" || args[0] == "positiony" || args[0] == "posy" || args[0] == "pos-y") {
143 unsigned int posy = 0;
144 PRETTY_INFO << "Position y is provided: '" << args[1] << "'" << std::endl;
145 if (args[1].empty()) {
146 std::cerr << "Error: position of x is required." << std::endl;
148 }
149 try {
150 posy = static_cast<int>(std::stoul(args[1]));
151 }
152 catch (const std::invalid_argument &e) {
153 PRETTY_CRITICAL << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
154 std::cerr << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
156 }
157 catch (const std::out_of_range &e) {
158 PRETTY_CRITICAL << "Out of range: '" << args[1] << "' is too large for an int." << std::endl;
159 std::cerr << "Out of range: '" << args[1] << "' is too large for an int." << std::endl;
161 }
162 main.setWindowPositionY(posy);
163 } else if (args[0] == "window-width" || args[0] == "ww" || args[0] == "windowwidth") {
164 unsigned int windowWidth = 0;
165 PRETTY_INFO << "Window width is provided: '" << args[1] << "'" << std::endl;
166 if (args[1].empty()) {
167 std::cerr << "Error: Window width is required." << std::endl;
169 }
170 try {
171 windowWidth = static_cast<unsigned int>(std::stoul(args[1]));
172 }
173 catch (const std::invalid_argument &e) {
174 PRETTY_CRITICAL << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
175 std::cerr << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
177 }
178 catch (const std::out_of_range &e) {
179 PRETTY_CRITICAL << "Out of range: '" << args[1] << "' is too large for an unsigned int." << std::endl;
180 std::cerr << "Out of range: '" << args[1] << "' is too large for an unsigned int." << std::endl;
182 }
183 main.setWindowWidth(windowWidth);
184 } else if (args[0] == "window-height" || args[0] == "wh" || args[0] == "windowheight") {
185 unsigned int windowHeight = 0;
186 PRETTY_INFO << "Window Height is provided: '" << args[1] << "'" << std::endl;
187 if (args[1].empty()) {
188 std::cerr << "Error: Window Height is required." << std::endl;
190 }
191 try {
192 windowHeight = static_cast<unsigned int>(std::stoul(args[1]));
193 }
194 catch (const std::invalid_argument &e) {
195 std::cerr << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
197 }
198 catch (const std::out_of_range &e) {
199 std::cerr << "Out of range: '" << args[1] << "' is too large for an unsigned int." << std::endl;
201 }
202 main.setWindowHeight(windowHeight);
203 } else if (args[0] == "frame-rate-limit" || args[0] == "frl" || args[0] == "frameratelimit") {
204 unsigned int frameLimit = 0;
205 PRETTY_INFO << "Frame limit is provided: '" << args[1] << "'" << std::endl;
206 if (args[1].empty()) {
207 std::cerr << "Error: Frame Rate is required." << std::endl;
209 }
210 try {
211 frameLimit = static_cast<unsigned int>(std::stoul(args[1]));
212 }
213 catch (const std::invalid_argument &e) {
214 std::cerr << "Invalid argument: '" << args[1] << "' is not a valid number." << std::endl;
216 }
217 catch (const std::out_of_range &e) {
218 std::cerr << "Out of range: '" << args[1] << "' is too large for an unsigned int." << std::endl;
220 }
221 main.setFrameLimit(frameLimit);
222 } else if (args[0] == "window-title" || args[0] == "wt" || args[0] == "windowtitle") {
223 PRETTY_INFO << "Window title is provided: '" << args[1] << "'" << std::endl;
224 if (args[1].empty()) {
225 std::cerr << "Error: The title for the window is required." << std::endl;
227 }
228 main.setWindowTitle(args[1]);
229 } else if (args[0] == "config-file" || args[0] == "cf" || args[0] == "configfile") {
230 PRETTY_INFO << "Config file is provided: '" << args[1] << "'" << std::endl;
231 if (args[1].empty()) {
232 std::cerr << "Error: TOML config file is required." << std::endl;
234 }
235 main.setConfigFile(args[1]);
236 } else {
238 }
239}
240
251void process_arguments(Main &main, int argc, char **argv)
252{
253 unsigned int index = 1;
254 PRETTY_INFO << "Dumping arguments" << std::endl;
255 std::string binName(argv[0]);
256 while (index < argc) {
257 std::vector<std::string> arg = extract_argument(argv[index]);
258 PRETTY_INFO << "Flag: '" << arg[0] << "', Value: '" << arg[1] << "'." << std::endl;
259 process_given_argument(main, arg, binName);
260 index++;
261 }
262}
263
277int RealMain(int argc, char **argv)
278{
279 int status = MY_SUCCESS;
280 bool help_found = false;
281 bool version_found = false;
282
283 Main MyMain("127.0.0.1", 9000, 800, 600, true, false, "R-Type", 0, 0, "NULL", false, false, false, 20, 20, 60, "client_config.toml", false, false);
284
285 if (argc > 1) {
286 try {
287 process_arguments(MyMain, argc, argv);
288 }
289 catch (const CustomExceptions::HelpFound &e) {
290 status = MY_SUCCESS;
291 help_found = true;
292 PRETTY_SUCCESS << "Help was found: '" << e.what() << "'." << std::endl;
293 }
294 catch (const CustomExceptions::VersionFound &e) {
295 status = MY_SUCCESS;
296 version_found = true;
297 PRETTY_SUCCESS << "Version was found: '" << e.what() << "'." << std::endl;
298 }
299 catch (const std::exception &e) {
300 status = MY_ERROR;
301 PRETTY_CRITICAL << "An error occurred: '" << e.what() << "'." << std::endl;
302 std::cerr << "An error occurred: '" << e.what() << "'." << std::endl;
303 }
304 }
305
306 if (help_found || version_found || status == MY_ERROR) {
307 PRETTY_INFO << "The program exited with status: " << status << std::endl;
308 std::cout << RESET_COL << std::endl;
309 return status;
310 }
311
312 try {
313 MyMain.setConfigFile(MyMain.getConfigFile());
314 }
315 catch (const std::exception &e) {
316 std::cerr << e.what() << std::endl;
317 PRETTY_CRITICAL << "Failed to load the config file, aborting program." << std::endl;
318 std::cout << RESET_COL << std::endl;
319 return MY_ERROR;
320 }
321
322 try {
323 MyMain.run();
324 }
325 catch (const std::exception &e) {
326 std::cerr << e.what() << std::endl;
327 status = MY_ERROR;
328 }
329 PRETTY_INFO << "Exit status : '" << status << "'." << std::endl;
330 std::cout << RESET_COL << std::endl;
331 return status;
332}
#define PRETTY_INFO
Info log with details and colour.
#define RESET_COL
Definition LogMacros.hpp:76
#define PRETTY_CRITICAL
Critical log with details and colour.
#define PRETTY_SUCCESS
Success log with details and colour.
void process_given_argument(Main &main, const std::vector< std::string > &args, std::string const &binName)
Process the single argument that is provided.
Definition RealMain.cpp:73
std::vector< std::string > extract_argument(char *arg)
Extracts the flag and value from a command-line argument.
Definition RealMain.cpp:26
int RealMain(int argc, char **argv)
The main function for initializing and running the application.
Definition RealMain.cpp:277
void process_arguments(Main &main, int argc, char **argv)
Processes the command-line arguments and applies them to the main application object.
Definition RealMain.cpp:251
This is the file in charge of containing the main class and other ressources at the root of the progr...
void DisplayVersion(bool helpMode=false)
Displays version information for the program.
void DisplayHelp(const std::string binName)
The main function of the help display.
This is the class in charge of informing the user that the help flag was found. This is not an error.
Definition Found.hpp:30
const char * what() const noexcept
Retrieves the error message.
Definition Found.cpp:26
This is the class in charge of informing the user that the window frame limit is invalid.
Definition Invalid.hpp:357
This is the class in charge of informing the user that the port is incorrect.
Definition Invalid.hpp:59
This is the class in charge of informing the user that the window height is invalid.
Definition Invalid.hpp:328
This is the class in charge of informing the user that the window width is invalid.
Definition Invalid.hpp:241
This is the class in charge of informing the user that the window x position is invalid.
Definition Invalid.hpp:270
This is the class in charge of informing the user that the window Y position is invalid.
Definition Invalid.hpp:299
This is the class in charge of informing the user was supposed to pass a parameter along with the fla...
Definition No.hpp:28
This is the class in charge of informing the user that the argument that was provided is not known to...
Definition Unknown.hpp:29
This is the class in charge of informing the user that the version flag was found....
Definition Found.hpp:58
The Main class is the main class of the program.
Definition RealMain.hpp:54
void run()
This is the function used to start the program's main section.
std::string getConfigFile() const
Function in charge of returning the path to the config file.
void setConfigFile(const std::string &configFile)
This is the function in charge of seting the config filepath.
int main(void)
Definition main.cpp:3