summaryrefslogtreecommitdiff
path: root/src/configmanager.cpp
blob: 325faa0f8ce1bbdeb2dd9511f9914b27681fb4ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 *  The ManaVerse Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2019  The ManaPlus Developers
 *
 *  This file is part of The ManaVerse Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "configmanager.h"

#include "client.h"
#include "configuration.h"
#include "settings.h"

#include "being/beingspeech.h"

#include "fs/files.h"
#include "fs/mkdir.h"
#include "fs/paths.h"

#include "utils/cast.h"
#include "utils/checkutils.h"
#include "utils/gettext.h"

#include "render/renderers.h"

#include "debug.h"

static void setDefaultOption(const char *const name,
                             const bool def)
{
    const int val = serverConfig.getValue(name, -1);
    if (val == -1)
        serverConfig.setValue(name, def);
}

/**
 * Initializes the home directory. On UNIX and FreeBSD, ~/.mana is used. On
 * Windows and other systems we use the current working directory.
 */
void ConfigManager::initServerConfig(const std::string &serverName)
{
    settings.serverConfigDir = pathJoin(settings.configDir, serverName);

    if (mkdir_r(settings.serverConfigDir.c_str()) != 0)
    {
        // TRANSLATORS: directory creation error
        logger->error(strprintf(_("%s doesn't exist and can't be created! "
            "Exiting."), settings.serverConfigDir.c_str()));
    }
    const std::string configPath = settings.serverConfigDir + "/config.xml";
    FILE *configFile = fopen(configPath.c_str(), "r");
    if (configFile == nullptr)
    {
        configFile = fopen(configPath.c_str(), "wb");
        logger->log("Creating new server config: " + configPath);
        if (configFile != nullptr)
        {
            fputs("<?xml version=\"1.0\"?>\n", configFile);
            fputs("<configuration>\n", configFile);
            fputs("</configuration>\n", configFile);
        }
    }
    if (configFile != nullptr)
    {
        fclose(configFile);
        serverConfig.init(configPath,
            UseVirtFs_false,
            SkipError_false);
        setConfigDefaults(serverConfig);
        logger->log("serverConfigPath: " + configPath);
    }
    else
    {
        reportAlways("Error creating server config: %s",
            configPath.c_str())
    }

    const bool val = Client::isTmw();
    setDefaultOption("enableManaMarketBot", val);
    setDefaultOption("enableRemoteCommands", !val);
}

void ConfigManager::initConfiguration()
{
#ifdef DEBUG_CONFIG
    config.setIsMain(true);
#endif  // DEBUG_CONFIG

    // Fill configuration with defaults
    config.setValue("hwaccel", false);
#ifdef USE_OPENGL
#if (defined __APPLE__)
    config.setValue("opengl", CAST_S32(RENDER_NORMAL_OPENGL));
#elif (defined ANDROID)
    config.setValue("opengl", CAST_S32(RENDER_GLES_OPENGL));
#elif (defined WIN32)
    config.setValue("opengl", CAST_S32(RENDER_SAFE_OPENGL));
#else  // (defined __APPLE__)

    config.setValue("opengl", CAST_S32(RENDER_SOFTWARE));
#endif  // (defined __APPLE__)
#else  // USE_OPENGL

    config.setValue("opengl", CAST_S32(RENDER_SOFTWARE));
#endif  // USE_OPENGL

    config.setValue("screen", false);
    config.setValue("sound", true);
    config.setValue("guialpha", 0.8F);
//    config.setValue("remember", true);
    config.setValue("sfxVolume", 100);
    config.setValue("musicVolume", 60);
    config.setValue("fpslimit", 60);
    std::string defaultUpdateHost = branding.getValue("defaultUpdateHost", "");
    if (!checkPath(defaultUpdateHost))
        defaultUpdateHost.clear();
    config.setValue("updatehost", defaultUpdateHost);
    config.setValue("useScreenshotDirectorySuffix", true);
    config.setValue("ChatLogLength", 128);

    std::string configPath;

#ifndef UNITTESTS
    if (settings.options.test.empty())
        configPath = settings.configDir + "/config.xml";
    else
        configPath = settings.configDir + "/test.xml";
#else  // UNITTESTS

    configPath = settings.configDir + "/unittestconfig.xml";
#endif  // UNITTESTS

    FILE *configFile = fopen(configPath.c_str(), "r");
    if (configFile == nullptr)
    {
        configFile = fopen(configPath.c_str(), "wb");
        logger->log1("Creating new config");
        if (configFile != nullptr)
        {
            fputs("<?xml version=\"1.0\"?>\n", configFile);
            fputs("<configuration>\n", configFile);
            fputs("</configuration>\n", configFile);
        }
    }
    if (configFile == nullptr)
    {
        reportAlways("Can't create %s. Using defaults.",
            configPath.c_str())
    }
    else
    {
        fclose(configFile);
        config.init(configPath,
            UseVirtFs_false,
            SkipError_false);
        logger->log1("init 3");
        setConfigDefaults(config);
        setConfigDefaults(serverConfig);
        logger->log("configuration file: " + configPath);
    }
}

