V-Gears 0
Free Final Fantasy VII engine.
ConfigCmdHandlerCommands.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The V-Gears Team
3 *
4 * This file is part of V-Gears
5 *
6 * V-Gears is free software: you can redistribute it and/or modify it under
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.0 (GPLv3) of the License.
9 *
10 * V-Gears is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#pragma once
17
18#include <OgreRenderWindow.h>
19#include <OgreRoot.h>
20#include <OgreStringConverter.h>
21#include "Console.h"
22#include "EntityManager.h"
23#include "Logger.h"
24#include "XmlMapFile.h"
25#include "XmlMapsFile.h"
26#include "VGearsGameState.h"
28#include "ConfigCmdHandler.h"
29#include "ConfigVarHandler.h"
30
36void CmdQuit(const Ogre::StringVector& params){
38}
39
46void CmdEcho(const Ogre::StringVector& params){
47 if (params.size() < 1){
48 Console::getSingleton().AddTextToOutput(
49 "Usage: /echo <string to output>"
50 );
51 return;
52 }
53 Ogre::String text = "";
54 for (size_t i = 1; i < params.size(); ++ i){
55 if (i != 1)text += " ";
56 text += params[i];
57 }
58 Console::getSingleton().AddTextToOutput(text + "\n");
59}
60
69void CmdConfigVarList(const Ogre::StringVector& params){
70 if (params.size() > 2){
71 Console::getSingleton().AddTextToOutput("Usage: /config_var_list [search string]");
72 return;
73 }
74
75 int number = 0;
76 int num_vars = ConfigVarHandler::getSingleton().GetConfigVarNumber();
77 for (int i = 0; i < num_vars; ++ i){
78 ConfigVar* var = ConfigVarHandler::getSingleton().GetConfigVar(i);
79 Ogre::String name = var->GetName();
80
81 if (params.size() > 1){
82 int found = name.find(params[1]);
83 if (found == 0){
84 Console::getSingleton().AddTextToOutput(
85 var->GetName() + " = \"" + var->GetS() + "\""
86 );
87 ++ number;
88 }
89 }
90 else{
91 Console::getSingleton().AddTextToOutput(var->GetName() + " = \"" + var->GetS() + "\"");
92 ++ number;
93 }
94 }
95 Console::getSingleton().AddTextToOutput(
96 Ogre::StringConverter::toString(number) + " config variables.\n"
97 );
98}
99
108void CmdConfigCmdList(const Ogre::StringVector& params){
109 if (params.size() > 2){
110 Console::getSingleton().AddTextToOutput("Usage: /config_cmd_list [search string]");
111 return;
112 }
113 int number = 0;
114 int num_cmds = ConfigCmdHandler::getSingleton().GetConfigCmdNumber();
115 for (int i = 0; i < num_cmds; ++ i){
116 ConfigCmd* cmd = ConfigCmdHandler::getSingleton().GetConfigCmd(i);
117 Ogre::String name = cmd->GetName();
118 if (params.size() > 1){
119 int found = name.find(params[1]);
120 if (found == 0){
121 Console::getSingleton().AddTextToOutput(cmd->GetName());
122 ++ number;
123 }
124 }
125 else{
126 Console::getSingleton().AddTextToOutput(cmd->GetName());
127 ++ number;
128 }
129 }
130 Console::getSingleton().AddTextToOutput(
131 Ogre::StringConverter::toString(number) + " config commands.\n"
132 );
133}
134
145void CmdSetConfigVar(const Ogre::StringVector& params){
146 if (params.size() < 2 || params.size() > 3){
147 Console::getSingleton().AddTextToOutput("Usage: /set <config variable> [value]");
148 return;
149 }
150 Ogre::String name = params[1];
151 ConfigVar* cvar = ConfigVarHandler::getSingleton().Find(name);
152 if (cvar == NULL){
153 LOG_ERROR("Config variable \"" + name + "\" not found.");
154 return;
155 }
156 if (params.size() == 3){
157 cvar->SetS(params[2]);
158 Console* console = Console::getSingletonPtr();
159 if (console != NULL) LOG_TRIVIAL(params[1] + " changed to \"" + params[2] + "\".");
160 }
161 else{
162 // Reset to default
163 cvar->SetS(cvar->GetDefaultValue());
164 LOG_TRIVIAL(params[1] + " changed to default \"" + cvar->GetDefaultValue() + "\".");
165 }
166}
167
177void CmdToggleConfigVar(const Ogre::StringVector& params){
178 if (params.size() < 4){
179 Console::getSingleton().AddTextToOutput(
180 "Usage: /toggle <config variable> [value1] [value2] ...");
181 return;
182 }
183 Ogre::String name = params[1];
184 ConfigVar* cvar = ConfigVarHandler::getSingleton().Find(name);
185 if (cvar == NULL){
186 LOG_ERROR("Config variable \"" + name + "\" not found.");
187 return;
188 }
189
190 // Sequentially trigger values
191 int number_of_values = params.size() - 2;
192 Ogre::String value = cvar->GetS();
193 int i = 0;
194 for (; i < number_of_values; ++ i){
195 if (value == params[i + 2]){
196 ++ i;
197 break;
198 }
199 }
200 if (i == number_of_values) i = 0;
201 cvar->SetS(params[i + 2]);
202}
203
215void CmdIncrementConfigVar(const Ogre::StringVector& params){
216 if (params.size() != 5){
217 Console::getSingleton().AddTextToOutput(
218 "Usage: /increment <cvar name> [value min] [value max] [step]"
219 );
220 return;
221 }
222 Ogre::String name = params[1];
223 ConfigVar* cvar = ConfigVarHandler::getSingleton().Find(name);
224 if (cvar == NULL){
225 LOG_ERROR("Config variable \"" + name + "\" not found.");
226 return;
227 }
228 float start_value = Ogre::StringConverter::parseReal(params[2]);
229 float end_value = Ogre::StringConverter::parseReal(params[3]);
230 float step = Ogre::StringConverter::parseReal(params[4]);
231
232 float new_value = cvar->GetF() + step;
233 if (new_value > end_value) new_value = end_value;
234 else if (new_value < start_value) new_value = start_value;
235 cvar->SetF(new_value);
236}
237
246void CmdSetLogLevel(const Ogre::StringVector& params){
247 if (params.size() != 2){
248 Console::getSingleton().AddTextToOutput(
249 "Usage: /log_level <level: 1 - only errors, 2 - errors and warnings, 3 - all>"
250 );
251 return;
252 }
253
254 int value = Ogre::StringConverter::parseInt(params[1]);
255 if (value > 0 && value < 4){
256 Ogre::LogManager::getSingletonPtr()->getDefaultLog()->setLogDetail(
257 (Ogre::LoggingLevel) value
258 );
259 switch(value){
260 case 1:
261 Console::getSingleton().AddTextToOutput(
262 "Logger level changed to \"only errors\".\n"
263 );
264 break;
265 case 2:
266 Console::getSingleton().AddTextToOutput(
267 "Logger level changed to \"errors and warnings\".\n"
268 );
269 break;
270 case 3:
271 Console::getSingleton().AddTextToOutput("Logger level changed to \"all\".\n");
272 break;
273 }
274 }
275 else{
276 Console::getSingleton().AddTextToOutput(
277 "Logger level can't be changed. Value \""
278 + params[1] + "\" isn't supported.\n"
279 );
280 }
281}
282
290void CmdMap(const Ogre::StringVector& params){
291 if (params.size() != 2){
292 Console::getSingleton().AddTextToOutput("Usage: /map [map_id]");
293 return;
294 }
295 EntityManager::getSingleton().Clear();
296 XmlMapsFile xml("./data/maps.xml");
297 Ogre::String file_name = xml.GetMapFileNameByName(params[1]);
298 XmlMapFile xml_map("./data/" + file_name);
299 xml_map.LoadMap();
300}
301
307void CmdMapCompletion(Ogre::StringVector& complete_params){
308 XmlMapsFile xml("./data/maps.xml");
309 xml.GetMapNames(complete_params);
310}
311
320void CmdResolution(const Ogre::StringVector& params){
321 if (params.size() < 3){
322 Console::getSingleton().AddTextToOutput(
323 "Usage: /resolution <width> <height> [full screen]"
324 );
325 return;
326 }
327 Ogre::RenderWindow* window = VGears::Application::getSingleton().getRenderWindow();
328 if (params.size() >= 4){
329 window->setFullscreen(
330 Ogre::StringConverter::parseBool(params[3]), Ogre::StringConverter::parseInt(params[1]),
331 Ogre::StringConverter::parseInt(params[2])
332 );
333 }
334 else{
335 window->resize(
336 Ogre::StringConverter::parseInt(params[1]), Ogre::StringConverter::parseInt(params[2])
337 );
338 window->getViewport(0)->setDimensions(0.0f, 0.0f, 1.0f, 1.0f);
339 }
340}
341
351void CmdResolutionCompletition(Ogre::StringVector& complete_params){
352 complete_params.push_back("640 480 0");
353 complete_params.push_back("640 480 1");
354 complete_params.push_back("800 600 0");
355 complete_params.push_back("800 600 1");
356 complete_params.push_back("1024 768 0");
357 complete_params.push_back("1024 768 1");
358 complete_params.push_back("1280 720 0");
359 complete_params.push_back("1280 720 1");
360 complete_params.push_back("1280 1024 0");
361 complete_params.push_back("1280 1024 1");
362}
363
369void CmdScreenshot(const Ogre::StringVector& params){
370 Ogre::RenderWindow* window = VGears::Application::getSingleton().getRenderWindow();
371 Ogre::String ret = window->writeContentsToTimestampedFile("screenshot_", ".tga");
372 Console::getSingleton().AddTextToOutput("Screenshot " + ret + " saved.");
373}
374
375
376
377/*
378void CmdViewer(const Ogre::StringVector& params){
379 if (params.size() == 2 || params.size() > 3){
380 Console::getSingleton().AddTextToOutput(
381 "Usage: /viewer [type_of_thing_to_view] [path_to_resource]"
382 );
383 return;
384 }
385 ModuleManager::getSingleton().RunViewer();
386 if (params.size() == 3){
387 ViewerModule* module
388 = (ViewerModule*)(ModuleManager::getSingleton().GetTopModule());
389
390 if (params[1] == "walkmesh") module->SetWalkmeshToLoad(params[2]);
391 else if (params[1] == "model") module->SetModelToLoad(params[2]);
392 else{
393 LOG_ERROR(
394 "Unsupported type \"" + params[1] + "\" in viewer command."
395 );
396 }
397 }
398}
399
400void CmdViewerCompletion(Ogre::StringVector& complete_params){
401 // models
402 complete_params.push_back("model");
403 Ogre::FileInfoListPtr resources
404 = Ogre::ResourceGroupManager::getSingleton().listResourceFileInfo("Game");
405 Ogre::FileInfoList resource_names = *resources;
406
407 Ogre::FileInfoList::iterator i = resource_names.begin();
408 for (; i != resource_names.end(); ++ i){
409 Ogre::String name;
410 Ogre::String ext;
411 Ogre::StringUtil::splitBaseFilename(i->filename, name, ext);
412 if (ext == "mesh") complete_params.push_back("model " + i->filename);
413 }
414
415 // walkmeshes
416 complete_params.push_back("walkmesh");
417 XmlMapsFile xml("./data/game_data/maps.xml");
418 Ogre::StringVector tmp;
419 xml.GetMapNames(tmp);
420 for (int i = 0; i < tmp.size(); ++i)
421 complete_params.push_back("walkmesh " + tmp[i]);
422}
423*/
424
429 AddCommand("quit", "Stops application and quit", "", CmdQuit, NULL);
430 AddCommand("echo", "Print command parameters", "", CmdEcho, NULL);
432 "config_var_list", "List of registered config variables", "[<filter substring>]",
433 CmdConfigVarList, NULL
434 );
436 "config_cmd_list", "List of registered config commands",
437 "[<filter substring>]", CmdConfigCmdList, NULL
438 );
439 AddCommand("set", "Set cvar value", "<cvar name> [value]", CmdSetConfigVar, NULL);
441 "toggle", "Toggle cvar value", "<cvar name> [value1] [value2] ...", CmdToggleConfigVar, NULL
442 );
444 "increment", "Increment cvar value", "<cvar name> [value min] [value max] [step]",
446 );
447 AddCommand("set_log_level", "Set log messages level", "", CmdSetLogLevel, NULL);
448 AddCommand("map", "Run game module", "", CmdMap, CmdMapCompletion);
449 AddCommand("resolution", "Change resolution", "", CmdResolution, CmdResolutionCompletition);
450 AddCommand("screenshot", "Capture current screen content", "", CmdScreenshot, NULL);
451 //AddCommand(
452 // "viewer", "Run viewer module", "", CmdViewer, CmdViewerCompletion
453 //);
454}
void CmdScreenshot(const Ogre::StringVector &params)
Saves a screenshot of the current game window.
Definition: ConfigCmdHandlerCommands.h:369
void CmdMap(const Ogre::StringVector &params)
Changes the game map.
Definition: ConfigCmdHandlerCommands.h:290
void CmdMapCompletion(Ogre::StringVector &complete_params)
Loads a list of map names.
Definition: ConfigCmdHandlerCommands.h:307
void CmdResolution(const Ogre::StringVector &params)
Sets the resolution and full screen mode.
Definition: ConfigCmdHandlerCommands.h:320
void CmdSetConfigVar(const Ogre::StringVector &params)
Sets the value of a configuration value.
Definition: ConfigCmdHandlerCommands.h:145
void CmdQuit(const Ogre::StringVector &params)
Command to quit the application.
Definition: ConfigCmdHandlerCommands.h:36
void CmdResolutionCompletition(Ogre::StringVector &complete_params)
Loads a list of resolution modes.
Definition: ConfigCmdHandlerCommands.h:351
void CmdEcho(const Ogre::StringVector &params)
Command to print to console.
Definition: ConfigCmdHandlerCommands.h:46
void CmdConfigVarList(const Ogre::StringVector &params)
Searches variables in the variable list and prints them.
Definition: ConfigCmdHandlerCommands.h:69
void CmdConfigCmdList(const Ogre::StringVector &params)
Searches the command list and prints the comands.
Definition: ConfigCmdHandlerCommands.h:108
void CmdToggleConfigVar(const Ogre::StringVector &params)
Changes the value of a configuration value conditionally.
Definition: ConfigCmdHandlerCommands.h:177
void CmdSetLogLevel(const Ogre::StringVector &params)
Configures the log level.
Definition: ConfigCmdHandlerCommands.h:246
void CmdIncrementConfigVar(const Ogre::StringVector &params)
Increments the value of a configuration variable.
Definition: ConfigCmdHandlerCommands.h:215
Ogre::RenderWindow * window
The Ogre render window.
Definition: OgreBase.cpp:24
void InitCmd()
Initializes the command.
Definition: ConfigCmdHandlerCommands.h:428
void AddCommand(const Ogre::String &name, const Ogre::String &description, const Ogre::String &params_description, ConfigCmdParams params, ConfigCmdCompletion completion)
Adds a command to the manager.
Definition: ConfigCmdHandler.cpp:30
A configuration command.
Definition: ConfigCmd.h:31
const Ogre::String & GetName() const
Retrieves the command name.
Definition: ConfigCmd.cpp:29
A configuration variable.
Definition: ConfigVar.h:25
void SetS(const Ogre::String &value)
Sets the string value of the variable.
Definition: ConfigVar.cpp:63
const Ogre::String & GetDefaultValue() const
Retrieves the variable default value.
Definition: ConfigVar.cpp:72
const Ogre::String & GetName() const
Retrieves the variable name.
Definition: ConfigVar.cpp:68
void SetF(float value)
Sets the float value of the variable.
Definition: ConfigVar.cpp:53
Ogre::String GetS() const
Retrieves the value of the variable in string form.
Definition: ConfigVar.cpp:46
float GetF() const
Retrieves the value of the variable in float form.
Definition: ConfigVar.cpp:42
The game console.
Definition: Console.h:30
Handles XML map files.
Definition: XmlMapFile.h:23
void LoadMap()
Parses the file and loads the map data.
Definition: XmlMapFile.cpp:30
Handles the main maps file.
Definition: XmlMapsFile.h:24
void GetMapNames(Ogre::StringVector &complete_params)
Retrieves a list of map names.
Definition: XmlMapsFile.cpp:41
const Ogre::String GetMapFileNameByName(const Ogre::String &name)
Retrieves a map file by map name.
Definition: XmlMapsFile.cpp:23
#define LOG_ERROR(message)
Prints an error log message.
Definition: Logger.h:28
#define LOG_TRIVIAL(message)
Prints a trivial log message.
Definition: Logger.h:50
GAME_STATE g_ApplicationState
Global application status indicator.
Definition: VGearsGameState.cpp:19
@ G_EXIT
The game is ready to be quit and can exit in the next loop pass.
Definition: VGearsGameState.h:28
Ogre::String String
Definition: TypeDefine.h:37