Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/orxonox.cc @ 9397

Last change on this file since 9397 was 9396, checked in by patrick, 18 years ago

committing my weekends work: 2100 lines :D

  • proxy server now accepts and synchronizes clients like a master server
  • network manager got different network setup interface
  • network stream got different constructure scheme
  • permissions checking and algorithm extended and changed
  • starting ability to connect to multiple network nodes at the same time
  • some very much smaller changes here and there
File size: 15.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20
21   ### File Specific:
22   main-programmer: Patrick Boenzli
23   co-programmer: Christian Meyer
24   co-programmer: Benjamin Grauer: injected ResourceManager/GraphicsEngine/GUI
25*/
26
27#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ORXONOX
28#include "orxonox.h"
29
30#include "globals.h"
31
32#include "gui/qt/qt_gui.h"
33#include "gui/qt/qt_gui_datadir_fallback.h"
34
35#include "parser/ini_parser/ini_parser.h"
36#include "util/loading/game_loader.h"
37#include "util/signal_handler.h"
38
39//ENGINES
40#include "graphics_engine.h"
41#include "sound_engine.h"
42#include "util/loading/resource_manager.h"
43#include "cd_engine.h"
44#include "text_engine.h"
45#include "event_handler.h"
46
47#include "util/loading/factory.h"
48#include "fast_factory.h"
49
50#include "benchmark.h"
51
52#include "class_list.h"
53#include "shell_command_class.h"
54#include "shell_command.h"
55#include "shell_buffer.h"
56
57#include "util/loading/load_param_description.h"
58
59#include "network_manager.h"
60
61#include "state.h"
62#include "lib/parser/preferences/cmd_line_prefs_reader.h"
63#include "lib/parser/preferences/ini_file_prefs_reader.h"
64#include <string.h>
65
66int verbose = 5;
67
68
69
70SHELL_COMMAND(restart, Orxonox, restart);
71
72REGISTER_ARG_FLAG( l, license,    "misc",  "showLicenseAndExit", "Prints the license and exit",      "1" );
73REGISTER_ARG_FLAG( c, client,     "game",  "gameType",           "Connect to Server (-H)",           "multiplayer_client" );
74REGISTER_ARG_FLAG( s, server,     "game",  "gameType",           "Start Orxonox as Game Server",     "multiplayer_master_server" );
75REGISTER_ARG_FLAG( x, proxy,      "game",  "gameType",           "Start Orxonox as Proxy Server",    "multiplayer_proxy_server" );
76REGISTER_ARG_ARG(  H, host,       "game",  "host",               "Host to connect to",               "host");
77REGISTER_ARG_ARG(  p, port,       "game",  "port",               "Port to use",                      "port" );
78REGISTER_ARG_FLAG( g, gui,        "game",  "showGui",            "starts the orxonox with the configuration GUI", "1");
79
80REGISTER_ARG_FLAG( f, fullscreen, "video", "Fullscreen-mode",    "start Orxonox in fullscreen mode", "1");
81REGISTER_ARG_FLAG( w, windowed,   "video", "Fullscreen-mode",    "start Orxonox in windowed mode",   "0");
82REGISTER_ARG_ARG(  r, resolution, "video", "Resolution",         "sets resolution / window size",    "res");
83REGISTER_ARG_FLAG( d, dedicated,  "video", "Norender-mode",      "the scene is not rendered",        "1" );
84
85REGISTER_ARG_FLAG( a, audio,      "audio", "Disable-Audio",      "Enable audio",                     "0" );
86REGISTER_ARG_FLAG( m, mute ,      "audio", "Disable-Audio",      "Disable audio",                    "1" );
87REGISTER_ARG_ARG(  _, audio_channels, "audio", "Audio-Channels", "Sets # audio channels", "num" );
88REGISTER_ARG_ARG(  _, music_volume, "audio", "Music-Volume", "Sets music volume", "vol" );
89REGISTER_ARG_ARG(  _, effects_volume, "audio", "Effects-Volume", "Sets effects volume", "vol" );
90
91#ifndef __WIN32__
92REGISTER_ARG_FLAG( _, write_bt_to_file, "misc", "bt-to-file", "Write backtrace to file", "1");
93#endif
94
95REGISTER_ARG_ARG(  t, telnetport,  "network","telnetport",        "Port to use for network debug output",               "port" );
96REGISTER_ARG_ARG(  _, write_dict,  "compression", "writedict",    "write packets to DATA/dicts/newdict",               "numBytes" );
97
98
99/**
100 *  create a new Orxonox
101
102   In this funcitons only global values are set. The game will not be started here.
103*/
104Orxonox::Orxonox ()
105{
106  this->setClassID(CL_ORXONOX, "Orxonox");
107  this->setName("orxonox-main");
108
109  this->argc = 0;
110  this->argv = NULL;
111
112  /* this way, there is no network enabled: */
113  this->serverName = "";
114  this->port = -1;
115
116  this->configFileName = "";
117}
118
119/**
120 *  remove Orxonox from memory
121*/
122Orxonox::~Orxonox ()
123{
124  // game-specific
125  delete GameLoader::getInstance();
126
127  // class-less services/factories
128  Factory::deleteFactories();
129  FastFactory::deleteAll();
130  OrxShell::ShellCommandClass::unregisterAllCommands();
131
132  LoadClassDescription::deleteAllDescriptions();
133
134  // handlers
135  delete ResourceManager::getInstance(); // deletes the Resource Manager
136
137  // engines
138  delete CDEngine::getInstance();
139  delete OrxSound::SoundEngine::getInstance();
140  delete GraphicsEngine::getInstance(); // deleting the Graphics
141  delete EventHandler::getInstance();
142
143  // output-buffer
144  delete OrxShell::ShellBuffer::getInstance();
145
146  SDL_QuitSubSystem(SDL_INIT_TIMER);
147  ClassList::debug();
148
149  Preferences::getInstance()->save();
150
151  PRINT(3)
152  (
153    "===================================================\n" \
154    "Thanks for playing orxonox.\n" \
155    "visit: http://www.orxonox.net for new versions.\n" \
156    "===================================================\n" \
157    ORXONOX_LICENSE_SHORT
158  );
159
160  Orxonox::singletonRef = NULL;
161}
162
163/**
164 *  this is a singleton class to prevent duplicates
165 */
166Orxonox* Orxonox::singletonRef = NULL;
167
168// DANGEROUS
169void Orxonox::restart()
170{
171  //   int argc = this->argc;
172  //   char** argv = this->argv;
173  //
174  //   Orxonox *orx = Orxonox::getInstance();
175  //
176  //   delete orx;
177  //
178  //   orx = Orxonox::getInstance();
179  //
180  //   if((*orx).init(argc, argv) == -1)
181  //   {
182  //     PRINTF(1)("! Orxonox initialization failed\n");
183  //     return;
184  //   }
185  //
186  //   printf("finished inizialisation\n");
187  //   orx->start();
188}
189
190/**
191 * @brief this finds the config file
192 * @returns the new config-fileName
193 * Since the config file varies from user to user and since one may want to specify different config files
194 * for certain occasions or platforms this function finds the right config file for every occasion and stores
195 * it's path and name into configfilename
196*/
197const std::string& Orxonox::getConfigFile ()
198{
199  File orxConfFile("orxonox.conf");
200  if (orxConfFile.isFile())
201  {
202    this->configFileName =  "orxonox.conf";
203  }
204  else
205    this->configFileName = File(DEFAULT_CONFIG_FILE).name();
206
207  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName.c_str());
208  return this->configFileName;
209}
210
211/**
212 * initialize Orxonox with command line
213 */
214int Orxonox::init (int argc, char** argv, const std::string & name, int port)
215{
216  this->argc = argc;
217  this->argv = argv;
218
219  this->serverName = name;
220  this->port = port;
221
222  // initialize the Config-file
223  this->getConfigFile();
224
225  // windows must not write into stdout.txt and stderr.txt
226  /*#ifdef __WIN32__
227  freopen( "CON", "w", stdout );
228  freopen( "CON", "w", stderr );
229  #endif*/
230
231  // initialize everything
232  SDL_Init(0);
233  if( initVideo() == -1)
234    return -1;
235  if( initResources () == -1)
236    return -1;
237  if( initSound() == -1)
238    return -1;
239  if( initInput() == -1)
240    return -1;
241  if( initNetworking () == -1)
242    return -1;
243  if( initMisc () == -1)
244    return -1;
245
246  return 0;
247}
248
249
250/**
251 * initializes SDL and OpenGL
252 */
253int Orxonox::initVideo()
254{
255  PRINTF(3)("> Initializing video\n");
256
257  GraphicsEngine::getInstance();
258
259  GraphicsEngine::getInstance()->initFromPreferences();
260
261  std::string iconName = ResourceManager::getFullName("pictures/fighter-top-32x32.bmp");
262  if (!iconName.empty())
263  {
264    GraphicsEngine::getInstance()->setWindowName(PACKAGE_NAME " " PACKAGE_VERSION, iconName);
265  }
266  return 0;
267}
268
269
270/**
271 * initializes the sound engine
272 */
273int Orxonox::initSound()
274{
275  PRINT(3)("> Initializing sound\n");
276  // SDL_InitSubSystem(SDL_INIT_AUDIO);
277  OrxSound::SoundEngine::getInstance();
278
279  OrxSound::SoundEngine::getInstance()->loadSettings();
280  OrxSound::SoundEngine::getInstance()->initAudio();
281  return 0;
282}
283
284
285/**
286 * initializes input functions
287 */
288int Orxonox::initInput()
289{
290  PRINT(3)("> Initializing input\n");
291
292  EventHandler::getInstance()->init();
293  EventHandler::getInstance()->subscribe(GraphicsEngine::getInstance(), ES_ALL, EV_VIDEO_RESIZE);
294
295  return 0;
296}
297
298
299/**
300 * initializes network system
301 */
302int Orxonox::initNetworking()
303{
304  PRINT(3)("> Initializing networking\n");
305  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
306
307  if( gameType == "multiplayer_client") // we are a client
308  {
309    State::setOnline(true);
310    NetworkManager::getInstance()->createClient(this->serverName, port);
311  }
312  else if( gameType == "multiplayer_master_server")
313  {    // we are a master server
314    State::setOnline(true);
315    NetworkManager::getInstance()->createMasterServer(port);
316  }
317  else if( gameType == "multiplayer_proxy_server")
318  {    // we are a proxy server
319    State::setOnline(true);
320    NetworkManager::getInstance()->createProxyServer(port);
321  }
322
323  return 0;
324
325}
326
327//#include "util/loading/dynamic_loader.h"
328
329/**
330 * initializes and loads resource files
331 */
332int Orxonox::initResources()
333{
334  PRINTF(3)("> Initializing resources\n");
335
336  PRINT(3)("initializing ResourceManager\n");
337
338  // init the resource manager
339  std::string dataPath;
340  if ((dataPath = Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""))!= "")
341  {
342    if (!ResourceManager::getInstance()->setDataDir(dataPath) &&
343        !ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
344    {
345      PRINTF(1)("Data Could not be located in %s\n", dataPath.c_str());
346    }
347  }
348
349
350  while (!ResourceManager::getInstance()->verifyDataDir(DEFAULT_DATA_DIR_CHECKFILE))
351  {
352
353    PRINTF(1)("The DataDirectory %s could not be verified\n\nh" \
354              "!!!  Please Change in File %s Section %s Entry %s to a suitable value !!!\n",
355              ResourceManager::getInstance()->getDataDir().c_str(),
356              this->configFileName.c_str(),
357              CONFIG_SECTION_GENERAL,
358              CONFIG_NAME_DATADIR );
359    OrxGui::Gui* gui = new OrxGui::QtGuiDataDirFallback(argc, argv);
360    gui->startGui();
361    if (gui->getState() == OrxGui::Gui::Quitting)
362      return(-1);
363    delete gui;
364    ResourceManager::getInstance()->setDataDir(Preferences::getInstance()->getString(CONFIG_SECTION_GENERAL, CONFIG_NAME_DATADIR, ""));
365
366  }
367
368
369  //! @todo this is a hack and should be loadable
370  std::string imageDir = ResourceManager::getInstance()->getFullName("maps");
371  ResourceManager::getInstance()->addImageDir(imageDir);
372  imageDir = ResourceManager::getInstance()->getFullName("pictures");
373  ResourceManager::getInstance()->addImageDir(imageDir);
374
375  //  DynamicLoader::loadDyLib("libtest.so");
376  return 0;
377}
378
379/**
380 * initializes miscelaneous features
381 * @return -1 on failure
382 */
383int Orxonox::initMisc()
384{
385  OrxShell::ShellBuffer::getInstance();
386
387  // start the collision detection engine
388  CDEngine::getInstance();
389
390  return 0;
391}
392
393/**
394 *  starts the orxonox game or menu
395 * here is the central orxonox state manager. There are currently two states
396 * - menu
397 * - game-play
398 * both states manage their states themselfs again.
399*/
400void Orxonox::start()
401{
402
403  this->gameLoader = GameLoader::getInstance();
404
405  if( this->port != -1)
406    this->gameLoader->loadNetworkCampaign("worlds/DefaultNetworkCampaign.oxc");
407  else
408    this->gameLoader->loadCampaign("worlds/DefaultCampaign.oxc");                       /* start orxonox in single player mode */
409
410  //  this->gameLoader->loadDebugCampaign(DEBUG_CAMPAIGN_0);
411  this->gameLoader->init();
412  this->gameLoader->start();
413}
414
415
416/**
417 * handles sprecial events from localinput
418 * @param event: an event not handled by the CommandNode
419 */
420// void Orxonox::graphicsHandler(SDL_Event* event)
421// {
422//   // Handle special events such as reshape, quit, focus changes
423//   switch (event->type)
424//     {
425//     case SDL_VIDEORESIZE:
426//       GraphicsEngine* tmpGEngine = GraphicsEngine::getInstance();
427//       tmpGEngine->resolutionChanged(event->resize);
428//       break;
429//     }
430// }
431
432
433
434
435
436
437bool showGui = false;
438
439/**********************************
440*** ORXONOX MAIN STARTING POINT ***
441**********************************/
442/**
443 *
444 *  main function
445 *
446 * here the journey begins
447*/
448int main(int argc, char** argv)
449{
450  CmdLinePrefsReader prefs;
451
452  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
453  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
454
455  prefs.parse(argc, argv);
456
457  if ( Preferences::getInstance()->getString("misc", "showLicenseAndExit", "") == "1" )
458  {
459    PRINT(0)(ORXONOX_LICENSE_SHORT);
460    return 0;
461  }
462
463  if ( Preferences::getInstance()->getString("misc", "bt-to-file", "1") == "1" )
464  {
465    SignalHandler::getInstance()->doCatch( argv[0], "orxonox.backtrace" );
466    SignalHandler::getInstance()->registerCallback( EventHandler::releaseMouse, NULL );
467  }
468
469  if( Preferences::getInstance()->getString("game", "showGui", "") == "1" )
470    showGui = true;
471  else if( Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_master_server" ||
472           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_proxy_server" ||
473           Preferences::getInstance()->getString( "game", "gameType", "" ) == "multiplayer_client" )
474    return startNetworkOrxonox(argc, argv);
475
476  return startOrxonox(argc, argv, "", -1);
477}
478
479
480
481/**
482 * starts orxonox in network mode
483 * @param argc parameters count given to orxonox
484 * @param argv parameters given to orxonox
485 */
486int startNetworkOrxonox(int argc, char** argv)
487{
488
489  std::string gameType = Preferences::getInstance()->getString( "game", "gameType", "" );
490
491  if ( gameType == "multiplayer_client" )
492  {
493    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
494    std::string host = Preferences::getInstance()->getString( "game", "host", "" );
495
496    if ( host == "" )
497    {
498      printf("You need to specify a host to connect to ( -H <host> )\n");
499      return 1;
500    }
501
502    printf("Starting Orxonox as client: connecting to %s, on port %i\n", host.c_str(), port);
503
504    startOrxonox(argc, argv, host.c_str(), port);
505  }
506  else if ( gameType == "multiplayer_master_server" )
507  {
508    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
509
510    printf("Starting Orxonox as server: listening on port %i\n", port);
511
512    startOrxonox(argc, argv, "", port);
513  }
514  else if ( gameType == "multiplayer_proxy_server" )
515  {
516    int port = Preferences::getInstance()->getInt( "game", "port", DEFAULT_ORXONOX_PORT );
517
518    printf("Starting Orxonox as proxy server: listening on port %i\n", port);
519
520    startOrxonox(argc, argv, "", port);
521  }
522  return 1;
523}
524
525
526
527/**
528 * starts orxonox
529 * @param argc parameters count given to orxonox
530 * @param argv parameters given to orxonox
531 */
532int startOrxonox(int argc, char** argv, const std::string & name, int port)
533{
534  // checking for existence of the configuration-files, or if the lock file is still used
535  if (showGui || (!File("./orxonox.conf").isFile() &&
536                  !File(DEFAULT_CONFIG_FILE).isFile())
537#if DEBUG_LEVEL <= 3 // developers do not need to see the GUI, when orxonox fails
538      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
539#endif
540     )
541  {
542    File lockFile(DEFAULT_LOCK_FILE);
543    if (lockFile.isFile())
544      lockFile.remove();
545
546    // starting the GUI
547    OrxGui::QtGui gui(argc, argv);
548    gui.startGui();
549
550    if (gui.getState() & OrxGui::Gui::Quitting)
551      return 0;
552
553  }
554
555  PRINT(0)(">>> Starting Orxonox <<<\n");
556
557  File(DEFAULT_LOCK_FILE).touch();
558
559  Orxonox *orx = Orxonox::getInstance();
560
561  if( orx->init(argc, argv, name, port) == -1)
562  {
563    PRINTF(1)("! Orxonox initialization failed\n");
564    return -1;
565  }
566
567  printf("finished inizialisation\n");
568  orx->start();
569
570  delete orx;
571  File("~/.orxonox/orxonox.lock").remove();
572  return 0;
573}
Note: See TracBrowser for help on using the repository browser.