RigsofRods
Soft-body Physics Simulation
AppCommandLine.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "Console.h"
23 #include "GameContext.h"
24 #include "ErrorUtils.h"
25 #include "PlatformUtils.h"
26 #include "Utils.h"
27 
28 using namespace Ogre;
29 using namespace RoR;
30 
31 #ifdef _UNICODE
32 # undef _UNICODE // We want narrow-string args.
33 #endif
34 #include "SimpleOpt.h" // https://github.com/brofield/simpleopt
35 
36 // option identifiers
37 enum {
50 };
51 
52 // option array
53 CSimpleOpt::SOption cmdline_options[] = {
54  { OPT_MAP, ("-map"), SO_REQ_SEP },
55  { OPT_MAP, ("-terrain"), SO_REQ_SEP },
56  { OPT_POS, ("-pos"), SO_REQ_SEP },
57  { OPT_ROT, ("-rot"), SO_REQ_SEP },
58  { OPT_TRUCK, ("-truck"), SO_REQ_SEP },
59  { OPT_TRUCKCONFIG, ("-truckconfig"), SO_REQ_SEP },
60  { OPT_RUNSCRIPT, ("-runscript"), SO_REQ_SEP },
61  { OPT_ENTERTRUCK, ("-enter"), SO_NONE },
62  { OPT_HELP, ("--help"), SO_NONE },
63  { OPT_HELP, ("-help"), SO_NONE },
64  { OPT_RESUME, ("-resume"), SO_NONE },
65  { OPT_CHECKCACHE, ("-checkcache"), SO_NONE },
66  { OPT_VER, ("-version"), SO_NONE },
67  { OPT_JOINMPSERVER, ("-joinserver"), SO_REQ_CMB },
68  SO_END_OF_OPTIONS
69 };
70 
71 void Console::processCommandLine(int argc, char *argv[])
72 {
73  CSimpleOpt args(argc, argv, cmdline_options);
74 
75  while (args.Next())
76  {
77  if (args.LastError() != SO_SUCCESS)
78  {
79  App::app_state->setVal((int)AppState::PRINT_HELP_EXIT);
80  return;
81  }
82  else if (args.OptionId() == OPT_HELP)
83  {
84  App::app_state->setVal((int)AppState::PRINT_HELP_EXIT);
85  return;
86  }
87  else if (args.OptionId() == OPT_VER)
88  {
89  App::app_state->setVal((int)AppState::PRINT_VERSION_EXIT);
90  return;
91  }
92  else if (args.OptionId() == OPT_TRUCK)
93  {
94  App::cli_preset_vehicle->setStr(args.OptionArg());
95  }
96  else if (args.OptionId() == OPT_TRUCKCONFIG)
97  {
98  App::cli_preset_veh_config->setStr(args.OptionArg());
99  }
100  else if (args.OptionId() == OPT_RUNSCRIPT)
101  {
102  // Append to startup script list cvar, separate by ','
103  if (App::cli_custom_scripts->getStr() == "")
104  {
105  App::cli_custom_scripts->setStr(args.OptionArg());
106  }
107  else
108  {
110  fmt::format("{},{}", App::cli_custom_scripts->getStr(), args.OptionArg()));
111  }
112  }
113  else if (args.OptionId() == OPT_MAP)
114  {
115  App::cli_preset_terrain->setStr(args.OptionArg());
116  }
117  else if (args.OptionId() == OPT_POS)
118  {
119  App::cli_preset_spawn_pos->setStr(args.OptionArg());
120  }
121  else if (args.OptionId() == OPT_ROT)
122  {
123  App::cli_preset_spawn_rot->setStr(args.OptionArg());
124  }
125  else if (args.OptionId() == OPT_RESUME)
126  {
128  }
129  else if (args.OptionId() == OPT_CHECKCACHE)
130  {
132  }
133  else if (args.OptionId() == OPT_ENTERTRUCK)
134  {
136  }
137  else if (args.OptionId() == OPT_JOINMPSERVER)
138  {
139  std::string server_args = args.OptionArg();
140  const int colon = static_cast<int>(server_args.rfind(":"));
141  if (colon != std::string::npos)
142  {
143  std::string host_str;
144  std::string port_str;
145  if (server_args.find("rorserver://") != String::npos) // Windows URI Scheme retuns rorserver://server:port/
146  {
147  host_str = server_args.substr(12, colon - 12);
148  port_str = server_args.substr(colon + 1, server_args.length() - colon - 2);
149  }
150  else
151  {
152  host_str = server_args.substr(0, colon);
153  port_str = server_args.substr(colon + 1, server_args.length());
154  }
155  App::cli_server_host->setStr(host_str.c_str());
156  App::cli_server_port->setVal(Ogre::StringConverter::parseInt(port_str));
157  }
158  }
159  }
160 }
161 
162 void Console::showCommandLineUsage()
163 {
165  _L("Command Line Arguments"),
166  _L("--help (this)" "\n"
167  "-map <map> (loads map on startup)" "\n"
168  "-pos <Vect> (overrides spawn position)" "\n"
169  "-rot <float> (overrides spawn rotation)" "\n"
170  "-truck <truck> (loads truck on startup)" "\n"
171  "-truckconfig <section>" "\n"
172  "-enter (player enters the selected truck)" "\n"
173  "-resume loads previous autosave" "\n"
174  "-checkcache forces cache update" "\n"
175  "-version shows the version information" "\n"
176  "-joinserver=<server>:<port> (join multiplayer server)" "\n"
177  "-runscript <filename> (load script, can be repeated)" "\n"
178  "For example: RoR.exe -map simple2 -pos '518 0 518' -rot 45 -truck semi.truck -enter"));
179 }
180 
181 void Console::showCommandLineVersion()
182 {
183  ErrorUtils::ShowInfo(_L("Version Information"), getVersionString());
184 #ifdef __GNUC__
185  printf(" * built with gcc %d.%d.%d\n", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
186 #endif //__GNUC__
187 }
188 
GameContext.h
Game state manager and message-queue provider.
RoR::getVersionString
Ogre::String getVersionString(bool multiline=true)
Definition: Utils.cpp:79
RoR::App::cli_force_cache_update
CVar * cli_force_cache_update
Definition: Application.cpp:184
colon
or anywhere else will not be considered a but parsed as regular data ! Each line is treated as values separated by separators Possible colon
Definition: ReadMe.txt:20
OPT_ROT
@ OPT_ROT
Definition: AppCommandLine.cpp:41
cmdline_options
CSimpleOpt::SOption cmdline_options[]
Definition: AppCommandLine.cpp:53
OPT_MAP
@ OPT_MAP
Definition: AppCommandLine.cpp:39
OPT_POS
@ OPT_POS
Definition: AppCommandLine.cpp:40
RoR::App::cli_resume_autosave
CVar * cli_resume_autosave
Definition: Application.cpp:185
OPT_TRUCK
@ OPT_TRUCK
Definition: AppCommandLine.cpp:42
RoR::App::cli_preset_vehicle
CVar * cli_preset_vehicle
Definition: Application.cpp:178
format
Truck file format(technical spec)
RoR::App::cli_server_host
CVar * cli_server_host
Definition: Application.cpp:176
Console.h
RoR::App::cli_preset_spawn_rot
CVar * cli_preset_spawn_rot
Definition: Application.cpp:182
Utils.h
OPT_ENTERTRUCK
@ OPT_ENTERTRUCK
Definition: AppCommandLine.cpp:48
OPT_RESUME
@ OPT_RESUME
Definition: AppCommandLine.cpp:44
ErrorUtils.h
RoR::App::app_state
CVar * app_state
Definition: Application.cpp:79
OPT_TRUCKCONFIG
@ OPT_TRUCKCONFIG
Definition: AppCommandLine.cpp:46
PlatformUtils.h
Platform-specific utilities. We use narrow UTF-8 encoded strings as paths. Inspired by http://utf8eve...
OPT_JOINMPSERVER
@ OPT_JOINMPSERVER
Definition: AppCommandLine.cpp:49
RoR::App::cli_custom_scripts
CVar * cli_custom_scripts
Definition: Application.cpp:186
RoR::App::cli_server_port
CVar * cli_server_port
Definition: Application.cpp:177
RoR::App::cli_preset_terrain
CVar * cli_preset_terrain
Definition: Application.cpp:180
OPT_VER
@ OPT_VER
Definition: AppCommandLine.cpp:43
OPT_HELP
@ OPT_HELP
Definition: AppCommandLine.cpp:38
RoR::CVar::setVal
void setVal(T val)
Definition: CVar.h:72
_L
#define _L
Definition: ErrorUtils.cpp:34
RoR::App::cli_preset_spawn_pos
CVar * cli_preset_spawn_pos
Definition: Application.cpp:181
RoR::App::cli_preset_veh_config
CVar * cli_preset_veh_config
Definition: Application.cpp:179
OPT_RUNSCRIPT
@ OPT_RUNSCRIPT
Definition: AppCommandLine.cpp:47
Ogre
Definition: ExtinguishableFireAffector.cpp:35
OPT_CHECKCACHE
@ OPT_CHECKCACHE
Definition: AppCommandLine.cpp:45
ErrorUtils::ShowInfo
static int ShowInfo(Ogre::UTFString title, Ogre::UTFString message)
shows a simple info message box
Definition: ErrorUtils.cpp:50
RoR
Definition: AppContext.h:36
RoR::CVar::setStr
void setStr(std::string const &str)
Definition: CVar.h:83
RoR::App::cli_preset_veh_enter
CVar * cli_preset_veh_enter
Definition: Application.cpp:183