void ConfigManager::backupConfig(const std::string &name)
{
    const std::string fileName3 = pathJoin(settings.configDir, name);
    StringVect arr;
    if (Files::existsLocal(fileName3) == false)
    {
        logger->log("Config %s not exists, backup skipped.",
            name.c_str());
        return;
    }
    if (Files::loadTextFileLocal(fileName3, arr) == true)
    {
        if (arr.empty())
            return;

        arr.clear();
        const std::string tmpName = pathJoin(settings.configDir,
            name).append(".tmp");
        Files::copyFile(fileName3, tmpName);
        if (Files::loadTextFileLocal(tmpName, arr) == false ||
            arr.empty())
        {
            logger->safeError("Error backuping configs. "
                "Probably no free space on disk.");
        }
        arr.clear();
    }

    const std::string confName = pathJoin(settings.configDir,
        name).append(".bak");
    const int maxFileIndex = 5;
    ::remove((confName + toString(maxFileIndex)).c_str());
    for (int f = maxFileIndex; f > 1; f --)
    {
        const std::string fileName1 = confName + toString(f - 1);
        const std::string fileName2 = confName + toString(f);
        Files::renameFile(fileName1, fileName2);
    }
    const std::string fileName4 = confName + toString(1);
    Files::copyFile(fileName3, fileName4);
}

#ifdef __native_client__
void ConfigManager::storeSafeParameters()
{
    RenderType tmpOpengl;

    isSafeMode = config.getBoolValue("safemode");
    if (isSafeMode)
        logger->log1("Run in safe mode");

    tmpOpengl = intToRenderType(config.getIntValue("opengl"));

    config.setValue("opengl", CAST_S32(RENDER_SOFTWARE));

    config.write();

    if (settings.options.safeMode)
    {
        isSafeMode = true;
        return;
    }

    config.setValue("safemode", false);
    config.setValue("opengl", CAST_S32(tmpOpengl));
}
#elif !defined(ANDROID)
void ConfigManager::storeSafeParameters()
{
    bool tmpHwaccel;
    RenderType tmpOpengl;
    int tmpFpslimit;
    int tmpAltFpslimit;
    bool tmpSound;
    int width;
    int height;
    std::string font;
    std::string bFont;
    std::string particleFont;
    std::string helpFont;
    std::string secureFont;
    std::string npcFont;
    std::string japanFont;
    std::string chinaFont;
    bool showBackground;
    bool enableMumble;
    bool enableMapReduce;

    isSafeMode = config.getBoolValue("safemode");
    if (isSafeMode)
        logger->log1("Run in safe mode");

    tmpOpengl = intToRenderType(config.getIntValue("opengl"));

    width = config.getIntValue("screenwidth");
    height = config.getIntValue("screenheight");
    tmpHwaccel = config.getBoolValue("hwaccel");

    tmpFpslimit = config.getIntValue("fpslimit");
    tmpAltFpslimit = config.getIntValue("altfpslimit");
    tmpSound = config.getBoolValue("sound");

    font = config.getStringValue("font");
    bFont = config.getStringValue("boldFont");
    particleFont = config.getStringValue("particleFont");
    helpFont = config.getStringValue("helpFont");
    secureFont = config.getStringValue("secureFont");
    npcFont = config.getStringValue("npcFont");
    japanFont = config.getStringValue("japanFont");
    chinaFont = config.getStringValue("chinaFont");

    showBackground = config.getBoolValue("showBackground");
    enableMumble = config.getBoolValue("enableMumble");
    enableMapReduce = config.getBoolValue("enableMapReduce");

    if (!settings.options.safeMode && tmpOpengl == RENDER_SOFTWARE)
    {
        // if video mode configured reset most settings to safe
        config.setValue("hwaccel", false);
        config.setValue("altfpslimit", 3);
        config.setValue("sound", false);
        config.setValue("safemode", true);
        config.setValue("screenwidth", 640);
        config.setValue("screenheight", 480);
        config.setValue("font", "fonts/dejavusans.ttf");
        config.setValue("boldFont", "fonts/dejavusans-bold.ttf");
        config.setValue("particleFont", "fonts/dejavusans.ttf");
        config.setValue("helpFont", "fonts/dejavusansmono.ttf");
        config.setValue("secureFont", "fonts/dejavusansmono.ttf");
        config.setValue("npcFont", "fonts/dejavusans.ttf");
        config.setValue("japanFont", "fonts/mplus-1p-regular.ttf");
        config.setValue("chinaFont", "fonts/wqy-microhei.ttf");
        config.setValue("showBackground", false);
        config.setValue("enableMumble", false);
        config.setValue("enableMapReduce", false);
    }
    else
    {
        // if video mode not configured reset only video mode to safe
        config.setValue("screenwidth", 640);
        config.setValue("screenheight", 480);
    }
#if defined(__APPLE__)
    config.setValue("opengl", CAST_S32(RENDER_NORMAL_OPENGL));
#else  // defined(__APPLE__)

    config.setValue("opengl", CAST_S32(RENDER_SOFTWARE));
#endif  // defined(__APPLE__)

    config.write();

    if (settings.options.safeMode)
    {
        isSafeMode = true;
        return;
    }

    config.setValue("safemode", false);
    if (tmpOpengl == RENDER_SOFTWARE)
    {
        config.setValue("hwaccel", tmpHwaccel);
        config.setValue("opengl", CAST_S32(tmpOpengl));
        config.setValue("fpslimit", tmpFpslimit);
        config.setValue("altfpslimit", tmpAltFpslimit);
        config.setValue("sound", tmpSound);
        config.setValue("screenwidth", width);
        config.setValue("screenheight", height);
        config.setValue("font", font);
        config.setValue("boldFont", bFont);
        config.setValue("particleFont", particleFont);
        config.setValue("helpFont", helpFont);
        config.setValue("secureFont", secureFont);
        config.setValue("npcFont", npcFont);
        config.setValue("japanFont", japanFont);
        config.setValue("chinaFont", chinaFont);
        config.setValue("showBackground", showBackground);
        config.setValue("enableMumble", enableMumble);
        config.setValue("enableMapReduce", enableMapReduce);
    }
    else
    {
        config.setValue("opengl", CAST_S32(tmpOpengl));
        config.setValue("screenwidth", width);
        config.setValue("screenheight", height);
    }
}
#endif  // __native_client__

