ManaPlus
Static Public Member Functions
ConfigManager Class Reference

#include <configmanager.h>

Static Public Member Functions

static void initConfiguration ()
 
static void initServerConfig (const std::string &serverName)
 
static void storeSafeParameters ()
 
static void backupConfig (const std::string &name)
 
static void checkConfigVersion ()
 

Detailed Description

Definition at line 31 of file configmanager.h.

Member Function Documentation

◆ backupConfig()

void ConfigManager::backupConfig ( const std::string &  name)
static

Definition at line 179 of file configmanager.cpp.

180 {
181  const std::string fileName3 = pathJoin(settings.configDir, name);
182  StringVect arr;
183  if (Files::existsLocal(fileName3) == false)
184  {
185  logger->log("Config %s not exists, backup skipped.",
186  name.c_str());
187  return;
188  }
189  if (Files::loadTextFileLocal(fileName3, arr) == true)
190  {
191  if (arr.empty())
192  return;
193 
194  arr.clear();
195  const std::string tmpName = pathJoin(settings.configDir,
196  name).append(".tmp");
197  Files::copyFile(fileName3, tmpName);
198  if (Files::loadTextFileLocal(tmpName, arr) == false ||
199  arr.empty())
200  {
201  logger->safeError("Error backuping configs. "
202  "Probably no free space on disk.");
203  }
204  arr.clear();
205  }
206 
207  const std::string confName = pathJoin(settings.configDir,
208  name).append(".bak");
209  const int maxFileIndex = 5;
210  ::remove((confName + toString(maxFileIndex)).c_str());
211  for (int f = maxFileIndex; f > 1; f --)
212  {
213  const std::string fileName1 = confName + toString(f - 1);
214  const std::string fileName2 = confName + toString(f);
215  Files::renameFile(fileName1, fileName2);
216  }
217  const std::string fileName4 = confName + toString(1);
218  Files::copyFile(fileName3, fileName4);
219 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
void safeError(const std::string &error_text) __attribute__((noreturn))
Definition: logger.cpp:435
std::string configDir
Definition: settings.h:111
Logger * logger
Definition: logger.cpp:89
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
bool loadTextFileLocal(const std::string &fileName, StringVect &lines)
Definition: files.cpp:229
int renameFile(const std::string &pFrom, const std::string &pTo)
Definition: files.cpp:134
int copyFile(const std::string &pFrom, const std::string &pTo)
Definition: files.cpp:176
bool existsLocal(const std::string &path)
Definition: files.cpp:209
bool remove(const std::string &filename)
Definition: fs.cpp:780
Settings settings
Definition: settings.cpp:32
std::string pathJoin(std::string str1, const std::string &str2)
std::vector< std::string > StringVect
Definition: stringvector.h:29

References Settings::configDir, Files::copyFile(), Files::existsLocal(), Files::loadTextFileLocal(), Logger::log(), logger, pathJoin(), VirtFs::remove(), Files::renameFile(), Logger::safeError(), settings, and Catch::toString().

Referenced by Client::gameInit().

◆ checkConfigVersion()

void ConfigManager::checkConfigVersion ( )
static

Definition at line 371 of file configmanager.cpp.

372 {
373  const int version = config.getIntValue("cfgver");
374  if (version < 1)
375  {
376  if (config.getIntValue("fontSize") == 11)
377  config.deleteKey("fontSize");
378  if (config.getIntValue("npcfontSize") == 13)
379  config.deleteKey("npcfontSize");
380  }
381  if (version < 2)
382  {
383  if (config.getIntValue("screenButtonsSize") == 1)
384  config.deleteKey("screenButtonsSize");
385  if (config.getIntValue("screenJoystickSize") == 1)
386  config.deleteKey("screenJoystickSize");
387  }
388  if (version < 3)
389  {
390  config.setValue("audioFrequency", 44100);
391 #ifdef ANDROID
392  config.setValue("customcursor", false);
393 #endif // ANDROID
394  }
395 #ifdef ANDROID
396  if (version < 4)
397  {
398  config.setValue("showDidYouKnow", false);
399  }
400 #endif // ANDROID
401 
402  if (version < 5)
403  {
405  {
406  config.setValue("speech", CAST_S32(
408  }
409  }
410  if (version < 6)
411  config.setValue("blur", false);
412 
413  if (version < 7)
414  config.setValue("download-music", true);
415 
416  if (version < 9)
417  {
418  config.deleteKey("videodetected");
419  config.setValue("moveToTargetType", 10);
420  }
421  if (version < 10)
422  config.setValue("enableLazyScrolling", false);
423 
424  if (version < 11)
425  {
426 #ifdef USE_SDL2
427  const std::string prefix = std::string("sdl2");
428 #else // USE_SDL2
429 
430  const std::string prefix = std::string();
431 #endif // USE_SDL2
432 
433  unassignKey("keyDirectUp", "k108")
434  unassignKey("keyDirectDown", "k59")
435  unassignKey("keyDirectLeft", "k107")
436  unassignKey("keyDirectRight", "k39")
437  }
438  if (version < 12)
439  {
440 #ifdef USE_SDL2
441  const std::string prefix = std::string("sdl2");
442 #else // USE_SDL2
443 
444  const std::string prefix = std::string();
445 #endif // USE_SDL2
446 
447  unassignKey("keyAttack", "k120")
448  }
449 
450  if (version < 13)
451  config.setValue("keyWindowBotChecker", -1);
452 
453  if (version < 14 && config.getIntValue("syncPlayerMoveDistance") == 2)
454  config.setValue("syncPlayerMoveDistance", 5);
455  config.setValue("cfgver", 14);
456 }
#define CAST_S32
Definition: cast.h:30
void deleteKey(const std::string &key)
void setValue(const std::string &key, const std::string &value)
int getIntValue(const std::string &key) const
#define unassignKey(key, value)
Configuration config
if(!vert) return
@ NO_NAME_IN_BUBBLE
Definition: beingspeech.h:33

References CAST_S32, config, ConfigurationObject::deleteKey(), Configuration::getIntValue(), BeingSpeech::NO_NAME_IN_BUBBLE, Configuration::setValue(), BeingSpeech::TEXT_OVERHEAD, and unassignKey.

Referenced by Client::gameInit().

◆ initConfiguration()

void ConfigManager::initConfiguration ( )
static

Definition at line 99 of file configmanager.cpp.

100 {
101 #ifdef DEBUG_CONFIG
102  config.setIsMain(true);
103 #endif // DEBUG_CONFIG
104 
105  // Fill configuration with defaults
106  config.setValue("hwaccel", false);
107 #ifdef USE_OPENGL
108 #if (defined __APPLE__)
110 #elif (defined ANDROID)
112 #elif (defined WIN32)
114 #else // (defined __APPLE__)
115 
117 #endif // (defined __APPLE__)
118 #else // USE_OPENGL
119 
121 #endif // USE_OPENGL
122 
123  config.setValue("screen", false);
124  config.setValue("sound", true);
125  config.setValue("guialpha", 0.8F);
126 // config.setValue("remember", true);
127  config.setValue("sfxVolume", 100);
128  config.setValue("musicVolume", 60);
129  config.setValue("fpslimit", 60);
130  std::string defaultUpdateHost = branding.getValue("defaultUpdateHost", "");
131  if (!checkPath(defaultUpdateHost))
132  defaultUpdateHost.clear();
133  config.setValue("updatehost", defaultUpdateHost);
134  config.setValue("useScreenshotDirectorySuffix", true);
135  config.setValue("ChatLogLength", 128);
136 
137  std::string configPath;
138 
139 #ifndef UNITTESTS
140  if (settings.options.test.empty())
141  configPath = settings.configDir + "/config.xml";
142  else
143  configPath = settings.configDir + "/test.xml";
144 #else // UNITTESTS
145 
146  configPath = settings.configDir + "/unittestconfig.xml";
147 #endif // UNITTESTS
148 
149  FILE *configFile = fopen(configPath.c_str(), "r");
150  if (configFile == nullptr)
151  {
152  configFile = fopen(configPath.c_str(), "wb");
153  logger->log1("Creating new config");
154  if (configFile != nullptr)
155  {
156  fputs("<?xml version=\"1.0\"?>\n", configFile);
157  fputs("<configuration>\n", configFile);
158  fputs("</configuration>\n", configFile);
159  }
160  }
161  if (configFile == nullptr)
162  {
163  reportAlways("Can't create %s. Using defaults.",
164  configPath.c_str())
165  }
166  else
167  {
168  fclose(configFile);
169  config.init(configPath,
172  logger->log1("init 3");
175  logger->log("configuration file: " + configPath);
176  }
177 }
#define reportAlways(...)
Definition: checkutils.h:253
std::string getValue(const std::string &key, const std::string &deflt) const
void init(const std::string &filename, const UseVirtFs useResManager, const SkipError skipError)
void log1(const char *const log_text)
Definition: logger.cpp:238
Options options
Definition: settings.h:130
Configuration serverConfig
Configuration branding
void setConfigDefaults(Configuration &cfg)
Definition: defaults.cpp:90
bool checkPath(const std::string &path)
Definition: paths.cpp:121
@ RENDER_SAFE_OPENGL
Definition: rendertype.h:29
@ RENDER_GLES_OPENGL
Definition: rendertype.h:30
@ RENDER_NORMAL_OPENGL
Definition: rendertype.h:28
@ RENDER_SOFTWARE
Definition: rendertype.h:27
const bool SkipError_false
Definition: skiperror.h:30
std::string test
Definition: options.h:88
const bool UseVirtFs_false
Definition: usevirtfs.h:30

References branding, CAST_S32, checkPath(), config, Settings::configDir, ConfigurationObject::getValue(), Configuration::init(), Logger::log(), Logger::log1(), logger, Settings::options, RENDER_GLES_OPENGL, RENDER_NORMAL_OPENGL, RENDER_SAFE_OPENGL, RENDER_SOFTWARE, reportAlways, serverConfig, setConfigDefaults(), settings, Configuration::setValue(), SkipError_false, Options::test, and UseVirtFs_false.

Referenced by Client::gameInit().

◆ initServerConfig()

void ConfigManager::initServerConfig ( const std::string &  serverName)
static

Initializes the home directory. On UNIX and FreeBSD, ~/.mana is used. On Windows and other systems we use the current working directory.

Definition at line 56 of file configmanager.cpp.

57 {
59 
60  if (mkdir_r(settings.serverConfigDir.c_str()) != 0)
61  {
62  // TRANSLATORS: directory creation error
63  logger->error(strprintf(_("%s doesn't exist and can't be created! "
64  "Exiting."), settings.serverConfigDir.c_str()));
65  }
66  const std::string configPath = settings.serverConfigDir + "/config.xml";
67  FILE *configFile = fopen(configPath.c_str(), "r");
68  if (configFile == nullptr)
69  {
70  configFile = fopen(configPath.c_str(), "wb");
71  logger->log("Creating new server config: " + configPath);
72  if (configFile != nullptr)
73  {
74  fputs("<?xml version=\"1.0\"?>\n", configFile);
75  fputs("<configuration>\n", configFile);
76  fputs("</configuration>\n", configFile);
77  }
78  }
79  if (configFile != nullptr)
80  {
81  fclose(configFile);
82  serverConfig.init(configPath,
86  logger->log("serverConfigPath: " + configPath);
87  }
88  else
89  {
90  reportAlways("Error creating server config: %s",
91  configPath.c_str())
92  }
93 
94  const bool val = Client::isTmw();
95  setDefaultOption("enableManaMarketBot", val);
96  setDefaultOption("enableRemoteCommands", !val);
97 }
static bool isTmw()
Definition: client.cpp:838
void error(const std::string &error_text) __attribute__((noreturn))
Definition: logger.cpp:472
std::string serverConfigDir
Definition: settings.h:117
static void setDefaultOption(const char *const name, const bool def)
#define _(s)
Definition: gettext.h:35
int mkdir_r(const char *const pathname)
Create a directory, making leading components first if necessary.
Definition: mkdir.cpp:109
std::string strprintf(const char *const format,...)

References _, Settings::configDir, Logger::error(), Configuration::init(), Client::isTmw(), Logger::log(), logger, mkdir_r(), pathJoin(), reportAlways, serverConfig, Settings::serverConfigDir, setConfigDefaults(), setDefaultOption(), settings, SkipError_false, strprintf(), and UseVirtFs_false.

Referenced by Client::stateConnectServer1().

◆ storeSafeParameters()

void ConfigManager::storeSafeParameters ( )
static

Definition at line 246 of file configmanager.cpp.

247 {
248  bool tmpHwaccel;
249  RenderType tmpOpengl;
250  int tmpFpslimit;
251  int tmpAltFpslimit;
252  bool tmpSound;
253  int width;
254  int height;
255  std::string font;
256  std::string bFont;
257  std::string particleFont;
258  std::string helpFont;
259  std::string secureFont;
260  std::string npcFont;
261  std::string japanFont;
262  std::string chinaFont;
263  bool showBackground;
264  bool enableMumble;
265  bool enableMapReduce;
266 
267  isSafeMode = config.getBoolValue("safemode");
268  if (isSafeMode)
269  logger->log1("Run in safe mode");
270 
271  tmpOpengl = intToRenderType(config.getIntValue("opengl"));
272 
273  width = config.getIntValue("screenwidth");
274  height = config.getIntValue("screenheight");
275  tmpHwaccel = config.getBoolValue("hwaccel");
276 
277  tmpFpslimit = config.getIntValue("fpslimit");
278  tmpAltFpslimit = config.getIntValue("altfpslimit");
279  tmpSound = config.getBoolValue("sound");
280 
281  font = config.getStringValue("font");
282  bFont = config.getStringValue("boldFont");
283  particleFont = config.getStringValue("particleFont");
284  helpFont = config.getStringValue("helpFont");
285  secureFont = config.getStringValue("secureFont");
286  npcFont = config.getStringValue("npcFont");
287  japanFont = config.getStringValue("japanFont");
288  chinaFont = config.getStringValue("chinaFont");
289 
290  showBackground = config.getBoolValue("showBackground");
291  enableMumble = config.getBoolValue("enableMumble");
292  enableMapReduce = config.getBoolValue("enableMapReduce");
293 
294  if (!settings.options.safeMode && tmpOpengl == RENDER_SOFTWARE)
295  {
296  // if video mode configured reset most settings to safe
297  config.setValue("hwaccel", false);
298  config.setValue("altfpslimit", 3);
299  config.setValue("sound", false);
300  config.setValue("safemode", true);
301  config.setValue("screenwidth", 640);
302  config.setValue("screenheight", 480);
303  config.setValue("font", "fonts/dejavusans.ttf");
304  config.setValue("boldFont", "fonts/dejavusans-bold.ttf");
305  config.setValue("particleFont", "fonts/dejavusans.ttf");
306  config.setValue("helpFont", "fonts/dejavusansmono.ttf");
307  config.setValue("secureFont", "fonts/dejavusansmono.ttf");
308  config.setValue("npcFont", "fonts/dejavusans.ttf");
309  config.setValue("japanFont", "fonts/mplus-1p-regular.ttf");
310  config.setValue("chinaFont", "fonts/wqy-microhei.ttf");
311  config.setValue("showBackground", false);
312  config.setValue("enableMumble", false);
313  config.setValue("enableMapReduce", false);
314  }
315  else
316  {
317  // if video mode not configured reset only video mode to safe
318  config.setValue("screenwidth", 640);
319  config.setValue("screenheight", 480);
320  }
321 #if defined(__APPLE__)
323 #else // defined(__APPLE__)
324 
326 #endif // defined(__APPLE__)
327 
328  config.write();
329 
331  {
332  isSafeMode = true;
333  return;
334  }
335 
336  config.setValue("safemode", false);
337  if (tmpOpengl == RENDER_SOFTWARE)
338  {
339  config.setValue("hwaccel", tmpHwaccel);
340  config.setValue("opengl", CAST_S32(tmpOpengl));
341  config.setValue("fpslimit", tmpFpslimit);
342  config.setValue("altfpslimit", tmpAltFpslimit);
343  config.setValue("sound", tmpSound);
344  config.setValue("screenwidth", width);
345  config.setValue("screenheight", height);
346  config.setValue("font", font);
347  config.setValue("boldFont", bFont);
348  config.setValue("particleFont", particleFont);
349  config.setValue("helpFont", helpFont);
350  config.setValue("secureFont", secureFont);
351  config.setValue("npcFont", npcFont);
352  config.setValue("japanFont", japanFont);
353  config.setValue("chinaFont", chinaFont);
354  config.setValue("showBackground", showBackground);
355  config.setValue("enableMumble", enableMumble);
356  config.setValue("enableMapReduce", enableMapReduce);
357  }
358  else
359  {
360  config.setValue("opengl", CAST_S32(tmpOpengl));
361  config.setValue("screenwidth", width);
362  config.setValue("screenheight", height);
363  }
364 }
bool getBoolValue(const std::string &key) const
std::string getStringValue(const std::string &key) const
bool isSafeMode
Definition: client.cpp:123
RenderType intToRenderType(const int mode)
Definition: renderers.cpp:43
RenderType
Definition: rendertype.h:26
bool safeMode
Definition: options.h:98

References CAST_S32, config, Configuration::getBoolValue(), Configuration::getIntValue(), Configuration::getStringValue(), intToRenderType(), isSafeMode, Logger::log1(), logger, Settings::options, RENDER_NORMAL_OPENGL, RENDER_SOFTWARE, Options::safeMode, settings, Configuration::setValue(), and Configuration::write().

Referenced by Client::gameInit().


The documentation for this class was generated from the following files: