From 1815b6f2239bf6bef292af4ed9b32b5272dab6d0 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Wed, 23 Mar 2011 12:07:16 +0000 Subject: [PATCH] Added files missing in previous commit (started adding support for new addon list, but that's work-in-progress). git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@8034 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/addons/network_http.cpp | 70 +++++++++++++++++++++++++++++++++---- src/addons/network_http.hpp | 2 ++ src/config/user_config.hpp | 8 ++++- src/graphics/irr_driver.cpp | 2 +- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/addons/network_http.cpp b/src/addons/network_http.cpp index 2264973ae..8b1b874be 100644 --- a/src/addons/network_http.cpp +++ b/src/addons/network_http.cpp @@ -56,6 +56,9 @@ NetworkHttp *network_http; * would need an additional handle to get the right data back). * This separate thread is running in NetworkHttp::mainLoop, and is being * waken up if a command is issued (e.g. using downloadFileAsynchronous). + * While UserConfigParams are modified, they can't (easily) be saved here, + * since the user might trigger another save in the menu (potentially + * ending up with an corrupted file). */ NetworkHttp::NetworkHttp() : m_news(""), m_progress(-1.0f), m_abort(false) { @@ -86,20 +89,21 @@ void *NetworkHttp::mainLoop(void *obj) // or if the time of the last update was more than news_frequency ago. bool download = UserConfigParams::m_news_last_updated==0 || UserConfigParams::m_news_last_updated - +UserConfigParams::m_news_frequency - > Time::getTimeSinceEpoch(); + +UserConfigParams::m_news_frequency + < Time::getTimeSinceEpoch(); if(!download) { // If there is no old news message file, force a new download std::string xml_file = file_manager->getAddonsFile("news.xml"); - if(xml_file=="") + if(!file_manager->fileExists(xml_file)) download=true; } - // Initialise the online portion of the addons manager. + // Initialise the online portion of the addons manager. if(download && UserConfigParams::m_verbosity>=3) printf("[addons] Downloading list.\n"); + if(!download || me->downloadFileSynchron("news.xml")) { std::string xml_file = file_manager->getAddonsFile("news.xml"); @@ -107,7 +111,8 @@ void *NetworkHttp::mainLoop(void *obj) UserConfigParams::m_news_last_updated = Time::getTimeSinceEpoch(); const XMLNode *xml = new XMLNode(xml_file); me->checkNewServer(xml); - me->updateNews(xml); + me->updateNews(xml, xml_file); + me->loadAddonsList(xml, xml_file); addons_manager->initOnline(xml); if(UserConfigParams::m_verbosity>=3) printf("[addons] Addons manager list downloaded\n"); @@ -204,16 +209,24 @@ void NetworkHttp::checkNewServer(const XMLNode *xml) << "[Addons] New server: " << new_server << std::endl; } UserConfigParams::m_server_addons = new_server; - user_config->saveConfig(); } } // checkNewServer // ---------------------------------------------------------------------------- /** Updates the 'news' string to be displayed in the main menu. + * \param xml The XML data from the news file. + * \param filename The filename of the news xml file. Only needed + * in case of an error (e.g. the file might be corrupted) + * - the file will be deleted so that on next start of stk it + * will be updated again. */ -void NetworkHttp::updateNews(const XMLNode *xml) +void NetworkHttp::updateNews(const XMLNode *xml, const std::string &filename) { bool error = true; + int frequency=0; + if(xml->get("frequency", &frequency)) + UserConfigParams::m_news_frequency = frequency; + for(unsigned int i=0; igetNumNodes(); i++) { const XMLNode *node = xml->getNode(i); @@ -224,10 +237,53 @@ void NetworkHttp::updateNews(const XMLNode *xml) error = false; } if(error) + { + // In case of an error (e.g. the file only contains + // an error message from the server), delete the file + // so that it is not read again (and this will force + // a new read on the next start, instead of waiting + // for some time). + file_manager->removeFile(filename); m_news.set("Can't access stkaddons server..."); + } } // updateNews +// ---------------------------------------------------------------------------- +/** Checks the last modified date and if necessary updates the + * list of addons. + * \param xml The news xml file which contains the data about + * the addon list. + * \param filename The filename of the news xml file. Only needed + * in case of an error (e.g. it might contain a corrupted + * url) - the file will be deleted so that on next start + * of stk it will be updated again. + */ +void NetworkHttp::loadAddonsList(const XMLNode *xml, + const std::string &filename) +{ + std::string addon_list_url(""); + Time::TimeType mtime(0); + const XMLNode *include = xml->getNode("include"); + if(include) + { + include->get("file", &addon_list_url); + include->get("mtime", &mtime ); + } + if(addon_list_url.size()==0) + { + file_manager->removeFile(filename); + m_news.set("Can't access stkaddons server..."); + return; + } + + bool download = mtime > UserConfigParams::m_addons_last_updated; + if(!download) + { + std::string filename=file_manager->getAddonsFile("addon_list.xml"); + } +} // loadAddonsList + // ---------------------------------------------------------------------------- /** Returns the last loaded news message (using mutex to make sure a valid * value is available). diff --git a/src/addons/network_http.hpp b/src/addons/network_http.hpp index 441e329a8..c62d5b40c 100644 --- a/src/addons/network_http.hpp +++ b/src/addons/network_http.hpp @@ -75,6 +75,8 @@ private: void updateNews(const XMLNode *xml, const std::string &filename); + void loadAddonsList(const XMLNode *xml, + const std::string &filename); std::string downloadToStrInternal(std::string url); bool downloadFileInternal(const std::string &file, const std::string &save_filename, diff --git a/src/config/user_config.hpp b/src/config/user_config.hpp index 18046a24a..09d6da7ec 100644 --- a/src/config/user_config.hpp +++ b/src/config/user_config.hpp @@ -423,7 +423,7 @@ namespace UserConfigParams "The server used for addon.") ); PARAM_PREFIX TimeUserConfigParam m_news_last_updated - PARAM_DEFAULT( TimeUserConfigParam(0, "last_updated", + PARAM_DEFAULT( TimeUserConfigParam(0, "news_last_updated", &m_addon_group, "Time news was updated last.") ); @@ -432,6 +432,12 @@ namespace UserConfigParams &m_addon_group, "How often news should be updated.") ); + PARAM_PREFIX TimeUserConfigParam m_addons_last_updated + PARAM_DEFAULT( TimeUserConfigParam(0, "addon_last_updated", + &m_addon_group, + "Time addon-list was updated last.") ); + + PARAM_PREFIX StringUserConfigParam m_language PARAM_DEFAULT( StringUserConfigParam("system", "language", "Which language to use (language code or 'system')") ); diff --git a/src/graphics/irr_driver.cpp b/src/graphics/irr_driver.cpp index 107c79615..6b6a2abfb 100644 --- a/src/graphics/irr_driver.cpp +++ b/src/graphics/irr_driver.cpp @@ -682,7 +682,7 @@ scene::IMesh *IrrDriver::createTexturedQuadMesh(const video::SMaterial *material void IrrDriver::removeNode(scene::ISceneNode *node) { node->remove(); -} // removeMesh +} // removeNode // ---------------------------------------------------------------------------- /** Removes a mesh from the mesh cache, freeing the memory.