#define unassignKey(key, value) \
    if (config.getStringValue(prefix + (key)) == (value)) \
        config.setValue(key, "-1");

void ConfigManager::checkConfigVersion()
{
    const int version = config.getIntValue("cfgver");
    if (version < 1)
    {
        if (config.getIntValue("fontSize") == 11)
            config.deleteKey("fontSize");
        if (config.getIntValue("npcfontSize") == 13)
            config.deleteKey("npcfontSize");
    }
    if (version < 2)
    {
        if (config.getIntValue("screenButtonsSize") == 1)
            config.deleteKey("screenButtonsSize");
        if (config.getIntValue("screenJoystickSize") == 1)
            config.deleteKey("screenJoystickSize");
    }
    if (version < 3)
    {
        config.setValue("audioFrequency", 44100);
#ifdef ANDROID
        config.setValue("customcursor", false);
#endif  // ANDROID
    }
#ifdef ANDROID
    if (version < 4)
    {
        config.setValue("showDidYouKnow", false);
    }
#endif  // ANDROID

    if (version < 5)
    {
        if (config.getIntValue("speech") == BeingSpeech::TEXT_OVERHEAD)
        {
            config.setValue("speech", CAST_S32(
                BeingSpeech::NO_NAME_IN_BUBBLE));
        }
    }
    if (version < 6)
        config.setValue("blur", false);

    if (version < 7)
        config.setValue("download-music", true);

    if (version < 9)
    {
        config.deleteKey("videodetected");
        config.setValue("moveToTargetType", 10);
    }
    if (version < 10)
        config.setValue("enableLazyScrolling", false);

    if (version < 11)
    {
#ifdef USE_SDL2
        const std::string prefix = std::string("sdl2");
#else  // USE_SDL2

        const std::string prefix = std::string();
#endif  // USE_SDL2

        unassignKey("keyDirectUp", "k108")
        unassignKey("keyDirectDown", "k59")
        unassignKey("keyDirectLeft", "k107")
        unassignKey("keyDirectRight", "k39")
    }
    if (version < 12)
    {
#ifdef USE_SDL2
        const std::string prefix = std::string("sdl2");
#else  // USE_SDL2

        const std::string prefix = std::string();
#endif  // USE_SDL2

        unassignKey("keyAttack", "k120")
    }

    if (version < 13)
        config.setValue("keyWindowBotChecker", -1);

    if (version < 14 && config.getIntValue("syncPlayerMoveDistance") == 2)
        config.setValue("syncPlayerMoveDistance", 5);
    config.setValue("cfgver", 14);
}

#undef unassignKey