diff --git a/sources.cmake b/sources.cmake index 47d9fe069..6a483354c 100644 --- a/sources.cmake +++ b/sources.cmake @@ -2,6 +2,7 @@ set(STK_SOURCES src/addons/addon.cpp src/addons/addons_manager.cpp +src/addons/inetwork_http.cpp src/addons/network_http.cpp src/addons/news_manager.cpp src/addons/request.cpp diff --git a/src/addons/addons_manager.cpp b/src/addons/addons_manager.cpp index c48342e8c..78d071a91 100644 --- a/src/addons/addons_manager.cpp +++ b/src/addons/addons_manager.cpp @@ -301,9 +301,9 @@ void AddonsManager::downloadIcons() continue; } std::string save = "icons/"+icon; - Request *r = network_http->downloadFileAsynchron(url, save, - /*priority*/1, - /*manage_mem*/true); + Request *r = INetworkHttp::get()->downloadFileAsynchron(url, save, + /*priority*/1, + /*manage_mem*/true); if (r != NULL) r->setAddonIconNotification(&addon); } diff --git a/src/addons/dummy_network_http.hpp b/src/addons/dummy_network_http.hpp index f5ab1552e..461913a93 100644 --- a/src/addons/dummy_network_http.hpp +++ b/src/addons/dummy_network_http.hpp @@ -21,6 +21,7 @@ #include "addons/request.hpp" +#include "addons/inetwork_http.hpp" class XMLNode; @@ -28,7 +29,7 @@ class XMLNode; * \ingroup addonsgroup * Dummy implementation used when curl is not available */ -class DummyNetworkHttp +class DummyNetworkHttp : public INetworkHttp { public: diff --git a/src/addons/inetwork_http.cpp b/src/addons/inetwork_http.cpp new file mode 100644 index 000000000..6dc8f64c6 --- /dev/null +++ b/src/addons/inetwork_http.cpp @@ -0,0 +1,48 @@ +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2012 Joerg Henrichs +// +// 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 3 +// 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 "addons/dummy_network_http.hpp" +#include "addons/inetwork_http.hpp" +#include "addons/network_http.hpp" + +#include + +INetworkHttp *INetworkHttp::m_network_http = NULL; + +/** Creates the network_http instance (depending on compile time options). + */ +void INetworkHttp::create() +{ + assert(m_network_http == NULL); +#ifdef NO_CURL + m_network_http = new DummyNetworkHttp(); +#else + m_network_http = new NetworkHttp(); +#endif +} // create + +// ---------------------------------------------------------------------------- +/** Destroys the network_http instance. + */ +void INetworkHttp::destroy() +{ + if(m_network_http) + { + delete m_network_http; + m_network_http = NULL; + } +} // destroy diff --git a/src/addons/inetwork_http.hpp b/src/addons/inetwork_http.hpp index 8714ebc02..f0b414f21 100644 --- a/src/addons/inetwork_http.hpp +++ b/src/addons/inetwork_http.hpp @@ -30,6 +30,11 @@ class XMLNode; */ class INetworkHttp { + +private: + /** The one instance of this object. */ + static INetworkHttp *m_network_http; + public: /** If stk has permission to access the internet (for news * server etc). @@ -51,9 +56,12 @@ public: int priority = 1, bool manage_memory=true) = 0; virtual void cancelAllDownloads() = 0; + static void create(); + static INetworkHttp *get() { return m_network_http; } + static void destroy(); + }; // NetworkHttp -extern INetworkHttp *network_http; #endif diff --git a/src/addons/network_http.hpp b/src/addons/network_http.hpp index 06b3d8fe5..2cbf19e80 100644 --- a/src/addons/network_http.hpp +++ b/src/addons/network_http.hpp @@ -42,16 +42,6 @@ class XMLNode; */ class NetworkHttp : public INetworkHttp { -public: - /** If stk has permission to access the internet (for news - * server etc). - * IPERM_NOT_ASKED: The user needs to be asked if he wants to - * grant permission - * IPERM_ALLOWED: STK is allowed to access server. - * IPERM_NOT_ALLOWED: STK must not access external servers. */ - enum InternetPermission {IPERM_NOT_ASKED =0, - IPERM_ALLOWED =1, - IPERM_NOT_ALLOWED=2 }; private: /** The list of pointes to all requests. */ diff --git a/src/main.cpp b/src/main.cpp index 0af592b70..85365007a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -141,8 +141,7 @@ #include "main_loop.hpp" #include "addons/addons_manager.hpp" -#include "addons/dummy_network_http.hpp" -#include "addons/network_http.hpp" +#include "addons/inetwork_http.hpp" #include "addons/news_manager.hpp" #include "audio/music_manager.hpp" #include "audio/sfx_manager.hpp" @@ -1101,15 +1100,12 @@ void initRest() news_manager = new NewsManager(); addons_manager = new AddonsManager(); -#ifdef NO_CURL - network_http = new DummyNetworkHttp(); -#else - network_http = new NetworkHttp(); -#endif + INetworkHttp::create(); + // Note that the network thread must be started after the assignment // to network_http (since the thread might use network_http, otherwise // a race condition can be introduced resulting in a crash). - network_http->startNetworkThread(); + INetworkHttp::get()->startNetworkThread(); music_manager = new MusicManager(); sfx_manager = new SFXManager(); // The order here can be important, e.g. KartPropertiesManager needs @@ -1160,14 +1156,14 @@ void cleanSuperTuxKart() { irr_driver->updateConfigIfRelevant(); - if(network_http) - network_http->stopNetworkThread(); + if(INetworkHttp::get()) + INetworkHttp::get()->stopNetworkThread(); //delete in reverse order of what they were created in. //see InitTuxkart() Referee::cleanup(); if(ReplayPlay::get()) ReplayPlay::destroy(); if(race_manager) delete race_manager; - if(network_http) delete network_http; + INetworkHttp::destroy(); if(news_manager) delete news_manager; if(addons_manager) delete addons_manager; if(network_manager) delete network_manager; @@ -1305,7 +1301,7 @@ int main(int argc, char *argv[] ) { StateManager::get()->pushScreen(StoryModeLobbyScreen::getInstance()); if(UserConfigParams::m_internet_status == - NetworkHttp::IPERM_NOT_ASKED) + INetworkHttp::IPERM_NOT_ASKED) { class ConfirmServer : public MessageDialog::IConfirmDialogListener @@ -1313,27 +1309,27 @@ int main(int argc, char *argv[] ) public: virtual void onConfirm() { - delete network_http; + INetworkHttp::destroy(); UserConfigParams::m_internet_status = - NetworkHttp::IPERM_ALLOWED; + INetworkHttp::IPERM_ALLOWED; GUIEngine::ModalDialog::dismiss(); - network_http = new NetworkHttp(); + INetworkHttp::create(); // Note that the network thread must be started after // the assignment to network_http (since the thread // might use network_http, otherwise a race condition // can be introduced resulting in a crash). - network_http->startNetworkThread(); + INetworkHttp::get()->startNetworkThread(); } // onConfirm // -------------------------------------------------------- virtual void onCancel() { - delete network_http; + INetworkHttp::destroy(); UserConfigParams::m_internet_status = - NetworkHttp::IPERM_NOT_ALLOWED; + INetworkHttp::IPERM_NOT_ALLOWED; GUIEngine::ModalDialog::dismiss(); - network_http = new NetworkHttp(); - network_http->startNetworkThread(); + INetworkHttp::create(); + INetworkHttp::get()->startNetworkThread(); } // onCancel }; // ConfirmServer diff --git a/src/states_screens/addons_screen.cpp b/src/states_screens/addons_screen.cpp index a9b5145c9..5d2f943f9 100644 --- a/src/states_screens/addons_screen.cpp +++ b/src/states_screens/addons_screen.cpp @@ -20,7 +20,7 @@ #include #include "addons/addons_manager.hpp" -#include "addons/network_http.hpp" +#include "addons/inetwork_http.hpp" #include "guiengine/CGUISpriteBank.h" #include "guiengine/modaldialog.hpp" #include "guiengine/scalable_font.hpp" @@ -306,7 +306,7 @@ void AddonsScreen::eventCallback(GUIEngine::Widget* widget, if (!m_reloading) { m_reloading = true; - network_http->insertReInit(); + INetworkHttp::get()->insertReInit(); GUIEngine::ListWidget* w_list = getWidget("list_addons"); @@ -376,7 +376,7 @@ void AddonsScreen::onUpdate(float dt, irr::video::IVideoDriver*) { if (m_reloading) { - if(UserConfigParams::m_internet_status!=NetworkHttp::IPERM_ALLOWED) + if(UserConfigParams::m_internet_status!=INetworkHttp::IPERM_ALLOWED) { // not allowed to access the net. how did you get to this menu in // the first place?? diff --git a/src/states_screens/dialogs/addons_loading.cpp b/src/states_screens/dialogs/addons_loading.cpp index 55d151b07..5cc0ef115 100644 --- a/src/states_screens/dialogs/addons_loading.cpp +++ b/src/states_screens/dialogs/addons_loading.cpp @@ -21,7 +21,7 @@ #include #include "addons/addons_manager.hpp" -#include "addons/network_http.hpp" +#include "addons/inetwork_http.hpp" #include "addons/request.hpp" #include "config/user_config.hpp" #include "guiengine/engine.hpp" @@ -281,9 +281,9 @@ void AddonsLoading::startDownload() std::string file = m_addon.getZipFileName(); std::string save = "tmp/" + StringUtils::getBasename(m_addon.getZipFileName()); - m_download_request = network_http->downloadFileAsynchron(file, save, - /*priority*/5, - /*manage memory*/false); + m_download_request = INetworkHttp::get()->downloadFileAsynchron(file, save, + /*priority*/5, + /*manage memory*/false); } // startDownload // ---------------------------------------------------------------------------- diff --git a/src/states_screens/main_menu_screen.cpp b/src/states_screens/main_menu_screen.cpp index 39edb473c..fa10f8f51 100644 --- a/src/states_screens/main_menu_screen.cpp +++ b/src/states_screens/main_menu_screen.cpp @@ -21,7 +21,7 @@ #include -#include "addons/network_http.hpp" +#include "addons/inetwork_http.hpp" #include "challenges/game_slot.hpp" #include "challenges/unlock_manager.hpp" #include "graphics/irr_driver.hpp" @@ -102,7 +102,7 @@ void MainMenuScreen::init() // the key bindings for the first player the default again. input_manager->getDeviceList()->clearLatestUsedDevice(); - if (UserConfigParams::m_internet_status!=NetworkHttp::IPERM_ALLOWED) + if (UserConfigParams::m_internet_status!=INetworkHttp::IPERM_ALLOWED) { IconButtonWidget* w = getWidget("addons"); w->setDeactivated(); @@ -140,7 +140,7 @@ void MainMenuScreen::onUpdate(float delta, irr::video::IVideoDriver* driver) IconButtonWidget* addons_icon = getWidget("addons"); if (addons_icon != NULL) { - if(UserConfigParams::m_internet_status!=NetworkHttp::IPERM_ALLOWED ) + if(UserConfigParams::m_internet_status!=INetworkHttp::IPERM_ALLOWED ) { addons_icon->setDeactivated(); addons_icon->resetAllBadges(); @@ -189,7 +189,7 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name, std::string selection = ribbon->getSelectionIDString(PLAYER_ID_GAME_MASTER); - /* + if (selection == "story") { StateManager::get()->enterGameState(); @@ -206,8 +206,7 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name, //race_manager->startSingleRace("introcutscene2", 999, false); return; } - */ - + #if DEBUG_MENU_ITEM if (selection == "options") { @@ -300,7 +299,6 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name, } else if (selection == "about") { - CreditsScreen::getInstance()->setVictoryMusic(false); StateManager::get()->pushScreen(CreditsScreen::getInstance()); } else if (selection == "help") @@ -312,25 +310,12 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name, GameSlot* slot = unlock_manager->getCurrentSlot(); if (slot->isFirstTime()) { - /* slot->setFirstTime(false); unlock_manager->save(); KartSelectionScreen* s = KartSelectionScreen::getInstance(); s->setMultiplayer(false); s->setFromOverworld(true); StateManager::get()->pushScreen( s ); - */ - StateManager::get()->enterGameState(); - race_manager->setMinorMode(RaceManager::MINOR_MODE_CUTSCENE); - race_manager->setNumKarts( 0 ); - race_manager->setNumPlayers(0); - race_manager->setNumLocalPlayers(0); - race_manager->startSingleRace("introcutscene", 999, false); - - std::vector parts; - parts.push_back("introcutscene"); - parts.push_back("introcutscene2"); - ((CutsceneWorld*)World::getWorld())->setParts(parts); } else { @@ -359,7 +344,7 @@ void MainMenuScreen::onDisabledItemClicked(const std::string& item) { if (item == "addons") { - if (UserConfigParams::m_internet_status != NetworkHttp::IPERM_ALLOWED) + if (UserConfigParams::m_internet_status != INetworkHttp::IPERM_ALLOWED) { new MessageDialog( _("The add-ons module is currently disabled in " "the Options screen") ); diff --git a/src/states_screens/options_screen_ui.cpp b/src/states_screens/options_screen_ui.cpp index d621aacd5..d6ad15e37 100644 --- a/src/states_screens/options_screen_ui.cpp +++ b/src/states_screens/options_screen_ui.cpp @@ -17,7 +17,7 @@ #include "states_screens/options_screen_ui.hpp" -#include "addons/network_http.hpp" +#include "addons/inetwork_http.hpp" #include "audio/music_manager.hpp" #include "audio/sfx_manager.hpp" #include "audio/sfx_base.hpp" @@ -125,7 +125,7 @@ void OptionsScreenUI::init() CheckBoxWidget* news = getWidget("enable-internet"); assert( news != NULL ); news->setState( UserConfigParams::m_internet_status - ==NetworkHttp::IPERM_ALLOWED ); + ==INetworkHttp::IPERM_ALLOWED ); CheckBoxWidget* min_gui = getWidget("minimal-racegui"); assert( min_gui != NULL ); min_gui->setState( UserConfigParams::m_minimal_race_gui); @@ -223,19 +223,19 @@ void OptionsScreenUI::eventCallback(Widget* widget, const std::string& name, con { CheckBoxWidget* news = getWidget("enable-internet"); assert( news != NULL ); - if(network_http) + if(INetworkHttp::get()) { - network_http->stopNetworkThread(); - delete network_http; + INetworkHttp::get()->stopNetworkThread(); + INetworkHttp::destroy(); } UserConfigParams::m_internet_status = - news->getState() ? NetworkHttp::IPERM_ALLOWED - : NetworkHttp::IPERM_NOT_ALLOWED; - network_http = new NetworkHttp(); + news->getState() ? INetworkHttp::IPERM_ALLOWED + : INetworkHttp::IPERM_NOT_ALLOWED; + INetworkHttp::create(); // Note that the network thread must be started after the assignment // to network_http (since the thread might use network_http, otherwise // a race condition can be introduced resulting in a crash). - network_http->startNetworkThread(); + INetworkHttp::get()->startNetworkThread(); } else if (name=="minimal-racegui") {