Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 18, 2009, 9:15:39 PM (15 years ago)
Author:
rgrieder
Message:

Moved all core related initialisations from Main.cc and GSRoot.cc to Core.cc so that everything sticks together more obviously.

Renamed —directory command line argument: Name really doesn't say what it is.
using —writingPathSuffix now. Not much better, but at least you wonder

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/core/Core.cc

    r2759 r2799  
    2222 *   Author:
    2323 *      Fabian 'x3n' Landau
     24 *      Reto Grieder
    2425 *   Co-authors:
    25  *      Reto Grieder
     26 *      ...
    2627 *
    2728 */
     
    4142
    4243#ifdef ORXONOX_PLATFORM_WINDOWS
     44#  ifndef WIN32_LEAN_AND_MEAN
     45#    define WIN32_LEAN_AND_MEAN
     46#  endif
    4347#  include <windows.h>
    4448#elif defined(ORXONOX_PLATFORM_APPLE)
     
    5155
    5256#include "SpecialConfig.h"
     57#include "util/Debug.h"
    5358#include "util/Exception.h"
     59#include "util/SignalHandler.h"
     60#include "Clock.h"
     61#include "CommandExecutor.h"
     62#include "CommandLine.h"
     63#include "ConfigFileManager.h"
     64#include "ConfigValueIncludes.h"
     65#include "CoreIncludes.h"
     66#include "Factory.h"
     67#include "Identifier.h"
    5468#include "Language.h"
    55 #include "CoreIncludes.h"
    56 #include "ConfigValueIncludes.h"
    5769#include "LuaBind.h"
    58 #include "CommandLine.h"
     70#include "Shell.h"
     71#include "TclBind.h"
     72#include "TclThreadManager.h"
    5973
    6074namespace orxonox
     
    7387    bool Core::bIsMaster_s      = false;
    7488
    75     bool Core::isDevBuild_s     = false;
    7689    Core* Core::singletonRef_s  = 0;
    7790
    7891    SetCommandLineArgument(mediaPath, "").information("PATH");
    79     SetCommandLineArgument(directory, "").information("DIR");
     92    SetCommandLineArgument(writingPathSuffix, "").information("DIR");
     93    SetCommandLineArgument(settingsFile, "orxonox.ini");
     94    SetCommandLineArgument(limitToCPU, 0).information("0: off | #cpu");
    8095
    8196    /**
     
    8398        @param A reference to a global variable, used to avoid an infinite recursion in getSoftDebugLevel()
    8499    */
    85     Core::Core()
     100    Core::Core(int argc, char** argv)
    86101    {
    87102        RegisterRootObject(Core);
     
    90105        Core::singletonRef_s = this;
    91106
    92         this->bInitializeRandomNumberGenerator_ = false;
    93         this->setConfigValues();
     107        // Parse command line arguments fist
     108        try
     109        {
     110            CommandLine::parseAll(argc, argv);
     111        }
     112        catch (ArgumentException& ex)
     113        {
     114            COUT(1) << ex.what() << std::endl;
     115            COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl;
     116        }
     117
     118        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
     119        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
     120        // the timer though).
     121        int limitToCPU = CommandLine::getValue("limitToCPU");
     122        if (limitToCPU > 0)
     123            setThreadAffinity((unsigned int)limitToCPU);
     124
     125        // Determine and set the location of the executable
     126        setExecutablePath();
     127
     128        // Determine whether we have an installed or a binary dir run
     129        // The latter occurs when simply running from the build directory
     130        checkDevBuild();
     131
     132        // Make sure the directories we write in exist or else make them
     133        createDirectories();
     134
     135        // create a signal handler (only active for linux)
     136        // This call is placed as soon as possible, but after the directories are set
     137        this->signalHandler_ = new SignalHandler();
     138        this->signalHandler_->doCatch(executablePath_g.string(), Core::getLogPathString() + "orxonox_crash.log");
    94139
    95140        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used
    96141        OutputHandler::getOutStream().setLogPath(Core::getLogPathString());
    97142
     143        // Manage ini files and set the default settings file (usually orxonox.ini)
     144        this->configFileManager_ = new ConfigFileManager();
     145        this->configFileManager_->setFilename(ConfigFileType::Settings,
     146            CommandLine::getValue("settingsFile").getString());
     147
     148        this->languageInstance_ = new Language();
     149
     150        // Do this soon after the ConfigFileManager has been created to open up the
     151        // possibility to configure everything below here
     152        this->setConfigValues();
     153
    98154        // Possible media path override by the command line
    99155        if (!CommandLine::getArgument("mediaPath")->hasDefaultValue())
     
    102158            Core::tsetMediaPath(CommandLine::getValue("mediaPath"));
    103159        }
     160
     161        // Create the lua interface
     162        this->luaBind_ = new LuaBind();
     163
     164        // initialise Tcl
     165        this->tclBind_ = new TclBind(Core::getMediaPathString());
     166        this->tclThreadManager_ = new TclThreadManager(tclBind_->getTclInterpreter());
     167
     168        // create a shell
     169        this->shell_ = new Shell();
     170
     171        // creates the class hierarchy for all classes with factories
     172        Factory::createClassHierarchy();
     173       
     174        this->loaded_ = true;
    104175    }
    105176
     
    109180    Core::~Core()
    110181    {
     182        this->loaded_ = false;
     183
     184        delete this->shell_;
     185        delete this->tclThreadManager_;
     186        delete this->tclBind_;
     187        delete this->luaBind_;
     188        delete this->languageInstance_;
     189        delete this->configFileManager_;
     190        delete this->signalHandler_;
     191
     192        // Destroy command line arguments
     193        CommandLine::destroyAllArguments();
     194        // Also delete external console command that don't belong to an Identifier
     195        CommandExecutor::destroyExternalCommands();
     196
    111197        assert(Core::singletonRef_s);
    112198        Core::singletonRef_s = 0;
     
    292378    }
    293379
    294     /**
    295     @brief
    296         Performs the rather lower level operations just after
    297         int main() has been called.
    298     @remarks
    299         This gets called AFTER pre-main stuff like AddFactory,
    300         SetConsoleCommand, etc.
    301     */
    302     /*static*/ void Core::postMainInitialisation()
    303     {
    304         // set location of the executable
    305         Core::setExecutablePath();
    306 
    307         // Determine whether we have an installed or a binary dir run
    308         // The latter occurs when simply running from the build directory
    309         Core::checkDevBuild();
    310 
    311         // Make sure the directories we write in exist or else make them
    312         Core::createDirectories();
     380
     381    /**
     382    @note
     383        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
     384            (Object-oriented Graphics Rendering Engine)
     385        For the latest info, see http://www.ogre3d.org/
     386
     387        Copyright (c) 2000-2008 Torus Knot Software Ltd
     388
     389        OGRE is licensed under the LGPL. For more info, see OGRE license.
     390    */
     391    void Core::setThreadAffinity(int limitToCPU)
     392    {
     393        if (limitToCPU <= 0)
     394            return;
     395
     396        unsigned int coreNr = limitToCPU - 1;
     397#ifdef ORXONOX_PLATFORM_WINDOWS
     398        // Get the current process core mask
     399        DWORD procMask;
     400        DWORD sysMask;
     401#  if _MSC_VER >= 1400 && defined (_M_X64)
     402        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
     403#  else
     404        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
     405#  endif
     406
     407        // If procMask is 0, consider there is only one core available
     408        // (using 0 as procMask will cause an infinite loop below)
     409        if (procMask == 0)
     410            procMask = 1;
     411
     412        // if the core specified with coreNr is not available, take the lowest one
     413        if (!(procMask & (1 << coreNr)))
     414            coreNr = 0;
     415
     416        // Find the lowest core that this process uses and coreNr suggests
     417        DWORD threadMask = 1;
     418        while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr)))
     419            threadMask <<= 1;
     420
     421        // Set affinity to the first core
     422        SetThreadAffinityMask(GetCurrentThread(), threadMask);
     423#endif
    313424    }
    314425
     
    317428        Compares the executable path with the working directory
    318429    */
    319     /*static*/ void Core::setExecutablePath()
     430    void Core::setExecutablePath()
    320431    {
    321432#ifdef ORXONOX_PLATFORM_WINDOWS
     
    369480        don't write the logs and config files to ~/.orxonox
    370481    */
    371     /*static*/ void Core::checkDevBuild()
     482    void Core::checkDevBuild()
    372483    {
    373484        if (boost::filesystem::exists(executablePath_g / "orxonox_dev_build.keep_me"))
    374485        {
    375486            COUT(1) << "Running from the build tree." << std::endl;
    376             Core::isDevBuild_s = true;
     487            Core::isDevBuild_ = true;
    377488            mediaPath_g  = ORXONOX_MEDIA_DEV_PATH;
    378489            configPath_g = ORXONOX_CONFIG_DEV_PATH;
     
    416527
    417528        // Option to put all the config and log files in a separate folder
    418         if (!CommandLine::getArgument("directory")->hasDefaultValue())
    419         {
    420             std::string directory(CommandLine::getValue("directory").getString());
     529        if (!CommandLine::getArgument("writingPathSuffix")->hasDefaultValue())
     530        {
     531            std::string directory(CommandLine::getValue("writingPathSuffix").getString());
    421532            configPath_g = configPath_g / directory;
    422533            logPath_g    = logPath_g    / directory;
     
    429540        if necessary. Otherwise me might have problems opening those files.
    430541    */
    431     /*static*/ void Core::createDirectories()
     542    void Core::createDirectories()
    432543    {
    433544        std::vector<std::pair<boost::filesystem::path, std::string> > directories;
     
    451562        }
    452563    }
     564
     565    void Core::tick(const Clock& time)
     566    {
     567        this->tclThreadManager_->tick(time.getDeltaTime());
     568    }
    453569}
Note: See TracChangeset for help on using the changeset viewer.