installer/UnitInstall.pas
author David Anderson <dvander@alliedmods.net>
Thu Sep 27 20:43:03 2007 +0000 (2007-09-27)
branchsourcemm-1.6.0
changeset 358 a4de8673bdbd
parent 347 e82968c21606
permissions -rw-r--r--
more filling in of the provider for episode1
     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, ListenInstall: 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, ListenInstall: Boolean; OS: TOS);
   222 var eStr: TStringList;
   223     i: integer;
   224     CopyConfig: Boolean;
   225     eFound: Boolean;
   226 begin
   227   frmMain.ggeAll.MaxValue := 8;
   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   if not Unpack() then begin
   257     AddStatus('No files attached!', clRed);
   258     Screen.Cursor := crDefault;
   259     exit;
   260   end;
   261   AddDone;
   262   frmMain.ggeAll.Progress := 2;
   263   frmMain.ggeItem.Progress := 1;
   264   { Check for installation / Create directories }
   265   CopyConfig := True;
   266   AddStatus('Creating directories...', clBlack);
   267   if DirectoryExists(ePath + 'addons\metamod\bin') then begin
   268     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
   269       mrNo: CopyConfig := False;
   270       mrCancel: begin
   271         Application.Terminate;
   272         exit;
   273       end;
   274     end;
   275     AddSkipped;
   276   end
   277   else begin
   278     frmMain.ggeItem.Progress := 0;
   279     ForceDirectories(ePath + 'addons\metamod\bin');
   280     AddDone;
   281   end;
   282   frmMain.ggeItem.Progress := 1;
   283   frmMain.ggeAll.Progress := 3;
   284 
   285   { gameinfo.txt }
   286   if not FileExists(ePath + 'gameinfo.txt') then begin
   287     if MessageBox(frmMain.Handle, 'The file "gameinfo.txt" couldn''t be found. Continue installation?', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNO) = mrNo then begin
   288       AddStatus('Installation canceled by user!', clRed, False);
   289       Screen.Cursor := crDefault;
   290       Cancel := True;
   291       exit;
   292     end;
   293   end
   294   else begin
   295     eStr := TStringList.Create;
   296     { Metaplugins.ini }
   297     frmMain.ggeItem.Progress := 0;
   298     AddStatus('Creating metaplugins.ini...', clBlack);
   299     if CopyConfig then begin
   300       eStr.SaveToFile(ePath + 'addons\metamod\metaplugins.ini');
   301       AddDone;
   302     end
   303     else
   304       AddSkipped;
   305     frmMain.ggeItem.Progress := 1;
   306     frmMain.ggeAll.Progress := 4;
   307     { Gameinfo.txt }
   308     frmMain.ggeItem.Progress := 0;
   309     eFound := False;
   310     AddStatus('Editing gameinfo.txt...', clBlack);
   311     eStr.LoadFromFile(ePath + 'gameinfo.txt');
   312     for i := 0 to eStr.Count -1 do begin
   313       if Trim(LowerCase(eStr[i])) = 'gamebin				|gameinfo_path|addons/metamod/bin' then begin
   314         eFound := True;
   315         break;
   316       end;
   317     end;
   318 
   319     if not eFound then begin
   320       for i := 0 to eStr.Count -1 do begin
   321         if Trim(eStr[i]) = 'SearchPaths' then begin
   322           eStr.Insert(i +2, '			GameBin				|gameinfo_path|addons/metamod/bin');
   323           AddDone;
   324           break;
   325         end;
   326       end;
   327       SetFileAttributes(PChar(ePath + 'gameinfo.txt'), 0);
   328       eStr.SaveToFile(ePath + 'gameinfo.txt');
   329       SetFileAttributes(PChar(ePath + 'gameinfo.txt'), faReadOnly); // important for listen servers
   330       AddDone;
   331     end
   332     else
   333       AddSkipped;
   334     eStr.Free;
   335     frmMain.ggeItem.Progress := 1;
   336     frmMain.ggeAll.Progress := 5;
   337   end;
   338   { Copy files }
   339   frmMain.ggeItem.Progress := 0;
   340   AddStatus('Copying server.dll...', clBlack);
   341   CopyFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'), PChar(ePath + 'addons\metamod\bin\server.dll'), False);
   342   AddDone;
   343   frmMain.ggeItem.Progress := 1;
   344   frmMain.ggeAll.Progress := 6;
   345   if ListenInstall then begin
   346     ePath := ExtractFilePath(Copy(ePath, 1, Length(ePath)-1));
   347     AddStatus('Copying hl2launch.exe...', clBlack);
   348     CopyFile(PChar(ExtractFilePath(ParamStr(0)) + 'hl2launch.exe'), PChar(ePath + 'hl2launch.exe'), False);
   349     AddDone;
   350   end;
   351   { Remove files }
   352   frmMain.ggeItem.Progress := 0;
   353   AddStatus('Removing temporary files...', clBlack);
   354   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'hl2launch.exe'));
   355   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'));
   356   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server_i486.so'));
   357   AddDone;
   358   frmMain.ggeItem.Progress := 1;
   359   frmMain.ggeAll.Progress := 8;
   360   { Finish }
   361   AddStatus('', clBlack, False);
   362   AddStatus('Finished installation!', clBlack, False);
   363   frmMain.cmdNext.Enabled := True;
   364   frmMain.cmdCancel.Hide;
   365   Screen.Cursor := crDefault;
   366 
   367   if ListenInstall then
   368     MessageBox(frmMain.Handle, PChar('hl2launch.exe has been copied to ' + ePath + '. You can use it if you want to start your Source game with Metamod:Source enabled.'), PChar(Application.Title), MB_ICONINFORMATION);
   369 end;
   370 
   371 { Dedicated Server }
   372 
   373 procedure InstallDedicated(eModPath: String; UseSteam: Boolean);
   374 begin
   375   StartTime := Now;
   376   Screen.Cursor := crHourGlass;
   377   AddStatus('Starting Metamod:Source installation on dedicated server...', clBlack, False);
   378   BasicInstallation(eModPath, UseSteam, False, osWindows);
   379 end;
   380 
   381 { Listen Server }
   382 
   383 procedure InstallListen(ePath: String);
   384 begin
   385   StartTime := Now;
   386   Screen.Cursor := crHourGlass;
   387   AddStatus('Starting Metamod:Source installation on the listen server...', clBlack);
   388   BasicInstallation(ePath, True, True, osWindows);
   389 end;
   390 
   391 { Custom mod }
   392 
   393 procedure InstallCustom(ePath: String; eOS: TOS);
   394 begin
   395   StartTime := Now;
   396   Screen.Cursor := crHourGlass;
   397   AddStatus('Starting Metamod:Source installation...', clBlack);
   398   BasicInstallation(ePath, False, False, eOS);
   399 end;
   400 
   401 { FTP }
   402 
   403 procedure InstallFTP(OS: TOS);
   404 function DoReconnect: Boolean;
   405 begin
   406   Result := False;
   407   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
   408     try
   409       frmMain.IdFTP.Connect;
   410       Result := True;
   411     except
   412       MessageBox(frmMain.Handle, 'Failed to reconnect. Installation aborted.', PChar(Application.Title), MB_ICONSTOP);
   413     end;
   414   end;
   415 end;
   416 
   417 label CreateAgain;
   418 label UploadAgain;
   419 var eStr: TStringList;
   420     i: integer;
   421     CopyConfig, eFound: Boolean;
   422 begin
   423   frmMain.cmdCancel.Show;
   424   frmMain.cmdNext.Hide;
   425   Screen.Cursor := crHourGlass;
   426 
   427   frmMain.ggeAll.MaxValue := 6;
   428   frmMain.ggeAll.Progress := 0;
   429   frmMain.ggeItem.MaxValue := 1;
   430   frmMain.ggeItem.Progress := 0;
   431 
   432   { Unpack }
   433   frmMain.ggeItem.Progress := 0;
   434   AddStatus('Unpacking files...', clBlack);
   435   if not Unpack() then begin
   436     AddStatus('No files attached!', clRed);
   437     Screen.Cursor := crDefault;
   438     exit;
   439   end;
   440   AddDone;
   441   frmMain.ggeAll.Progress := 2;
   442   frmMain.ggeItem.Progress := 1;
   443   { Check for installation }
   444   AddStatus('Editing gameinfo.txt...', clBlack);
   445   eStr := TStringList.Create;
   446   DownloadFile('gameinfo.txt', ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
   447   eStr.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
   448   
   449   CopyConfig := True;
   450   eFound := False;
   451   for i := 0 to eStr.Count -1 do begin
   452     if Trim(LowerCase(eStr[i])) = 'gamebin				|gameinfo_path|addons/metamod/bin' then begin
   453       eFound := True;
   454       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
   455         mrNo: CopyConfig := False;
   456         mrCancel: begin
   457           Application.Terminate;
   458           eStr.Free;
   459           exit;
   460         end;
   461       end;
   462       break;
   463     end;
   464   end;
   465 
   466   if not eFound then begin
   467     for i := 0 to eStr.Count -1 do begin
   468       if Trim(eStr[i]) = 'SearchPaths' then begin
   469         eStr.Insert(i +2, '			GameBin				|gameinfo_path|addons/metamod/bin');
   470         AddDone;
   471         break;
   472       end;
   473     end;
   474     eStr.SaveToFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
   475     UploadFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt', 'gameinfo.txt');
   476     try
   477       AddStatus('Trying to set gameinfo.txt to read-only...', clBlack);
   478       frmMain.IdFTP.Site('CHMOD 744 gameinfo.txt');
   479       AddDone;
   480     except
   481       AddStatus('Warning: CHMOD not supported.', clMaroon);
   482     end;
   483     DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt'));
   484   end
   485   else
   486     AddSkipped;
   487   frmMain.ggeAll.Progress := 3;
   488   frmMain.ggeItem.Progress := 1;
   489 
   490   { Create directories }
   491   frmMain.ggeItem.Progress := 0;
   492   frmMain.ggeItem.MaxValue := 3;  
   493   AddStatus('Creating directories...', clBlack);
   494   if not eFound then begin
   495     FTPMakeDir('addons');
   496     frmMain.IdFTP.ChangeDir('addons');
   497     frmMain.ggeItem.Progress := 1;
   498     FTPMakeDir('metamod');
   499     frmMain.IdFTP.ChangeDir('metamod');
   500     frmMain.ggeItem.Progress := 2;
   501     FTPMakeDir('bin');
   502     frmMain.ggeItem.Progress := 3;
   503     AddDone;
   504   end
   505   else
   506     AddSkipped;
   507   frmMain.ggeAll.Progress := 4;
   508   frmMain.ggeItem.Progress := 3;
   509   { Upload metaplugins.ini }
   510   frmMain.ggeAll.Progress := 4;
   511   frmMain.ggeItem.MaxValue := 1;
   512   frmMain.ggeItem.Progress := 0;
   513   AddStatus('Uploading metaplugins.ini...', clBlack);
   514   if CopyConfig then begin
   515     eStr.Clear;
   516     eStr.SaveToFile(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini');
   517     UploadFile(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini', 'metaplugins.ini');
   518     DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini'));
   519   end
   520   else
   521     AddSkipped;
   522   frmMain.ggeAll.Progress := 5;
   523   frmMain.ggeItem.Progress := 1;
   524   { Upload server.dll / server_i486.so }
   525   frmMain.tmrSpeed.Enabled := True;
   526   frmMain.ggeItem.Progress := 0;
   527   frmMain.IdFTP.ChangeDir('bin');
   528   if OS = osWindows then begin
   529     AddStatus('Uploading server.dll...', clBlack);
   530     frmMain.ggeItem.MaxValue := FSize(ExtractFilePath(ParamStr(0)) + 'server.dll');
   531     UploadFile(ExtractFilePath(ParamStr(0)) + 'server.dll', 'server.dll');
   532   end
   533   else begin
   534     AddStatus('Uploading server_i486.so...', clBlack);
   535     frmMain.ggeItem.MaxValue := FSize(ExtractFilePath(ParamStr(0)) + 'server_i486.so');
   536     UploadFile(ExtractFilePath(ParamStr(0)) + 'server_i486.so', 'server_i486.so');
   537   end;
   538   { Remove created files }
   539   AddStatus('Removing temporary files...', clBlack);
   540   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'hl2launch.exe'));
   541   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'));
   542   DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server_i486.so'));
   543   AddDone;
   544   { End }
   545   frmMain.IdFTP.Disconnect;
   546   frmMain.ggeAll.Progress := frmMain.ggeAll.MaxValue;
   547   frmMain.ggeItem.Progress := frmMain.ggeItem.MaxValue;
   548   AddStatus('', clBlack, False);
   549   AddStatus('Finished installation!', clBlack, False);
   550   frmMain.tmrSpeed.Enabled := False;
   551   eStr.Free;
   552 
   553   Screen.Cursor := crDefault;
   554   frmMain.cmdNext.Enabled := True;
   555   frmMain.cmdCancel.Hide;
   556   frmMain.cmdNext.Show;
   557   frmMain.tmrSpeed.Enabled := False;
   558   frmMain.Caption := Application.Title;
   559 end;
   560 
   561 end.