summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 03be62543b179853cccc24ab5e9caa33e14c24a1 (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
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2013  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus 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 "main.h"

#include "client.h"

#include <getopt.h>
#include <iostream>
#include <unistd.h>

#include "utils/gettext.h"
#ifdef ANDROID
#include "utils/mkdir.h"
#endif
#include "utils/paths.h"
#include "utils/physfscheckutils.h"
#include "utils/physfstools.h"
#include "utils/physfsrwops.h"
#include "utils/process.h"
#include "utils/xml.h"

#ifdef UNITTESTS
#include <gtest/gtest.h>
#endif

#ifdef __MINGW32__
#include <windows.h>
#endif

#include <SDL_image.h>
#include <SDL_mixer.h>

#include "debug.h"

char *selfName = nullptr;

static void printHelp()
{
    std::cout
        // TRANSLATORS: command line help
        << _("manaplus [options] [manaplus-file]") << std::endl << std::endl
        // TRANSLATORS: command line help
        << _("[manaplus-file] : The manaplus file is an XML file (.manaplus)")
        << std::endl
        // TRANSLATORS: command line help
        << _("                  used to set custom parameters") << std::endl
        // TRANSLATORS: command line help
        << _("                  to the manaplus client.")
        << std::endl << std::endl
        // TRANSLATORS: command line help
        << _("Options:") << std::endl
        // TRANSLATORS: command line help
        << _("  -l --log-file       : Log file to use") << std::endl
        // TRANSLATORS: command line help
        << _("  -a --chat-log-dir   : Chat log dir to use") << std::endl
        // TRANSLATORS: command line help
        << _("  -v --version        : Display the version") << std::endl
        // TRANSLATORS: command line help
        << _("  -h --help           : Display this help") << std::endl
        // TRANSLATORS: command line help
        << _("  -C --config-dir     : Configuration directory to use")
        << std::endl
        // TRANSLATORS: command line help
        << _("  -U --username       : Login with this username") << std::endl
        // TRANSLATORS: command line help
        << _("  -P --password       : Login with this password") << std::endl
        // TRANSLATORS: command line help
        << _("  -c --character      : Login with this character") << std::endl
        // TRANSLATORS: command line help
        << _("  -s --server         : Login server name or IP") << std::endl
        // TRANSLATORS: command line help
        << _("  -p --port           : Login server port") << std::endl
        // TRANSLATORS: command line help
        << _("  -H --update-host    : Use this update host") << std::endl
        // TRANSLATORS: command line help
        << _("  -D --default        : Choose default character server and "
                                     "character") << std::endl
        // TRANSLATORS: command line help
        << _("  -u --skip-update    : Skip the update downloads") << std::endl
        // TRANSLATORS: command line help
        << _("  -d --data           : Directory to load game "
             "data from") << std::endl
        // TRANSLATORS: command line help
        << _("  -L --localdata-dir  : Directory to use as local data"
             " directory") << std::endl
        // TRANSLATORS: command line help
        << _("     --screenshot-dir : Directory to store screenshots")
        << std::endl
        // TRANSLATORS: command line help
        << _("     --safemode       : Start game in safe mode") << std::endl
        // TRANSLATORS: command line help
        << _("  -T --tests          : Start testing drivers and "
                                     "auto configuring") << std::endl
#ifdef USE_OPENGL
        // TRANSLATORS: command line help
        << _("  -O --no-opengl      : Disable OpenGL for this session")
        << std::endl
#endif
        ;
}

static void printVersion()
{
    std::cout << strprintf("ManaPlus client %s", FULL_VERSION) << std::endl;
}

static void parseOptions(const int argc, char *const argv[],
                         Client::Options &options)
{
    const char *const optstring = "hvud:U:P:Dc:p:l:L:C:s:t:T:a";

    const struct option long_options[] =
    {
        { "config-dir",     required_argument, nullptr, 'C' },
        { "data",           required_argument, nullptr, 'd' },
        { "default",        no_argument,       nullptr, 'D' },
        { "password",       required_argument, nullptr, 'P' },
        { "character",      required_argument, nullptr, 'c' },
        { "help",           no_argument,       nullptr, 'h' },
        { "localdata-dir",  required_argument, nullptr, 'L' },
        { "update-host",    required_argument, nullptr, 'H' },
        { "port",           required_argument, nullptr, 'p' },
        { "server",         required_argument, nullptr, 's' },
        { "skip-update",    no_argument,       nullptr, 'u' },
        { "username",       required_argument, nullptr, 'U' },
        { "no-opengl",      no_argument,       nullptr, 'O' },
        { "chat-log-dir",   required_argument, nullptr, 'a' },
        { "version",        no_argument,       nullptr, 'v' },
        { "log-file",       required_argument, nullptr, 'l' },
        { "screenshot-dir", required_argument, nullptr, 'i' },
        { "safemode",       no_argument,       nullptr, 'm' },
        { "tests",          no_argument,       nullptr, 'T' },
        { "test",           required_argument, nullptr, 't' },
        { nullptr,          0,                 nullptr, 0 }
    };

    while (optind < argc)
    {
        const int result = getopt_long(argc, argv,
            optstring, long_options, nullptr);

        if (result == -1)
            break;

        switch (result)
        {
            case 'C':
                options.configDir = optarg;
                break;
            case 'd':
                options.dataPath = optarg;
                break;
            case 'D':
                options.chooseDefault = true;
                break;
            case '?':  // Unknown option
            case ':':  // Missing argument
            case 'h':
                options.printHelp = true;
                break;
            case 'H':
                if (checkPath(optarg))
                    options.updateHost = optarg;
                else
                    options.updateHost.clear();
                break;
            case 'c':
                options.character = optarg;
                break;
            case 'P':
                options.password = optarg;
                break;
            case 's':
                options.serverName = optarg;
                break;
            case 'p':
                options.serverPort = static_cast<uint16_t>(atoi(optarg));
                break;
            case 'u':
                options.skipUpdate = true;
                break;
            case 'U':
                options.username = optarg;
                break;
            case 'v':
                options.printVersion = true;
                break;
            case 'L':
                options.localDataDir = optarg;
                break;
            case 'O':
                options.noOpenGL = true;
                break;
            case 'l':
                options.logFileName = std::string(optarg);
                break;
            case 'a':
                options.chatLogDir = std::string(optarg);
                break;
            case 'i':
                options.screenshotDir = optarg;
                break;
            case 'm':
                options.safeMode = true;
                break;
            case 'T':
                options.testMode = true;
                options.test.clear();
                break;
            case 't':
                options.testMode = true;
                options.test = std::string(optarg);
                break;
            default:
                break;
        }
    }

    // when there are still options left use the last
    // one as branding file
    if (optind < argc)
    {
        options.brandingPath = argv[optind];
    }
}

#ifdef WIN32
extern "C" char const *_nl_locale_name_default(void);
#endif

#ifndef UNITTESTS
// main for normal game usage
int main(int argc, char *argv[])
{
#if defined(__MINGW32__)
    // load mingw crash handler. Won't fail if dll is not present.
    // may load libray from current dir, it may not same as program dir
    LoadLibrary("exchndl.dll");
#endif

    selfName = argv[0];

    // Parse command line options
    Client::Options options;
    parseOptions(argc, argv, options);

    if (options.printHelp)
    {
        printHelp();
        _exit(0);
    }
    else if (options.printVersion)
    {
        printVersion();
        _exit(0);
    }

    // Initialize PhysicsFS
#ifdef ANDROID
    mkdir_r(getSdStoragePath().c_str());

    if (!PHYSFS_init((getRealPath(".").append("/fakebinary")).c_str()))
#else
    if (!PHYSFS_init(argv[0]))
#endif
    {
        std::cout << "Error while initializing PhysFS: "
                  << PHYSFS_getLastError() << std::endl;
        return 1;
    }

    PhysFs::updateDirSeparator();

    atexit((void(*)()) PHYSFS_deinit);

    XML::initXML();
    IMG_Init(IMG_INIT_PNG);
    Mix_Init(MIX_INIT_OGG);

#ifdef WIN32
    SetCurrentDirectory(PhysFs::getBaseDir());
#endif
    setPriority(true);
    client = new Client(options);
    int ret = 0;
    if (!options.testMode)
    {
        client->gameInit();
        ret = client->gameExec();
    }
    else
    {
        client->testsInit();
        ret = client->testsExec();
    }
    delete client;
    client = nullptr;

    Mix_Quit();
    IMG_Quit();

#ifdef DUMP_LEAKED_RESOURCES
    reportRWops();
#endif
#ifdef DEBUG_PHYSFS
    reportPhysfsLeaks();
#endif
    return ret;
}

#else

// main for unit testing
int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

#endif