xoreos  0.0.5
cline.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cstdio>
26 #include <cstring>
27 
28 #include "src/cline.h"
29 
30 #include "src/version/version.h"
31 
32 #include "src/common/util.h"
33 #include "src/common/platform.h"
34 #include "src/common/configman.h"
35 
36 static void displayUsage(const Common::UString &name) {
37  std::printf("xoreos - A reimplementation of BioWare's Aurora engine\n");
38  std::printf("Usage: %s [options] [target]\n\n", name.c_str());
39  std::printf(" --help Display this text and exit.\n");
40  std::printf(" --version Display version information and exit.\n");
41  std::printf(" -cFILE --config=FILE Load the config from file FILE.\n");
42  std::printf(" -pDIR --path=DIR Override the game path with DIR.\n");
43  std::printf(" -wSIZE --width=SIZE Set the window's width to SIZE.\n");
44  std::printf(" -hSIZE --height=SIZE Set the window's height to SIZE.\n");
45  std::printf(" -fBOOL --fullscreen=BOOL Switch fullscreen on/off.\n");
46  std::printf(" -kBOOL --skipvideos=BOOL Disable videos on/off.\n");
47  std::printf(" -vVOL --volume=VOL Set global volume to VOL.\n");
48  std::printf(" -mVOL --volume_music=VOL Set music volume to VOL.\n");
49  std::printf(" -sVOL --volume_sfx=VOL Set SFX volume to VOL.\n");
50  std::printf(" -oVOL --volume_voice=VOL Set voice volume to VOL.\n");
51  std::printf(" -iVOL --volume_video=VOL Set video volume to VOL.\n");
52  std::printf(" -qLANG --lang=LANG Set the game's language.\n");
53  std::printf(" --langtext=LANG Set the game's text language.\n");
54  std::printf(" --langvoice=LANG Set the game's voice language.\n");
55  std::printf(" -dDLVL --debug=DLVL Set the debug channel verbosities.\n");
56  std::printf(" --debuggl=BOOL Create OpenGL debug context.\n");
57  std::printf(" --listdebug List all available debug channels.\n");
58  std::printf(" --listlangs List all available languages for this target.\n");
59  std::printf(" --saveconf=BOOL If false, never write to the config file.\n");
60  std::printf(" --logfile=FILE Write all debug output into this file too.\n");
61  std::printf(" --nologfile=BOOL Don't write a log file.\n");
62  std::printf(" --consolelog=FILE Write all debug console output into this file too.\n");
63  std::printf(" --noconsolelog=BOOL Don't write a debug console log file.\n");
64  std::printf("\n");
65  std::printf("FILE: Absolute or relative path to a file.\n");
66  std::printf("DIR: Absolute or relative path to a directory.\n");
67  std::printf("SIZE: A positive integer.\n");
68  std::printf("BOOL: \"true\", \"yes\", \"y\", \"on\" and \"1\" are true, everything else is false.\n");
69  std::printf("VOL: A double ranging from 0.0 (min) - 1.0 (max).\n");
70  std::printf("LANG: A language identifier. Full name, ISO 639-1 or ISO 639-2 language code;\n");
71  std::printf(" or IETF language tag with ISO 639-1 and ISO 3166-1 country code.\n");
72  std::printf(" Examples: en, de_de, hun, Czech, zh-tw, zh_cn, zh-cht, zh-chs.\n");
73  std::printf("DLVL: A comma-separated list of a debug channel name, a colon and the desired\n");
74  std::printf(" verbosity level. For example: GGraphics:4,GVideo:1,EEvents:9\n");
75  std::printf(" Use \"All\" to refer to all debug channels. OpenGL debug channels\n");
76  std::printf(" need the debuggl option to be enabled and require OpenGL driver support\n");
77  std::printf(" of the GL_ARB_debug_output extension.\n");
78  std::printf("\n");
79  std::printf("Examples:\n");
80  std::printf("%s -p/path/to/nwn/\n", name.c_str());
81  std::printf(" xoreos will start the game in /path/to/nwn/. Should a target with this\n");
82  std::printf(" path not yet exist in the config file, xoreos will create one named \"nwn\".\n");
83  std::printf("%s -p/path/to/nwn/ foobar\n", name.c_str());
84  std::printf(" xoreos will start the game in /path/to/nwn/. If a target \"foobar\"\n");
85  std::printf(" does not yet exist in the config file, xoreos will create it.\n");
86  std::printf("%s nwn\n", name.c_str());
87  std::printf(" xoreos will start the game specified by target \"nwn\", which must exit\n");
88  std::printf(" in the config file already.\n");
89  std::printf("\n");
90  std::printf("Configuration directory: %s\n", Common::Platform::getConfigDirectory().c_str());
91  std::printf("User data directory: %s\n", Common::Platform::getUserDataDirectory().c_str());
92  std::printf("Configuration file: %s\n", ConfigMan.getConfigFile().c_str());
93  std::printf("\n");
94 }
95 
96 static void displayVersion() {
97  std::printf("%s\n", Version::getProjectNameVersionFull());
98  std::printf("%s\n", Version::getProjectURL());
99  std::printf("\n");
100  std::printf("%s\n", Version::getProjectAuthors());
101  std::printf("\n");
102 }
103 
105  static const char * const kOptions[] = {
106  "p", "path",
107  "c", "config",
108  "f", "fullscreen",
109  "w", "width",
110  "h", "height",
111  "k", "skipvideos",
112  "v", "volume",
113  "m", "volume_music",
114  "s", "volume_sfx",
115  "o", "volume_voice",
116  "i", "volume_video",
117  "d", "debug",
118  "q", "lang"
119  };
120 
121  for (size_t i = 0; i < (ARRAYSIZE(kOptions) / 2); i++)
122  if (((uint32) kOptions[2 * i + 0][0]) == shortOption)
123  return kOptions[2 * i + 1];
124 
125  return "";
126 }
127 
128 static bool setOption(Common::UString &key, const Common::UString &value) {
129  if (key.equalsIgnoreCase("config")) {
130  ConfigMan.setConfigFile(value);
131  if (!ConfigMan.load()) {
132  if (!ConfigMan.fileExists())
133  warning("No such config file \"%s\"", value.c_str());
134  return false;
135  }
136 
137  key.clear();
138  return true;
139  }
140 
141  ConfigMan.setCommandlineKey(key, value);
142  key.clear();
143  return true;
144 }
145 
146 static bool parseOption(const Common::UString &arg, Common::UString &key) {
147  if (arg.size() < 2) {
148  warning("Unrecognized command line argument \"%s\"", arg.c_str());
149  return false;
150  }
151 
152  Common::UString::iterator start = arg.begin();
153  ++start;
154 
155  Common::UString value;
156  if (*start == '-') {
157  // Long option
158 
159  ++start;
160 
162  if (e != arg.end()) {
163  key = arg.substr(start, e++);
164  value = arg.substr(e, arg.end());
165  } else
166  key = arg.substr(start, arg.end());
167 
168  } else {
169  // Short option
170 
171  key = convertShortToLongOption(*start++);
172  value = arg.substr(start, arg.end());
173  }
174 
175  if (key.empty()) {
176  warning("Unrecognized command line argument \"%s\"", arg.c_str());
177  return false;
178  }
179 
180  if (value.empty())
181  return true;
182 
183  if (!setOption(key, value))
184  return false;
185 
186  return true;
187 }
188 
189 bool parseCommandline(const std::vector<Common::UString> &argv, Common::UString &target, int &code) {
190  target.clear();
191 
192  Common::UString key;
193 
194  bool stopMark = false;
195  for (size_t i = 1; i < argv.size(); i++) {
196 
197  if (!key.empty()) {
198  // Still got one parameter missing from last time
199  if (!setOption(key, argv[i])) {
200  code = 1;
201  return false;
202  }
203 
204  continue;
205  }
206 
207  if (!stopMark && argv[i].beginsWith("-")) {
208  if (argv[i] == "--") {
209  stopMark = true;
210  continue;
211  }
212 
213  if (argv[i] == "--help") {
214  code = 0;
215  displayUsage(argv[0]);
216  return false;
217  }
218 
219  if (argv[i] == "--version") {
220  code = 0;
221  displayVersion();
222  return false;
223  }
224 
225  if (!parseOption(argv[i], key)) {
226  code = 1;
227  return false;
228  }
229 
230  if (key == "listdebug") {
231  setOption(key, "true");
232  key.clear();
233  }
234 
235  if (key == "listlangs") {
236  setOption(key, "true");
237  key.clear();
238  }
239 
240  continue;
241  }
242 
243  if (!target.empty()) {
244  warning("Found multiple target (\"%s\" and \"%s\")", target.c_str(), argv[i].c_str());
245  code = 1;
246  return false;
247  }
248 
249  target = argv[i];
250  }
251 
252  if (target.empty() && !ConfigMan.hasKey("path") && !ConfigMan.getBool("listdebug", false)) {
253  displayUsage(argv[0]);
254  code = 1;
255  return false;
256  }
257 
258  return true;
259 }
bool parseCommandline(const std::vector< Common::UString > &argv, Common::UString &target, int &code)
Definition: cline.cpp:189
A class holding an UTF-8 string.
Definition: ustring.h:48
The global config manager.
static void displayVersion()
Definition: cline.cpp:96
static UString getUserDataDirectory()
Return the OS-specific path of the user data directory.
Definition: platform.cpp:266
bool equalsIgnoreCase(const UString &str) const
Definition: ustring.cpp:218
iterator begin() const
Definition: ustring.cpp:253
#define ARRAYSIZE(x)
Macro which determines the number of entries in a fixed size array.
Definition: util.h:131
Command line arguments parser.
static bool setOption(Common::UString &key, const Common::UString &value)
Definition: cline.cpp:128
iterator findFirst(uint32 c) const
Definition: ustring.cpp:261
Platform-dependant functions, mostly for internal use in the Common namespace.
static UString getConfigDirectory()
Return the OS-specific path of the config directory.
Definition: platform.cpp:206
UString substr(iterator from, iterator to) const
Definition: ustring.cpp:706
utf8::iterator< std::string::const_iterator > iterator
Definition: ustring.h:50
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
Utility templates and functions.
Basic xoreos version information.
static void displayUsage(const Common::UString &name)
Definition: cline.cpp:36
const char * getProjectAuthors()
Definition: version.cpp:104
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
void warning(const char *s,...)
Definition: util.cpp:33
const char * getProjectURL()
Definition: version.cpp:100
const char * getProjectNameVersionFull()
Definition: version.cpp:96
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
uint32_t uint32
Definition: types.h:204
static bool parseOption(const Common::UString &arg, Common::UString &key)
Definition: cline.cpp:146
static Common::UString convertShortToLongOption(uint32 shortOption)
Definition: cline.cpp:104
iterator end() const
Definition: ustring.cpp:257
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236