installer/UnitInstall.pas
author Christian Hammacher <bmasterc@gmail.com>
Fri Oct 21 20:50:01 2005 +0000 (2005-10-21)
changeset 146 7b81c8f8ee1f
parent 126 b072ae770357
child 169 314eb459448e
permissions -rw-r--r--
Changed version number to 1.1
     1 unit UnitInstall;
     2 
     3 interface
     4 
     5 uses SysUtils, Classes, Windows, Graphics, Forms, ShellAPI, Controls, Messages,
     6      TlHelp32, IdFTPCommon, ComCtrls;
     7 
     8 type TOS = (osWindows, osLinux);
     9 
    10 procedure AddStatus(Text: String; Color: TColor; ShowTime: Boolean = True);
    11 procedure AddDone(Additional: String = '');
    12 procedure AddSkipped;
    13 procedure AddNotFound;
    14 procedure DownloadFile(eFile: String; eDestination: String);
    15 
    16 procedure BasicInstallation(ePath: String; SteamInstall: Boolean; OS: TOS);
    17 procedure InstallDedicated(eModPath: String; UseSteam: Boolean);
    18 procedure InstallListen(ePath: String);
    19 procedure InstallCustom(ePath: String; eOS: TOS);
    20 procedure InstallFTP(OS: TOS);
    21 
    22 var StartTime: TDateTime;
    23     SteamPath: String;
    24     StandaloneServer: String;
    25     Cancel: Boolean = False;
    26 
    27 implementation
    28 
    29 uses UnitfrmMain, UnitfrmProxy, UnitFunctions, UnitPackSystem;
    30 
    31 // useful stuff
    32 
    33 function InstallTime: String;
    34 begin
    35   Result := FormatDateTime('HH:MM:SS', Now - StartTime);
    36 end;
    37 
    38 procedure AddStatus(Text: String; Color: TColor; ShowTime: Boolean = True);
    39 begin
    40   frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
    41   if ShowTime then begin
    42     frmMain.rtfDetails.SelAttributes.Color := clBlack;
    43     if frmMain.rtfDetails.Text = '' then
    44       frmMain.rtfDetails.SelText := '[' + InstallTime + '] '
    45     else
    46       frmMain.rtfDetails.SelText := #13#10 + '[' + InstallTime + '] ';
    47     frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
    48   end
    49   else
    50     frmMain.rtfDetails.SelText := #13#10;
    51 
    52   frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
    53   frmMain.rtfDetails.SelAttributes.Color := Color;
    54   frmMain.rtfDetails.SelText := Text;
    55   frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
    56 
    57   frmMain.Repaint;
    58   Application.ProcessMessages;
    59 end;
    60 
    61 procedure AddDone(Additional: String = '');
    62 begin
    63   frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
    64   frmMain.rtfDetails.SelAttributes.Color := clGreen;
    65   if Additional = '' then
    66     frmMain.rtfDetails.SelText := ' Done.'
    67   else
    68     frmMain.rtfDetails.SelText := ' Done, ' + Additional + '.';
    69   frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
    70 
    71   frmMain.Repaint;
    72   Application.ProcessMessages;
    73 end;
    74 
    75 procedure AddSkipped;
    76 begin
    77   frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
    78   frmMain.rtfDetails.SelAttributes.Color := $004080FF; // orange
    79   frmMain.rtfDetails.SelText := ' Skipped.';
    80   frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
    81 
    82   frmMain.Repaint;
    83   Application.ProcessMessages;
    84 end;
    85 
    86 procedure AddNotFound;
    87 begin
    88   frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
    89   frmMain.rtfDetails.SelAttributes.Color := clRed;
    90   frmMain.rtfDetails.SelText := ' Not found.';
    91   frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
    92 
    93   frmMain.Repaint;
    94   Application.ProcessMessages;
    95 end;
    96 
    97 procedure FileCopy(Source, Destination: String; CopyConfig: Boolean; AddStatus: Boolean = True);
    98 begin
    99   if (not CopyConfig) and (Pos('config', Source) <> 0) then begin
   100     if AddStatus then
   101       AddSkipped;
   102     exit;
   103   end;
   104 
   105   if not FileExists(Source) then begin
   106     if AddStatus then
   107       AddNotFound;
   108     exit;
   109   end;
   110 
   111   try
   112     if FileExists(Destination) then
   113       DeleteFile(PChar(Destination));
   114     CopyFile(PChar(Source), PChar(Destination), False);
   115   except
   116     Application.ProcessMessages;
   117   end;
   118 
   119   if AddStatus then
   120     AddDone;
   121 end;
   122 
   123 procedure DownloadFile(eFile: String; eDestination: String);
   124 var TransferType: TIdFTPTransferType;
   125 begin
   126   // There's only one file to download and it's ASCII :]
   127   TransferType := ftASCII;
   128   if frmMain.IdFTP.TransferType <> TransferType then
   129     frmMain.IdFTP.TransferType := TransferType;
   130   // download the file
   131   frmMain.IdFTP.Get(eFile, eDestination, True);
   132 end;
   133 
   134 procedure UploadFile(eFile: String; eDestination: String; CopyConfig: Boolean = True);
   135 var TransferType: TIdFTPTransferType;
   136 begin
   137   if (Pos('config', eFile) > 0) and (not CopyConfig) then begin
   138     AddSkipped;
   139     exit;
   140   end;
   141   
   142   eDestination := StringReplace(eDestination, '\', '/', [rfReplaceAll]);
   143 
   144   // the same as in DownloadFile()
   145   TransferType := ftBinary;
   146   if ExtractFileExt(LowerCase(eFile)) = '.txt' then TransferType := ftASCII;
   147   if frmMain.IdFTP.TransferType <> TransferType then
   148     frmMain.IdFTP.TransferType := TransferType;
   149   // upload the file
   150   frmMain.IdFTP.Put(eFile, eDestination);
   151   AddDone;
   152 end;
   153 
   154 procedure FTPMakeDir(eDir: String);
   155 begin
   156   eDir := StringReplace(eDir, '\', '/', [rfReplaceAll]);
   157   try
   158     frmMain.IdFTP.MakeDir(eDir);
   159   except
   160     Application.ProcessMessages;
   161   end;
   162 end;
   163 
   164 function FSize(eFile: String): Cardinal;
   165 var eRec: TSearchRec;
   166 begin
   167   if FindFirst(eFile, faAnyFile, eRec) = 0 then
   168     Result := eRec.Size
   169   else
   170     Result := 0;
   171 end;
   172 
   173 // stuff for killing processes
   174 
   175 function GetProcessID(sProcName: String): Integer;
   176 var
   177   hProcSnap: THandle;
   178   pe32: TProcessEntry32;
   179 begin
   180   result := -1;
   181   hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
   182   if hProcSnap = INVALID_HANDLE_VALUE then
   183     exit;
   184 
   185   pe32.dwSize := SizeOf(ProcessEntry32);
   186   if Process32First(hProcSnap, pe32) = true then begin
   187     while Process32Next(hProcSnap, pe32) = true do begin
   188       if pos(sProcName, pe32.szExeFile) <> 0then
   189         result := pe32.th32ProcessID;
   190     end;
   191   end;
   192   CloseHandle(hProcSnap);
   193 end; 
   194 
   195 procedure KillProcess(dwProcID: DWORD); 
   196 var 
   197   hProcess : Cardinal; 
   198   dw       : DWORD; 
   199 begin 
   200   hProcess := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, dwProcID);
   201   TerminateProcess(hProcess, 0);
   202   dw := WaitForSingleObject(hProcess, 5000);
   203   case dw of 
   204     WAIT_TIMEOUT: begin
   205       CloseHandle(hProcess);
   206       exit;
   207     end;
   208     WAIT_FAILED: begin 
   209       RaiseLastOSError; 
   210       CloseHandle(hProcess); 
   211       exit; 
   212     end; 
   213   end; 
   214   CloseHandle(hProcess); 
   215 end; 
   216 
   217 // Installation here
   218 
   219 { Basic Installation }   
   220 
   221 procedure BasicInstallation(ePath: String; SteamInstall: Boolean; OS: TOS);
   222 var eStr: TStringList;
   223     i: integer;
   224     CopyConfig: Boolean;
   225     eFound: Boolean;
   226 begin
   227   frmMain.ggeAll.MaxValue := 7;
   228   frmMain.ggeAll.Progress := 0;
   229   frmMain.ggeItem.MaxValue := 1;
   230   frmMain.ggeItem.Progress := 0;
   231 
   232   if (GetProcessID('Steam.exe') <> -1) and (SteamInstall) then begin
   233     if MessageBox(frmMain.Handle, 'Steam is still running. It is necersarry to shut it down before you install Metamod:Source. Shut it down now?', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
   234       AddStatus('Shutting down Steam...', clBlack, False);
   235       if GetProcessID('Steam.exe') = -1 then
   236         AddDone
   237       else
   238         KillProcess(GetProcessID('Steam.exe'));
   239         
   240       while GetProcessID('Steam.exe') <> -1 do begin // sure is sure...
   241         Sleep(50);
   242         Application.ProcessMessages;
   243       end;
   244       AddDone;
   245     end
   246     else begin
   247       Application.Terminate;
   248       exit;
   249     end;
   250   end;
   251   frmMain.ggeAll.Progress := 1;
   252   frmMain.ggeItem.Progress := 1;
   253   { Unpack }
   254   frmMain.ggeItem.Progress := 0;
   255   AddStatus('Unpacking files...', clBlack);
   256   Unpack;
   257   AddDone;
   258   frmMain.ggeAll.Progress := 2;
   259   frmMain.ggeItem.Progress := 1;
   260   { Check for installation }
   261   CopyConfig := True;
   262   if DirectoryExists(ePath + 'addons\metamod\bin') then begin
   263     case MessageBox(frmMain.Handle, 'A Metamod:Source installation was already detected. If you choose to reinstall, your configuration files will be erased. Click Yes to continue, No to Upgrade, or Cancel to abort the install.', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNOCANCEL) of
   264       mrNo: CopyConfig := False;
   265       mrCancel: begin
   266         Application.Terminate;
   267         exit;
   268       end;
   269     end;
   270   end;
   271   AddStatus('Creating directories...', clBlack);
   272   if not eFound then begin
   273     { Create directories }
   274     frmMain.ggeItem.Progress := 0;
   275     ForceDirectories(ePath + 'addons\metamod\bin');
   276     AddDone;
   277   end
   278   else
   279     AddSkipped;
   280   frmMain.ggeItem.Progress := 1;
   281   frmMain.ggeAll.Progress := 3;
   282 
   283   { gameinfo.txt }
   284   if not FileExists(ePath + 'gameinfo.txt') then begin
   285     if MessageBox(frmMain.Handle, 'The file "gameinfo.txt" couldn''t be found. Continue installation?', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNO) = mrNo then begin
   286       AddStatus('Installation canceled by user!', clRed, False);
   287       Screen.Cursor := crDefault;
   288       Cancel := True;
   289       exit;
   290     end;
   291   end
   292   else begin
   293     eStr := TStringList.Create;
   294     { Metaplugins.ini }
   295     frmMain.ggeItem.Progress := 0;
   296     AddStatus('Creating metaplugins.ini...', clBlack);
   297     if CopyConfig then begin
   298       eStr.SaveToFile(ePath + 'addons\metamod\metaplugins.ini');
   299       AddDone;
   300     end
   301     else
   302       AddSkipped;
   303     frmMain.ggeItem.Progress := 1;
   304     frmMain.ggeAll.Progress := 4;
   305     { Gameinfo.txt }
   306     frmMain.ggeItem.Progress := 0;
   307     eFound := False;
   308     AddStatus('Editing gameinfo.txt...', clBlack);
   309     eStr.LoadFromFile(ePath + 'gameinfo.txt');
   310     for i := 0 to eStr.Count -1 do begin
   311       if Trim(LowerCase(eStr[i])) = 'gamebin				|gameinfo_path|addons/metamod/bin' then begin
   312         eFound := True;
   313         break;
   314       end;
   315     end;
   316 
   317     if not eFound then begin
   318       for i := 0 to eStr.Count -1 do begin
   319         if Trim(eStr[i]) = 'SearchPaths' then begin
   320           eStr.Insert(i +2, '			GameBin				|gameinfo_path|addons/metamod/bin');
   321           AddDone;
   322           break;
   323         end;
   324       end;
   325       FileSetAttr(ePath + 'gameinfo.txt', 0);
   326       eStr.SaveToFile(ePath + 'gameinfo.txt');
   327       FileSetAttr(ePath + 'gameinfo.txt', faReadOnly); // important for listen servers
   328       AddDone;
   329     end
   330     else
   331       AddSkipped;
   332     eStr.Free;
   333     frmMain.ggeItem.Progress := 1;
   334     frmMain.ggeAll.Progress := 5;
   335   end;
   336   { Copy files }
   337   frmMain.ggeItem.Progress := 0;
   338   AddStatus('Copying server.dll...', clBlack);
   339   CopyFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'), PChar(ePath + 'addons\metamod\bin\server.dll'), False);
   340   AddDone;
   341   frmMain.ggeItem.Progress := 1;
   342   frmMain.ggeAll.Progress := 6;
   343   { Remove files }
   344   frmMain.ggeItem.Progress := 0;
   345   AddStatus('Removing temporary files...', clBlack);
   346   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'));
   347   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server_i486.so'));
   348   AddDone;
   349   frmMain.ggeItem.Progress := 1;
   350   frmMain.ggeAll.Progress := 7;
   351   { Finish }
   352   AddStatus('', clBlack, False);
   353   AddStatus('Finished installation!', clBlack, False);
   354   frmMain.cmdNext.Enabled := True;
   355   frmMain.cmdCancel.Hide;
   356 
   357   Screen.Cursor := crDefault;
   358 end;
   359 
   360 { Dedicated Server }
   361 
   362 procedure InstallDedicated(eModPath: String; UseSteam: Boolean);
   363 begin
   364   StartTime := Now;
   365   Screen.Cursor := crHourGlass;
   366   AddStatus('Starting Metamod:Source installation on dedicated server...', clBlack, False);
   367   BasicInstallation(eModPath, UseSteam, osWindows);
   368 end;
   369 
   370 { Listen Server }
   371 
   372 procedure InstallListen(ePath: String);
   373 begin
   374   StartTime := Now;
   375   Screen.Cursor := crHourGlass;
   376   AddStatus('Starting Metamod:Source installation on the listen server...', clBlack);
   377   BasicInstallation(ePath, True, osWindows);
   378 end;
   379 
   380 { Custom mod }
   381 
   382 procedure InstallCustom(ePath: String; eOS: TOS);
   383 begin
   384   StartTime := Now;
   385   Screen.Cursor := crHourGlass;
   386   AddStatus('Starting Metamod:Source installation...', clBlack);
   387   BasicInstallation(ePath, False, eOS);
   388 end;
   389 
   390 { FTP }
   391 
   392 procedure InstallFTP(OS: TOS);
   393 function DoReconnect: Boolean;
   394 begin
   395   Result := False;
   396   if MessageBox(frmMain.Handle, 'You have been disconnected due to an error. Try to reconnect?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
   397     try
   398       frmMain.IdFTP.Connect;
   399       Result := True;
   400     except
   401       MessageBox(frmMain.Handle, 'Failed to reconnect. Installation aborted.', PChar(Application.Title), MB_ICONSTOP);
   402     end;
   403   end;
   404 end;
   405 
   406 label CreateAgain;
   407 label UploadAgain;
   408 var eStr: TStringList;
   409     i: integer;
   410     ePath: String;
   411     CurNode: TTreeNode;
   412     CopyConfig, eFound: Boolean;
   413     eGoBack: Boolean;
   414 begin
   415   frmMain.cmdCancel.Show;
   416   frmMain.cmdNext.Hide;
   417   Screen.Cursor := crHourGlass;
   418 
   419   frmMain.ggeAll.MaxValue := 6;
   420   frmMain.ggeAll.Progress := 0;
   421   frmMain.ggeItem.MaxValue := 1;
   422   frmMain.ggeItem.Progress := 0;
   423 
   424   { Unpack }
   425   frmMain.ggeItem.Progress := 0;
   426   AddStatus('Unpacking files...', clBlack);
   427   Unpack;
   428   AddDone;
   429   frmMain.ggeAll.Progress := 2;
   430   frmMain.ggeItem.Progress := 1;
   431   { Check for installation }
   432   AddStatus('Editing gameinfo.txt...', clBlack);
   433   eStr := TStringList.Create;
   434   DownloadFile('gameinfo.txt', ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
   435   eStr.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
   436   
   437   CopyConfig := True;
   438   eFound := False;
   439   for i := 0 to eStr.Count -1 do begin
   440     if Trim(LowerCase(eStr[i])) = 'gamebin				|gameinfo_path|addons/metamod/bin' then begin
   441       eFound := True;
   442       case MessageBox(frmMain.Handle, 'A Metamod:Source installation was already detected. If you choose to reinstall, your configuration files will be erased. Click Yes to continue, No to Upgrade, or Cancel to abort the install.', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNOCANCEL) of
   443         mrNo: CopyConfig := False;
   444         mrCancel: begin
   445           Application.Terminate;
   446           eStr.Free;
   447           exit;
   448         end;
   449       end;
   450       break;
   451     end;
   452   end;
   453 
   454   if not eFound then begin
   455     for i := 0 to eStr.Count -1 do begin
   456       if Trim(eStr[i]) = 'SearchPaths' then begin
   457         eStr.Insert(i +2, '			GameBin				|gameinfo_path|addons/metamod/bin');
   458         AddDone;
   459         break;
   460       end;
   461     end;
   462     eStr.SaveToFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
   463     UploadFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt', 'gameinfo.txt');
   464     try
   465       AddStatus('Trying to set gameinfo.txt to read-only...', clBlack);
   466       frmMain.IdFTP.Site('CHMOD 744 gameinfo.txt');
   467       AddDone;
   468     except
   469       AddStatus('Warning: CHMOD not supported.', clMaroon);
   470     end;
   471     DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt'));
   472   end
   473   else
   474     AddSkipped;
   475   frmMain.ggeAll.Progress := 3;
   476   frmMain.ggeItem.Progress := 1;
   477 
   478   { Create directories }
   479   frmMain.ggeItem.Progress := 0;
   480   frmMain.ggeItem.MaxValue := 3;  
   481   AddStatus('Creating directories...', clBlack);
   482   if not eFound then begin
   483     FTPMakeDir('addons');
   484     frmMain.IdFTP.ChangeDir('addons');
   485     frmMain.ggeItem.Progress := 1;
   486     FTPMakeDir('metamod');
   487     frmMain.IdFTP.ChangeDir('metamod');
   488     frmMain.ggeItem.Progress := 2;
   489     FTPMakeDir('bin');
   490     frmMain.ggeItem.Progress := 3;
   491     AddDone;
   492   end
   493   else
   494     AddSkipped;
   495   frmMain.ggeAll.Progress := 4;
   496   frmMain.ggeItem.Progress := 3;
   497   { Upload metaplugins.ini }
   498   frmMain.ggeAll.Progress := 4;
   499   frmMain.ggeItem.MaxValue := 1;
   500   frmMain.ggeItem.Progress := 0;
   501   AddStatus('Uploading metaplugins.ini...', clBlack);
   502   if CopyConfig then begin
   503     eStr.Clear;
   504     eStr.SaveToFile(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini');
   505     UploadFile(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini', 'metaplugins.ini');
   506     DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini'));
   507   end
   508   else
   509     AddSkipped;
   510   frmMain.ggeAll.Progress := 5;
   511   frmMain.ggeItem.Progress := 1;
   512   { Upload server.dll / server_i486.so }
   513   frmMain.tmrSpeed.Enabled := True;
   514   frmMain.ggeItem.Progress := 0;
   515   frmMain.IdFTP.ChangeDir('bin');
   516   if OS = osWindows then begin
   517     AddStatus('Uploading server.dll...', clBlack);
   518     frmMain.ggeItem.MaxValue := FSize(ExtractFilePath(ParamStr(0)) + 'server.dll');
   519     UploadFile(ExtractFilePath(ParamStr(0)) + 'server.dll', 'server.dll');
   520   end
   521   else begin
   522     AddStatus('Uploading server_i486.so...', clBlack);
   523     frmMain.ggeItem.MaxValue := FSize(ExtractFilePath(ParamStr(0)) + 'server_i486.so');
   524     UploadFile(ExtractFilePath(ParamStr(0)) + 'server_i486.so', 'server_i486.so');
   525   end;
   526   { Remove created files }
   527   AddStatus('Removing temporary files...', clBlack);
   528   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'));
   529   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server_i486.so'));
   530   AddDone;
   531   { End }
   532   frmMain.IdFTP.Disconnect;
   533   frmMain.ggeAll.Progress := frmMain.ggeAll.MaxValue;
   534   frmMain.ggeItem.Progress := frmMain.ggeItem.MaxValue;
   535   AddStatus('', clBlack, False);
   536   AddStatus('Finished installation!', clBlack, False);
   537   frmMain.tmrSpeed.Enabled := False;
   538   eStr.Free;
   539 
   540   Screen.Cursor := crDefault;
   541   frmMain.cmdNext.Enabled := True;
   542   frmMain.cmdCancel.Hide;
   543   frmMain.cmdNext.Show;
   544   frmMain.tmrSpeed.Enabled := False;
   545   frmMain.Caption := Application.Title;
   546 end;
   547 
   548 end.