1 /****************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2012 OmegaSDG **
5 ****************************/
10 #include <boost/config.hpp>
11 #include <boost/program_options.hpp>
12 #include <boost/program_options/detail/config_file.hpp>
13 #include <boost/program_options/parsers.hpp>
15 #include "client-conf.h"
16 #include "nbcl/nbcl.h"
19 Conf conf
; // Project-wide global configuration.
24 if (!strcmp(DEF_ENGINE_VERBOSITY
, "quiet"))
25 conf
.verbosity
= V_QUIET
;
26 else if (!strcmp(DEF_ENGINE_VERBOSITY
, "normal"))
27 conf
.verbosity
= V_NORMAL
;
28 else if (!strcmp(DEF_ENGINE_VERBOSITY
, "verbose"))
29 conf
.verbosity
= V_VERBOSE
;
30 windowSize
.x
= DEF_WINDOW_WIDTH
;
31 windowSize
.y
= DEF_WINDOW_HEIGHT
;
32 fullscreen
= DEF_WINDOW_FULLSCREEN
;
33 audioEnabled
= DEF_AUDIO_ENABLED
;
34 cacheEnabled
= DEF_CACHE_ENABLED
;
35 cacheTTL
= DEF_CACHE_TTL
;
36 cacheSize
= DEF_CACHE_SIZE
;
42 bool Conf::validate(const char* filename
)
44 if (conf
.worldFilename
== "") {
45 Log::fatal(filename
, "\"[engine] world\" option or equivalent command line option expected");
51 /* Output compiled-in engine defaults. */
52 static void defaultsQuery()
54 std::cerr
<< "CLIENT_CONF_PATH: "
55 << CLIENT_CONF_PATH
<< std::endl
;
56 std::cerr
<< "BASE_ZIP_PATH: "
57 << BASE_ZIP_PATH
<< std::endl
;
58 std::cerr
<< "XML_DTD_PATH: "
59 << XML_DTD_PATH
<< std::endl
;
60 std::cerr
<< "DEF_ENGINE_VERBOSITY: "
61 << DEF_ENGINE_VERBOSITY
<< std::endl
;
62 std::cerr
<< "DEF_WINDOW_WIDTH: "
63 << DEF_WINDOW_WIDTH
<< std::endl
;
64 std::cerr
<< "DEF_WINDOW_HEIGHT: "
65 << DEF_WINDOW_HEIGHT
<< std::endl
;
66 std::cerr
<< "DEF_WINDOW_FULLSCREEN: "
67 << DEF_WINDOW_FULLSCREEN
<< std::endl
;
68 std::cerr
<< "DEF_AUDIO_ENABLED: "
69 << DEF_AUDIO_ENABLED
<< std::endl
;
70 std::cerr
<< "DEF_CACHE_ENABLED: "
71 << DEF_CACHE_ENABLED
<< std::endl
;
72 std::cerr
<< "DEF_CACHE_TTL: "
73 << DEF_CACHE_TTL
<< std::endl
;
74 std::cerr
<< "DEF_CACHE_SIZE: "
75 << DEF_CACHE_SIZE
<< std::endl
;
79 * Load the values we need to start initializing the game from an ini file.
81 * We need to know what size window to create and which World to load. This
82 * information will be stored in an ini file which we parse here.
84 * @param filename Name of the ini file to load from.
86 * @return false if error occured during processing
88 bool parseConfig(const char* filename
)
90 namespace pod
= boost::program_options::detail
;
92 conf
.cacheEnabled
= DEF_CACHE_TTL
&& DEF_CACHE_SIZE
;
94 std::ifstream
config(filename
);
96 Log::err(filename
, "could not parse config");
100 std::set
<std::string
> options
;
101 std::map
<std::string
, std::string
> parameters
;
104 for (pod::config_file_iterator
i(config
, options
), e
; i
!= e
; ++i
)
105 parameters
[i
->string_key
] = i
->value
[0];
107 if (!parameters
["engine.world"].empty())
108 conf
.worldFilename
= parameters
["engine.world"];
110 if (!parameters
["engine.datapath"].empty())
111 conf
.dataPath
= splitStr(parameters
["engine.datapath"], ",");
113 if (!parameters
["engine.scripthalt"].empty())
114 conf
.scriptHalt
= parseBool(parameters
["engine.scripthalt"]);
116 if (!parameters
["window.width"].empty())
117 conf
.windowSize
.x
= atoi(parameters
["window.width"].c_str());
119 if (!parameters
["window.height"].empty())
120 conf
.windowSize
.y
= atoi(parameters
["window.height"].c_str());
122 if (!parameters
["window.fullscreen"].empty())
123 conf
.fullscreen
= parseBool(parameters
["window.fullscreen"]);
125 if (parameters
["audio.enabled"].size())
126 conf
.audioEnabled
= parseBool(parameters
["audio.enabled"]);
128 conf
.audioEnabled
= true;
130 if (!parameters
["cache.enabled"].empty()) {
131 if (parseBool(parameters
["cache.enabled"]))
132 conf
.cacheEnabled
= true;
134 conf
.cacheEnabled
= false;
137 if (parameters
["cache.ttl"].empty())
138 conf
.cacheTTL
= DEF_CACHE_TTL
;
140 if (atoi(parameters
["cache.ttl"].c_str()) == 0)
141 conf
.cacheEnabled
= 0;
142 conf
.cacheTTL
= atoi(parameters
["cache.ttl"].c_str());
145 if (parameters
["cache.size"].empty())
146 conf
.cacheSize
= DEF_CACHE_SIZE
;
148 if (atoi(parameters
["cache.size"].c_str()) == 0)
149 conf
.cacheEnabled
= 0;
150 conf
.cacheSize
= atoi(parameters
["cache.size"].c_str());
153 std::string verbosity
= parameters
["engine.verbosity"];
154 conf
.verbosity
= V_NORMAL
;
155 if (verbosity
.empty())
157 else if (verbosity
== "quiet")
158 conf
.verbosity
= V_QUIET
;
159 else if (verbosity
== "normal")
160 conf
.verbosity
= V_NORMAL
;
161 else if (verbosity
== "verbose")
162 conf
.verbosity
= V_VERBOSE
;
164 Log::err(filename
, "unknown value for \"[engine] verbosity\", using default");
170 //! Parse and process command line options and arguments.
171 bool parseCommandLine(int argc
, char* argv
[])
173 NBCL
cmd(argc
, argv
);
175 cmd
.setStrayArgsDesc("[WORLD FILE]");
177 cmd
.insert("-h", "--help", "", "Display this help message");
178 cmd
.insert("-c", "--config", "<config file>", "Client config file to use");
179 cmd
.insert("-p", "--datapath", "<file,file,...>", "Prepend zips to data path");
180 cmd
.insert("-q", "--quiet", "", "Display only fatal errors");
181 cmd
.insert("", "--normal", "", "Display all errors");
182 cmd
.insert("-v", "--verbose", "", "Display additional information");
183 cmd
.insert("-t", "--cache-ttl", "<seconds>", "Cache time-to-live in seconds");
184 cmd
.insert("-m", "--cache-size", "<megabytes>", "Cache size in megabytes");
185 cmd
.insert("-s", "--size", "<WxH>", "Window dimensions");
186 cmd
.insert("-f", "--fullscreen", "", "Run in fullscreen mode");
187 cmd
.insert("-w", "--window", "", "Run in windowed mode");
188 cmd
.insert("", "--script-halt", "", "Stop engine on script errors");
189 cmd
.insert("", "--no-audio", "", "Disable audio");
190 cmd
.insert("", "--query", "", "Query compiled-in engine defaults");
191 cmd
.insert("", "--version", "", "Print the engine version string");
198 std::vector
<std::string
> strayArgs
= cmd
.getStrayArgsList();
199 if (strayArgs
.size() > 1) {
203 else if (strayArgs
.size() == 1)
204 conf
.worldFilename
= strayArgs
[0];
206 if (cmd
.check("--help")) {
211 if (cmd
.check("--version")) {
212 std::cout
<< TSUNAGARI_RELEASE_VERSION
<< std::endl
;
216 if (cmd
.check("--query")) {
221 if (cmd
.check("--config")) {
222 if (!parseConfig(cmd
.get("--config").c_str()))
226 if (cmd
.check("--datapath"))
227 conf
.dataPath
= splitStr(cmd
.get("--datapath"), ",");
230 if (cmd
.check("--quiet")) {
231 conf
.verbosity
= V_QUIET
;
234 if (cmd
.check("--normal")) {
235 conf
.verbosity
= V_NORMAL
;
238 if (cmd
.check("--verbose")) {
239 conf
.verbosity
= V_VERBOSE
;
243 Log::err("cmdline", "multiple verbosity flags on cmdline, using most verbose");
245 if (cmd
.check("--no-audio"))
246 conf
.audioEnabled
= false;
248 if (cmd
.check("--cache-ttl")) {
249 conf
.cacheTTL
= atoi(cmd
.get("--cache-ttl").c_str());
250 if (conf
.cacheTTL
== 0)
251 conf
.cacheEnabled
= false;
254 if (cmd
.check("--cache-size")) {
255 conf
.cacheSize
= atoi(cmd
.get("--cache-size").c_str());
256 if (conf
.cacheSize
== 0)
257 conf
.cacheEnabled
= false;
260 if (cmd
.check("--size")) {
261 std::vector
<std::string
> dim
= splitStr(cmd
.get("--size"), "x");
262 if (dim
.size() != 2) {
263 Log::fatal("cmdline", "invalid argument for -s/--size");
266 conf
.windowSize
.x
= atoi(dim
[0].c_str());
267 conf
.windowSize
.y
= atoi(dim
[1].c_str());
270 if (cmd
.check("--fullscreen") && cmd
.check("--window")) {
271 Log::fatal("cmdline", "-f/--fullscreen and -w/--window mutually exclusive");
275 if (cmd
.check("--fullscreen"))
276 conf
.fullscreen
= true;
278 if (cmd
.check("--window"))
279 conf
.fullscreen
= false;
281 if (cmd
.check("--script-halt"))
282 conf
.scriptHalt
= true;