File: main.cpp

package info (click to toggle)
netpanzer 0.8.7%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,344 kB
  • sloc: cpp: 45,217; sh: 278; objc: 259; python: 102; perl: 30; xml: 30; makefile: 15
file content (370 lines) | stat: -rw-r--r-- 11,326 bytes parent folder | download | duplicates (4)
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
/*
Copyright (C) 2003 Matthias Braun <matze@braunis.de>

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
(at your option) 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#include "Scripts/ScriptManager.hpp"

#include "lua5.1/lua.hpp"

#ifdef WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <unistd.h>
#endif

#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <signal.h>
#include "SDL.h"

#include <optionmm/command_line.hpp>

#include "Util/Log.hpp"
//#include "Util/Exception.hpp"
#include "Util/FileSystem.hpp"
#include "Network/NetworkManager.hpp"

#include "Interfaces/BaseGameManager.hpp"
#include "Interfaces/GameConfig.hpp"
#include "Interfaces/DedicatedGameManager.hpp"
#include "Interfaces/BotGameManager.hpp"
#include "Interfaces/PlayerGameManager.hpp"
#include "Core/NetworkGlobals.hpp"
#include "Scripts/ScriptManager.hpp"
#include "2D/Palette.hpp"

#ifndef PACKAGE_VERSION
	#define PACKAGE_VERSION "testing"
#endif

//---------------------------------------------------------------------------

static std::string pidfile;

void shutdown()
{
#ifndef WIN32
    if(pidfile != "") {
        unlink(pidfile.c_str());
    }
#endif

    SDL_Quit();
    if(gameconfig)
        gameconfig->saveConfig();
    LOGGER.closeLogFile();
    filesystem::shutdown();
}

#ifndef WIN32
void signalhandler(int signum)
{
    // set signal handler back to default
    signal(signum, SIG_DFL);

    const char* sigtype;
    switch(signum) {
        case SIGINT: sigtype = "SIGINT"; break;
        case SIGHUP: sigtype = "SIGHUP"; break;
        case SIGTERM: sigtype = "SIGTERM"; break;
        case SIGFPE: sigtype = "SIGFPE"; break;
        case SIGILL: sigtype = "SIGILL"; break;
        case SIGSEGV: sigtype = "SIGSEGV"; break;
        case SIGBUS: sigtype = "SIGBUS"; break;
        case SIGABRT: sigtype = "SIGABRT"; break;
        case SIGTRAP: sigtype = "SIGTRAP"; break;
        case SIGSYS: sigtype = "SIGSYS"; break;
        default: sigtype = "UNKNOWN"; break;
    }

    std::cerr << "Received signal " << sigtype << "(" << signum << ")"
        << std::endl << "aborting and trying to shutdown." << std::endl;
    shutdown();
    raise(signum);
}
#endif

//-----------------------------------------------------------------
BaseGameManager *initialise(int argc, char** argv)
{
#if !defined(WIN32) && !defined(DEBUG)
    // Install signal handlers for a clean shutdown
    signal(SIGILL, signalhandler);
    signal(SIGINT, signalhandler);
    signal(SIGHUP, signalhandler);
    signal(SIGTERM, signalhandler);
    signal(SIGFPE, signalhandler);
    signal(SIGSEGV, signalhandler);
    signal(SIGBUS, signalhandler);
    signal(SIGABRT, signalhandler);
    signal(SIGTRAP, signalhandler);
    signal(SIGSYS, signalhandler);
#endif
#if !defined(WIN32)
    signal(SIGPIPE, SIG_IGN);
#endif

    // Parse commandline
    using namespace optionmm;
    command_line commandline("NetPanzer", PACKAGE_VERSION,
            "Copyright(c) 1998 Pyrosoft Inc. and nepanzer-devel team", "",
            argc, argv);

    option<std::string, true, false> connect_option('c', "connect",
            "Connect to the specified netpanzer server", "");
    commandline.add(&connect_option);
    bool_option dedicated_option('d', "dedicated", "Run as dedicated server", false);
    commandline.add(&dedicated_option);
    option<std::string, true, false> bot_option('b', "bot", "Connect as bot to specific server", "");
    commandline.add(&bot_option);

    bool_option need_password(0, "password", "Ask for password when connecting, only useful on quick connect", false);
    commandline.add(&need_password);

    option<int> port_option('p', "port", "Run server on specific port", 0);
    commandline.add(&port_option);
    bool_option debug_option('g', "debug", "Enable debug output", false);
    commandline.add(&debug_option);
    option<std::string, true, false> master_server_option('\0', "master_server", "Use 'none' if you dont want to use the master server", "");
    commandline.add(&master_server_option);
    option<std::string, true, false> game_config_option(0, "game_config",
        "Which config file should be used (only files inside config directory)",
        "");
    commandline.add(&game_config_option);
    option<bool, false, false> protocol_option('\0', "protocol",
            "Show version of network protocol", false);
    commandline.add(&protocol_option);
    option<std::string, true, false> pidfile_option(0, "pidfile",
            "Create a pidfile at the specified location", "");
    commandline.add(&pidfile_option);

    if(!commandline.process() || commandline.help() || commandline.version())
        exit(0);

    if(protocol_option.value()) {
        std::cout << (int) NETPANZER_PROTOCOL_VERSION << std::endl;
        exit(0);
    }

    if (debug_option.value()) {
        LOGGER.setLogLevel(Logger::LEVEL_DEBUG);
        LOGGER.debug("debug option enabled");
    } else if (dedicated_option.value()) {
        LOGGER.setLogLevel(Logger::LEVEL_INFO);
    }

    // Initialize SDL (we have to start the video subsystem as well so that
    // the eventloop is started, otherwise we'll have problems in dedicated
    // server)
    if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) {
        fprintf(stderr, "SDL_Init error: %s.\n", SDL_GetError());
        exit(1);
    }
    SDL_EnableUNICODE(1);

    // Initialize libphysfs
    try {
        filesystem::initialize(argv[0], "netpanzer", "netpanzer");
    } catch(std::exception& e) {
        fprintf(stderr, "%s", e.what());
        shutdown();
        exit(1);
    }

#ifdef NP_DATADIR
    try {
	filesystem::addToSearchPath(NP_DATADIR);
    } catch(...)
    { }
#endif

#ifdef __APPLE__
    // Mac OS X puts the data files into NetPanzer.app/Contents/Resources
    try {
      std::ostringstream dir;
      dir << PHYSFS_getBaseDir() << "/NetPanzer.app/Contents/Resources/";
      filesystem::addToSearchPath(dir.str().c_str());
    } catch(...)
    { }
#endif

    if(dedicated_option.value())
        LOGGER.openLogFile("server");
    else if(bot_option.value().size() > 0)
        LOGGER.openLogFile("bot");
    else
        LOGGER.openLogFile("netpanzer");

#ifdef WIN32
    // SDL redirects stdout and stderr to 2 textfiles, better open a new console
    // for the dedicated server and the bot
    if(dedicated_option.value() || bot_option.value().size() > 0) {
        AllocConsole();
        freopen("CON", "w", stdout);
        freopen("CON", "w", stderr);
        freopen("CON", "r", stdin);
    }
#endif

    // Initialize random number generator
    srand(time(0));
    // the STL functions in gcc3 seem to use the 48er versions of the random
    // generator instead of the default libc one, so we have todo a double srand
    // :-/
#ifdef __USE_SVID
    srand48(time(0));
#endif

    // create a pidfile
    pidfile = pidfile_option.value();
#ifndef WIN32
    if(pidfile != "") {
        std::ofstream out(pidfile.c_str());
        if(!out.good()) {
            fprintf(stderr, "Couldn't create pidfile '%s'.\n", pidfile.c_str());
            shutdown();
            exit(1);
        }
        out << getpid() << std::endl;
        out.close();
    }
#else
    if(pidfile != "") {
        fprintf(stderr, "Pidfiles not supported on win32.\n");
    }
#endif

    BaseGameManager *manager;
    // finally initialize the game objects
    try {
        if (dedicated_option.value()) {
            manager = new DedicatedGameManager();
        }
        else if (bot_option.value().size() > 0) {
            manager = new BotGameManager(bot_option.value());
        } else {
            manager = new PlayerGameManager();
        }

        manager->initialize(game_config_option.value());

        // gameconfig exists now...
        if(connect_option.value() != "") {
            std::string connecthost = connect_option.value();
            if(connecthost.find("netpanzer://") == 0) {
                connecthost = connecthost.substr(12, connecthost.size()-12);
                std::string::size_type p = connecthost.find("/");
                if(p != std::string::npos) {
                    if ( connecthost[p+1] == 'p' )
                    {
                        gameconfig->needPassword = true;
                    }
                    connecthost = connecthost.substr(0, p);
                }
            }

            if ( need_password.value() )
            {
                gameconfig->needPassword = true;
            }

            gameconfig->serverConnect = connecthost;
            gameconfig->quickConnect = true;
        }
        if (master_server_option.value().size() > 0) {
            if (master_server_option.value() == "none") {
                *GameConfig::server_masterservers = "";
            }
            else {
                *GameConfig::server_masterservers = master_server_option.value();
            }
        }
        if ( port_option.value() )
        {
            GameConfig::server_port=port_option.value();
        }

        return manager;
    } catch(std::exception& e) {
        LOGGER.warning("Couldn't initialize the game: %s", e.what());
        shutdown();
        exit(1);
    }
}

//-----------------------------------------------------------------
int netpanzer_main(int argc, char** argv)
{
    network::NetworkManager::initialize();
    ScriptManager::initialize();

    //ScriptManager::runStr("LuaInitialize","print('Lua is working just fine');");

    BaseGameManager *manager = initialise(argc, argv);

    ScriptManager::runFile("unused","scripts/initialize.lua");

    // we'll catch every exception here, to be sure the user gets at least
    // a usefull error message and SDL has a chance to shutdown...
    try {
        if (manager->launchNetPanzerGame()) {
            while(manager->mainLoop())
                ;
        }

        manager->shutdown();
        delete manager;
        LOGGER.info("successfull shutdown.");
        shutdown();
    }
// in debug mode we want the exception to abort, so that we have the original
// stack backtrace
#if !defined(DEBUG) || defined(WIN32)
    catch(std::exception& e) {
        LOGGER.warning("An unexpected exception occured: '%s'\nShutdown needed.",
                e.what());
        shutdown();
        return 1;
    } catch(...) {
        LOGGER.warning("An unexpected exception occured.\nShutdown needed.");
        shutdown();
        return 1;
    }
#else
    catch(const float dummy) {
    }
#endif

    ScriptManager::close();
    network::NetworkManager::cleanUp();
    return 0;
}

// XXX these tweaks are need for SDL on win32
extern "C" {
    int main(int argc, char** argv)
    {
        return netpanzer_main(argc, argv);
    }
}