CHATAPI.CPP 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  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. //
  19. // This module takes care of all the Chat API stuff
  20. //
  21. #include <list>
  22. #include "GameSpy/ghttp/ghttp.h"
  23. #include "downloadManager.h"
  24. #include "chatapi.h"
  25. //#include "api/wolapi_i.c" // This should only be in one .cpp file
  26. #include <objbase.h>
  27. #include <windows.h>
  28. #include <initguid.h>
  29. #include <olectl.h>
  30. #include <mapicode.h>
  31. #include "resource.h"
  32. #include "winblows.h"
  33. #include <crtdbg.h>
  34. #include "process.h"
  35. #include "WWDownload/registry.h"
  36. #include "WWDownload/urlBuilder.h"
  37. #include "debug.h"
  38. enum EVENT_TYPES
  39. {
  40. NOUPDATE_EVENT=0, // don't need to update
  41. ABORT_EVENT,
  42. NUM_EVENTS // keep last
  43. };
  44. #define GAME_NAME "Command & Conquer"
  45. HANDLE Events[NUM_EVENTS];
  46. char g_UpdateString[256]; // for the filename
  47. char g_DLTimeRem[80];
  48. char g_DLBytesLeft[80];
  49. char g_DLBPS[80];
  50. int g_Finished=0;
  51. HWND g_DownloadWindow;
  52. HWND g_ContactWindow;
  53. HWND g_PrimaryWindow;
  54. static bool checkingForPatch = false;
  55. static int checksLeft = 0;
  56. static bool cantConnect = false;
  57. static std::list<QueuedDownload> queuedDownloads;
  58. BOOL CALLBACK downloadDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam,
  59. LPARAM lParam );
  60. ///////////////////////////////////////////////////////////////////////////////////////
  61. static void startOnline( void );
  62. ///////////////////////////////////////////////////////////////////////////////////////
  63. QueuedDownload TheDownload;
  64. class DownloadManagerMunkee : public DownloadManager
  65. {
  66. public:
  67. DownloadManagerMunkee() { }
  68. virtual HRESULT OnError( int error );
  69. virtual HRESULT OnEnd();
  70. virtual HRESULT OnProgressUpdate( int bytesread, int totalsize, int timetaken, int timeleft );
  71. virtual HRESULT OnStatusUpdate( int status );
  72. virtual HRESULT downloadFile( std::string server, std::string username, std::string password, std::string file, std::string localfile, std::string regkey, bool tryResume );
  73. };
  74. ///////////////////////////////////////////////////////////////////////////////////////
  75. HRESULT DownloadManagerMunkee::downloadFile( std::string server, std::string username, std::string password, std::string file, std::string localfile, std::string regkey, bool tryResume )
  76. {
  77. /*
  78. if (staticTextFile)
  79. {
  80. UnicodeString fileString;
  81. fileString.translate(file);
  82. GadgetStaticTextSetText(staticTextFile, fileString);
  83. }
  84. */
  85. return DownloadManager::downloadFile( server, username, password, file, localfile, regkey, tryResume );
  86. }
  87. HRESULT DownloadManagerMunkee::OnError( int error )
  88. {
  89. HRESULT ret = DownloadManager::OnError( error );
  90. g_Finished = -1;
  91. return ret;
  92. }
  93. HRESULT DownloadManagerMunkee::OnEnd()
  94. {
  95. HRESULT ret = DownloadManager::OnEnd();
  96. g_Finished = 1;
  97. return ret;
  98. }
  99. HRESULT DownloadManagerMunkee::OnProgressUpdate( int bytesread, int totalsize, int timetaken, int timeleft )
  100. {
  101. HRESULT ret = DownloadManager::OnProgressUpdate( bytesread, totalsize, timetaken, timeleft );
  102. SendDlgItemMessage( g_DownloadWindow, IDC_PROGRESS, PBM_SETPOS, (WPARAM)(bytesread * 100) / totalsize, 0 );
  103. char temp[256];
  104. if( timeleft > 0 )
  105. {
  106. //DBGMSG("Bytes read: "<<bytesread<<". Time left: "<<timeleft<<" seconds");
  107. LoadString(Global_instance, TXT_TIME_REMAIN, temp, sizeof(temp));
  108. sprintf(g_DLTimeRem,temp,(timeleft/60),(timeleft%60));
  109. LoadString(Global_instance, TXT_BPS, temp, sizeof(temp));
  110. sprintf(g_DLBPS,temp,bytesread/timetaken);
  111. }
  112. LoadString(Global_instance, TXT_BYTES_READ, temp, sizeof(temp));
  113. sprintf(g_DLBytesLeft,temp,bytesread,totalsize);
  114. return ret;
  115. }
  116. HRESULT DownloadManagerMunkee::OnStatusUpdate( int status )
  117. {
  118. HRESULT ret = DownloadManager::OnStatusUpdate( status );
  119. SetWindowText(g_DownloadWindow, getStatusString().c_str());
  120. return ret;
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////////////
  123. BOOL CALLBACK simpleDialogProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  124. {
  125. switch( uMsg )
  126. {
  127. case WM_INITDIALOG:
  128. return(TRUE);
  129. break;
  130. case WM_CLOSE:
  131. DestroyWindow(hwnd);
  132. PostQuitMessage(0);
  133. exit(0);
  134. break;
  135. }
  136. return(FALSE);
  137. }
  138. static void startOnline( void )
  139. {
  140. checkingForPatch = false;
  141. // Close that contacting window
  142. DestroyWindow(g_ContactWindow);
  143. g_ContactWindow=NULL;
  144. if (cantConnect)
  145. {
  146. MessageBox(NULL, "Can't Connect", GAME_NAME, MB_OK);
  147. exit(0);
  148. }
  149. else if (queuedDownloads.size())
  150. {
  151. if (MessageBox(NULL, "Patches Available. Download?", GAME_NAME, MB_YESNO) == IDYES)
  152. {
  153. DEBUG_LOG(("Downloading patches\n"));
  154. while (queuedDownloads.size())
  155. {
  156. TheDownload = queuedDownloads.front();
  157. queuedDownloads.pop_front();
  158. TheDownloadManager = new DownloadManagerMunkee;
  159. /**/
  160. int retVal = DialogBox(Global_instance, MAKEINTRESOURCE(IDD_DOWNLOAD_DIALOG), g_PrimaryWindow, downloadDialogProc);
  161. if (retVal)
  162. {
  163. DEBUG_LOG(("Error %d\n", GetLastError()));
  164. }
  165. /**/
  166. /*
  167. {
  168. //char *res = MAKEINTRESOURCE(IDD_CONNECTING);
  169. char *res = MAKEINTRESOURCE(IDD_DOWNLOAD_DIALOG1);
  170. g_DownloadWindow=CreateDialog(Global_instance,res,g_PrimaryWindow,simpleDialogProc);
  171. ShowWindow(g_DownloadWindow,SW_SHOWNORMAL);
  172. SetForegroundWindow(g_DownloadWindow);
  173. }
  174. */
  175. delete TheDownloadManager;
  176. TheDownloadManager = NULL;
  177. if (g_Finished != 1)
  178. {
  179. // Download failed
  180. //DBGMSG("Download failed: "<<retval);
  181. SetEvent(Events[ABORT_EVENT]);
  182. return;
  183. }
  184. }
  185. exit(0);
  186. }
  187. else
  188. {
  189. exit(0);
  190. }
  191. }
  192. else
  193. {
  194. MessageBox(NULL, "No Patches Available", GAME_NAME, MB_OK);
  195. exit(0);
  196. }
  197. }
  198. ///////////////////////////////////////////////////////////////////////////////////////
  199. static std::string trim(std::string s, const std::string& delim)
  200. {
  201. unsigned int i;
  202. i = s.find_first_not_of(delim);
  203. if (i != s.npos)
  204. {
  205. s = s.substr(i);
  206. }
  207. i = s.find_last_not_of(delim);
  208. if (i>=0 && i<s.npos)
  209. {
  210. s = s.substr(0, i+1);
  211. }
  212. return s;
  213. }
  214. ///////////////////////////////////////////////////////////////////////////////////////
  215. static std::string getNextLine(std::string in, std::string& remainder)
  216. {
  217. int lineEnd;
  218. lineEnd = in.find_first_of("\n\r", 0);
  219. if (lineEnd < 1)
  220. {
  221. remainder = "";
  222. return in;
  223. }
  224. std::string out = in.substr(0, lineEnd);
  225. remainder = in.substr(lineEnd+1);
  226. remainder = trim(remainder, "\r\n\t ");
  227. out = trim(out, "\r\t\n ");
  228. }
  229. ///////////////////////////////////////////////////////////////////////////////////////
  230. //-----------------------------------------------------------------------------
  231. inline const char* skipSeps(const char* p, const char* seps)
  232. {
  233. while (*p && strchr(seps, *p) != NULL)
  234. ++p;
  235. return p;
  236. }
  237. //-----------------------------------------------------------------------------
  238. inline const char* skipNonSeps(const char* p, const char* seps)
  239. {
  240. while (*p && strchr(seps, *p) == NULL)
  241. ++p;
  242. return p;
  243. }
  244. //-----------------------------------------------------------------------------
  245. bool nextToken(std::string& base, std::string& tok, const char* seps = NULL)
  246. {
  247. if (base.empty())
  248. return false;
  249. if (seps == NULL)
  250. seps = " \n\r\t";
  251. const char* start = skipSeps(base.c_str(), seps);
  252. const char* end = skipNonSeps(start, seps);
  253. if (end > start)
  254. {
  255. int len = end - start;
  256. char* tmp = new char[len+1];
  257. memcpy(tmp, start, len);
  258. tmp[len] = 0;
  259. tok = tmp;
  260. delete[] tmp;
  261. base = end;
  262. return true;
  263. }
  264. else
  265. {
  266. base = tok = "";
  267. return false;
  268. }
  269. }
  270. ///////////////////////////////////////////////////////////////////////////////////////
  271. static void queuePatch(bool mandatory, std::string downloadURL)
  272. {
  273. // downloadURL is of the form "ftp://ftp.ea.com:user@pass/pub/munkee/bananna.rtp"
  274. QueuedDownload q;
  275. bool success = true;
  276. std::string connectionType;
  277. success = success && nextToken(downloadURL, connectionType, ":");
  278. std::string server;
  279. success = success && nextToken(downloadURL, server, ":/");
  280. std::string user;
  281. success = success && nextToken(downloadURL, user, ":@");
  282. std::string pass;
  283. success = success && nextToken(downloadURL, pass, "@/");
  284. std::string filePath;
  285. success = success && nextToken(downloadURL, filePath, "");
  286. if (!success && !user.empty())
  287. {
  288. // no user/pass combo - move the file into it's proper place
  289. filePath = user;
  290. user = ""; // LFeenanEA - Credentials removed as per Security requirements
  291. pass = "";
  292. success = true;
  293. }
  294. std::string fileStr = filePath;
  295. unsigned int slashPos = filePath.find_last_of('/');
  296. std::string fileDir = "patches\\";
  297. std::string fileName = "";
  298. if (slashPos == filePath.npos)
  299. {
  300. fileName = filePath;
  301. }
  302. else
  303. {
  304. fileName = filePath.substr(slashPos+1);
  305. }
  306. fileDir.append(fileName);
  307. DEBUG_LOG(("download URL split: %d [%s] [%s] [%s] [%s] [%s] [%s] [%s]\n",
  308. success, connectionType.c_str(), server.c_str(), user.c_str(), pass.c_str(),
  309. filePath.c_str(), fileName.c_str(), fileDir.c_str()));
  310. if (!success)
  311. return;
  312. q.file = filePath;
  313. q.localFile = fileDir;
  314. q.password = pass;
  315. q.regKey = "";
  316. q.server = server;
  317. q.tryResume = true;
  318. q.userName = user;
  319. std::list<QueuedDownload>::iterator it = queuedDownloads.begin();
  320. while (it != queuedDownloads.end())
  321. {
  322. if (it->localFile == q.localFile)
  323. return; // don't add it if it exists already (because we can check multiple times)
  324. ++it;
  325. }
  326. queuedDownloads.push_back(q);
  327. }
  328. ///////////////////////////////////////////////////////////////////////////////////////
  329. static GHTTPBool patchCheckCallback( GHTTPRequest request, GHTTPResult result, char * buffer, int bufferLen, void * param )
  330. {
  331. --checksLeft;
  332. DEBUG_ASSERTCRASH(checksLeft>=0, ("Too many callbacks"));
  333. DEBUG_LOG(("Result=%d, buffer=[%s], len=%d\n", result, buffer, bufferLen));
  334. if (result != GHTTPSuccess)
  335. {
  336. cantConnect = true;
  337. if (!checksLeft)
  338. {
  339. startOnline();
  340. }
  341. return GHTTPTrue;
  342. }
  343. std::string message = buffer;
  344. std::string line;
  345. while (nextToken(message, line, "\r\n"))
  346. {
  347. std::string type, req, url;
  348. bool ok = true;
  349. ok = ok && nextToken(line, type, " ");
  350. ok = ok && nextToken(line, req, " ");
  351. ok = ok && nextToken(line, url, " ");
  352. if (ok && type == "patch")
  353. {
  354. DEBUG_LOG(("Saw a patch: %d/[%s]\n", atoi(req.c_str()), url.c_str()));
  355. queuePatch( atoi(req.c_str()), url );
  356. }
  357. else if (ok && type == "server")
  358. {
  359. }
  360. }
  361. if (!checksLeft)
  362. {
  363. startOnline();
  364. }
  365. return GHTTPTrue;
  366. }
  367. ///////////////////////////////////////////////////////////////////////////////////////
  368. static void StartPatchCheck( void )
  369. {
  370. checkingForPatch = true;
  371. std::string gameURL, mapURL;
  372. std::string configURL, motdURL;
  373. FormatURLFromRegistry(gameURL, mapURL, configURL, motdURL);
  374. std::string proxy;
  375. if (GetStringFromRegistry("", "Proxy", proxy))
  376. {
  377. if (!proxy.empty())
  378. {
  379. ghttpSetProxy(proxy.c_str());
  380. }
  381. }
  382. // check for a patch first
  383. checksLeft = 2;
  384. cantConnect = false;
  385. ghttpGet(gameURL.c_str(), GHTTPFalse, patchCheckCallback, NULL);
  386. ghttpGet(mapURL.c_str(), GHTTPFalse, patchCheckCallback, NULL);
  387. DEBUG_LOG(("Started looking for patches at '%s' && '%s'\n", gameURL.c_str(), mapURL.c_str()));
  388. }
  389. ///////////////////////////////////////////////////////////////////////////////////////
  390. BOOL CALLBACK downloadDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  391. {
  392. // HRESULT res;
  393. int cmd = LOWORD(wParam);
  394. switch( uMsg )
  395. {
  396. case WM_COMMAND:
  397. if ( cmd == IDC_DLABORT )
  398. {
  399. char abort[128];
  400. char abortdload[256];
  401. LoadString(Global_instance, TXT_ABORT_DOWNLOAD, abortdload, sizeof(abortdload));
  402. LoadString(Global_instance, TXT_ABORT, abort, sizeof(abort));
  403. if (MessageBox(g_PrimaryWindow,abortdload,abort,MB_YESNO)==IDYES)
  404. {
  405. TheDownloadManager->reset();
  406. EndDialog( hwndDlg, g_Finished );
  407. DestroyWindow(hwndDlg);
  408. }
  409. }
  410. else
  411. {
  412. return FALSE;
  413. }
  414. break;
  415. case WM_INITDIALOG:
  416. //SetupDownload();
  417. SendMessage(hwndDlg, WM_SETICON,(WPARAM)ICON_SMALL,
  418. (LPARAM)LoadIcon(Global_instance, MAKEINTRESOURCE(IDI_ICON1)));
  419. g_DLTimeRem[0]=0;
  420. g_DLBytesLeft[0]=0;
  421. g_DLBPS[0]=0;
  422. //SetDlgItemText( hwndDlg, IDC_DOWNLOADTITLE, g_UpdateString);
  423. //SetWindowText(hwndDlg, g_UpdateString);
  424. SetDlgItemText( hwndDlg, IDC_TIMEREM, g_DLTimeRem);
  425. SetDlgItemText( hwndDlg, IDC_BYTESLEFT, g_DLBytesLeft);
  426. // SetDlgItemText( hwndDlg, IDC_BPS, g_DLBPS );
  427. // Work out the full file name
  428. //char fullpath[_MAX_PATH];
  429. //char localfile[_MAX_PATH];
  430. //sprintf( fullpath, "%s/%s", g_Update->patchpath,g_Update->patchfile);
  431. //sprintf(localfile,"%s\\%s",g_Update->localpath,g_Update->patchfile);
  432. // Create the directory
  433. //CreateDirectory((char *)g_Update->localpath, NULL );
  434. TheDownloadManager->downloadFile(TheDownload.server, TheDownload.userName, TheDownload.password,
  435. TheDownload.file, TheDownload.localFile, TheDownload.regKey, TheDownload.tryResume);
  436. /*
  437. res=pDownload->DownloadFile((char *)g_Update->server, (char *)g_Update->login, (char *)g_Update->password,
  438. fullpath, localfile, APP_REG_KEY);
  439. */
  440. g_DownloadWindow = hwndDlg;
  441. g_Finished = 0;
  442. SetTimer( hwndDlg, 1, 200, NULL ); // was 50
  443. break;
  444. case WM_TIMER:
  445. DEBUG_LOG(("TIMER\n"));
  446. if( g_Finished == 0 )
  447. {
  448. DEBUG_LOG(("Entering PumpMsgs\n"));
  449. TheDownloadManager->update();
  450. /*
  451. pDownload->PumpMessages();
  452. */
  453. DEBUG_LOG(("Done with PumpMsgs\n"));
  454. if (strlen(g_DLTimeRem))
  455. SetDlgItemText( hwndDlg, IDC_TIMEREM, g_DLTimeRem );
  456. if (strlen(g_DLBytesLeft))
  457. SetDlgItemText( hwndDlg, IDC_BYTESLEFT, g_DLBytesLeft );
  458. //if (strlen(g_DLBPS))
  459. // SetDlgItemText( hwndDlg, IDC_BPS, g_DLBPS );
  460. }
  461. else
  462. {
  463. DEBUG_LOG(("TIMER: Finished\n"));
  464. EndDialog( hwndDlg, g_Finished );
  465. DestroyWindow( hwndDlg );
  466. }
  467. break;
  468. case WM_DESTROY:
  469. KillTimer( hwndDlg, 1 );
  470. //ClosedownDownload();
  471. //DBGMSG("WM_DESTROY");
  472. break;
  473. case WM_SETFONT:
  474. return TRUE;
  475. default:
  476. return FALSE;
  477. }
  478. return TRUE;
  479. }
  480. DWORD dwChatAdvise;
  481. DWORD dwDownloadAdvise;
  482. //Update *g_Update;
  483. uint32 g_AppVer=-1;
  484. BOOL CALLBACK Download_Dialog_Proc( HWND hwndDlg, UINT uMsg, WPARAM wParam,
  485. LPARAM lParam );
  486. BOOL CALLBACK Simple_Dialog_Proc( HWND hwndDlg, UINT uMsg, WPARAM wParam,
  487. LPARAM lParam );
  488. HWND CreatePrimaryWin(void);
  489. char const * Fetch_String(int id);
  490. //
  491. // Create a primary window
  492. //
  493. HWND CreatePrimaryWin(void)
  494. {
  495. HWND hwnd;
  496. WNDCLASS wc;
  497. char name[256];
  498. sprintf(name,Fetch_String(TXT_TITLE));
  499. //DBGMSG("CreatePrimary: "<<name);
  500. /*
  501. ** set up and register window class
  502. */
  503. wc.style = CS_HREDRAW | CS_VREDRAW;
  504. wc.lpfnWndProc = DefWindowProc;
  505. wc.cbClsExtra = 0; // Don't need any extra class data
  506. wc.cbWndExtra = 0; // No extra win data
  507. wc.hInstance = Global_instance;
  508. wc.hIcon=LoadIcon(Global_instance, MAKEINTRESOURCE(IDI_ICON1));
  509. wc.hCursor = NULL; /////////LoadCursor( NULL, IDC_ARROW );
  510. wc.hbrBackground = NULL;
  511. wc.lpszMenuName = name;
  512. wc.lpszClassName = name;
  513. RegisterClass( &wc );
  514. /*
  515. ** create a window
  516. */
  517. hwnd = CreateWindowEx(
  518. WS_EX_APPWINDOW,
  519. name,
  520. name,
  521. WS_POPUP,
  522. 0, 0,
  523. //GetSystemMetrics( SM_CXSCREEN ),
  524. //GetSystemMetrics( SM_CYSCREEN ),
  525. 0,0,
  526. NULL,
  527. NULL,
  528. Global_instance,
  529. NULL );
  530. SendMessage(hwnd,WM_SETICON,(WPARAM)ICON_SMALL,
  531. (LPARAM)LoadIcon(Global_instance, MAKEINTRESOURCE(IDI_ICON1)));
  532. ShowWindow(hwnd,SW_SHOWNORMAL);
  533. return(hwnd);
  534. }
  535. //
  536. // Dispatch pending windows events
  537. //
  538. void DispatchEvents(void)
  539. {
  540. MSG msg;
  541. int counter=0;
  542. while(PeekMessage(&msg,NULL,0,0, PM_REMOVE))
  543. {
  544. TranslateMessage(&msg);
  545. DispatchMessage(&msg);
  546. counter++;
  547. if (counter==256) // just in case
  548. break;
  549. }
  550. }
  551. //
  552. // Check for patches
  553. //
  554. int main(int argc, char *argv[])
  555. {
  556. InitCommonControls();
  557. /*
  558. g_PrimaryWindow=CreatePrimaryWin(); // Create the main window
  559. DispatchEvents(); // process some win messages
  560. */
  561. /*
  562. // Check if they've registered before, if not ask them if they want to
  563. bool have_registered=false;
  564. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,REGISTER_REG_KEY,0,KEY_READ,&rKey)==ERROR_SUCCESS)
  565. {
  566. char username[64];
  567. valuesize=sizeof(username);
  568. if (RegQueryValueEx(rKey,"UserName",NULL,&type,(uint8 *)username,&valuesize)==ERROR_SUCCESS)
  569. have_registered=true;
  570. RegCloseKey(rKey);
  571. }
  572. if (!have_registered)
  573. {
  574. if (RegOpenKeyEx(HKEY_CLASSES_ROOT,NICK_REG_KEY,0,KEY_READ,&rKey)==ERROR_SUCCESS)
  575. {
  576. have_registered=true;
  577. RegCloseKey(rKey);
  578. }
  579. }
  580. if (!have_registered)
  581. {
  582. if (MessageBox(NULL,Fetch_String(TXT_REGNOW),Fetch_String(TXT_TITLE),MB_YESNO)==IDNO)
  583. have_registered=true; // pretend they've alredy registered
  584. }
  585. if (!have_registered)
  586. {
  587. // figure out where the registration app is installed & launch it, continue
  588. // after it exits.
  589. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,REGISTER_REG_APP,0,KEY_READ,&rKey)==ERROR_SUCCESS)
  590. {
  591. char regapp[300];
  592. valuesize=sizeof(regapp);
  593. if ((RegQueryValueEx(rKey,"InstallPath",NULL,&type,(uint8 *)regapp,&valuesize)==ERROR_SUCCESS)&&
  594. (strlen(regapp) > 8))
  595. {
  596. // Launch the process
  597. SHELLEXECUTEINFO info;
  598. memset(&info,0,sizeof(info));
  599. info.cbSize=sizeof(info);
  600. info.fMask=SEE_MASK_NOCLOSEPROCESS;
  601. info.hwnd=g_PrimaryWindow;
  602. info.lpVerb=NULL;
  603. info.lpFile=regapp;
  604. info.lpParameters=NULL;
  605. info.lpDirectory=".";
  606. info.nShow=SW_SHOW;
  607. ShellExecuteEx(&info);
  608. // Can't wait infinite or the other process will never create its window
  609. // Only Bill himself knows why this is happening
  610. while(1) // Wait for completion
  611. {
  612. DispatchEvents();
  613. if (WaitForSingleObject(info.hProcess,500)!=WAIT_TIMEOUT)
  614. break;
  615. }
  616. }
  617. RegCloseKey(rKey);
  618. }
  619. }
  620. // OK, done with that crap go on to the task at hand now....
  621. */
  622. // Find the game version
  623. g_AppVer = -1;
  624. if (!GetUnsignedIntFromRegistry("", "Version", g_AppVer))
  625. {
  626. MessageBox(g_PrimaryWindow,Fetch_String(TXT_INSTALL_PROBLEM),Fetch_String(TXT_ERROR),MB_OK);
  627. exit(0);
  628. }
  629. // OK, have the current game version now
  630. g_PrimaryWindow=CreatePrimaryWin(); // Create the main window
  631. DispatchEvents(); // process some win messages
  632. // Popup the "contacting" window
  633. g_ContactWindow=CreateDialog(Global_instance,MAKEINTRESOURCE(IDD_CONNECTING),g_PrimaryWindow,Simple_Dialog_Proc);
  634. ShowWindow(g_ContactWindow,SW_SHOWNORMAL);
  635. SetForegroundWindow(g_ContactWindow);
  636. DispatchEvents(); // process some win messages
  637. // Setup the Westwood Online stuff
  638. Startup_Chat();
  639. Update_If_Required();
  640. Shutdown_Chat();
  641. return(0);
  642. }
  643. typedef struct SRecord {
  644. int ID; // ID number of the string resource.
  645. int TimeStamp; // 'Time' that this string was last requested.
  646. char String[2048]; // Copy of string resource.
  647. SRecord(void) : ID(-1), TimeStamp(-1) {}
  648. } SRecord;
  649. /***********************************************************************************************
  650. * Fetch_String -- Fetches a string resource. *
  651. * *
  652. * Fetches a string resource and returns a pointer to its text. *
  653. * *
  654. * INPUT: id -- The ID number of the string resource to fetch. *
  655. * *
  656. * OUTPUT: Returns with a pointer to the actual text of the string resource. *
  657. * *
  658. * WARNINGS: none *
  659. * *
  660. * HISTORY: *
  661. * 12/25/1996 JLB : Created. *
  662. *=============================================================================================*/
  663. char const * Fetch_String(int id)
  664. {
  665. static SRecord _buffers[64];
  666. static int _time = 0;
  667. /*
  668. ** Determine if the string ID requested is valid. If not then return an empty string pointer.
  669. */
  670. if (id == -1 || id == TXT_NONE) return("");
  671. /*
  672. ** Adjust the 'time stamp' tracking value. This is an artificial value used merely to track
  673. ** the relative age of the strings requested.
  674. */
  675. _time = _time+1;
  676. /*
  677. ** Check to see if the requested string has already been fetched into a buffer. If so, then
  678. ** return a pointer to that string (update the time stamp as well).
  679. */
  680. for (int index = 0; index < ARRAY_SIZE(_buffers); index++) {
  681. if (_buffers[index].ID == id) {
  682. _buffers[index].TimeStamp = _time;
  683. return(_buffers[index].String);
  684. }
  685. }
  686. /*
  687. ** Find a suitable buffer to hold the string to be fetched. The buffer should either be
  688. ** empty or have the oldest fetched string.
  689. */
  690. int oldest = -1;
  691. int oldtime = -1;
  692. for (int text = 0; text < ARRAY_SIZE(_buffers); text++) {
  693. if (oldest == -1 || oldtime > _buffers[text].TimeStamp) {
  694. oldest = text;
  695. oldtime = _buffers[text].TimeStamp;
  696. if (oldtime == -1 || _buffers[text].ID == -1) break;
  697. }
  698. }
  699. /*
  700. ** A suitable buffer has been found so fetch the string resource and then return a pointer
  701. ** to the string.
  702. */
  703. char * stringptr = _buffers[oldest].String;
  704. _buffers[oldest].ID = id;
  705. _buffers[oldest].TimeStamp = _time;
  706. if (LoadString(Global_instance, id, stringptr, sizeof(_buffers[oldest].String)) == 0) {
  707. return("");
  708. }
  709. /******
  710. char resname[32];
  711. sprintf(resname,"#%d",id);
  712. HMODULE hmod=GetModuleHandle(NULL);
  713. HRSRC hrsrc=FindResourceEx(hmod, RT_STRING, MAKEINTRESOURCE(id), LANGID);
  714. if (hrsrc==0)
  715. {
  716. char message_buffer[256];
  717. FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &message_buffer[0], 256, NULL );
  718. }
  719. HGLOBAL resdata=LoadResource(NULL,hrsrc);
  720. LPVOID vdata=LockResource(resdata);
  721. strcpy(stringptr,(char *)vdata);
  722. *********/
  723. stringptr[sizeof(_buffers[oldest].String)-1] = '\0';
  724. return(stringptr);
  725. }
  726. void LogMsg(char *msg)
  727. {
  728. #ifdef _DEBUG
  729. FILE *out=fopen("register.log","a");
  730. fprintf(out,"%s\n",msg);
  731. fflush(out);
  732. fclose(out);
  733. #endif
  734. }
  735. void Startup_Chat(void)
  736. {
  737. /*
  738. //////CComObject<CChatEventSink>* g_pChatSink;
  739. HRESULT hRes;
  740. g_pChatSink=NULL;
  741. CoCreateInstance(CLSID_Chat, NULL, CLSCTX_INPROC_SERVER,
  742. IID_IChat, (void**)&pChat);
  743. if (pChat==NULL)
  744. {
  745. char error[128];
  746. char apimissing[256];
  747. LoadString(Global_instance, TXT_API_MISSING, apimissing, sizeof(apimissing));
  748. LoadString(Global_instance, TXT_ERROR, error, sizeof(error));
  749. MessageBox(g_PrimaryWindow,apimissing,error,MB_OK);
  750. exit(-5);
  751. }
  752. g_pChatSink=new CChatEventSink;
  753. // Get a connection point from the chat class
  754. IConnectionPoint *pConnectionPoint=NULL;
  755. IConnectionPointContainer *pContainer=NULL;
  756. dwChatAdvise=0;
  757. hRes=pChat->QueryInterface(IID_IConnectionPointContainer,(void**)&pContainer);
  758. _ASSERTE(SUCCEEDED(hRes));
  759. hRes=pContainer->FindConnectionPoint(IID_IChatEvent,&pConnectionPoint);
  760. _ASSERTE(SUCCEEDED(hRes));
  761. hRes=pConnectionPoint->Advise((IChatEvent *)g_pChatSink,&dwChatAdvise);
  762. _ASSERTE(SUCCEEDED(hRes));
  763. pChat->SetAttributeValue("RegPath",APP_REG_KEY);
  764. // ADD pConnectionPoint->Release();
  765. */
  766. }
  767. void Shutdown_Chat(void)
  768. {
  769. /*
  770. /////AtlUnadvise(pChat, IID_IChatEvent, dwChatAdvise);
  771. IConnectionPoint *pConnectionPoint=NULL;
  772. IConnectionPointContainer *pContainer=NULL;
  773. HRESULT hRes;
  774. hRes=pChat->QueryInterface(IID_IConnectionPointContainer,(void**)&pContainer);
  775. _ASSERTE(SUCCEEDED(hRes));
  776. hRes=pContainer->FindConnectionPoint(IID_IChatEvent,&pConnectionPoint);
  777. _ASSERTE(SUCCEEDED(hRes));
  778. pConnectionPoint->Unadvise(dwChatAdvise);
  779. pChat->Release();
  780. /////delete(g_pChatSink); This appears to be bad....
  781. // ADD g_pChatSink->Release();
  782. // ADD pConnectionPoint->Release();
  783. // ADD pContainer->Release();
  784. */
  785. }
  786. //
  787. // Download a patch for the registration client if required
  788. // This uses the chat API for ver checking and FTP.
  789. //
  790. void Update_If_Required(void)
  791. {
  792. int retval;
  793. int i;
  794. // Create the events
  795. for (i=0; i<NUM_EVENTS; i++)
  796. Events[i]=CreateEvent(NULL,FALSE,FALSE,NULL);
  797. StartPatchCheck();
  798. while (1)
  799. {
  800. ghttpThink();
  801. MSG msg;
  802. while(PeekMessage(&msg,NULL,0,0, PM_REMOVE))
  803. {
  804. TranslateMessage(&msg);
  805. DispatchMessage(&msg);
  806. }
  807. retval=WaitForMultipleObjectsEx(NUM_EVENTS,Events,FALSE,50,FALSE);
  808. if (retval==WAIT_TIMEOUT)
  809. continue;
  810. //DBGMSG("An event was set");
  811. retval-=WAIT_OBJECT_0;
  812. break;
  813. }
  814. //DBGMSG("Out of the loop")
  815. if (retval==ABORT_EVENT)
  816. {
  817. exit(0);
  818. }
  819. else
  820. {
  821. //DBGMSG("NO update required");
  822. }
  823. //DBGMSG("Shutting down");
  824. // close all the event objects
  825. for (i=0; i<NUM_EVENTS; i++)
  826. CloseHandle(Events[i]);
  827. /*
  828. Startup_Chat();
  829. int retval;
  830. int i;
  831. // Create the events
  832. for (i=0; i<NUM_EVENTS; i++)
  833. Events[i]=CreateEvent(NULL,FALSE,FALSE,NULL);
  834. /// For Testing....
  835. ///pChat->RequestServerList(1000,262364,"register","regpas98",15);
  836. ///pChat->RequestServerList(1000,300,"register","regpas98",15);
  837. pChat->RequestServerList(g_AppSku,g_AppVer,"register","regpas98",40);
  838. while(1)
  839. {
  840. pChat->PumpMessages();
  841. MSG msg;
  842. while(PeekMessage(&msg,NULL,0,0, PM_REMOVE))
  843. {
  844. TranslateMessage(&msg);
  845. DispatchMessage(&msg);
  846. }
  847. retval=WaitForMultipleObjectsEx(NUM_EVENTS,Events,FALSE,50,FALSE);
  848. if (retval==WAIT_TIMEOUT)
  849. continue;
  850. //DBGMSG("An event was set");
  851. retval-=WAIT_OBJECT_0;
  852. break;
  853. }
  854. //DBGMSG("Out of the loop")
  855. if (retval==ABORT_EVENT)
  856. {
  857. exit(0);
  858. }
  859. else
  860. {
  861. //DBGMSG("NO update required");
  862. }
  863. //DBGMSG("Shutting down");
  864. // close all the event objects
  865. for (i=0; i<NUM_EVENTS; i++)
  866. CloseHandle(Events[i]);
  867. Shutdown_Chat();
  868. */
  869. }
  870. /*
  871. CChatEventSink::CChatEventSink()
  872. {
  873. m_cRef=0; // init the refrence count
  874. }
  875. ///////////////////////////////////////////////////////////
  876. //
  877. // Interface IUnknown Methods
  878. //
  879. ///////////////////////////////////////////////////////////
  880. //
  881. // QueryInterface
  882. //
  883. HRESULT __stdcall
  884. CChatEventSink::QueryInterface(const IID& iid, void** ppv)
  885. {
  886. if ((iid == IID_IUnknown) ||(iid == IID_IChatEvent))
  887. {
  888. *ppv = static_cast<IChatEvent*>(this) ;
  889. }
  890. else
  891. {
  892. *ppv = NULL;
  893. return E_NOINTERFACE;
  894. }
  895. (reinterpret_cast<IUnknown*>(*ppv))->AddRef() ;
  896. return S_OK ;
  897. }
  898. ///////////////////////////////////////////////////////////
  899. //
  900. // AddRef
  901. //
  902. ULONG __stdcall
  903. CChatEventSink::AddRef()
  904. {
  905. return InterlockedIncrement(&m_cRef) ;
  906. }
  907. ///////////////////////////////////////////////////////////
  908. //
  909. // Release
  910. //
  911. ULONG __stdcall
  912. CChatEventSink::Release()
  913. {
  914. if (InterlockedDecrement(&m_cRef) == 0)
  915. {
  916. delete this ;
  917. return 0 ;
  918. }
  919. return m_cRef;
  920. }
  921. ///// DOWNLOAD
  922. CDownloadEventSink::CDownloadEventSink()
  923. {
  924. m_cRef=0; // Ref counter
  925. }
  926. ///////////////////////////////////////////////////////////
  927. //
  928. // Interface IUnknown Methods
  929. //
  930. ///////////////////////////////////////////////////////////
  931. //
  932. // QueryInterface
  933. //
  934. HRESULT __stdcall
  935. CDownloadEventSink::QueryInterface(const IID& iid, void** ppv)
  936. {
  937. if ((iid == IID_IUnknown) ||(iid == IID_IDownloadEvent))
  938. {
  939. *ppv = static_cast<IDownloadEvent*>(this) ;
  940. }
  941. else
  942. {
  943. *ppv = NULL;
  944. return E_NOINTERFACE;
  945. }
  946. (reinterpret_cast<IUnknown*>(*ppv))->AddRef() ;
  947. return S_OK ;
  948. }
  949. ///////////////////////////////////////////////////////////
  950. //
  951. // AddRef
  952. //
  953. ULONG __stdcall
  954. CDownloadEventSink::AddRef()
  955. {
  956. return InterlockedIncrement(&m_cRef) ;
  957. }
  958. ///////////////////////////////////////////////////////////
  959. //
  960. // Release
  961. //
  962. ULONG __stdcall
  963. CDownloadEventSink::Release()
  964. {
  965. if (InterlockedDecrement(&m_cRef) == 0)
  966. {
  967. delete this ;
  968. return 0 ;
  969. }
  970. return m_cRef;
  971. }
  972. */
  973. //// FTP Download stuff
  974. void SetupDownload( void )
  975. {
  976. /*
  977. HRESULT hRes;
  978. g_pDownloadSink=NULL;
  979. CoCreateInstance(CLSID_Download, NULL, CLSCTX_INPROC_SERVER,
  980. IID_IDownload, (void**)&pDownload);
  981. _ASSERTE(pDownload);
  982. g_pDownloadSink=new CDownloadEventSink;
  983. // Get a connection point from the chat class
  984. IConnectionPoint *pConnectionPoint=NULL;
  985. IConnectionPointContainer *pContainer=NULL;
  986. dwDownloadAdvise = 0;
  987. hRes=pDownload->QueryInterface(IID_IConnectionPointContainer,(void**)&pContainer);
  988. _ASSERTE(SUCCEEDED(hRes));
  989. hRes=pContainer->FindConnectionPoint(IID_IDownloadEvent,&pConnectionPoint);
  990. _ASSERTE(SUCCEEDED(hRes));
  991. hRes=pConnectionPoint->Advise((IDownloadEvent *)g_pDownloadSink,&dwDownloadAdvise);
  992. _ASSERTE(SUCCEEDED(hRes));
  993. */
  994. }
  995. void ClosedownDownload( void )
  996. {
  997. /*
  998. // AtlUnadvise(pDownload, IID_IDownloadEvent, dwDownloadAdvise);
  999. IConnectionPoint *pConnectionPoint=NULL;
  1000. IConnectionPointContainer *pContainer=NULL;
  1001. HRESULT hRes;
  1002. hRes=pDownload->QueryInterface(IID_IConnectionPointContainer,(void**)&pContainer);
  1003. _ASSERTE(SUCCEEDED(hRes));
  1004. hRes=pContainer->FindConnectionPoint(IID_IDownloadEvent,&pConnectionPoint);
  1005. _ASSERTE(SUCCEEDED(hRes));
  1006. pConnectionPoint->Unadvise(dwDownloadAdvise);
  1007. pDownload->Release();
  1008. //////delete(g_pDownloadSink); This appears to be bad....
  1009. */
  1010. }
  1011. BOOL CALLBACK Download_Dialog_Proc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  1012. {
  1013. // char fullpath[ 256 ];
  1014. // char localfile[ 256];
  1015. // HRESULT res;
  1016. switch( uMsg )
  1017. {
  1018. case WM_COMMAND:
  1019. switch( LOWORD( wParam ) )
  1020. {
  1021. case IDC_DLABORT:
  1022. {
  1023. char abort[128];
  1024. char abortdload[256];
  1025. LoadString(Global_instance, TXT_ABORT_DOWNLOAD, abortdload, sizeof(abortdload));
  1026. LoadString(Global_instance, TXT_ABORT, abort, sizeof(abort));
  1027. if (MessageBox(g_PrimaryWindow,abortdload,abort,MB_YESNO)==IDYES)
  1028. {
  1029. /*
  1030. pDownload->Abort();
  1031. */
  1032. EndDialog( hwndDlg, g_Finished );
  1033. DestroyWindow(hwndDlg);
  1034. }
  1035. }
  1036. break;
  1037. default:
  1038. return FALSE;
  1039. }
  1040. break;
  1041. case WM_INITDIALOG:
  1042. SetupDownload();
  1043. SendMessage(hwndDlg, WM_SETICON,(WPARAM)ICON_SMALL,
  1044. (LPARAM)LoadIcon(Global_instance, MAKEINTRESOURCE(IDI_ICON1)));
  1045. g_DLTimeRem[0]=0;
  1046. g_DLBytesLeft[0]=0;
  1047. g_DLBPS[0]=0;
  1048. //SetDlgItemText( hwndDlg, IDC_DOWNLOADTITLE, g_UpdateString);
  1049. //SetWindowText(hwndDlg, g_UpdateString);
  1050. SetDlgItemText( hwndDlg, IDC_TIMEREM, g_DLTimeRem);
  1051. SetDlgItemText( hwndDlg, IDC_BYTESLEFT, g_DLBytesLeft);
  1052. // SetDlgItemText( hwndDlg, IDC_BPS, g_DLBPS );
  1053. /*
  1054. // Work out the full file name
  1055. sprintf( fullpath, "%s/%s", g_Update->patchpath,g_Update->patchfile);
  1056. sprintf(localfile,"%s\\%s",g_Update->localpath,g_Update->patchfile);
  1057. // Create the directory
  1058. CreateDirectory((char *)g_Update->localpath, NULL );
  1059. res=pDownload->DownloadFile((char *)g_Update->server, (char *)g_Update->login, (char *)g_Update->password,
  1060. fullpath, localfile, APP_REG_KEY);
  1061. */
  1062. g_DownloadWindow = hwndDlg;
  1063. g_Finished = 0;
  1064. SetTimer( hwndDlg, 1, 200, NULL ); // was 50
  1065. break;
  1066. case WM_TIMER:
  1067. LogMsg("TIMER");
  1068. if( g_Finished == 0 )
  1069. {
  1070. LogMsg("Entering PumpMsgs");
  1071. /*
  1072. pDownload->PumpMessages();
  1073. */
  1074. LogMsg("Done with PumpMsgs");
  1075. if (strlen(g_DLTimeRem))
  1076. SetDlgItemText( hwndDlg, IDC_TIMEREM, g_DLTimeRem );
  1077. if (strlen(g_DLBytesLeft))
  1078. SetDlgItemText( hwndDlg, IDC_BYTESLEFT, g_DLBytesLeft );
  1079. //if (strlen(g_DLBPS))
  1080. // SetDlgItemText( hwndDlg, IDC_BPS, g_DLBPS );
  1081. }
  1082. else
  1083. {
  1084. LogMsg("TIMER: Finished");
  1085. EndDialog( hwndDlg, g_Finished );
  1086. DestroyWindow( hwndDlg );
  1087. }
  1088. break;
  1089. case WM_DESTROY:
  1090. KillTimer( hwndDlg, 1 );
  1091. ClosedownDownload();
  1092. //DBGMSG("WM_DESTROY");
  1093. break;
  1094. case WM_SETFONT:
  1095. return TRUE;
  1096. default:
  1097. return FALSE;
  1098. }
  1099. return TRUE;
  1100. }
  1101. // Whoeee this is an exciting one...
  1102. BOOL CALLBACK Simple_Dialog_Proc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  1103. {
  1104. switch( uMsg )
  1105. {
  1106. case WM_INITDIALOG:
  1107. return(TRUE);
  1108. break;
  1109. case WM_CLOSE:
  1110. DestroyWindow(hwnd);
  1111. PostQuitMessage(0);
  1112. exit(0);
  1113. break;
  1114. }
  1115. return(FALSE);
  1116. }
  1117. /////////////////////////////////////////////////////////////////////////////
  1118. // CDownloadEventSink
  1119. //////////////////////////////////////////////////////////////////////////////
  1120. /*
  1121. STDMETHODIMP CDownloadEventSink::OnEnd(void)
  1122. {
  1123. LogMsg("Finished!");
  1124. g_Finished = 1;
  1125. return(S_OK);
  1126. }
  1127. STDMETHODIMP CDownloadEventSink::OnError(int error)
  1128. {
  1129. LogMsg("ERROR");
  1130. g_Finished = -1;
  1131. return(S_OK);
  1132. }
  1133. STDMETHODIMP CDownloadEventSink::OnProgressUpdate(int bytesread, int totalsize,
  1134. int timetaken, int timeleft)
  1135. {
  1136. SendDlgItemMessage( g_DownloadWindow, IDC_PROGRESS, PBM_SETPOS, (WPARAM)(bytesread * 100) / totalsize, 0 );
  1137. char temp[256];
  1138. if( timeleft > 0 )
  1139. {
  1140. //DBGMSG("Bytes read: "<<bytesread<<". Time left: "<<timeleft<<" seconds");
  1141. LoadString(Global_instance, TXT_TIME_REMAIN, temp, sizeof(temp));
  1142. sprintf(g_DLTimeRem,temp,(timeleft/60),(timeleft%60));
  1143. LoadString(Global_instance, TXT_BPS, temp, sizeof(temp));
  1144. sprintf(g_DLBPS,temp,bytesread/timetaken);
  1145. }
  1146. LoadString(Global_instance, TXT_BYTES_READ, temp, sizeof(temp));
  1147. sprintf(g_DLBytesLeft,temp,bytesread,totalsize);
  1148. return(S_OK);
  1149. }
  1150. STDMETHODIMP CDownloadEventSink::OnStatusUpdate(int status)
  1151. {
  1152. switch( status )
  1153. {
  1154. case DOWNLOADSTATUS_CONNECTING:
  1155. {
  1156. //LogMsg( "Connecting..." );
  1157. SetWindowText(g_DownloadWindow, Fetch_String(TXT_CONNECTING));
  1158. }
  1159. break;
  1160. case DOWNLOADSTATUS_FINDINGFILE:
  1161. //LogMsg( "Finding patch..." );
  1162. SetWindowText(g_DownloadWindow, g_UpdateString);
  1163. break;
  1164. case DOWNLOADSTATUS_DOWNLOADING:
  1165. //LogMsg( "Downloading patch..." );
  1166. SetWindowText(g_DownloadWindow, g_UpdateString);
  1167. break;
  1168. default:
  1169. //LogMsg("Unknown status update!");
  1170. break;
  1171. }
  1172. return(S_OK);
  1173. }
  1174. //
  1175. // Just tell the FTP module to go ahead and resume
  1176. //
  1177. STDMETHODIMP CDownloadEventSink::OnQueryResume(void)
  1178. {
  1179. return(DOWNLOADEVENT_RESUME);
  1180. }
  1181. /////////////////////////////////////////////////////////////////////////////
  1182. // CChatEventSink
  1183. //////////////////////////////////////////////////////////////////////////////
  1184. STDMETHODIMP CChatEventSink::OnServerList(HRESULT res, Server* servers)
  1185. {
  1186. // If we get this, then we don't need to patch
  1187. // Close that contacting window
  1188. DestroyWindow(g_ContactWindow);
  1189. g_ContactWindow=NULL;
  1190. LogMsg("Server List");
  1191. MessageBox(g_PrimaryWindow,Fetch_String(TXT_NO_PATCHES),Fetch_String(TXT_AUTO_UPDATE),MB_OK);
  1192. SetEvent(Events[NOUPDATE_EVENT]);
  1193. return(S_OK);
  1194. }
  1195. STDMETHODIMP CChatEventSink::OnServerBannedYou(HRESULT, time_t)
  1196. {
  1197. return(S_OK);
  1198. }
  1199. STDMETHODIMP CChatEventSink::OnPageSend(HRESULT)
  1200. {
  1201. return(S_OK);
  1202. }
  1203. STDMETHODIMP CChatEventSink::OnPaged(HRESULT, User *, LPCSTR)
  1204. {
  1205. return(S_OK);
  1206. }
  1207. STDMETHODIMP CChatEventSink::OnFind(HRESULT, Channel *)
  1208. {
  1209. return(S_OK);
  1210. }
  1211. STDMETHODIMP CChatEventSink::OnLogout(HRESULT, User *)
  1212. {
  1213. return(S_OK);
  1214. }
  1215. STDMETHODIMP CChatEventSink::OnBusy(HRESULT)
  1216. {
  1217. return(S_OK);
  1218. }
  1219. STDMETHODIMP CChatEventSink::OnIdle(HRESULT)
  1220. {
  1221. return(S_OK);
  1222. }
  1223. STDMETHODIMP CChatEventSink::OnConnection(HRESULT, LPCSTR)
  1224. {
  1225. return(S_OK);
  1226. }
  1227. STDMETHODIMP CChatEventSink::OnUserFlags(HRESULT,LPCSTR,unsigned int, unsigned int)
  1228. {
  1229. return(S_OK);
  1230. }
  1231. STDMETHODIMP CChatEventSink::OnChannelCreate(HRESULT, Channel *)
  1232. {
  1233. return(S_OK);
  1234. }
  1235. STDMETHODIMP CChatEventSink::OnChannelModify(HRESULT, Channel *)
  1236. {
  1237. return(S_OK);
  1238. }
  1239. STDMETHODIMP CChatEventSink::OnChannelJoin(HRESULT, Channel *, User *)
  1240. {
  1241. return(S_OK);
  1242. }
  1243. STDMETHODIMP CChatEventSink::OnChannelLeave(HRESULT, Channel *, User *)
  1244. {
  1245. return(S_OK);
  1246. }
  1247. STDMETHODIMP CChatEventSink::OnChannelTopic(HRESULT, Channel *, LPCSTR)
  1248. {
  1249. return(S_OK);
  1250. }
  1251. STDMETHODIMP CChatEventSink::OnChannelBan(HRESULT, LPCSTR, int)
  1252. {
  1253. return(S_OK);
  1254. }
  1255. STDMETHODIMP CChatEventSink::OnGroupList(HRESULT, Group *)
  1256. {
  1257. return(S_OK);
  1258. }
  1259. STDMETHODIMP CChatEventSink::OnPublicMessage(HRESULT, Channel *, User *, LPCSTR)
  1260. {
  1261. return(S_OK);
  1262. }
  1263. STDMETHODIMP CChatEventSink::OnPrivateMessage(HRESULT, User *,LPCSTR)
  1264. {
  1265. return(S_OK);
  1266. }
  1267. STDMETHODIMP CChatEventSink::OnSystemMessage(HRESULT, LPCSTR)
  1268. {
  1269. return(S_OK);
  1270. }
  1271. STDMETHODIMP CChatEventSink::OnUserLocale(long, struct User *)
  1272. {
  1273. return(S_OK);
  1274. }
  1275. STDMETHODIMP CChatEventSink::OnUserTeam(long, struct User *)
  1276. {
  1277. return(S_OK);
  1278. }
  1279. STDMETHODIMP CChatEventSink::OnSetLocale(long, enum Locale)
  1280. {
  1281. return(S_OK);
  1282. }
  1283. STDMETHODIMP CChatEventSink::OnSetTeam(long, int)
  1284. {
  1285. return(S_OK);
  1286. }
  1287. STDMETHODIMP CChatEventSink::OnNetStatus(HRESULT hr)
  1288. {
  1289. char string[60];
  1290. sprintf(string,"NetStatus %ld",hr);
  1291. LogMsg(string);
  1292. if ((hr==CHAT_E_CON_NETDOWN)||(hr==CHAT_E_CON_LOOKUP_FAILED)||
  1293. (hr==CHAT_E_CON_ERROR)||(hr==CHAT_E_TIMEOUT))
  1294. {
  1295. char error[128];
  1296. char cantconn[256];
  1297. LoadString(Global_instance, TXT_CANT_CONTACT, cantconn, sizeof(cantconn));
  1298. LoadString(Global_instance, TXT_ERROR, error, sizeof(error));
  1299. MessageBox(g_PrimaryWindow,cantconn,error,MB_OK);
  1300. exit(-1);
  1301. }
  1302. return(S_OK);
  1303. }
  1304. STDMETHODIMP CChatEventSink::OnChannelList(HRESULT, Channel*)
  1305. {
  1306. return(S_OK);
  1307. }
  1308. STDMETHODIMP CChatEventSink::OnUserList(HRESULT, Channel*, User*)
  1309. {
  1310. return(S_OK);
  1311. }
  1312. //
  1313. // We got a list of updates to apply
  1314. //
  1315. STDMETHODIMP CChatEventSink::OnUpdateList(HRESULT r, Update * updates)
  1316. {
  1317. Update *tmp;
  1318. int numupdates=0;
  1319. int i=0;
  1320. int retval;
  1321. static int alreadyGotOne=0;
  1322. LogMsg("Update list");
  1323. // Close that contacting window
  1324. DestroyWindow(g_ContactWindow);
  1325. g_ContactWindow=NULL;
  1326. if( FAILED(r) )
  1327. {
  1328. LogMsg("Failed");
  1329. SetEvent(Events[ABORT_EVENT]); // An error occurred, bail out
  1330. }
  1331. if( updates == NULL ) // shouldn't happen
  1332. return S_OK;
  1333. if (alreadyGotOne) // Should only get one update list
  1334. return(S_OK);
  1335. alreadyGotOne=1;
  1336. // Count the updates;
  1337. tmp = updates;
  1338. while( tmp != NULL )
  1339. {
  1340. tmp = tmp->next;
  1341. numupdates++;
  1342. }
  1343. // Got a list of updates - If an update is required, the user must either
  1344. // patch or quit.
  1345. tmp = updates;
  1346. LogMsg("Found an update");
  1347. // We have a required update
  1348. char upreq[256];
  1349. char title[128];
  1350. LoadString(Global_instance, TXT_AN_UPGRADE_AVAILABLE, upreq, sizeof(upreq));
  1351. strcat(upreq,"\n");
  1352. LoadString(Global_instance, TXT_DOWNLOAD_NOW, upreq+strlen(upreq), sizeof(upreq));
  1353. LoadString(Global_instance, TXT_UPGRADE_AVAILABLE, title, sizeof(title));
  1354. if( MessageBox(g_PrimaryWindow,upreq,title, MB_YESNO ) == IDNO )
  1355. {
  1356. // If they don't want to patch now, just exit...
  1357. //DBGMSG("Must patch to continue, so exit");
  1358. exit(0);
  1359. }
  1360. // Do the downloads
  1361. while( tmp != NULL )
  1362. {
  1363. g_Update = tmp;
  1364. char dloading[256];
  1365. LoadString(Global_instance, TXT_DOWNLOADING_FILE, dloading, sizeof(dloading));
  1366. sprintf( g_UpdateString, dloading, ++i, numupdates );
  1367. LogMsg("Creating Download dialog box");
  1368. //if( (retval=DialogBox(Global_instance, MAKEINTRESOURCE(IDD_DOWNLOAD_DIALOG), g_PrimaryWindow,
  1369. // (DLGPROC)Download_Dialog_Proc)) != 1 )
  1370. retval=DialogBox(Global_instance, MAKEINTRESOURCE(IDD_DOWNLOAD_DIALOG), g_PrimaryWindow,(DLGPROC)Download_Dialog_Proc);
  1371. if (g_Finished != 1)
  1372. {
  1373. // Download failed
  1374. //DBGMSG("Download failed: "<<retval);
  1375. SetEvent(Events[ABORT_EVENT]);
  1376. return(E_FAIL);
  1377. }
  1378. tmp = tmp->next;
  1379. }
  1380. // Quit so the launcher can apply the patches
  1381. exit(0);
  1382. return(S_OK); // make silly compiler happy
  1383. }
  1384. STDMETHODIMP CChatEventSink::OnServerError(HRESULT, LPCSTR)
  1385. {
  1386. LogMsg("Server Error");
  1387. return(S_OK);
  1388. }
  1389. STDMETHODIMP CChatEventSink::OnMessageOfTheDay(HRESULT, LPCSTR)
  1390. {
  1391. return(S_OK);
  1392. }
  1393. STDMETHODIMP CChatEventSink::OnPrivateAction(HRESULT, User *, LPCSTR)
  1394. {
  1395. return(S_OK);
  1396. }
  1397. STDMETHODIMP CChatEventSink::OnPublicAction(HRESULT, Channel *, User *, LPCSTR)
  1398. {
  1399. return(S_OK);
  1400. }
  1401. STDMETHODIMP CChatEventSink::OnPrivateGameOptions(HRESULT, User *, LPCSTR)
  1402. {
  1403. return(S_OK);
  1404. }
  1405. STDMETHODIMP CChatEventSink::OnPublicGameOptions(HRESULT, Channel *, User *, LPCSTR)
  1406. {
  1407. return(S_OK);
  1408. }
  1409. STDMETHODIMP CChatEventSink::OnGameStart(HRESULT, Channel *, User *, int)
  1410. {
  1411. return(S_OK);
  1412. }
  1413. STDMETHODIMP CChatEventSink::OnUserKick(HRESULT, Channel *, User *, User *)
  1414. {
  1415. return(S_OK);
  1416. }
  1417. STDMETHODIMP CChatEventSink::OnUserIP(HRESULT, User *)
  1418. {
  1419. return(S_OK);
  1420. }
  1421. STDMETHODIMP CChatEventSink::OnSquadInfo(HRESULT, unsigned long, Squad *)
  1422. {
  1423. return(S_OK);
  1424. }
  1425. */