Revert "Move version check out of main()"

This reverts commit 97da8593d9.
This commit is contained in:
Simon Robertshaw 2011-08-19 23:44:09 +01:00
parent 799c99257c
commit 2e2694e0fb
3 changed files with 137 additions and 166 deletions

View File

@ -20,16 +20,9 @@
#ifndef UPDATE_H
#define UPDATE_H
#include <defines.h>
extern int version_check;
char *exe_name(void);
int update_start(char *data, int len);
int update_finish(void);
void update_cleanup(void);
void update_finalize(pixel *vid_buf);
void update_notify_ui_draw(pixel *vid_buf);
void update_notify_ui_process(pixel *vid_buf, int mb, int mbq, int mx, int my);
#endif

View File

@ -164,6 +164,10 @@ typedef struct
pixel *vid;
} upstruc;
#ifdef BETA
static const char *old_ver_msg_beta = "A new beta is available - click here!";
#endif
static const char *old_ver_msg = "A new version is available - click here!";
char new_message_msg[255];
float mheat = 0.0f;
@ -1408,6 +1412,30 @@ char *tag = "(c) 2008-9 Stanislaw Skowronek";
int itc = 0;
char itc_msg[64] = "[?]";
char my_uri[] = "http://" SERVER "/Update.api?Action=Download&Architecture="
#if defined WIN32
"Windows32"
#elif defined LIN32
"Linux32"
#elif defined LIN64
"Linux64"
#elif defined MACOSX
"MacOSX"
#else
"Unknown"
#endif
"&InstructionSet="
#if defined X86_SSE3
"SSE3"
#elif defined X86_SSE2
"SSE2"
#elif defined X86_SSE
"SSE"
#else
"SSE"
#endif
;
int set_scale(int scale, int kiosk){
int old_scale = sdl_scale, old_kiosk = kiosk_enable;
sdl_scale = scale;
@ -1587,9 +1615,9 @@ int main(int argc, char *argv[])
int currentTime = 0;
int FPS = 0, pastFPS = 0, elapsedTime = 0;
void *http_ver_check, *http_session_check = NULL;
char *check_data=NULL, *tmp;
char *ver_data=NULL, *check_data=NULL, *tmp;
//char console_error[255] = "";
int result, i, j, bq, fire_fc=0, do_s_check=0, http_ret=0,http_s_ret=0, new_message_len=0;
int result, i, j, bq, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len, new_message_len=0;
#ifdef INTERNAL
int vs = 0;
#endif
@ -1640,6 +1668,19 @@ int main(int argc, char *argv[])
#ifdef MT
numCores = core_count();
#endif
//TODO: Move out version stuff
#ifdef BETA
if (is_beta)
{
old_ver_len = textwidth((char*)old_ver_msg_beta);
}
else
{
old_ver_len = textwidth((char*)old_ver_msg);
}
#else
old_ver_len = textwidth((char*)old_ver_msg);
#endif
menu_count();
parts = calloc(sizeof(particle), NPART);
@ -1732,6 +1773,7 @@ int main(int argc, char *argv[])
return 1;
}
http_ver_check = http_async_req_start(NULL, "http://" SERVER "/Update.api?Action=CheckVersion", NULL, 0, 0);
if (svf_login) {
http_session_check = http_async_req_start(NULL, "http://" SERVER "/Login.api?Action=CheckSession", NULL, 0, 0);
http_auth_headers(http_session_check, svf_user_id, NULL, svf_session_id);
@ -1873,6 +1915,28 @@ int main(int argc, char *argv[])
draw_debug_info(vid_buf, lm, lx, ly, x, y, line_x, line_y);
}
if (http_ver_check)
{
if (!do_check && http_async_req_status(http_ver_check))
{
ver_data = http_async_req_stop(http_ver_check, &http_ret, NULL);
if (http_ret==200 && ver_data)
{
#ifdef BETA
if (sscanf(ver_data, "%d.%d.%d", &major, &minor, &is_beta)==3)
if (major>SAVE_VERSION || (major==SAVE_VERSION && minor>MINOR_VERSION) || (major==SAVE_VERSION && is_beta == 0))
old_version = 1;
#else
if (sscanf(ver_data, "%d.%d", &major, &minor)==2)
if (major>SAVE_VERSION || (major==SAVE_VERSION && minor>MINOR_VERSION))
old_version = 1;
#endif
free(ver_data);
}
http_ver_check = NULL;
}
do_check = (do_check+1) & 15;
}
if (http_session_check)
{
if (!do_s_check && http_async_req_status(http_session_check))
@ -2587,9 +2651,60 @@ int main(int argc, char *argv[])
{
open_link("http://" SERVER "/Conversations.html");
}
if (update_flag) update_finalize(vid_buf);
if (version_check) update_notify_ui_process(vid_buf, b, bq, x, y);
if (update_flag)
{
info_box(vid_buf, "Finalizing update...");
if (last_major>SAVE_VERSION || (last_major==SAVE_VERSION && last_minor>=MINOR_VERSION))
{
update_cleanup();
error_ui(vid_buf, 0, "Update failed - try downloading a new version.");
}
else
{
if (update_finish())
error_ui(vid_buf, 0, "Update failed - try downloading a new version.");
else
info_ui(vid_buf, "Update success", "You have successfully updated the Powder Toy!");
}
update_flag = 0;
}
if (b && !bq && x>=(XRES-19-old_ver_len)*sdl_scale &&
x<=(XRES-14)*sdl_scale && y>=(YRES-22)*sdl_scale && y<=(YRES-9)*sdl_scale && old_version)
{
tmp = malloc(64);
#ifdef BETA
if (is_beta)
{
sprintf(tmp, "Your version: %d (Beta %d), new version: %d (Beta %d).", SAVE_VERSION, MINOR_VERSION, major, minor);
}
else
{
sprintf(tmp, "Your version: %d (Beta %d), new version: %d.%d.", SAVE_VERSION, MINOR_VERSION, major, minor);
}
#else
sprintf(tmp, "Your version: %d.%d, new version: %d.%d.", SAVE_VERSION, MINOR_VERSION, major, minor);
#endif
if (confirm_ui(vid_buf, "Do you want to update The Powder Toy?", tmp, "Update"))
{
free(tmp);
tmp = download_ui(vid_buf, my_uri, &i);
if (tmp)
{
save_presets(1);
if (update_start(tmp, i))
{
update_cleanup();
save_presets(0);
error_ui(vid_buf, 0, "Update failed - try downloading a new version.");
}
else
return 0;
}
}
else
free(tmp);
}
if (y>=sdl_scale*(YRES+(MENUSIZE-20))) //mouse checks for buttons at the bottom, to draw mouseover texts
{
if (x>=189*sdl_scale && x<=202*sdl_scale && svf_login && svf_open && svf_myvote==0)
@ -3235,7 +3350,23 @@ int main(int argc, char *argv[])
drawtext(vid_buf, 16, 20, it_msg, 255, 255, 255, it>51?255:it*5);
}
if (version_check) update_notify_ui_draw(vid_buf);
if (old_version)
{
clearrect(vid_buf, XRES-21-old_ver_len, YRES-24, old_ver_len+9, 17);
#ifdef BETA
if (is_beta)
{
drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg_beta, 255, 216, 32, 255);
}
else
{
drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg, 255, 216, 32, 255);
}
#else
drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg, 255, 216, 32, 255);
#endif
drawrect(vid_buf, XRES-19-old_ver_len, YRES-22, old_ver_len+5, 13, 255, 216, 32, 255);
}
if (svf_messages)
{

View File

@ -38,46 +38,8 @@
#include <sys/param.h>
#endif
#include <graphics.h>
#include <http.h>
#include <interface.h>
#include <misc.h>
#include <update.h>
int version_check = 1;
int version_check_http_counter = 0;
void *version_check_http = NULL;
int version_check_major = 0, version_check_minor = 0, version_check_isbeta = 0;
int old_ver_len = 0;
#ifdef BETA
static const char *old_ver_msg_beta = "A new beta is available - click here!";
#endif
static const char *old_ver_msg = "A new version is available - click here!";
char update_uri[] = "http://" SERVER "/Update.api?Action=Download&Architecture="
#if defined WIN32
"Windows32"
#elif defined LIN32
"Linux32"
#elif defined LIN64
"Linux64"
#elif defined MACOSX
"MacOSX"
#else
"Unknown"
#endif
"&InstructionSet="
#if defined X86_SSE3
"SSE3"
#elif defined X86_SSE2
"SSE2"
#elif defined X86_SSE
"SSE"
#else
"SSE"
#endif
;
char *exe_name(void)
{
#if defined WIN32
@ -241,118 +203,3 @@ void update_cleanup(void)
update_finish();
#endif
}
void update_notify_ui_draw(pixel *vid_buf)
{
if (version_check==2)
{
clearrect(vid_buf, XRES-21-old_ver_len, YRES-24, old_ver_len+9, 17);
#ifdef BETA
if (version_check_isbeta)
{
drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg_beta, 255, 216, 32, 255);
}
else
{
drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg, 255, 216, 32, 255);
}
#else
drawtext(vid_buf, XRES-16-old_ver_len, YRES-19, old_ver_msg, 255, 216, 32, 255);
#endif
drawrect(vid_buf, XRES-19-old_ver_len, YRES-22, old_ver_len+5, 13, 255, 216, 32, 255);
}
}
void update_notify_ui_process(pixel *vid_buf, int mb, int mbq, int mx, int my)
{
if (version_check==1 && !version_check_http)
version_check_http = http_async_req_start(NULL, "http://" SERVER "/Update.api?Action=CheckVersion", NULL, 0, 0);
if (version_check_http)
{
if (!version_check_http_counter && http_async_req_status(version_check_http))
{
int http_ret;
char *ver_data;
ver_data = http_async_req_stop(version_check_http, &http_ret, NULL);
if (http_ret==200 && ver_data)
{
#ifdef BETA
if (sscanf(ver_data, "%d.%d.%d", &version_check_major, &version_check_minor, &version_check_isbeta)==3)
if (version_check_major>SAVE_VERSION ||
(version_check_major==SAVE_VERSION && version_check_minor>MINOR_VERSION) ||
(version_check_major==SAVE_VERSION && version_check_isbeta == 0))
version_check = 2;
#else
if (sscanf(ver_data, "%d.%d", &version_check_major, &version_check_minor)==2)
if (version_check_major>SAVE_VERSION || (version_check_major==SAVE_VERSION && version_check_minor>MINOR_VERSION))
version_check = 2;
#endif
#ifdef BETA
if (version_check_isbeta) old_ver_len = textwidth(old_ver_msg_beta);
else old_ver_len = textwidth(old_ver_msg);
#else
old_ver_len = textwidth(old_ver_msg);
#endif
}
if (ver_data) free(ver_data);
version_check_http = NULL;
}
version_check_http_counter = (version_check_http_counter+1) & 15;
}
if (mb && !mbq && mx>=(XRES-19-old_ver_len) &&
mx<=(XRES-14) && my>=(YRES-22) && my<=(YRES-9) && version_check==2)
{
char *tmp = malloc(64);
#ifdef BETA
if (version_check_isbeta)
{
sprintf(tmp, "Your version: %d (Beta %d), new version: %d (Beta %d).", SAVE_VERSION, MINOR_VERSION, version_check_major, version_check_minor);
}
else
{
sprintf(tmp, "Your version: %d (Beta %d), new version: %d.%d.", SAVE_VERSION, MINOR_VERSION, version_check_major, version_check_minor);
}
#else
sprintf(tmp, "Your version: %d.%d, new version: %d.%d.", SAVE_VERSION, MINOR_VERSION, version_check_major, version_check_minor);
#endif
if (confirm_ui(vid_buf, "Do you want to update The Powder Toy?", tmp, "Update"))
{
int i;
free(tmp);
tmp = download_ui(vid_buf, update_uri, &i);
if (tmp)
{
save_presets(1);
if (update_start(tmp, i))
{
update_cleanup();
save_presets(0);
error_ui(vid_buf, 0, "Update failed - try downloading a new version.");
}
else
exit(0);
}
}
else
free(tmp);
}
}
void update_finalize(pixel *vid_buf)
{
if (!update_flag) return;
info_box(vid_buf, "Finalizing update...");
if (last_major>SAVE_VERSION || (last_major==SAVE_VERSION && last_minor>=MINOR_VERSION))
{
update_cleanup();
error_ui(vid_buf, 0, "Update failed - try downloading a new version.");
}
else
{
if (update_finish())
error_ui(vid_buf, 0, "Update failed - try downloading a new version.");
else
info_ui(vid_buf, "Update success", "You have successfully updated the Powder Toy!");
}
update_flag = 0;
}