nel_launcherDlg.cpp

Go to the documentation of this file.
00001 // nel_launcherDlg.cpp : implementation file
00002 //
00003 #include "stdafx.h"
00004 #include "nel_launcher.h"
00005 #include "nel_launcherDlg.h"
00006 #include <string>
00007 #include <vector>
00008 #include <comdef.h>
00009 
00010 #include "nel/misc/path.h"
00011 #include "patch.h"
00012 #include "LoginDlg.h"
00013 #include "MsgDlg.h"
00014 
00015 using namespace std;
00016 using namespace NLMISC;
00017 
00018 //CConfigFile ConfigFile;
00019 
00021 // CNel_launcherDlg dialog
00022 
00023 CNel_launcherDlg::CNel_launcherDlg(CWnd* pParent /*=NULL*/)
00024     : CDialog(CNel_launcherDlg::IDD, pParent)
00025 {
00026     //{{AFX_DATA_INIT(CNel_launcherDlg)
00027         // NOTE: the ClassWizard will add member initialization here
00028     //}}AFX_DATA_INIT
00029     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
00030     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
00031 
00032     m_iX        = 0;
00033     m_iY        = 0;
00034     m_bMoving   = FALSE;
00035     m_pdlgLogin = NULL;
00036 }
00037 
00038 void CNel_launcherDlg::DoDataExchange(CDataExchange* pDX)
00039 {
00040     CDialog::DoDataExchange(pDX);
00041     //{{AFX_DATA_MAP(CNel_launcherDlg)
00042     //}}AFX_DATA_MAP
00043 }
00044 
00045 BEGIN_MESSAGE_MAP(CNel_launcherDlg, CDialog)
00046     //{{AFX_MSG_MAP(CNel_launcherDlg)
00047     ON_WM_PAINT()
00048     ON_WM_QUERYDRAGICON()
00049     ON_WM_TIMER()
00050     ON_WM_LBUTTONDOWN()
00051     ON_WM_LBUTTONUP()
00052     ON_WM_MOUSEMOVE()
00053     ON_WM_DESTROY()
00054     ON_WM_SETCURSOR()
00055     ON_WM_ERASEBKGND()
00056     //}}AFX_MSG_MAP
00057 END_MESSAGE_MAP()
00058 
00060 // CNel_launcherDlg message handlers
00061 
00062 BOOL CNel_launcherDlg::OnInitDialog()
00063 {
00064     CDialog::OnInitDialog();
00065 
00066     // Set the icon for this dialog.  The framework does this automatically
00067     //  when the application's main window is not a dialog
00068     SetIcon(m_hIcon, TRUE);         // Set big icon
00069     SetIcon(m_hIcon, FALSE);        // Set small icon
00070 
00071     SetWindowText(MAIN_DLG_TITLE);
00072 
00073     m_pictBG.LoadPicture(IDP_BACKGROUND);
00074 
00075     CreateProgressBar();
00076 
00077     return TRUE;  // return TRUE  unless you set the focus to a control
00078 }
00079 
00080 void CNel_launcherDlg::OnPaint()  
00081 {
00082     if (IsIconic())
00083     {
00084         CPaintDC dc(this); // device context for painting
00085 
00086         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
00087 
00088         // Center icon in client rectangle
00089         int cxIcon = GetSystemMetrics(SM_CXICON);
00090         int cyIcon = GetSystemMetrics(SM_CYICON);
00091         CRect rect;
00092         GetClientRect(&rect);
00093         int x = (rect.Width() - cxIcon + 1) / 2;
00094         int y = (rect.Height() - cyIcon + 1) / 2;
00095 
00096         // Draw the icon
00097         dc.DrawIcon(x, y, m_hIcon);
00098     }
00099     else
00100     {
00101         CDialog::OnPaint();
00102 
00103         Login();
00104     }
00105 }
00106 
00107 // The system calls this to obtain the cursor to display while the user drags
00108 //  the minimized window.
00109 HCURSOR CNel_launcherDlg::OnQueryDragIcon()
00110 {
00111     return (HCURSOR) m_hIcon;
00112 }
00113 
00114 void CNel_launcherDlg::OnLButtonDown(UINT nFlags, CPoint point)
00115 {
00116     if(point.y < TITLEBAR_H)
00117     {
00118         POINT p;
00119 
00120         m_bMoving = TRUE;
00121         SetCapture();
00122         GetCursorPos(&p);
00123         m_iX = p.x;
00124         m_iY = p.y;
00125     }
00126 }
00127 
00128 void CNel_launcherDlg::OnLButtonUp(UINT nFlags, CPoint point)
00129 {
00130     if(m_bMoving)
00131     {
00132         m_bMoving   = FALSE;
00133         ReleaseCapture();
00134     }
00135     else if(point.x >= CLOSE_BTN_X && point.x < CLOSE_BTN_X + CLOSE_BTN_W &&
00136         point.y >= CLOSE_BTN_Y && point.y < CLOSE_BTN_Y + CLOSE_BTN_H)
00137     {
00138         // The user has clicked on the exit button
00139         PostQuitMessage(0);
00140     }
00141 }
00142 
00143 void CNel_launcherDlg::OnMouseMove(UINT nFlags, CPoint point)
00144 {
00145     if(m_bMoving) 
00146     {
00147         POINT   p;
00148 
00149         GetCursorPos(&p);
00150 
00151         MoveBy(p.x - m_iX, p.y - m_iY);
00152 
00153         m_iX = p.x;
00154         m_iY = p.y;
00155     }
00156 }
00157 
00158 void CNel_launcherDlg::MoveBy(int x, int y)
00159 {
00160     CRect   rect;
00161 
00162     GetWindowRect(&rect);
00163     MoveWindow(rect.left+x, rect.top+y, rect.right-rect.left,rect.bottom-rect.top, TRUE);
00164 }
00165 
00166 string getValue(const string &str, const string &token)
00167 {
00168     string  realtoken   = token+"=\"";
00169     uint    spos        = str.find (realtoken);
00170     
00171     if (spos == string::npos) 
00172         return "";
00173 
00174     uint spos2 = str.find("\"", spos+realtoken.size ());
00175     if(spos == string::npos) 
00176         return "";
00177 
00178     return str.substr(spos+realtoken.size(), spos2-spos-realtoken.size());
00179 }
00180 
00181 void CNel_launcherDlg::launch(CString cs)
00182 {   
00183     string str((LPCSTR)cs);
00184     launch(str);
00185 }
00186 
00187 void CNel_launcherDlg::launch(const string &str)
00188 {
00189 /*  string exe = ConfigFile.getVar ("Application").asString(1);
00190     string path;
00191     
00192     if(ConfigFile.getVar ("Application").size() == 4)
00193         path    = ConfigFile.getVar ("Application").asString(3);
00194 
00195     string rawargs = getValue (str, "nelArgs");
00196 
00197     nlinfo("extracting string '%s'", str.c_str ());
00198 
00199     // convert the command line in an array of string for the _execvp() function
00200 
00201     vector<string> vargs;
00202     const char *args[50];
00203     int argspos = 0;
00204 
00205     uint pos1 = 0, pos2 = 0;
00206     while(TRUE)
00207     {
00208         pos2 = rawargs.find(" ", pos1);
00209         if(pos2 == string::npos)
00210         {
00211             if(pos1 != rawargs.size())
00212             {
00213                 string res = rawargs.substr(pos1);
00214                 vargs.push_back(res);
00215             }
00216             break;
00217         }
00218         if(pos1 != pos2)
00219         {
00220             string res = rawargs.substr (pos1, pos2-pos1);
00221             vargs.push_back(res);
00222         }
00223         pos1 = pos2 + 1;
00224     }
00225 
00226     int i;
00227     int size;
00228 
00229     if(vargs.size()>47) 
00230         size = 47;
00231     else 
00232         size = vargs.size();
00233 
00234     args[0] = exe.c_str();
00235     for(i = 0; i < size; i++)
00236     {
00237         args[i+1] = vargs[i].c_str();
00238     }
00239     args[i+1] = NULL;
00240 
00241     // go in the good path before launching
00242     if(!path.empty())
00243         _chdir(path.c_str());
00244 
00245     // execute, should better use CreateProcess()
00246     if(_execvp(exe.c_str(), args) == -1)
00247     {
00248         CMsgDlg dlgMsg("Error", "Can't execute the game");
00249 
00250         dlgMsg.DoModal();
00251         APP.ResetConnection();
00252     }
00253     else    
00254     {
00255         APP.ResetConnection();
00256         PostQuitMessage(0);
00257     }*/
00258 
00259 //  string exe = ConfigFile.getVar ("Application").asString(1);
00260 //  string path;
00261     
00262 //  if(ConfigFile.getVar ("Application").size() == 4)
00263 //      path    = ConfigFile.getVar ("Application").asString(3);
00264     CString csPath  = APP.m_config.m_csAppBasePath;
00265 
00266     string rawargs = getValue (str, "nelArgs");
00267 
00268     // convert the command line in an array of string for the _execvp() function
00269 
00270     vector<string> vargs;
00271     const char *args[50];
00272     int argspos = 0;
00273 
00274     uint pos1 = 0, pos2 = 0;
00275     while(TRUE)
00276     {
00277         pos2 = rawargs.find(" ", pos1);
00278         if(pos2 == string::npos)
00279         {
00280             if(pos1 != rawargs.size())
00281             {
00282                 string res = rawargs.substr(pos1);
00283                 vargs.push_back(res);
00284             }
00285             break;
00286         }
00287         if(pos1 != pos2)
00288         {
00289             string res = rawargs.substr (pos1, pos2-pos1);
00290             vargs.push_back(res);
00291         }
00292         pos1 = pos2 + 1;
00293     }
00294 
00295     int i;
00296     int size;
00297 
00298     if(vargs.size()>47) 
00299         size = 47;
00300     else 
00301         size = vargs.size();
00302 
00303     args[0] = APP.m_config.m_csExe;
00304     for(i = 0; i < size; i++)
00305     {
00306         args[i+1] = vargs[i].c_str();
00307     }
00308     args[i+1] = NULL;
00309 
00310     // go in the good path before launching
00311     if(!csPath.IsEmpty())
00312         _chdir(csPath);
00313 
00314     // execute, should better use CreateProcess()
00315     if(_execvp(APP.m_config.m_csExe, args) == -1)
00316     {
00317         CMsgDlg dlgMsg("Error", "Can't execute the game");
00318 
00319         dlgMsg.DoModal();
00320         APP.ResetConnection();
00321     }
00322     else    
00323     {
00324         APP.ResetConnection();
00325         PostQuitMessage(0);
00326     }
00327 }
00328 
00329 void CNel_launcherDlg::patch(CString cs)
00330 {
00331     string str((LPCSTR)cs);
00332     patch(str);
00333 }
00334 
00335 void CNel_launcherDlg::patch(const string &str)
00336 {
00337     string url = "http://" + APP.m_config.m_csHost;
00338 
00339     string serverVersion = getValue(str, "serverVersion");
00340 
00341     string urlok = url;
00342     urlok += getValue (str, "nelUrl");
00343     urlok += "&newClientVersion=";
00344     urlok += serverVersion;
00345 
00346     string urlfailed = url;
00347     urlfailed += getValue (str, "nelUrlFailed");
00348     urlfailed += "&reason=";
00349 
00350     startPatchThread(getValue (str, "nelServerPath"), serverVersion, urlok, urlfailed, "<br>");
00351 
00352     m_dlgProgress.SetRange(getTotalFilesToGet());
00353     m_dlgProgress.UpdatePos(0);
00354     m_dlgProgress.Show();
00355 
00356     SetTimer(0, 500, NULL);
00357 }
00358 
00359 void CNel_launcherDlg::OnTimer(UINT nIDEvent) 
00360 {
00361     m_dlgProgress.SetRange(getTotalFilesToGet());
00362     m_dlgProgress.UpdatePos(getCurrentFilesToGet());
00363 
00364     string state, log;
00365     if(patchState(state, log))
00366         m_dlgProgress.UpdateMsg(state.c_str());
00367 
00368     string  url;
00369     bool    res;
00370 
00371     if(patchEnded(url, res))
00372     {
00373         KillTimer(0);
00374         if(res)
00375             m_dlgProgress.UpdateMsg("Patch completed succesfuly.");
00376         else
00377             m_dlgProgress.UpdateMsg("Patch could not be completed due to an error.");
00378         ::Sleep(2000);
00379         m_dlgProgress.Show(FALSE);
00380         m_dlgProgress.UpdatePos(0);
00381 
00382         // If we are on the servers page, reload it
00383         if(m_wndTabs.GetFocusPos() == 0)
00384             OnTab(0);
00385 
00386 //      m_dlgWeb.OpenUrl(url);
00387     }
00388 }
00389 
00390 void CNel_launcherDlg::OnTab(int iTab)
00391 {
00392     CString csErrPage;
00393     CString csUrl;
00394 
00395     getcwd(csErrPage.GetBuffer(_MAX_PATH), _MAX_PATH);
00396     csErrPage.ReleaseBuffer();
00397     if(csErrPage.Right(1) != '\\')
00398         csErrPage   += '\\';
00399     csErrPage   += "error.html";
00400 
00401     switch(iTab)
00402     {
00403         // Only for Win98
00404         case -1:
00405         {
00406             if(!IS_WINNT)
00407             {
00408                 m_dlgWeb.ShowWindow(SW_HIDE);
00409                 string Version = getVersion();
00410 
00411                 string url = "http://" + APP.m_config.m_csHost + APP.m_config.m_csUrlMain;
00412                 url += "?newClientVersion=" + Version;
00413                 url += "&newClientApplication=" + APP.m_config.m_csApp;
00414 
00415                 m_dlgWeb.OpenUrl(url);
00416 
00417                 OnTab(0);
00418                 m_dlgWeb.ShowWindow(SW_SHOW);
00419             }
00420             break;
00421         }
00422         case 0:
00423         {
00424             // Servers tab
00425             string Version = getVersion();
00426 
00427             string url = "http://" + APP.m_config.m_csHost + APP.m_config.m_csUrlMain;
00428             url += "?newClientVersion=" + Version;
00429             url += "&newClientApplication=" + APP.m_config.m_csApp;
00430 
00431             m_dlgWeb.OpenUrl(url);
00432             break;
00433         }
00434         case 1:
00435         {
00436             // RN tab
00437             if(!APP.m_bAuthWeb)
00438                 m_dlgWeb.OpenUrl(csErrPage);
00439             else
00440             {
00441                 csUrl   = APP.m_config.m_csUrlRN;
00442                 if(!csUrl.IsEmpty())
00443                     m_dlgWeb.OpenUrl(csUrl);
00444             }
00445             break;
00446         }
00447         case 2:
00448         {
00449             // News tab
00450             if(!APP.m_bAuthWeb)
00451                 m_dlgWeb.OpenUrl(csErrPage);
00452             else
00453             {
00454                 csUrl   = APP.m_config.m_csUrlNews;
00455                 if(!csUrl.IsEmpty())
00456                     m_dlgWeb.OpenUrl(csUrl);
00457             }
00458             break;
00459         }
00460     }
00461 }
00462 
00463 BOOL CNel_launcherDlg::Login()
00464 {
00465     CString csLogin = APP.GetRegKeyValue(_T("Login"));
00466     
00467     if(!m_pdlgLogin && !APP.m_bAuthGame)
00468     {
00469         m_pdlgLogin = new CLoginDlg(this);
00470 
00471         if(m_pdlgLogin->DoModal() == IDOK)
00472         {
00473             CreateTabs();           
00474             CreateWebZone();
00475 
00476             // remove temp files
00477             if (NLMISC::CFile::fileExists(PATCH_BAT_FILE))
00478                 NLMISC::CFile::deleteFile(PATCH_BAT_FILE);
00479                         
00480             if(IS_WINNT)
00481                 OnTab(0);
00482             else
00483                 OnTab(-1);
00484 
00485             delete m_pdlgLogin;
00486             m_pdlgLogin = NULL;
00487             return TRUE;
00488         }
00489         else
00490         {
00491             delete m_pdlgLogin;
00492             m_pdlgLogin = NULL;
00493             PostQuitMessage(0);
00494         }
00495         return FALSE;
00496     }
00497     return TRUE;
00498 }
00499 
00500 void CNel_launcherDlg::CreateTabs()
00501 {
00502     if(m_wndTabs.Create(NULL, "Tabs", WS_CHILD, CRect(), this, 9645))
00503     {
00504         m_wndTabs.AddTab(IDP_TAB_SERVERS, IDP_TAB_SERVERS_FOCUS);
00505         if(APP.m_bAuthWeb)
00506         {
00507             m_wndTabs.AddTab(IDP_TAB_RN, IDP_TAB_RN_FOCUS);
00508             m_wndTabs.AddTab(IDP_TAB_NEWS, IDP_TAB_NEWS_FOCUS);
00509         }
00510         m_wndTabs.Move(BROWSER_X, BROWSER_Y - TAB_H);
00511         m_wndTabs.SetObserver(this);
00512         m_wndTabs.SetFocusPos(0);
00513         m_wndTabs.ShowWindow(SW_SHOW);
00514     }
00515 }
00516 
00517 void CNel_launcherDlg::CreateWebZone()
00518 {
00519     // Creating the web browser window
00520     m_dlgWeb.Create(IDD_WEB, this);
00521     m_dlgWeb.MoveWindow(BROWSER_X, BROWSER_Y, BROWSER_W, BROWSER_H);
00522     m_dlgWeb.ShowWindow(SW_SHOW);           
00523 }
00524 
00525 void CNel_launcherDlg::CreateProgressBar()
00526 {
00527     CRect   r;
00528 
00529     GetClientRect(&r);
00530     m_dlgProgress.Create(IDD_PROGRESS, this);
00531     m_dlgProgress.Show(SW_HIDE);
00532     m_dlgProgress.MoveWindow(0, r.Height() - PROGRESS_H, r.Width()+1, PROGRESS_H);
00533 }
00534 
00535 void CNel_launcherDlg::OnDestroy() 
00536 {
00537     APP.ResetConnection();
00538 
00539     CDialog::OnDestroy();   
00540 }
00541 
00542 BOOL CNel_launcherDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
00543 {
00544     POINT   p;
00545 
00546     GetCursorPos(&p);
00547     ScreenToClient(&p);
00548 
00549     if(p.x >= CLOSE_BTN_X && p.x < CLOSE_BTN_X + CLOSE_BTN_W && p.y >= CLOSE_BTN_Y && p.y < CLOSE_BTN_Y + CLOSE_BTN_H)
00550         SetCursor(APP.m_hcPointer);
00551     else if(p.y < TITLEBAR_H && !m_pdlgLogin)
00552         SetCursor(APP.m_hcPointer);
00553     else
00554         return CDialog::OnSetCursor(pWnd, nHitTest, message);
00555     return TRUE;
00556 }
00557 
00558 BOOL CNel_launcherDlg::OnEraseBkgnd(CDC* pDC) 
00559 {
00560     CRect   r;
00561 
00562     GetClientRect(&r);
00563     m_pictBG.Display(*pDC, r);
00564 
00565     return TRUE;
00566 }

Generated on Thu Jan 7 08:30:41 2010 for NeLNS by  doxygen 1.6.1