Debian Codegraph @

Descent3/mission_download.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/mission_download.cpp $
21 * $Revision: 29 $
22 * $Date: 10/21/01 6:34p $
23 * $Author: Kevin $
24 *
25 * Mission file downloading system
26 *
27 *
28 * $Log: /DescentIII/Main/mission_download.cpp $
29 *
30 * 29 10/21/01 6:34p Kevin
31 * minor fix to prevent a buffer overflow (not a reported bug, just
32 * something I noticed and thought was a good idea to avoid).
33 *
34 * 28 8/29/01 4:04p Matt
35 * Changed download directory from pxo.net to outrage.com
36 *
37 * 27 4/28/00 6:48p Jeff
38 * created _strlwr for linux
39 *
40 * 26 4/19/00 5:18p Matt
41 * From Duane for 1.4
42 * ifdef-out mod download code for Mac
43 *
44 * 25 3/26/00 10:29p Kevin
45 * MOD Downloader for 1.4 patch.
46 *
47 * 24 11/04/99 12:35a Chris
48 * Added support for Merc
49 *
50 * 23 10/22/99 1:36p Matt
51 * Mac merge
52 *
53 * 22 10/18/99 1:27p Kevin
54 * Added cf_IsFileInHog
55 *
56 * 21 8/23/99 5:12p Kevin
57 * Proxy support
58 *
59 * 20 8/15/99 8:07p Jeff
60 * display interface when extracting files from a zip
61 *
62 * 19 8/15/99 4:27p Kevin
63 * possibly fixed mission download bug
64 *
65 * 18 8/13/99 8:00p Jeff
66 * handle zip files with new unzip class
67 *
68 * 17 7/21/99 3:48p Kevin
69 * changed left border.
70 *
71 * 16 5/18/99 10:31p Kevin
72 * fixed problem with joining d3.mn3 games and using the minimum install
73 * level
74 *
75 * 15 4/23/99 6:02p Kevin
76 * fixed bug when you can't download a mission
77 *
78 * 14 4/14/99 3:56a Jeff
79 * fixed case mismatch in #includes
80 *
81 * 13 4/07/99 9:38a Kevin
82 * Fixed bug when there were no URLs in the mission file
83 *
84 * 12 4/03/99 9:26p Jeff
85 * changed dialogs that weren't using UID_OK and UID_CANCEL to use and
86 * handle them properly
87 *
88 * 11 3/23/99 8:57a Kevin
89 * Fixed a bug if the user hits cancel
90 *
91 * 10 3/03/99 2:32p Kevin
92 *
93 * 9 3/03/99 12:33a Kevin
94 * Minor OEM changes
95 *
96 * 8 2/07/99 1:17a Jeff
97 * peppered UI dialogs that were missing NEWUIRES_FORCEQUIT to handle it
98 *
99 * 7 2/03/99 4:20p Kevin
100 * Got multiplayer working with .mn3 files, and setup autodownloading
101 *
102 * 6 1/29/99 5:22p Jeff
103 * localization
104 *
105 * 5 12/30/98 12:42p Kevin
106 * Mission downloading localization
107 *
108 * 4 12/30/98 12:19p Kevin
109 *
110 * 3 12/30/98 12:15p Kevin
111 * Auto Mission Download system
112 *
113 * 2 12/28/98 2:22p Kevin
114 * Initial mission downloading system
115 *
116 * 1 12/28/98 11:42a Kevin
117 *
118 *
119 */
120
121 #include <stdio.h>
122
123 #include "pstypes.h"
124 #include "mem.h"
125 #include "args.h"
126 #include "descent.h"
127 #include "networking.h"
128 #include "multi.h"
129 #include "ui.h"
130 #include "newui.h"
131 #include "ddio.h"
132 #include "stringtable.h"
133 #include "multi_dll_mgr.h"
134 // #include "inetgetfile.h"
135 #include "grtext.h"
136 #include "Mission.h"
137 #include "mission_download.h"
138 #include "renderer.h"
139
140 #include "unzip.h"
141
142 int Got_url;
143 msn_urls msn_URL = {"", {"", "", "", "", ""}};
144 msn_urls Net_msn_URLs;
145
146 extern char Proxy_server[200];
147 extern int16_t Proxy_port;
148
149 int msn_ExtractZipFile(char *zipfilename, char *mn3name);
150
151 // Request a URL structure from a server containing a list of download locations
152 // For the current mission being played
153 msn_urls *msn_GetURL(network_address *net_addr) {
154
155 int count = 0;
156 int size;
157 int tries = 0;
158 uint8_t data[MAX_GAME_DATA_SIZE];
159 float start_time;
160 network_address from_addr;
161
162 size = START_DATA(MP_ASK_FOR_URL, data, &count);
163 END_DATA(count, data, size);
164
165 Got_url = -1;
166
167 while (tries < 5 && Got_url == -1) {
168 // Send the request
169 ASSERT(size > 0);
170 nw_Send(net_addr, data, count, 0);
171 tries++;
172
173 start_time = timer_GetTime();
174 while ((timer_GetTime() - start_time < URL_ASK_POLL_TIME) && Got_url == -1) {
175 int packsize;
176 while (((packsize = nw_Receive(Multi_receive_buffer, &from_addr)) > 0) && Got_url == -1) {
177 MultiProcessBigData(Multi_receive_buffer, packsize, &from_addr);
178 }
179 }
180 }
181
182 if (tries >= 5 || Got_url != 1)
183 return NULL;
184
185 return &msn_URL;
186 }
187
188 // Get the list of URLs in the msn file
189 msn_urls *msn_GetURL(char *msnfile) {
190 // static msn_urls url;
191 // strcpy(url.msnname,msnfile);
192 return &Net_msn_URLs;
193 }
194
195 #define MSN_DWNLD_CHOICE_H 256
196 #define MSN_DWNLD_CHOICE_W 512
197
198 // Present the user with a list of URLs to download from. Return the 0 based selection, or -1 if cancel is hit.
199 int msn_ShowDownloadChoices(msn_urls *urls) {
200 UITextItem title_text(TXT_DOWNLOADPROMPT, UICOL_WINDOW_TITLE);
201 UITextItem title1_text(TXT_DONTHAVEMSN, UICOL_WINDOW_TITLE);
202 UITextItem header_text(TXT_DOWNLOADMSN, UICOL_TEXT_NORMAL);
203 UIHotspot ok_hot;
204 UIHotspot cancel_hot;
205 NewUIListBox choices_list;
206
207 NewUIGameWindow menu_wnd;
208
209 UIText texts[10];
210
211 int exit_menu = 0;
212 int ret = 0;
213
214 menu_wnd.Create(10, 10, MSN_DWNLD_CHOICE_W, MSN_DWNLD_CHOICE_H, UIF_PROCESS_ALL | UIF_CENTER);
215 texts[0].Create(&menu_wnd, &header_text, 0, 8, UIF_CENTER);
216 texts[1].Create(&menu_wnd, &title_text, 0, 55, UIF_CENTER);
217 texts[2].Create(&menu_wnd, &title1_text, 0, 35, UIF_CENTER);
218
219 UITextItem ok_off(TXT_OK, UICOL_HOTSPOT_LO, UIALPHA_HOTSPOT_LO);
220 UITextItem ok_on(TXT_OK, UICOL_HOTSPOT_HI, UIALPHA_HOTSPOT_HI);
221 UITextItem cancel_off(TXT_CANCEL, UICOL_HOTSPOT_LO, UIALPHA_HOTSPOT_LO);
222 UITextItem cancel_on(TXT_CANCEL, UICOL_HOTSPOT_HI, UIALPHA_HOTSPOT_HI);
223
224 int ok_x, cancel_x;
225 GetCenteredTextPos(menu_wnd.W(), 20, ok_on.width(), cancel_on.width(), &ok_x, &cancel_x);
226
227 ok_hot.Create(&menu_wnd, UID_OK, KEY_ENTER, &ok_off, &ok_on, ok_x, MSN_DWNLD_CHOICE_H - OKCANCEL_YOFFSET, 0, 0,
228 UIF_FIT);
229 cancel_hot.Create(&menu_wnd, UID_CANCEL, KEY_ESC, &cancel_off, &cancel_on, cancel_x,
230 MSN_DWNLD_CHOICE_H - OKCANCEL_YOFFSET, 0, 0, UIF_FIT);
231
232 choices_list.Create(&menu_wnd, 1, 0, 70, 480, 128, UIF_CENTER | UILB_NOSORT);
233 choices_list.SetSelectedColor(UICOL_LISTBOX_HI);
234 choices_list.SetHiliteColor(UICOL_LISTBOX_HI);
235 choices_list.RemoveAll();
236
237 UITextItem urlti[MAX_MISSION_URL_COUNT];
238 // urls
239 int i;
240 int urlcount = 0;
241 for (i = 0; i < MAX_MISSION_URL_COUNT; i++) {
242 if (urls->URL[i][0]) {
243 urlti[i] = UITextItem(urls->URL[i], UICOL_LISTBOX_LO);
244 choices_list.AddItem(&urlti[i]);
245 urlcount++;
246 }
247 }
248 if (urlcount == 0) {
249 // Message that the mission can't be downloaded
250 DoMessageBox(TXT_ERROR, TXT_FMTCANTDNLD, MSGBOX_OK);
251 menu_wnd.Destroy();
252 return -1;
253 }
254 menu_wnd.Open();
255
256 while (!exit_menu) {
257 int res;
258
259 res = DoUI();
260 switch (res) {
261 case NEWUIRES_FORCEQUIT:
262 case UID_CANCEL:
263 ret = -1;
264 menu_wnd.Close();
265 exit_menu = 1;
266 break;
267 case UID_OK:
268 ret = choices_list.GetSelectedIndex();
269 menu_wnd.Close();
270 exit_menu = 1;
271 break;
272 }
273 }
274 menu_wnd.Destroy();
275 return ret;
276 }
277
278 #define MSN_REFRESH_INTERVAL .5
279
280 #define MSN_DWNLD_STATUS_H 256
281 #define MSN_DWNLD_STATUS_W 512
282
283 #define MSN_BORDER_W 30
284 #define MSN_BORDER_H 40
285
286 #define MSN_COL_1 MSN_BORDER_W
287 #define MSN_COL_2 ((MSN_DWNLD_STATUS_W / 2) + (MSN_BORDER_W / 2))
288
289 #define MSN_ROW_1 MSN_BORDER_H
290 #define MSN_ROW_2 (MSN_BORDER_H + 30)
291 #define MSN_ROW_3 (MSN_BORDER_H + 60)
292 #define MSN_ROW_4 (MSN_BORDER_H + 90)
293 #define MSN_ROW_5 (MSN_BORDER_H + 120)
294
295 #define DOWNLOAD_STATUS_URL_TEXT TXT_FMTDOWNLOADING
296 #define DOWNLOAD_STATUS_RCVD_TEXT TXT_FMTRECEIVED
297 #define DOWNLOAD_STATUS_TOTAL_TEXT TXT_FMTTOTAL
298 #define DOWNLOAD_STATUS_ELAPS_TEXT TXT_FMTTIMEELAPSED
299 #define DOWNLOAD_STATUS_TIME_R_TEXT TXT_FMTTIMELEFT
300 #define DOWNLOAD_STATUS_XFERRATE_TEXT TXT_FMTXFERRATE
301
302 #define MSN_MAX_STRING_LEN 100
303
304 // Start downloading the file at the url specifies, showing a status screen
305 // Return codes:
306 // 0 Failed or cancelled
307 // 1 Success
308 int msn_DownloadWithStatus(char *url, char *filename) {
309 return 0;
310 /*
311 char qualfile[_MAX_PATH*2];
312 float last_refresh;
313 int total_bytes = 0;
314 int received_bytes = 0;
315 int time_elapsed = 0;
316 int time_remain = 0;
317 int xfer_rate = 0;
318 int starttime = timer_GetTime();
319 bool file_is_zip = false;
320
321 //check to see if we are downloading a zip file
322 char *url_ptr;
323 url_ptr = url + strlen(url);
324 while(url_ptr > url && *url_ptr!='.') url_ptr--;
325 if(*url_ptr=='.')
326 {
327 //now see if the rest of the extension is ZIP
328 if(!stricmp(url_ptr,".ZIP"))
329 {
330 mprintf(0,"We're downloading a zip file!!!\n");
331 file_is_zip = true;
332 }
333 }
334
335 char fmturl[MSN_MAX_STRING_LEN];
336 char fmtrcvd[MSN_MAX_STRING_LEN];
337 char fmttotal[MSN_MAX_STRING_LEN];
338 char fmtelaps[MSN_MAX_STRING_LEN];
339 char fmttimer[MSN_MAX_STRING_LEN];
340 char fmtrate[MSN_MAX_STRING_LEN];
341
342 sprintf(fmturl,DOWNLOAD_STATUS_URL_TEXT,url);
343 msn_ClipURLToWidth(MSN_DWNLD_STATUS_W-(MSN_COL_1+MSN_BORDER_W),fmturl);
344 sprintf(fmtrcvd,DOWNLOAD_STATUS_RCVD_TEXT,received_bytes);
345 sprintf(fmttotal,DOWNLOAD_STATUS_TOTAL_TEXT,total_bytes);
346 sprintf(fmtelaps,DOWNLOAD_STATUS_ELAPS_TEXT,msn_SecondsToString(time_elapsed));
347 sprintf(fmttimer,DOWNLOAD_STATUS_TIME_R_TEXT,msn_SecondsToString(time_remain));
348 sprintf(fmtrate,DOWNLOAD_STATUS_XFERRATE_TEXT,xfer_rate);
349
350 if(file_is_zip)
351 {
352 char fname[_MAX_FNAME];
353 char *s_ptr,*d_ptr;
354 s_ptr = filename;
355 d_ptr = fname;
356 while(*s_ptr && *s_ptr!='.'){ *d_ptr = *s_ptr; s_ptr++; d_ptr++;}
357 *d_ptr = '\0';
358 strcat(fname,".zip");
359 ddio_MakePath(qualfile,D3MissionsDir,fname,NULL);
360 }else
361 {
362 ddio_MakePath(qualfile,D3MissionsDir,filename,NULL);
363 }
364 InetGetFile *getmsnfile;
365 if(Proxy_server[0])
366 {
367 getmsnfile = new InetGetFile(url,qualfile,Proxy_server,Proxy_port);
368 }
369 else
370 {
371 getmsnfile = new InetGetFile(url,qualfile);
372 }
373
374 UITextItem title_text(TXT_MD_DOWNLOADSTATUS,UICOL_TEXT_NORMAL);
375
376 UIHotspot cancel_hot;
377
378 UITextItem download_text(fmturl,UICOL_TEXT_NORMAL);;
379 UITextItem rcvd_text(fmtrcvd,UICOL_TEXT_NORMAL);
380 UITextItem total_text(fmttotal,UICOL_TEXT_NORMAL);
381 UITextItem elaps_text(fmtelaps,UICOL_TEXT_NORMAL);
382 UITextItem time_r_text(fmttimer,UICOL_TEXT_NORMAL);
383 UITextItem rate_text(fmtrate,UICOL_TEXT_NORMAL);
384
385 NewUIGameWindow menu_wnd;
386
387 UIText texts[10];
388 UIProgress progress;
389
390 int exit_menu=0;
391 int ret=0;
392
393 menu_wnd.Create(10,10,MSN_DWNLD_STATUS_W,MSN_DWNLD_STATUS_H,UIF_PROCESS_ALL | UIF_CENTER);
394 texts[0].Create (&menu_wnd,&title_text,0,8,UIF_CENTER);
395 texts[1].Create (&menu_wnd,&download_text,MSN_COL_1,MSN_ROW_1,0);
396 texts[2].Create (&menu_wnd,&rcvd_text,MSN_COL_1,MSN_ROW_2,0);
397 texts[3].Create (&menu_wnd,&total_text,MSN_COL_2,MSN_ROW_2,0);
398 texts[4].Create (&menu_wnd,&elaps_text,MSN_COL_1,MSN_ROW_3,0);
399 texts[5].Create (&menu_wnd,&time_r_text,MSN_COL_2,MSN_ROW_3,0);
400 texts[6].Create (&menu_wnd,&rate_text,MSN_COL_1,MSN_ROW_4,0);
401
402 UITextItem cancel_off(TXT_CANCEL,UICOL_HOTSPOT_LO,UIALPHA_HOTSPOT_LO);
403 UITextItem cancel_on(TXT_CANCEL,UICOL_HOTSPOT_HI,UIALPHA_HOTSPOT_HI);
404
405 progress.Create(&menu_wnd,MSN_COL_1,MSN_ROW_5,MSN_DWNLD_STATUS_W-(MSN_BORDER_W*2),35,0);
406
407 int cancel_x = 0;
408
409 cancel_hot.Create(&menu_wnd, UID_CANCEL, KEY_ESC, &cancel_off,&cancel_on,
410 cancel_x, MSN_DWNLD_STATUS_H - OKCANCEL_YOFFSET,
411 0,0,UIF_FIT|UIF_CENTER); menu_wnd.Open();
412
413 last_refresh = timer_GetTime()-MSN_REFRESH_INTERVAL;
414
415 while (!exit_menu)
416 {
417 int res;
418
419 if((timer_GetTime()-last_refresh)>MSN_REFRESH_INTERVAL)
420 {
421
422 //Update the dialog
423 //mprintf(0,"!");
424
425 if(getmsnfile->IsFileReceived())
426 {
427 //File transfer successful!
428 mprintf(0,"Succesfully received the file!\n");
429 exit_menu = 1;
430
431 if(file_is_zip)
432 {
433 // now we gotta handle the zip file
434 ret = msn_ExtractZipFile(qualfile,filename);
435 }else
436 {
437 ret = 1;
438 }
439 }
440
441 if(getmsnfile->IsFileError())
442 {
443 //File transfer Error!
444 DoMessageBox(TXT_ERROR,TXT_FMTCANTDNLD,MSGBOX_OK);
445 //Delete the file that didn't finish!
446 ddio_DeleteFile(qualfile);
447 mprintf(0,"Couldn't download the file! Error: %d\n",getmsnfile->GetErrorCode());
448 exit_menu = 1;
449 ret = 0;
450 }
451
452 last_refresh = timer_GetTime();
453 received_bytes = getmsnfile->GetBytesIn();
454 total_bytes = getmsnfile->GetTotalBytes();
455
456 time_elapsed = timer_GetTime()-starttime;
457
458 if(total_bytes)
459 {
460 time_remain = ((float)(total_bytes-received_bytes))/((float)(received_bytes/time_elapsed));
461 }
462 if(time_elapsed&&received_bytes)
463 {
464 xfer_rate = ((float)(received_bytes/time_elapsed));
465 }
466 texts[1].Destroy();
467 texts[2].Destroy();
468 texts[3].Destroy();
469 texts[4].Destroy();
470 texts[5].Destroy();
471 texts[6].Destroy();
472
473 sprintf(fmturl,DOWNLOAD_STATUS_URL_TEXT,url);
474 msn_ClipURLToWidth(MSN_DWNLD_STATUS_W-(MSN_COL_1+MSN_BORDER_W),fmturl);
475 sprintf(fmtrcvd,DOWNLOAD_STATUS_RCVD_TEXT,received_bytes);
476 sprintf(fmttotal,DOWNLOAD_STATUS_TOTAL_TEXT,total_bytes);
477 sprintf(fmtelaps,DOWNLOAD_STATUS_ELAPS_TEXT,msn_SecondsToString(time_elapsed));
478 sprintf(fmttimer,DOWNLOAD_STATUS_TIME_R_TEXT,msn_SecondsToString(time_remain));
479 msn_ClipURLToWidth(MSN_DWNLD_STATUS_W-(MSN_COL_2+MSN_BORDER_W),fmttimer);
480 sprintf(fmtrate,DOWNLOAD_STATUS_XFERRATE_TEXT,xfer_rate);
481
482 download_text = UITextItem (fmturl,UICOL_TEXT_NORMAL);;
483 rcvd_text = UITextItem (fmtrcvd,UICOL_TEXT_NORMAL);
484 total_text = UITextItem (fmttotal,UICOL_TEXT_NORMAL);
485 elaps_text = UITextItem (fmtelaps,UICOL_TEXT_NORMAL);
486 time_r_text = UITextItem (fmttimer,UICOL_TEXT_NORMAL);
487 rate_text = UITextItem (fmtrate,UICOL_TEXT_NORMAL);
488
489 texts[1].Create (&menu_wnd,&download_text,MSN_COL_1,MSN_ROW_1,0);
490 texts[2].Create (&menu_wnd,&rcvd_text,MSN_COL_1,MSN_ROW_2,0);
491 texts[3].Create (&menu_wnd,&total_text,MSN_COL_2,MSN_ROW_2,0);
492 texts[4].Create (&menu_wnd,&elaps_text,MSN_COL_1,MSN_ROW_3,0);
493 texts[5].Create (&menu_wnd,&time_r_text,MSN_COL_2,MSN_ROW_3,0);
494 texts[6].Create (&menu_wnd,&rate_text,MSN_COL_1,MSN_ROW_4,0);
495
496 progress.Update(((float)((float)received_bytes)/((float)total_bytes)));
497 //mprintf(0,"@");
498 }
499 //mprintf(0,"-In");
500 res = PollUI();
501 //mprintf(0,"-Out");
502 switch(res)
503 {
504 case UID_CANCEL:
505 getmsnfile->AbortGet();
506 ddio_DeleteFile(qualfile);
507 exit_menu = 1;
508 ret = 0;
509 break;
510 }
511 }
512 menu_wnd.Close();
513 menu_wnd.Destroy();
514 delete getmsnfile;
515 return ret;
516 */
517 }
518
519 void msn_DoAskForURL(uint8_t *indata, network_address *net_addr) {
520 static msn_urls *url;
521 int count = 0;
522 int size;
523 int i;
524 uint8_t data[MAX_GAME_DATA_SIZE];
525 int num_urls = 0;
526
527 if (Netgame.local_role == LR_SERVER) {
528 size = START_DATA(MP_CUR_MSN_URLS, data, &count);
529
530 url = msn_GetURL(Netgame.mission);
531 if (url) {
532 for (i = 0; i < MAX_MISSION_URL_COUNT; i++) {
533 if (url->URL[0]) {
534 num_urls++;
535 }
536 }
537 }
538 // length of the msn
539 int msnlen = strlen(Netgame.mission) + 1;
540 MultiAddByte(msnlen, data, &count);
541 // Copy the mission name
542 memcpy(data + count, url->URL[i], msnlen);
543 count += msnlen;
544
545 // Silly copy protection. Don't download the mn3 if
546 //"clang.wav" is in it.
547 if (cf_IsFileInHog(Netgame.mission, "clang.wav")) {
548 num_urls = 0;
549 return;
550 }
551
552 // Number of URLs
553 MultiAddByte(num_urls, data, &count);
554 for (i = 0; i < num_urls; i++) {
555 uint16_t urllen = strlen(url->URL[i]) + 1;
556 if ((count + urllen) >= MAX_GAME_DATA_SIZE) {
557 // if for some reason the URLS exceed what a packet can send
558 // rather than overflow the memory buffer, just don't send out the packet.
559 // I would just fit any URLS that I can but this is safer to put in a patch.
560 return;
561 }
562
563 // Write the lenght of the url
564 MultiAddUshort(urllen, data, &count);
565 // Now write the url
566 memcpy(data + count, url->URL[i], urllen);
567 count += urllen;
568 }
569 END_DATA(count, data, size);
570 nw_Send(net_addr, data, count, 0);
571 }
572 }
573
574 void msn_DoCurrMsnURLs(uint8_t *data, network_address *net_addr) {
575 int count = 0;
576 int num_urls = 0;
577 int i;
578
579 memset(&msn_URL, 0, sizeof(msn_URL));
580
581 SKIP_HEADER(data, &count);
582 // Get the mission name
583 int msnlen = MultiGetByte(data, &count);
584 memcpy(&msn_URL.msnname, data + count, msnlen);
585 count += msnlen;
586 num_urls = MultiGetByte(data, &count);
587
588 for (i = 0; i < num_urls; i++) {
589 uint16_t urllen = MultiGetUshort(data, &count);
590 memcpy(msn_URL.URL[i], data + count, urllen);
591 count += urllen;
592 }
593 Got_url = 1;
594 }
595
596 // Check if a file exists.
597 // If it does, return true
598 // If it doesn't attempt to download it. On success return true
599 // Otherwise return false
600 int msn_CheckGetMission(network_address *net_addr, char *filename) {
601 #ifdef OEM
602 return 1;
603 #else
604 // Don't download local missions
605 char pathname[_MAX_PATH];
606 ddio_MakePath(pathname, D3MissionsDir, filename, NULL);
607 if (cfexist(filename) || cfexist(pathname)) {
608 return 1;
609 }
610
611 msn_urls *murls;
612
613 murls = msn_GetURL(net_addr);
614 if (murls) {
615 int sel = msn_ShowDownloadChoices(murls);
616 if (sel != -1) {
617 // Get the item that was selected!
618 mprintf(0, "Downloading missions file from %s\n", murls->URL[sel]);
619 if (msn_DownloadWithStatus(murls->URL[sel], filename)) {
620 return 1;
621 }
622 }
623 } else {
624 DoMessageBox(TXT_ERROR, TXT_FMTCANTDNLD, MSGBOX_OK);
625 }
626 return 0;
627 #endif
628 }
629
630 void msn_ClipURLToWidth(int width, char *string) {
631 if (!string)
632 return;
633
634 int string_length = strlen(string);
635
636 int size = 0;
637 char save = string[0];
638 string[0] = '\0';
639 while (size < string_length) {
640 if ((grtext_GetTextLineWidth(string)) >= width) {
641 return;
642 }
643 // replace the char and move to the next
644 string[size] = save;
645 size++;
646 save = string[size];
647 string[size] = '\0';
648 }
649 }
650
651 char *msn_SecondsToString(int time_sec) {
652 static char fmttime[100];
653 char fmttemp[100] = "";
654 int hours;
655 int minutes;
656 int seconds;
657 int i;
658 fmttime[0] = '\0';
659 hours = time_sec / 3600;
660 i = time_sec % 3600;
661 minutes = i / 60;
662 seconds = time_sec % 60;
663 if (hours) {
664 snprintf(fmttemp, sizeof(fmttemp), TXT_FMTTIMEHOUR, hours);
665 strcat(fmttime, " ");
666 strcat(fmttime, fmttemp);
667 }
668 if (minutes) {
669 snprintf(fmttemp, sizeof(fmttemp), TXT_FMTTIMEMIN, minutes);
670 strcat(fmttime, " ");
671 strcat(fmttime, fmttemp);
672 }
673 if (seconds) {
674 snprintf(fmttemp, sizeof(fmttemp), TXT_FMTTIMESEC, seconds);
675 strcat(fmttime, fmttemp);
676 }
677 return fmttime;
678 }
679
680 void _get_zipfilename(char *output, char *directory, char *zipfilename) {
681 char *s = strrchr(zipfilename, '/');
682 if (s)
683 s++;
684 else
685 s = zipfilename;
686
687 ddio_MakePath(output, directory, s, NULL);
688 }
689
690 // return 0 on failure
691 // return 1 on success
692 int msn_ExtractZipFile(char *zipfilename, char *mn3name) {
693
694 mprintf(0, "Extracting ZIP File (%s) to missions directory\n", zipfilename);
695 if (!cfexist(zipfilename)) {
696 mprintf(0, "Zip file doesn't exist\n");
697 return 0;
698 }
699
700 char mission_directory[_MAX_PATH];
701 char output_filename[_MAX_PATH];
702 char buffer[256 + 32];
703
704 ddio_MakePath(mission_directory, LocalD3Dir, "missions", NULL);
705
706 // now go through the zip file and extract all the files out that we can
707 ZIP zfile;
708 zipentry *ze;
709
710 if (!zfile.OpenZip(zipfilename)) {
711 mprintf(0, "Unable to open zip file\n");
712 return 0;
713 }
714
715 NewUIGameWindow window;
716 UIConsoleGadget console;
717
718 // Create all the UI Items
719 window.Create(0, 0, 384, 288);
720 console.Create(&window, 0x99, 25, 25, 0, 35, 18, 0);
721 window.Open();
722
723 bool found_mn3 = false;
724 bool process_file;
725
726 while ((ze = zfile.ReadNextZipEntry()) != NULL) {
727 if (ze->crc32 == 0)
728 continue; // skip directories
729
730 Descent->defer();
731 process_file = true;
732
733 mprintf(0, "Processesing: %s\n", ze->name);
734
735 if (ze->compression_method == 0x0000 || ze->compression_method == 0x0008) {
736 char *rfile = strrchr(ze->name, '/');
737 if (rfile)
738 rfile++;
739 else
740 rfile = ze->name;
741
742 snprintf(buffer, sizeof(buffer), "%s %s...", (ze->compression_method == 0x0000) ? "Extracting" : "Inflating",
743 rfile);
744 console.puts(GR_GREEN, buffer);
745
746 // create the filename for this file
747 _get_zipfilename(output_filename, mission_directory, ze->name);
748
749 if (cfexist(output_filename)) {
750 snprintf(buffer, sizeof(buffer), "%s already exists. Overwrite?", output_filename);
751 if (DoMessageBox("Confirm", buffer, MSGBOX_YESNO, UICOL_WINDOW_TITLE, UICOL_TEXT_NORMAL)) {
752 // delete the file
753 mprintf(0, "Deleting %s\n", zipfilename);
754 if (!ddio_DeleteFile(output_filename)) {
755 process_file = false;
756 console.puts(GR_GREEN, "[Unable to Write] ");
757 } else {
758 console.puts(GR_GREEN, "[Overwriting] ");
759 }
760 } else {
761 process_file = false;
762 console.puts(GR_GREEN, "[Skipped] ");
763 }
764 }
765
766 // extract this file
767 if (process_file) {
768 // update the screen
769 DoUIFrame();
770 rend_Flip();
771
772 int ret = zfile.ExtractFile(ze, output_filename);
773 if (ret < 0) {
774 if (ret == -9) {
775 mprintf(0, " Error writing to file\n");
776 snprintf(buffer, sizeof(buffer), "\nError writing to file (Out of space?)");
777 } else {
778 mprintf(0, " Error %d extracting file\n", ret);
779 snprintf(buffer, sizeof(buffer), "\nError %d extracting file", ret);
780 }
781 console.puts(GR_GREEN, buffer);
782 if (cfexist(output_filename)) {
783 ddio_DeleteFile(output_filename);
784 }
785 } else {
786 // check the CRC
787 uint32_t crc = cf_GetfileCRC(output_filename);
788 if (crc == ze->crc32) {
789 console.puts(GR_GREEN, "CRC OK");
790
791 // check to see if we extracted our mn3
792 if (CompareZipFileName(ze->name, mn3name)) {
793 found_mn3 = true;
794 }
795 } else {
796 console.puts(GR_GREEN, "CRC FAIL!");
797 ddio_DeleteFile(output_filename);
798 }
799 }
800 }
801
802 } else {
803 mprintf(0, "Unsupported compression for file (%s)\n", ze->name);
804 console.puts(GR_GREEN, "Unsupported compression!!");
805 }
806
807 console.puts(GR_GREEN, "\n");
808 DoUIFrame();
809 rend_Flip();
810 }
811 zfile.CloseZip();
812
813 if (DoMessageBox("Confirm", "Do you want to delete the zip file? It is no longer needed.", MSGBOX_YESNO,
814 UICOL_WINDOW_TITLE, UICOL_TEXT_NORMAL)) {
815 // delete the file
816 mprintf(0, "Deleting %s\n", zipfilename);
817 ddio_DeleteFile(zipfilename);
818 }
819
820 window.Close();
821 window.Destroy();
822
823 if (!found_mn3)
824 return 0;
825
826 return 1;
827 }
828
829 #if defined(POSIX)
830 char *_strlwr(char *string) {
831 char *ptr = string;
832 while (*ptr) {
833 *ptr = tolower(*ptr);
834 ptr++;
835 }
836 return string;
837 }
838 #endif
839
840 #define MOD_URL_BASEPATH "http://www.descent3.com/mods/" // WAS: "http://www.pxo.net/descent3/mods/"
841
842 int CheckGetD3M(char *d3m) {
843 #if !defined(OEM)
844
845 char modurl[MAX_MISSION_URL_LEN + 1];
846 char *lowurl;
847 char pathname[_MAX_PATH];
848 char *fixedd3m = NULL;
849
850 ddio_MakePath(pathname, LocalD3Dir, "Netgames", d3m, NULL);
851 if (cfexist(d3m) || cfexist(pathname)) {
852 return 1;
853 }
854
855 // Now we need to replace evil spaces with underscores, so the downloader will work
856 fixedd3m = mem_strdup(d3m);
857 char *p = fixedd3m;
858 while (*p) {
859 if (*p == ' ') {
860 *p = '_';
861 }
862 p++;
863 }
864 // This should be the url we can download from.
865 int iurlbase = FindArg("-d3mbaseurl");
866 if (!iurlbase) {
867 strcpy(modurl, MOD_URL_BASEPATH);
868 } else {
869 strcpy(modurl, GameArgs[iurlbase + 1]);
870 }
871
872 strcat(modurl, fixedd3m);
873
874 lowurl = mem_strdup(_strlwr(modurl));
875 mprintf(0, "Downloading mod file from %s\n", modurl);
876
877 if (ModDownloadWithStatus(modurl, d3m)) {
878 mem_free(fixedd3m);
879 return 1;
880 }
881
882 mem_free(lowurl);
883 mem_free(fixedd3m);
884 return 0;
885 #endif
886 }
887
888 // Start downloading the file at the url specifies, showing a status screen
889 // Return codes:
890 // 0 Failed or cancelled
891 // 1 Success
892 int ModDownloadWithStatus(char *url, char *filename) {
893 return 0;
894 /*
895 char qualfile[_MAX_PATH*2];
896 float last_refresh;
897 int total_bytes = 0;
898 int received_bytes = 0;
899 int time_elapsed = 0;
900 int time_remain = 0;
901 int xfer_rate = 0;
902 int starttime = timer_GetTime();
903 bool file_is_zip = false;
904
905 //check to see if we are downloading a zip file
906 char *url_ptr;
907 url_ptr = url + strlen(url);
908 while(url_ptr > url && *url_ptr!='.') url_ptr--;
909 if(*url_ptr=='.')
910 {
911 //now see if the rest of the extension is ZIP
912 if(!stricmp(url_ptr,".ZIP"))
913 {
914 mprintf(0,"We're downloading a zip file!!!\n");
915 file_is_zip = true;
916 }
917 }
918
919 char fmturl[MSN_MAX_STRING_LEN];
920 char fmtrcvd[MSN_MAX_STRING_LEN];
921 char fmttotal[MSN_MAX_STRING_LEN];
922 char fmtelaps[MSN_MAX_STRING_LEN];
923 char fmttimer[MSN_MAX_STRING_LEN];
924 char fmtrate[MSN_MAX_STRING_LEN];
925
926 sprintf(fmturl,DOWNLOAD_STATUS_URL_TEXT,url);
927 msn_ClipURLToWidth(MSN_DWNLD_STATUS_W-(MSN_COL_1+MSN_BORDER_W),fmturl);
928 sprintf(fmtrcvd,DOWNLOAD_STATUS_RCVD_TEXT,received_bytes);
929 sprintf(fmttotal,DOWNLOAD_STATUS_TOTAL_TEXT,total_bytes);
930 sprintf(fmtelaps,DOWNLOAD_STATUS_ELAPS_TEXT,msn_SecondsToString(time_elapsed));
931 sprintf(fmttimer,DOWNLOAD_STATUS_TIME_R_TEXT,msn_SecondsToString(time_remain));
932 sprintf(fmtrate,DOWNLOAD_STATUS_XFERRATE_TEXT,xfer_rate);
933
934 if(file_is_zip)
935 {
936 char fname[_MAX_FNAME];
937 char *s_ptr,*d_ptr;
938 s_ptr = filename;
939 d_ptr = fname;
940 while(*s_ptr && *s_ptr!='.'){ *d_ptr = *s_ptr; s_ptr++; d_ptr++;}
941 *d_ptr = '\0';
942 strcat(fname,".zip");
943 ddio_MakePath(qualfile, LocalD3Dir, "Netgames", fname, NULL);
944 //ddio_MakePath(qualfile,D3MissionsDir,fname,NULL);
945 }else
946 {
947 //ddio_MakePath(qualfile,D3MissionsDir,filename,NULL);
948 ddio_MakePath(qualfile, LocalD3Dir, "Netgames", filename, NULL);
949 }
950 InetGetFile *getmsnfile;
951 if(Proxy_server[0])
952 {
953 getmsnfile = new InetGetFile(url,qualfile,Proxy_server,Proxy_port);
954 }
955 else
956 {
957 getmsnfile = new InetGetFile(url,qualfile);
958 }
959 //InetGetFile getmsnfile(url,qualfile);
960
961 UITextItem title_text(TXT_MD_DOWNLOADSTATUS,UICOL_TEXT_NORMAL);
962
963 UIHotspot cancel_hot;
964
965 UITextItem download_text(fmturl,UICOL_TEXT_NORMAL);;
966 UITextItem rcvd_text(fmtrcvd,UICOL_TEXT_NORMAL);
967 UITextItem total_text(fmttotal,UICOL_TEXT_NORMAL);
968 UITextItem elaps_text(fmtelaps,UICOL_TEXT_NORMAL);
969 UITextItem time_r_text(fmttimer,UICOL_TEXT_NORMAL);
970 UITextItem rate_text(fmtrate,UICOL_TEXT_NORMAL);
971
972 NewUIGameWindow menu_wnd;
973
974 UIText texts[10];
975 UIProgress progress;
976
977 int exit_menu=0;
978 int ret=0;
979
980 menu_wnd.Create(10,10,MSN_DWNLD_STATUS_W,MSN_DWNLD_STATUS_H,UIF_PROCESS_ALL | UIF_CENTER);
981 texts[0].Create (&menu_wnd,&title_text,0,8,UIF_CENTER);
982 texts[1].Create (&menu_wnd,&download_text,MSN_COL_1,MSN_ROW_1,0);
983 texts[2].Create (&menu_wnd,&rcvd_text,MSN_COL_1,MSN_ROW_2,0);
984 texts[3].Create (&menu_wnd,&total_text,MSN_COL_2,MSN_ROW_2,0);
985 texts[4].Create (&menu_wnd,&elaps_text,MSN_COL_1,MSN_ROW_3,0);
986 texts[5].Create (&menu_wnd,&time_r_text,MSN_COL_2,MSN_ROW_3,0);
987 texts[6].Create (&menu_wnd,&rate_text,MSN_COL_1,MSN_ROW_4,0);
988
989 UITextItem cancel_off(TXT_CANCEL,UICOL_HOTSPOT_LO,UIALPHA_HOTSPOT_LO);
990 UITextItem cancel_on(TXT_CANCEL,UICOL_HOTSPOT_HI,UIALPHA_HOTSPOT_HI);
991
992 progress.Create(&menu_wnd,MSN_COL_1,MSN_ROW_5,MSN_DWNLD_STATUS_W-(MSN_BORDER_W*2),35,0);
993
994 int cancel_x = 0;
995
996 cancel_hot.Create(&menu_wnd, UID_CANCEL, KEY_ESC, &cancel_off,&cancel_on,
997 cancel_x, MSN_DWNLD_STATUS_H - OKCANCEL_YOFFSET,
998 0,0,UIF_FIT|UIF_CENTER); menu_wnd.Open();
999
1000 last_refresh = timer_GetTime()-MSN_REFRESH_INTERVAL;
1001
1002 while (!exit_menu)
1003 {
1004 int res;
1005
1006 if((timer_GetTime()-last_refresh)>MSN_REFRESH_INTERVAL)
1007 {
1008
1009 //Update the dialog
1010 //mprintf(0,"!");
1011
1012 if(getmsnfile->IsFileReceived())
1013 {
1014 //File transfer successful!
1015 mprintf(0,"Succesfully received the file!\n");
1016 exit_menu = 1;
1017
1018 if(file_is_zip)
1019 {
1020 // now we gotta handle the zip file
1021 ret = msn_ExtractZipFile(qualfile,filename);
1022 }else
1023 {
1024 ret = 1;
1025 }
1026 }
1027
1028 if(getmsnfile->IsFileError())
1029 {
1030 //File transfer Error!
1031 DoMessageBox(TXT_ERROR,TXT_FMTCANTDNLD,MSGBOX_OK);
1032 //Delete the file that didn't finish!
1033 ddio_DeleteFile(qualfile);
1034 mprintf(0,"Couldn't download the file! Error: %d\n",getmsnfile->GetErrorCode());
1035 exit_menu = 1;
1036 ret = 0;
1037 }
1038
1039 last_refresh = timer_GetTime();
1040 received_bytes = getmsnfile->GetBytesIn();
1041 total_bytes = getmsnfile->GetTotalBytes();
1042
1043 time_elapsed = timer_GetTime()-starttime;
1044
1045 if(total_bytes)
1046 {
1047 time_remain = ((float)(total_bytes-received_bytes))/((float)(received_bytes/time_elapsed));
1048 }
1049 if(time_elapsed&&received_bytes)
1050 {
1051 xfer_rate = ((float)(received_bytes/time_elapsed));
1052 }
1053 texts[1].Destroy();
1054 texts[2].Destroy();
1055 texts[3].Destroy();
1056 texts[4].Destroy();
1057 texts[5].Destroy();
1058 texts[6].Destroy();
1059
1060 sprintf(fmturl,DOWNLOAD_STATUS_URL_TEXT,url);
1061 msn_ClipURLToWidth(MSN_DWNLD_STATUS_W-(MSN_COL_1+MSN_BORDER_W),fmturl);
1062 sprintf(fmtrcvd,DOWNLOAD_STATUS_RCVD_TEXT,received_bytes);
1063 sprintf(fmttotal,DOWNLOAD_STATUS_TOTAL_TEXT,total_bytes);
1064 sprintf(fmtelaps,DOWNLOAD_STATUS_ELAPS_TEXT,msn_SecondsToString(time_elapsed));
1065 sprintf(fmttimer,DOWNLOAD_STATUS_TIME_R_TEXT,msn_SecondsToString(time_remain));
1066 msn_ClipURLToWidth(MSN_DWNLD_STATUS_W-(MSN_COL_2+MSN_BORDER_W),fmttimer);
1067 sprintf(fmtrate,DOWNLOAD_STATUS_XFERRATE_TEXT,xfer_rate);
1068
1069 download_text = UITextItem (fmturl,UICOL_TEXT_NORMAL);;
1070 rcvd_text = UITextItem (fmtrcvd,UICOL_TEXT_NORMAL);
1071 total_text = UITextItem (fmttotal,UICOL_TEXT_NORMAL);
1072 elaps_text = UITextItem (fmtelaps,UICOL_TEXT_NORMAL);
1073 time_r_text = UITextItem (fmttimer,UICOL_TEXT_NORMAL);
1074 rate_text = UITextItem (fmtrate,UICOL_TEXT_NORMAL);
1075
1076 texts[1].Create (&menu_wnd,&download_text,MSN_COL_1,MSN_ROW_1,0);
1077 texts[2].Create (&menu_wnd,&rcvd_text,MSN_COL_1,MSN_ROW_2,0);
1078 texts[3].Create (&menu_wnd,&total_text,MSN_COL_2,MSN_ROW_2,0);
1079 texts[4].Create (&menu_wnd,&elaps_text,MSN_COL_1,MSN_ROW_3,0);
1080 texts[5].Create (&menu_wnd,&time_r_text,MSN_COL_2,MSN_ROW_3,0);
1081 texts[6].Create (&menu_wnd,&rate_text,MSN_COL_1,MSN_ROW_4,0);
1082
1083 progress.Update(((float)((float)received_bytes)/((float)total_bytes)));
1084 //mprintf(0,"@");
1085 }
1086 //mprintf(0,"-In");
1087 res = PollUI();
1088 //mprintf(0,"-Out");
1089 switch(res)
1090 {
1091 case UID_CANCEL:
1092 getmsnfile->AbortGet();
1093 ddio_DeleteFile(qualfile);
1094 exit_menu = 1;
1095 ret = 0;
1096 break;
1097 }
1098 }
1099 menu_wnd.Close();
1100 menu_wnd.Destroy();
1101 delete getmsnfile;
1102 return ret;
1103 */
1104 }