FEATURE: Entity "stance" phase (fallback)
[Tsunagari.git] / src / client-conf.cpp
blobc22dc1c805aea5de79dd8794e61c202c0b0789c4
1 /****************************
2 ** Tsunagari Tile Engine **
3 ** client-conf.cpp **
4 ** Copyright 2012 OmegaSDG **
5 ****************************/
7 #include <iostream>
8 #include <fstream>
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"
17 #include "string.h"
19 Conf conf; // Project-wide global configuration.
21 Conf::Conf()
23 worldFilename = "";
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;
37 persistInit = 0;
38 persistCons = 0;
39 scriptHalt = false;
42 bool Conf::validate(const char* filename)
44 if (conf.worldFilename == "") {
45 Log::fatal(filename, "\"[engine] world\" option or equivalent command line option expected");
46 return false;
48 return true;
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;
78 /**
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);
95 if (!config) {
96 Log::err(filename, "could not parse config");
97 return false;
100 std::set<std::string> options;
101 std::map<std::string, std::string> parameters;
102 options.insert("*");
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"]);
127 else
128 conf.audioEnabled = true;
130 if (!parameters["cache.enabled"].empty()) {
131 if (parseBool(parameters["cache.enabled"]))
132 conf.cacheEnabled = true;
133 else
134 conf.cacheEnabled = false;
137 if (parameters["cache.ttl"].empty())
138 conf.cacheTTL = DEF_CACHE_TTL;
139 else {
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;
147 else {
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;
163 else {
164 Log::err(filename, "unknown value for \"[engine] verbosity\", using default");
167 return true;
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");
193 if (!cmd.parse()) {
194 cmd.usage();
195 return false;
198 std::vector<std::string> strayArgs = cmd.getStrayArgsList();
199 if (strayArgs.size() > 1) {
200 cmd.usage();
201 return false;
203 else if (strayArgs.size() == 1)
204 conf.worldFilename = strayArgs[0];
206 if (cmd.check("--help")) {
207 cmd.usage();
208 return false;
211 if (cmd.check("--version")) {
212 std::cout << TSUNAGARI_RELEASE_VERSION << std::endl;
213 return false;
216 if (cmd.check("--query")) {
217 defaultsQuery();
218 return false;
221 if (cmd.check("--config")) {
222 if (!parseConfig(cmd.get("--config").c_str()))
223 return false;
226 if (cmd.check("--datapath"))
227 conf.dataPath = splitStr(cmd.get("--datapath"), ",");
229 int verbcount = 0;
230 if (cmd.check("--quiet")) {
231 conf.verbosity = V_QUIET;
232 verbcount++;
234 if (cmd.check("--normal")) {
235 conf.verbosity = V_NORMAL;
236 verbcount++;
238 if (cmd.check("--verbose")) {
239 conf.verbosity = V_VERBOSE;
240 verbcount++;
242 if (verbcount > 1)
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");
264 return false;
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");
272 return false;
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;
284 return true;