1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
// Description:
// Online update check.
//
// Copyright (C) 2005 Frank Becker
//
// 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 2 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
//
#include <defines.h>
#include <Config.hpp>
#include <OnlineUpdate.hpp>
#include <OSName.hpp>
#include <Constants.hpp>
#include <zStream.hpp>
#include <gettimeofday.hpp>
#include <curl/curl.h>
#include <tinyxml.h>
#include <sys/stat.h>
#include <sys/types.h>
int const UPDATE_INTERVAL = 60*60*24*7; //once per week
OnlineUpdate::OnlineUpdate( void):
_thread(0),
_status( eNotChecking),
_isLatest(true)
{
}
OnlineUpdate::~OnlineUpdate()
{
if( _thread)
{
LOG_INFO << "Waiting for online update thread to finish\n";
int status;
SDL_WaitThread( _thread, &status);
_thread = 0;
LOG_INFO << "Online update thread done status = " << status << endl;
}
list<NewsItem*>::iterator i;
for( i=_newsItemList.begin(); i!=_newsItemList.end(); i++)
{
delete (*i);
}
_newsItemList.clear();
}
void OnlineUpdate::init( const string &defaultUpdateURL)
{
_updateURL = defaultUpdateURL;
ConfigS::instance()->getString( "masterURL", _updateURL);
}
void OnlineUpdate::getUpdate( void)
{
bool onlineCheck = false;
ConfigS::instance()->getBoolean( "onlineCheck", onlineCheck);
if( onlineCheck)
{
_text = "";
_status = eDownloading;
#if 1
_thread = SDL_CreateThread(OnlineUpdate::run, (void*)this);
if ( _thread == NULL )
{
_status = eFailure;
LOG_ERROR << "Unable to create thread.\n";
}
#else
OnlineUpdate::run( this);
#endif
}
}
size_t OnlineUpdate::write(void *buffer, size_t size, size_t nmemb, void *data)
{
char *tmpBuf = new char[size * nmemb +1];
memcpy( tmpBuf, buffer, size * nmemb);
tmpBuf[size * nmemb] = '\0';
OnlineUpdate *onlineUpdate = (OnlineUpdate*)data;
onlineUpdate->_text += tmpBuf;
// LOG_INFO << tmpBuf << endl;
delete [] tmpBuf;
return size*nmemb;
}
void OnlineUpdate::loadUpdateCache(const string &updateCache)
{
LOG_INFO << "Loading cached update\n";
ifstream infile( updateCache.c_str(), ios::in | ios::binary);
if( infile.good())
{
ziStream zin(infile);
string line;
while( !getline( zin, line).eof())
{
_text += line;
}
if( process())
_status = eSuccess;
else
_status = eFailure;
}
}
int OnlineUpdate::run(void *data)
{
OnlineUpdate *onlineUpdate = (OnlineUpdate*)data;
string updateCache = ConfigS::instance()->getConfigDirectory() + "update.cache";
struct stat statInfo;
if( stat( updateCache.c_str(), &statInfo) != -1)
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
int delta = tv.tv_sec - statInfo.st_mtime;
if( delta < UPDATE_INTERVAL)
{
onlineUpdate->loadUpdateCache( updateCache);
return 0;
}
}
CURL *handle = curl_easy_init();
if( handle)
{
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, OnlineUpdate::write);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, data);
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(handle, CURLOPT_TIMEOUT, 30);
curl_easy_setopt(handle, CURLOPT_USERAGENT, PACKAGE " " VERSION " " OSNAME " (" __DATE__ " " __TIME__ ")");
curl_easy_setopt(handle, CURLOPT_URL, onlineUpdate->_updateURL.c_str());
// curl_easy_setopt(handle, CURLOPT_REFERER, "None");
LOG_INFO << "Checking for updates at " << onlineUpdate->_updateURL << endl;
CURLcode success = curl_easy_perform(handle);
if( success == CURLE_OK)
{
// LOG_INFO << "Update text: [" << onlineUpdate->_text << "]\n";
if( onlineUpdate->process())
{
ofstream outfile( updateCache.c_str(), ios::out | ios::binary);
if( outfile.good())
{
zoStream zout(outfile);
zout << onlineUpdate->_text;
}
onlineUpdate->_status = eSuccess;
}
else
onlineUpdate->_status = eFailure;
}
else
{
onlineUpdate->_status = eFailure;
}
curl_easy_cleanup(handle);
}
else
{
onlineUpdate->_status = eFailure;
}
if( onlineUpdate->_status != eSuccess)
{
LOG_WARNING << "Failed to get online update - trying cache.\n";
onlineUpdate->loadUpdateCache( updateCache);
}
return 0;
}
int getVersionID( const string &version)
{
int major = 0;
int minor = 0;
int patch = 0;
sscanf( version.c_str(), "%d.%d.%d", &major, &minor, &patch);
return major*10000 + minor*100 + patch;
}
bool OnlineUpdate::process( void)
{
bool result = false;
TiXmlDocument *doc = new TiXmlDocument();
doc->Parse( _text.c_str());
if( doc->Error())
{
LOG_ERROR << "Failed to parse xml content" << endl;
LOG_ERROR << "--> XML: " << doc->ErrorDesc() << endl;
}
else
{
TiXmlNode *node;
node = doc->FirstChild( "Program");
while( node)
{
TiXmlElement *element = node->ToElement();
const string *name = element->Attribute("Name");
if( name && (*name == GAMETITLE))
{
// LOG_INFO << "Found " << GAMETITLE << " update section\n";
TiXmlNode *release = node->FirstChild( "Release");
int thisVersionID = getVersionID(GAMEVERSION);
int latestVersionID = 0;
while( release)
{
element = release->ToElement();
const string *version = element->Attribute("Version");
const string *date = element->Attribute("Date");
if( version && date)
{
// LOG_INFO << "Version " << *version << " released on " << *date << endl;
int versionID = getVersionID(*version);
if( versionID > latestVersionID)
{
latestVersionID = versionID;
_latestVersion = *version;
_latestVersionDate = *date;
TiXmlNode *body = release->FirstChild( "Body");
if( body)
{
TiXmlText *leaf = body->FirstChild()->ToText();
if( leaf)
_latestVersionText = leaf->Value();
}
}
if( thisVersionID == versionID)
{
// LOG_INFO << "Looking for config updates\n";
TiXmlNode *updateVarNode=release->FirstChild( "UpdateVar");
if( updateVarNode)
{
TiXmlElement *updateVar=updateVarNode->ToElement();
TiXmlAttribute *attribute = updateVar->FirstAttribute();
while( attribute)
{
string key = attribute->Name();
string value = attribute->Value();
// LOG_INFO << "Update config " << key << "=" << value << endl;
ConfigS::instance()->updateKeyword( key, value);
attribute = attribute->Next();
}
}
}
}
release = release->NextSibling( "Release");
}
if( latestVersionID > thisVersionID)
{
_isLatest = false;
LOG_INFO << "A newer version is available\n";
LOG_INFO << "Version " << _latestVersion << " released on " << _latestVersionDate << endl;
// LOG_INFO << "** " << _latestVersionText << endl;
}
break;
}
node = node->NextSibling( "Program");
}
node = doc->FirstChild( "NewsItem");
while( node)
{
TiXmlElement *element = node->ToElement();
const string *title = element->Attribute("Title");
const string *date = element->Attribute("Date");
if( title && date)
{
TiXmlNode *body = element->FirstChild( "Body");
if( body)
{
TiXmlText *leaf = body->FirstChild()->ToText();
if( leaf)
{
NewsItem *newsItem = new NewsItem;
newsItem->title = *title;
newsItem->date = *date;
newsItem->text = leaf->Value();
newsItem->r = 1.0;
newsItem->g = 1.0;
newsItem->b = 1.0;
TiXmlElement *bodyElement = body->ToElement();
TiXmlAttribute *attribute = bodyElement->FirstAttribute();
while( attribute)
{
string key = attribute->Name();
string value = attribute->Value();
if( key == "Color")
{
sscanf( value.c_str(), "%f,%f,%f",
&newsItem->r, &newsItem->g, &newsItem->b);
}
attribute = attribute->Next();
}
LOG_INFO << "News: " << newsItem->title << " " << newsItem->date << endl;
LOG_INFO << " Text: " << newsItem->text << endl;
_newsItemList.insert( _newsItemList.end(), newsItem);
}
}
}
node = node->NextSibling( "NewsItem");
}
result = true;
}
delete doc;
return result;
}
|