1 /* TomeNET updater (cross-platform) for client and audio packs. - C. Blue */
2 
3 #include <stdio.h>
4 #include <string.h>
5 
6 #include <stdlib.h> /* system() call */
7 
8 #ifdef WINDOWS
9  #include <windows.h>
10  #include <winreg.h>	/* remote control of installed 7-zip via registry approach */
11  #if 1
12   #include <process.h>	/* use _spawnl() instead of normal system() (WINE bug/Win inconsistency even maybe) */
13  #else
14   #include <wchar.h>	/* use _wspawnl() instead of normal system() (WINE bug/Win inconsistency even maybe) */
15  #endif
16  #define MAX_KEY_LENGTH 255
17  #define MAX_VALUE_NAME 16383
18 
19 // #include <gtk-2.0/gtk.h>
20  #include <gtk/gtk.h>
21 
22 //redundant?
23 // #include <glib/gerror.h>
24 // #include <gdk-pixbuf/gdk-pixbuf.h>
25 
26  char path_7z[1024], path_7z_quoted[1024];
27 
28  #include <unistd.h> /* for chdir() */
29 #else
30  #include <gtk/gtk.h>
31  #include <sys/stat.h> /* for mkdir() */
32 #endif
33 
34 
35 //#define WINDOWS
36 
37 
38 #ifndef WINDOWS
39 static short int posix_terminal = -1;//5
40 
41 unsigned char is_linux = 1;
42 #ifdef __x86_64__
43 unsigned char is_64bit = 1;
44 #else
45 unsigned char is_64bit = 0;
46 #endif
47 #endif
48 
49 GtkWidget *top_window;
50 
51 /* use URL2FILE instead of wget? */
52 //#define USE_URL2FILE
53 
54 
create_pixbuf(const gchar * filename)55 GdkPixbuf *create_pixbuf(const gchar * filename) {
56 	GdkPixbuf *pixbuf;
57 	GError *error = NULL;
58 
59 	pixbuf = gdk_pixbuf_new_from_file(filename, &error);
60 	if (!pixbuf) {
61 		fprintf(stderr, "%s\n", error->message);
62 		g_error_free(error);
63 	}
64 	return pixbuf;
65 }
66 
show_error(gpointer window)67 void show_error(gpointer window) {
68 	GtkWidget *dialog;
69 	dialog = gtk_message_dialog_new(GTK_WINDOW(window),
70 	    GTK_DIALOG_DESTROY_WITH_PARENT,
71 	    GTK_MESSAGE_ERROR,
72 	    GTK_BUTTONS_OK,
73 #ifdef WINDOWS
74 	    "You must first install 7-zip from www.7-zip.org !");
75 #else
76 	    "You must install 7-zip-GUI (7zG) first! Package name is 'p7zip'.");
77 #endif
78 	gtk_window_set_title(GTK_WINDOW(dialog), "Error");
79 	gtk_dialog_run(GTK_DIALOG(dialog));
80 	gtk_widget_destroy(dialog);
81 }
82 
show_error_broken(gpointer window)83 void show_error_broken(gpointer window) {
84 	GtkWidget *dialog;
85 	dialog = gtk_message_dialog_new(GTK_WINDOW(window),
86 	    GTK_DIALOG_DESTROY_WITH_PARENT,
87 	    GTK_MESSAGE_ERROR,
88 	    GTK_BUTTONS_OK,
89 	    "The download link seems to be invalid or deleted!\n" \
90 	    "Please notify us about this error message and make\n" \
91 	    "sure it was not just an internet connection problem.");
92 	gtk_window_set_title(GTK_WINDOW(dialog), "Error");
93 	gtk_dialog_run(GTK_DIALOG(dialog));
94 	gtk_widget_destroy(dialog);
95 }
96 
97 //void install_client(GtkWidget *widget, gpointer label) {
install_client(GtkButton * button,gpointer label)98 void install_client(GtkButton *button, gpointer label) {
99 	gtk_button_set_label(button, "..please wait..");
100 	while(gtk_events_pending())
101 	    gtk_main_iteration();
102 
103 #ifdef WINDOWS
104 	int res;
105 
106 	remove("TomeNET-latest-install.exe");
107  #if 0 /* winbash is dysfunctional, no alternatives available ----------------------------------------- */
108   #if 0 /* problem: 7z doesn't support stripping a path level */
109 	/* Download */
110 	_spawnl(_P_WAIT, "updater/dl_win32/winbash/wget.exe", "wget.exe", "http://www.tomenet.eu/downloads/TomeNET-latest-client.zip", NULL); /* supposed to work on WINE, yet crashes if not exit(0)ing next oO */
111 	/* Extract */
112 	_spawnl(_P_WAIT, path_7z, path_7z_quoted, "x", "TomeNET-latest-client.zip", NULL);
113   #else /* workaround: we just use the installer instead */
114 	/* Download */
115 	_spawnl(_P_WAIT, "updater/dl_win32/winbash/wget.exe", "wget.exe", "http://www.tomenet.eu/downloads/TomeNET-latest-install.exe", NULL); /* supposed to work on WINE, yet crashes if not exit(0)ing next oO */
116 	/* Extract */
117 	_spawnl(_P_WAIT, "TomeNET-latest-install.exe", "TomeNET-latest-install.exe", NULL);
118   #endif
119  #else /* just use wget and silly html processing for now, ew ----------------------------------------- */
120 	/* Download */
121   #ifndef USE_URL2FILE
122 	res = _spawnl(_P_WAIT, "updater\\wget.exe", "wget.exe", "--dot-style=mega", "http://www.tomenet.eu/downloads/TomeNET-latest-install.exe", NULL);
123 	if (res != 0) show_error_broken(top_window);
124 	else
125   #else
126 	res = _spawnl(_P_WAIT, "updater\\URL2FILE.EXE", "URL2FILE.EXE", "http://www.tomenet.eu/downloads/TomeNET-latest-install.exe", "TomeNET-latest-install.exe", NULL);
127 	if (res != 0) show_error_broken(top_window);
128 	else
129   #endif
130 	/* Extract */
131 	_spawnl(_P_WAIT, "TomeNET-latest-install.exe", "TomeNET-latest-install.exe", NULL);
132  #endif
133 #else
134 	char cwd_path[1024], out_val[1024];
135 	void *cwd;
136 	const char *clname;
137 	FILE *fp;
138 
139 
140 	if (is_linux) {
141 		/* Linux */
142 		if (is_64bit)
143 			clname = "tomenet-latest-linux-amd64.tar.bz2";
144 		else
145 			clname = "tomenet-latest-linux.tar.bz2";
146 	} else {
147 		/* OSX */
148 		if (is_64bit)
149 			clname = "TomeNET-latest-client-OSX-amd64.tar.bz2";
150 		else
151 			clname = "TomeNET-latest-client-OSX-x86.tar.bz2";
152 	}
153 
154 	/* open a new terminal window to process the download in it, for showing the user what's going on */
155 	/* get own app path and prepare path'ed batch file */
156 	cwd = getcwd(out_val, sizeof(out_val));
157 	if (!cwd) return;//paranoia?
158 	strcpy(cwd_path, out_val);
159 	strcat(cwd_path, "/updater/_update_cl.sh");
160 	fp = fopen(cwd_path, "w");
161 	if (!fp) return;//paranoia?
162 	fprintf(fp, "cd %s\n", out_val);
163 	fprintf(fp, "rm %s\n", clname);
164 	fprintf(fp, "rm updater/result.tmp\n");
165 	fprintf(fp, "wget http://www.tomenet.eu/downloads/%s\n", clname);
166 	fprintf(fp, "RETVAL=$?\n");
167 	fprintf(fp, "echo $RETVAL > updater/result.tmp\n");
168 	fprintf(fp, "tar xvjf %s --strip-components 1\n", clname);
169 	fprintf(fp, "echo\n");
170 	fprintf(fp, "read -p \"Press ENTER to finish.\"\n");
171 	fclose(fp);
172 	sprintf(out_val, "chmod +x %s", cwd_path);
173 	system(out_val);
174 	/* execute the batch file */
175 	switch (posix_terminal) {
176 	case 0: sprintf(out_val, "xfce4-terminal --disable-server -x %s", cwd_path); break;
177 	case 1: sprintf(out_val, "konsole -e /bin/sh %s", cwd_path); break;
178 	case 2: sprintf(out_val, "gnome-terminal -e %s", cwd_path); break;
179 	case 3: sprintf(out_val, "xterm -e %s", cwd_path); break;
180 	case 4: sprintf(out_val, "bash -c %s", cwd_path); break;
181 	case 5: sprintf(out_val, "sh -c %s", cwd_path); break;
182 	}
183 	/* download + extract */
184 	system(out_val);
185 
186 	fp = fopen("updater/result.tmp", "r");
187 	if (fp) {//~paranoia?
188 		long res;
189 
190 		fgets(out_val, 20, fp);
191 		res = atoi(out_val);
192 		if (res != 0) show_error_broken(top_window);
193 		fclose(fp);
194 	}
195 #endif
196 
197 	gtk_button_set_label(button, "Install/update game client");
198 	while(gtk_events_pending())
199 	    gtk_main_iteration();
200 }
201 
install_sound(GtkButton * button,gpointer label)202 void install_sound(GtkButton *button, gpointer label) {
203 	gtk_button_set_label(button, "..please wait..");
204 	while(gtk_events_pending())
205 	    gtk_main_iteration();
206 
207 	//path_build(path, 1024, ANGBAND_DIR_XTRA, "sound");
208 #ifdef WINDOWS
209 	char path[1024], out_val[1024];
210 	FILE *fp;
211 
212 	remove("TomeNET-soundpack.7z");
213 
214 	/* download */
215  #if 0 /* winbash is dysfunctional, no alternatives available ----------------------------------------- */
216   #if 0
217 	//_spawnl(_P_WAIT, "wget.exe", "wget.exe", "http://download814.mediafire.com/wzihil4gthxg/issv5sdv7kv3odq/TomeNET-soundpack.7z", NULL); /* supposed to work on WINE, yet crashes if not exit(0)ing next oO */
218 	//_spawnl(_P_WAIT, "cmd.exe", "cmd.exe", "/c", "_dl_win32.bat", NULL);
219 	_spawnl(_P_WAIT, "updater/_dl_sp_win32.bat", "updater/_dl_sp_win32.bat", NULL);
220   #else /* do it without helper bat files */
221 	chdir("updater/dl_win32/winbash");
222 	//_spawnl(_P_WAIT, "updater/dl_win32/winbash/bash.exe", "updater/dl_win32/winbash/bash.exe", "-c", "\"./download.sh http://www.mediafire.com/?issv5sdv7kv3odq\"", NULL);
223 	//_spawnl(_P_WAIT, "cmd.exe", "cmd.exe", "/c", "start", "bash.exe", "bash.exe", "-c", "\"./download.sh http://www.mediafire.com/?issv5sdv7kv3odq\"", NULL);
224 	_spawnl(_P_WAIT, "bash.exe", "bash.exe", "-c", "\"./download.sh http://www.mediafire.com/?issv5sdv7kv3odq\"", NULL);
225    #if 0 /* no need to use the shell for this.. */
226 	//_spawnl(_P_WAIT, "bash.exe", "bash.exe", "-c", "\"./mv *.7z ../../..\"", NULL);
227    #else /* ..better use the ansi c function: */
228 	rename("TomeNET-soundpack.7z", "../../../TomeNET-soundpack.7z");
229    #endif
230 	chdir("../../..");
231   #endif
232 
233 printf("mhhhHDHHAHFH---\n");
234 exit(0);
235  #else /* just use wget and silly html processing for now, ew ----------------------------------------- */
236   #ifndef USE_URL2FILE
237 	//_spawnl(_P_WAIT, "updater\\wget.exe", "wget.exe", "-O", "temp.html", "--dot-style=mega", "http://www.mediafire.com/download/issv5sdv7kv3odq/TomeNET-soundpack.7z", NULL);
238 	_spawnl(_P_WAIT, "updater\\wget.exe", "wget.exe", "-O", "temp.html", "--dot-style=mega", "http://www.mediafire.com/download/issv5sdv7kv3odq/TomeNET-soundpack.7z", NULL);
239   #else
240 	_spawnl(_P_WAIT, "updater\\URL2FILE.EXE", "URL2FILE.EXE", "http://www.mediafire.com/download/issv5sdv7kv3odq/TomeNET-soundpack.7z", "temp.html", NULL);
241   #endif
242 	fp = fopen("temp.html", "r");
243 	out_val[0] = out_val[1] = 0;
244 	path[0] = 0;
245 	while (!feof(fp)) {
246 		fread(out_val + 5, sizeof(char), 1000, fp);
247 		out_val[1005] = 0;
248 		if (strstr(out_val, "kNO = ")) {
249 			/* extract URL */
250 			strncpy(path, strstr(out_val, "kNO = ") + 7, 1000);
251 			*(strstr(path, "\"")) = 0;
252 			break;
253 		}
254 		/* wrap around (n-1) chars regarding our search string */
255 		out_val[0] = out_val[1000];//k
256 		out_val[1] = out_val[1001];//N
257 		out_val[2] = out_val[1002];//O
258 		out_val[3] = out_val[1003];//
259 		out_val[4] = out_val[1004];// =
260 	}
261 	fclose(fp);
262 	/* no valid dl link? We probably got a captcha request -_- */
263 	if (!path[0]) {
264 		GtkWidget *dialog;
265 		dialog = gtk_message_dialog_new(GTK_WINDOW(top_window),
266 		    GTK_DIALOG_DESTROY_WITH_PARENT,
267 		    GTK_MESSAGE_ERROR,
268 		    GTK_BUTTONS_CLOSE,
269 		    "Error - the download server received too many requests\n" \
270 		    "and responded with a captcha request which this\n" \
271 		    "downloader currently does not support, sorry!\n \nPlease just try again later.");
272 		gtk_dialog_run (GTK_DIALOG (dialog));
273 		gtk_widget_destroy (dialog);
274 
275 		gtk_button_set_label(button, "Install/update sound pack");
276 		while(gtk_events_pending())
277 		    gtk_main_iteration();
278 		return;
279 	}
280 	remove("temp.html");
281   #ifndef USE_URL2FILE
282 	_spawnl(_P_WAIT, "updater\\wget.exe", "wget.exe", "--dot-style=mega", path, NULL);
283   #else
284 	_spawnl(_P_WAIT, "updater\\URL2FILE.EXE", "URL2FILE.EXE", path, "TomeNET-soundpack.7z", NULL);
285   #endif
286  #endif
287 	/* extract */
288 	_spawnl(_P_WAIT, path_7z, path_7z_quoted, "x", "TomeNET-soundpack.7z", NULL);
289 	strcpy(path, ".\\lib\\xtra\\sound");
290 	sprintf(out_val, "xcopy /I /E /Q /Y /H sound %s", path);
291 	system(out_val);
292 	system("rmdir /S /Q sound");
293 #else /* assume posix */
294 	char cwd_path[1024], out_val[1024];
295 	void *cwd;
296 	FILE *fp;
297 
298 	/* download */
299 	/* get own app path and prepare path'ed batch file */
300 	cwd = getcwd(out_val, sizeof(out_val));
301 	if (!cwd) return;//paranoia?
302 	strcpy(cwd_path, out_val);
303 	strcat(cwd_path, "/updater/_update_sp.sh");
304 	fp = fopen(cwd_path, "w");
305 	if (!fp) return;//paranoia?
306 	/* download */
307 	fprintf(fp, "cd %s/updater\n", out_val);
308 	fprintf(fp, "rm result.tmp\n");
309 	fprintf(fp, "./_dl_sp_posix.sh\n");
310 	/* move to correct location and extract */
311 	fprintf(fp, "mv TomeNET-soundpack.7z ..\n");
312 	fprintf(fp, "mkdir -p %s/lib/xtra\n", out_val); /* in case someone deleted his whole lib folder */
313 	fprintf(fp, "cd %s/lib/xtra\n", out_val);
314 	fprintf(fp, "7zG x -y ../../TomeNET-soundpack.7z\n");
315 	/* done, wait for confirmation */
316 	fprintf(fp, "echo\n");
317 	fprintf(fp, "read -p \"Press ENTER to finish.\"\n");
318 	fclose(fp);
319 	/* make the batch file executable */
320 	sprintf(out_val, "chmod +x %s", cwd_path);
321 	system(out_val);
322 	/* execute the batch file */
323 	switch (posix_terminal) {
324 	case 0: sprintf(out_val, "xfce4-terminal --disable-server -x %s", cwd_path); break;
325 	case 1: sprintf(out_val, "konsole -e %s", cwd_path); break;
326 	case 2: sprintf(out_val, "gnome-terminal -e %s", cwd_path); break;
327 	case 3: sprintf(out_val, "xterm -e %s", cwd_path); break;
328 	case 4: sprintf(out_val, "bash -c %s", cwd_path); break;
329 	case 5: sprintf(out_val, "sh -c %s", cwd_path); break;
330 	}
331 	/* download + extract */
332 	system(out_val);
333 
334 	fp = fopen("updater/result.tmp", "r");
335 	if (fp) {//~paranoia?
336 		long res;
337 
338 		fgets(out_val, 20, fp);
339 		res = atoi(out_val);
340 		if (res != 0) show_error_broken(top_window);
341 		fclose(fp);
342 	}
343 #endif
344 
345 	gtk_button_set_label(button, "Install/update sound pack");
346 	while(gtk_events_pending())
347 	    gtk_main_iteration();
348 }
349 
install_music(GtkButton * button,gpointer label)350 void install_music(GtkButton *button, gpointer label) {
351 	gtk_button_set_label(button, "..please wait..");
352 	while(gtk_events_pending())
353 	    gtk_main_iteration();
354 
355 	//path_build(path, 1024, ANGBAND_DIR_XTRA, "music");
356 #ifdef WINDOWS
357 	char path[1024], out_val[1024];
358 	FILE *fp;
359 
360 	remove("TomeNET-musicpack.7z");
361 	/* download */
362  #if 0 /* winbash is dysfunctional, no alternatives available ----------------------------------------- */
363   #if 0
364 	//_spawnl(_P_WAIT, "wget.exe", "wget.exe", "http://download1140.mediafire.com/352dj7foeneg/3j87kp3fgzpqrqn/TomeNET-musicpack.7z", NULL); /* supposed to work on WINE, yet crashes if not exit(0)ing next oO */
365 	//_spawnl(_P_WAIT, "cmd.exe", "cmd.exe", "/c", "_dl_win32.bat", NULL);
366 	_spawnl(_P_WAIT, "updater/_dl_mp_win32.bat", "updater/_dl_mp_win32.bat", NULL);
367   #else /* do it without helper bat files */
368 	chdir("updater/dl_win32/winbash");
369 	//_spawnl(_P_WAIT, "updater/dl_win32/winbash/bash.exe", "updater/dl_win32/winbash/bash.exe", "-c", "\"./download.sh http://www.mediafire.com/?3j87kp3fgzpqrqn\"", NULL);
370 	_spawnl(_P_WAIT, "bash.exe", "bash.exe", "-c", "\"./download.sh http://www.mediafire.com/?3j87kp3fgzpqrqn\"", NULL);
371 	_spawnl(_P_WAIT, "bash.exe", "bash.exe", "-c", "\"./mv *.7z ../../..\"", NULL);
372 	chdir("../../..");
373   #endif
374  #else /* just use wget and silly html processing for now, ew ----------------------------------------- */
375   #ifndef USE_URL2FILE
376 	_spawnl(_P_WAIT, "updater\\wget.exe", "wget.exe", "-O", "temp.html", "--dot-style=mega", "http://www.mediafire.com/download/3j87kp3fgzpqrqn/TomeNET-musicpack.7z", NULL);
377   #else
378 	_spawnl(_P_WAIT, "updater\\URL2FILE.EXE", "URL2FILE.EXE", "http://www.mediafire.com/download/3j87kp3fgzpqrqn/TomeNET-musicpack.7z", "temp.html", NULL);
379   #endif
380  	fp = fopen("temp.html", "r");
381 	out_val[0] = out_val[1] = 0;
382 	path[0] = 0;
383 	while (!feof(fp)) {
384 		fread(out_val + 5, sizeof(char), 1000, fp);
385 		out_val[1005] = 0;
386 		if (strstr(out_val, "kNO = ")) {
387 			/* extract URL */
388 			strncpy(path, strstr(out_val, "kNO = ") + 7, 1000);
389 			*(strstr(path, "\"")) = 0;
390 			break;
391 		}
392 		/* wrap around (n-1) chars regarding our search string */
393 		out_val[0] = out_val[1000];//k
394 		out_val[1] = out_val[1001];//N
395 		out_val[2] = out_val[1002];//O
396 		out_val[3] = out_val[1003];//
397 		out_val[4] = out_val[1004];// =
398 	}
399 	fclose(fp);
400 	/* no valid dl link? We probably got a captcha request -_- */
401 	if (!path[0]) {
402 		GtkWidget *dialog;
403 		dialog = gtk_message_dialog_new(GTK_WINDOW(top_window),
404 		    GTK_DIALOG_DESTROY_WITH_PARENT,
405 		    GTK_MESSAGE_ERROR,
406 		    GTK_BUTTONS_CLOSE,
407 		    "Error - the download server received too many requests\n" \
408 		    "and responded with a captcha request which this\n" \
409 		    "downloader currently does not support, sorry!\n \nPlease just try again later.");
410 		gtk_dialog_run (GTK_DIALOG (dialog));
411 		gtk_widget_destroy (dialog);
412 
413 		gtk_button_set_label(button, "Install/update music pack");
414 		while(gtk_events_pending())
415 		    gtk_main_iteration();
416 		return;
417 	}
418 	remove("temp.html");
419   #ifndef USE_URL2FILE
420 	_spawnl(_P_WAIT, "updater\\wget.exe", "wget.exe", "--dot-style=mega", path, NULL);
421   #else
422 	_spawnl(_P_WAIT, "updater\\URL2FILE.EXE", "URL2FILE.EXE", path, "TomeNET-musicpack.7z", NULL);
423   #endif
424  #endif
425 	/* extract */
426 	_spawnl(_P_WAIT, path_7z, path_7z_quoted, "x", "TomeNET-musicpack.7z", NULL);
427 	strcpy(path, ".\\lib\\xtra\\music");
428 	sprintf(out_val, "xcopy /I /E /Q /Y /H music %s", path);
429 	system(out_val);
430 	system("rmdir /S /Q music");
431 #else /* assume posix */
432 	char cwd_path[1024], out_val[1024];
433 	void *cwd;
434 	FILE *fp;
435 
436 	/* download */
437 	/* get own app path and prepare path'ed batch file */
438 	cwd = getcwd(out_val, sizeof(out_val));
439 	if (!cwd) return;//paranoia?
440 	strcpy(cwd_path, out_val);
441 	strcat(cwd_path, "/updater/_update_mp.sh");
442 	fp = fopen(cwd_path, "w");
443 	if (!fp) return;//paranoia?
444 	/* download */
445 	fprintf(fp, "cd %s/updater\n", out_val);
446 	fprintf(fp, "rm result.tmp\n");
447 	fprintf(fp, "./_dl_mp_posix.sh\n");
448 	/* move to correct location and extract */
449 	fprintf(fp, "mv TomeNET-musicpack.7z ..\n");
450 	fprintf(fp, "mkdir -p %s/lib/xtra\n", out_val); /* in case someone deleted his whole lib folder */
451 	fprintf(fp, "cd %s/lib/xtra\n", out_val);
452 	fprintf(fp, "7zG x -y ../../TomeNET-musicpack.7z\n");
453 	/* done, wait for confirmation */
454 	fprintf(fp, "echo\n");
455 	fprintf(fp, "read -p \"Press ENTER to finish.\"\n");
456 	fclose(fp);
457 	/* make the batch file executable */
458 	sprintf(out_val, "chmod +x %s", cwd_path);
459 	system(out_val);
460 	/* execute the batch file */
461 	switch (posix_terminal) {
462 	case 0: sprintf(out_val, "xfce4-terminal --disable-server -x %s", cwd_path); break;
463 	case 1: sprintf(out_val, "konsole -e %s", cwd_path); break;
464 	case 2: sprintf(out_val, "gnome-terminal -e %s", cwd_path); break;
465 	case 3: sprintf(out_val, "xterm -e %s", cwd_path); break;
466 	case 4: sprintf(out_val, "bash -c %s", cwd_path); break;
467 	case 5: sprintf(out_val, "sh -c %s", cwd_path); break;
468 	}
469 	/* download + extract */
470 	system(out_val);
471 
472 	fp = fopen("updater/result.tmp", "r");
473 	if (fp) {//~paranoia?
474 		long res;
475 
476 		fgets(out_val, 20, fp);
477 		res = atoi(out_val);
478 		if (res != 0) show_error_broken(top_window);
479 		fclose(fp);
480 	}
481 #endif
482 
483 	gtk_button_set_label(button, "Install/update music pack");
484 	while(gtk_events_pending())
485 	    gtk_main_iteration();
486 }
487 
main(int argc,char * argv[])488 int main(int argc, char *argv[]) {
489 	FILE *fp;
490 #ifdef WINDOWS /* use windows registry to locate 7-zip */
491 	HKEY hTestKey;
492 	LPBYTE path_7z_p = (LPBYTE)path_7z;
493  #if 0
494 	int path_7z_size = 1023;
495 	LPDWORD path_7z_size_p = (LPDWORD)&path_7z_size;
496  #else
497 	DWORD path_7z_size = 1023;
498 	LPDWORD path_7z_size_p = &path_7z_size;
499  #endif
500  	unsigned long path_7z_type = REG_SZ;
501 #else
502 	char buf[1024];
503 #endif
504 
505 	GtkWidget *label;
506 	GtkWidget *frame;
507 	GtkWidget *insclient;
508 	GtkWidget *inssfx;
509 	GtkWidget *insmus;
510 
511 	gtk_init(&argc, &argv);
512 	top_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
513 
514 	gtk_window_set_title(GTK_WINDOW(top_window), "TomeNET Updater v1.0");
515 	gtk_window_set_default_size(GTK_WINDOW(top_window), 320, 220);
516 	gtk_window_set_position(GTK_WINDOW(top_window), GTK_WIN_POS_CENTER);
517 	//gtk_window_set_icon(GTK_WINDOW(window), create_pixbuf("updater/tomenet4.png"));
518 	//gtk_window_set_icon(GTK_WINDOW(top_window), create_pixbuf("updater/TomeNET-Updater.png"));
519 	gtk_window_set_icon(GTK_WINDOW(top_window), create_pixbuf("updater/TomeNET-Updater.ico"));
520 
521 	frame = gtk_fixed_new();
522 	gtk_container_add(GTK_CONTAINER(top_window), frame);
523 
524 	label = gtk_label_new("  Make sure to quit and close the\ngame before running this updater!");
525 	gtk_fixed_put(GTK_FIXED(frame), label, 20, 15);
526 
527 	insclient = gtk_button_new_with_label("Install/update game client");
528 	gtk_widget_set_size_request(insclient, 255, 35);
529 	gtk_fixed_put(GTK_FIXED(frame), insclient, 30, 70);
530 
531 	inssfx = gtk_button_new_with_label("Install/update sound pack");
532 	gtk_widget_set_size_request(inssfx, 255, 35);
533 	gtk_fixed_put(GTK_FIXED(frame), inssfx, 30, 120);
534 
535 	insmus = gtk_button_new_with_label("Install/update music pack");
536 	gtk_widget_set_size_request(insmus, 255, 35);
537 	gtk_fixed_put(GTK_FIXED(frame), insmus, 30, 170);
538 
539 	gtk_widget_show_all(top_window);
540 
541 	g_signal_connect(insclient, "clicked", G_CALLBACK(install_client), NULL);
542 	g_signal_connect(inssfx, "clicked", G_CALLBACK(install_sound), NULL);
543 	g_signal_connect(insmus, "clicked", G_CALLBACK(install_music), NULL);
544 
545 	g_signal_connect_swapped(G_OBJECT(top_window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
546 
547 
548 	/* check whether 7-zip is installed, otherwise
549 	   give error message about it and terminate */
550 
551 #ifdef WINDOWS /* use windows registry to locate 7-zip */
552 	/* check registry for 7zip (note that for example WinRAR could cause problems with 7z files) */
553 	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\7-Zip\\"), 0, KEY_READ, &hTestKey) == ERROR_SUCCESS) {
554 		if (RegQueryValueEx(hTestKey, "Path", NULL, &path_7z_type, path_7z_p, path_7z_size_p) == ERROR_SUCCESS) {
555 			path_7z[path_7z_size] = '\0';
556 		} else {
557 			// odd case
558 			RegCloseKey(hTestKey);
559 			//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
560 			show_error(top_window);
561 			return -1;
562 		}
563 	} else {
564 		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\7-Zip\\"), 0, KEY_READ | 0x0200, &hTestKey) == ERROR_SUCCESS) {//KEY_WOW64_32KEY (0x0200)
565 			if (RegQueryValueEx(hTestKey, "Path", NULL, &path_7z_type, path_7z_p, path_7z_size_p) == ERROR_SUCCESS) {
566 				path_7z[path_7z_size] = '\0';
567 			} else {
568 				// odd case
569 				RegCloseKey(hTestKey);
570 				//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
571 				show_error(top_window);
572 				return -1;
573 			}
574 		} else {
575 			/* This case should work on 64-bit Windows (w/ 32-bit client) */
576 			if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\7-Zip\\"), 0, KEY_READ | 0x0100, &hTestKey) == ERROR_SUCCESS) {//KEY_WOW64_64KEY (0x0100)
577 				if (RegQueryValueEx(hTestKey, "Path", NULL, &path_7z_type, path_7z_p, path_7z_size_p) == ERROR_SUCCESS) {
578 					path_7z[path_7z_size] = '\0';
579 				} else {
580 					// odd case
581 					RegCloseKey(hTestKey);
582 					//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
583 					show_error(top_window);
584 					return -1;
585 				}
586 			} else {
587 				//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
588 				show_error(top_window);
589 				return -1;
590 			}
591 		}
592 	}
593 	RegCloseKey(hTestKey);
594 	/* enclose full path in quotes, to handle possible spaces */
595 	strcpy(path_7z_quoted, "\"");
596 	strcat(path_7z_quoted, path_7z);
597 	strcat(path_7z_quoted, "\\7zG.exe\"");
598 	strcat(path_7z, "\\7zG.exe");
599 
600 	/* do the same tests once more as for posix clients */
601 	fp = fopen("tmp.file", "w");
602 	fprintf(fp, "mh");
603 	fclose(fp);
604 
605 	_spawnl(_P_WAIT, path_7z, path_7z_quoted, "a", "tmp.7z", "tmp.file", NULL); /* supposed to work on WINE, yet crashes if not exit(0)ing next oO */
606 	remove("tmp.file");
607 
608 	if (!(fp = fopen("tmp.7z", "r"))) { /* paranoia? */
609 		//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
610 		show_error(top_window);
611 		return -1;
612 	} else if (fgetc(fp) == EOF) { /* normal */
613 		fclose(fp);
614 		//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
615 		show_error(top_window);
616 		return -1;
617 	}
618 #else /* assume posix */
619  #if 0	/* command-line 7z */
620 	system("7z > tmp.7z");
621  #else	/* GUI 7z (for password prompts) */
622 	fp = fopen("tmp.file", "w");
623 	fclose(fp);
624 	system("7zG a tmp.7z tmp.file");
625 	remove("tmp.file");
626  #endif
627         if (!(fp = fopen("tmp.7z", "r"))) { /* paranoia? */
628 		//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
629 		show_error(top_window);//, "7-zip GUI not found ('7zG'). Install it first. (Package name is 'p7zip'.)");
630 		return -1;
631 	} else if (fgetc(fp) == EOF) { /* normal */
632 		fclose(fp);
633 		//MessageBox(NULL, "You must first install 7-zip from www.7-zip.org !", "Error", MB_OK);
634 		show_error(top_window);//, "7-zip GUI not found ('7zG'). Install it first. (Package name is 'p7zip'.)");
635 		return -1;
636 	}
637 #endif
638 	fclose(fp);
639 	remove("tmp.7z");
640 
641 #ifndef WINDOWS
642 	/* find out about Linux vs OS X */
643 	system("uname -s > arch.tmp");
644 	fp = fopen("arch.tmp", "r");
645 	fgets(buf, 64, fp);
646 	fclose(fp);
647 	remove("arch.tmp");
648 	if (strncmp(buf, "Linux", 5)) is_linux = 0; //usually 'Darwin' for OS X
649 
650 	/* find out about 32 vs 64 bit */
651 	system("uname -m > arch.tmp");
652 	fp = fopen("arch.tmp", "r");
653 	fgets(buf, 64, fp);
654 	fclose(fp);
655 	remove("arch.tmp");
656 	if (strncmp(buf, "x86_64", 6)) is_64bit = 0;
657 
658 	/* find out which posix terminal we have available */
659 	if (posix_terminal == -1) {/* no pre-defined favourite? */
660 		if (system("xfce4-terminal -x echo") == 0) posix_terminal = 0;
661 		else if (system("konsole -e echo") == 0) posix_terminal = 1;
662 		else if (system("gnome-terminal -e echo") == 0) posix_terminal = 2;
663 		else if (system("xterm -e echo") == 0) posix_terminal = 3;
664 		else if (system("bash -c echo") == 0) posix_terminal = 4;
665 		else if (system("sh -c echo") == 0) posix_terminal = 5;
666 	}
667 #endif
668 
669 	gtk_main();
670 
671 	return 0;
672 }
673