Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem3/src/core/Core.cc @ 2685

Last change on this file since 2685 was 2685, checked in by rgrieder, 15 years ago

Fixed install target:

  • log and config file go a to separate folder each
  • The SignalHandler crash log is now "orxonox_crash.log" to avoid opening the file twice which might result in problems
  • moved tcl scripts to media/tcl8.#/ as a temporary solution. I've also created a ticket to fix this.
  • UPDATE YOUR MEDIA REPOSITORY
  • orxonox.log pre-main gets written to either %TEMP% (windows) or /tmp (Unix) and when the path was set, the content is copied.
  • removed Settings class and moved media path to Core
  • media, log and config path are now all in Core where only the media path can be configured via ini file or command line
  • Core::isDevBuild() tells whether we are running in the build or the installation directory (determined by the presence of "orxonox_dev_build.kepp_me" in the binary dir)
  • renamed Settings::getDataPath to Core::getMediaPath
  • Property svn:eol-style set to native
File size: 9.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29/**
30    @file
31    @brief Implementation of the Core class.
32*/
33
34#include "Core.h"
35#include <cassert>
36#include "Language.h"
37#include "CoreIncludes.h"
38#include "ConfigValueIncludes.h"
39#include "LuaBind.h"
40#include "CommandLine.h"
41
42namespace orxonox
43{
44    bool Core::bShowsGraphics_s = false;
45    bool Core::bHasServer_s     = false;
46    bool Core::bIsClient_s      = false;
47    bool Core::bIsStandalone_s  = false;
48    bool Core::bIsMaster_s      = false;
49
50    bool Core::isDevBuild_s     = false;
51    std::string Core::configPath_s(ORXONOX_CONFIG_INSTALL_PATH); // from OrxonoxConfig.h
52    std::string Core::logPath_s   (ORXONOX_LOG_INSTALL_PATH);    // from OrxonoxConfig.h
53
54    Core* Core::singletonRef_s = 0;
55
56    SetCommandLineArgument(mediaPath, "").information("PATH");
57
58    /**
59        @brief Constructor: Registers the object and sets the config-values.
60        @param A reference to a global variable, used to avoid an infinite recursion in getSoftDebugLevel()
61    */
62    Core::Core()
63    {
64        RegisterRootObject(Core);
65
66        assert(Core::singletonRef_s == 0);
67        Core::singletonRef_s = this;
68
69        this->bInitializeRandomNumberGenerator_ = false;
70        this->setConfigValues();
71
72        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used
73        OutputHandler::getOutStream().setLogPath(Core::logPath_s);
74
75        // Possible media path override by the command line
76        if (!CommandLine::getArgument("mediaPath")->hasDefaultValue())
77        {
78            std::string mediaPath = CommandLine::getValue("mediaPath");
79            Core::tsetMediaPath(mediaPath);
80        }
81    }
82
83    /**
84        @brief Sets the bool to true to avoid static functions accessing a deleted object.
85    */
86    Core::~Core()
87    {
88        assert(Core::singletonRef_s);
89        Core::singletonRef_s = 0;
90    }
91
92    /**
93        @brief Function to collect the SetConfigValue-macro calls.
94    */
95    void Core::setConfigValues()
96    {
97#ifdef NDEBUG
98        const unsigned int defaultLevelConsole = 1;
99        const unsigned int defaultLevelLogfile = 3;
100        const unsigned int defaultLevelShell   = 1;
101#else
102        const unsigned int defaultLevelConsole = 3;
103        const unsigned int defaultLevelLogfile = 4;
104        const unsigned int defaultLevelShell   = 3;
105#endif
106        SetConfigValue(softDebugLevelConsole_, defaultLevelConsole)
107            .description("The maximal level of debug output shown in the console").callback(this, &Core::debugLevelChanged);
108        SetConfigValue(softDebugLevelLogfile_, defaultLevelLogfile)
109            .description("The maximal level of debug output shown in the logfile").callback(this, &Core::debugLevelChanged);
110        SetConfigValue(softDebugLevelShell_, defaultLevelShell)
111            .description("The maximal level of debug output shown in the ingame shell").callback(this, &Core::debugLevelChanged);
112
113        SetConfigValue(language_, Language::getLanguage().defaultLanguage_).description("The language of the ingame text").callback(this, &Core::languageChanged);
114        SetConfigValue(bInitializeRandomNumberGenerator_, true).description("If true, all random actions are different each time you start the game").callback(this, &Core::initializeRandomNumberGenerator);
115
116        // Media path (towards config and log path) is ini-configurable
117        const char* defaultMediaPath = ORXONOX_MEDIA_INSTALL_PATH;
118        if (Core::isDevBuild())
119            defaultMediaPath = ORXONOX_MEDIA_DEV_PATH;
120
121        SetConfigValue(mediaPath_, defaultMediaPath)
122            .description("Relative path to the game data.").callback(this, &Core::mediaPathChanged);
123
124    }
125
126    /**
127        @brief Callback function if the debug level has changed.
128    */
129    void Core::debugLevelChanged()
130    {
131        // softDebugLevel_ is the maximum of the 3 variables
132        this->softDebugLevel_ = this->softDebugLevelConsole_;
133        if (this->softDebugLevelLogfile_ > this->softDebugLevel_)
134            this->softDebugLevel_ = this->softDebugLevelLogfile_;
135        if (this->softDebugLevelShell_ > this->softDebugLevel_)
136            this->softDebugLevel_ = this->softDebugLevelShell_;
137
138        OutputHandler::setSoftDebugLevel(OutputHandler::LD_All,     this->softDebugLevel_);
139        OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_);
140        OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_);
141        OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell,   this->softDebugLevelShell_);
142    }
143
144    /**
145        @brief Callback function if the language has changed.
146    */
147    void Core::languageChanged()
148    {
149        // Read the translation file after the language was configured
150        Language::getLanguage().readTranslatedLanguageFile();
151    }
152
153    /**
154    @brief
155        Callback function if the media path has changed.
156    */
157    void Core::mediaPathChanged()
158    {
159        if (mediaPath_ != "" && mediaPath_[mediaPath_.size() - 1] != '/')
160        {
161            ModifyConfigValue(mediaPath_, set, mediaPath_ + "/");
162        }
163
164        if (mediaPath_ == "")
165        {
166            ModifyConfigValue(mediaPath_, set, "/");
167            COUT(2) << "Warning: Data path set to \"/\", is that really correct?" << std::endl;
168        }
169    }
170
171    /**
172        @brief Returns the softDebugLevel for the given device (returns a default-value if the class ist right about to be created).
173        @param device The device
174        @return The softDebugLevel
175    */
176    int Core::getSoftDebugLevel(OutputHandler::OutputDevice device)
177    {
178        switch (device)
179        {
180        case OutputHandler::LD_All:
181            return Core::getInstance().softDebugLevel_;
182        case OutputHandler::LD_Console:
183            return Core::getInstance().softDebugLevelConsole_;
184        case OutputHandler::LD_Logfile:
185            return Core::getInstance().softDebugLevelLogfile_;
186        case OutputHandler::LD_Shell:
187            return Core::getInstance().softDebugLevelShell_;
188        default:
189            assert(0);
190            return 2;
191        }
192    }
193
194     /**
195        @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value.
196        @param device The device
197        @param level The level
198    */
199     void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level)
200     {
201        if (device == OutputHandler::LD_All)
202            Core::getInstance().softDebugLevel_ = level;
203        else if (device == OutputHandler::LD_Console)
204            Core::getInstance().softDebugLevelConsole_ = level;
205        else if (device == OutputHandler::LD_Logfile)
206            Core::getInstance().softDebugLevelLogfile_ = level;
207        else if (device == OutputHandler::LD_Shell)
208            Core::getInstance().softDebugLevelShell_ = level;
209
210        OutputHandler::setSoftDebugLevel(device, level);
211     }
212
213    /**
214        @brief Returns the configured language.
215    */
216    const std::string& Core::getLanguage()
217    {
218        return Core::getInstance().language_;
219    }
220
221    /**
222        @brief Sets the language in the config-file back to the default.
223    */
224    void Core::resetLanguage()
225    {
226        Core::getInstance().resetLanguageIntern();
227    }
228
229    /**
230        @brief Sets the language in the config-file back to the default.
231    */
232    void Core::resetLanguageIntern()
233    {
234        ResetConfigValue(language_);
235    }
236
237    /**
238    @brief
239        Temporary sets the media path
240    @param path
241        The new media path
242    */
243    void Core::_tsetMediaPath(const std::string& path)
244    {
245        if (*path.end() != '/' && *path.end() != '\\')
246        {
247            ModifyConfigValue(mediaPath_, tset, path + "/");
248        }
249        else
250        {
251            ModifyConfigValue(mediaPath_, tset, path);
252        }
253    }
254
255    void Core::initializeRandomNumberGenerator()
256    {
257        static bool bInitialized = false;
258        if (!bInitialized && this->bInitializeRandomNumberGenerator_)
259        {
260            srand(time(0));
261            rand();
262            bInitialized = true;
263        }
264    }
265
266    /*static*/ void Core::setDevBuild()
267    {
268        // Be careful never to call this function before main()!
269
270        Core::isDevBuild_s = true;
271        // Constants taken from OrxonoxConfig.h
272        Core::configPath_s = ORXONOX_CONFIG_DEV_PATH;
273        Core::logPath_s    = ORXONOX_LOG_DEV_PATH;
274    }
275}
Note: See TracBrowser for help on using the repository browser.