installer/HL2Launch.dpr
author Christian Hammacher <bmasterc@gmail.com>
Wed Dec 28 23:56:45 2005 +0000 (2005-12-28)
changeset 173 c36e2cf2baad
parent 172 21327f4ee466
child 182 f60d482a6e07
permissions -rw-r--r--
again design stuff
     1 program HL2Launch;
     2 
     3 {$APPTYPE CONSOLE}
     4 
     5 uses
     6   SysUtils,
     7   ShellApi,
     8   Windows,
     9   Classes;
    10 
    11 procedure LaunchFile(eFile, eStartDir, eParams: String);
    12 var eStartInfo: TStartupInfo;
    13     eProcInfo: TProcessInformation;
    14 begin
    15    FillChar(eStartInfo, SizeOf(TStartupInfo), 0);
    16    with eStartInfo do begin
    17      cb := SizeOf(eStartInfo);
    18      dwFlags := STARTF_USESHOWWINDOW;
    19    end;
    20 
    21    if (CreateProcess(nil, PChar(eFile + #32 + eParams), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, PChar(eStartDir), eStartInfo, eProcInfo)) then begin
    22      try
    23        WaitForSingleObject(eProcInfo.hProcess, INFINITE);
    24      finally
    25        CloseHandle(eProcInfo.hProcess);
    26        CloseHandle(eProcInfo.hThread);
    27      end;
    28      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    29      Write(' Done.' + #13#10);
    30    end
    31    else begin
    32      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    33      Write(' Couldn''t start hl2.exe!' + #13#10);
    34    end;
    35 end;
    36 
    37 function GetFileSize(eFile: String): Int64;
    38 var eFindHandle: THandle;
    39     eFindData: TWIN32FINDDATA;
    40 begin
    41   Result := 0;
    42   if not FileExists(eFile) then exit;
    43   eFindHandle := FindFirstFile(PChar(eFile), eFindData);
    44   if eFindHandle = INVALID_HANDLE_VALUE then exit;
    45   Result := (eFindData.nFileSizeHigh * (Int64(MAXDWORD) + 1)) + eFindData.nFileSizeLow;
    46   FindClose(eFindHandle);
    47 end;
    48 
    49 var eStream: TFileStream;
    50     ePath, eParams: String;
    51     eModDir: String;
    52     eSearchRec: TSearchRec;
    53     eStr: TStringList;
    54     i: integer;
    55     CheckSuccessful: Boolean;
    56 begin
    57   ePath := ExtractFilePath(ParamStr(0));
    58   for i := 1 to ParamCount do
    59     eParams := eParams + #32 + ParamStr(i);
    60   Delete(eParams, 1, 1);
    61   if Pos('console', LowerCase(eParams)) = 0 then
    62     eParams := eParams + ' -console';
    63   eStream := nil;
    64   eModDir := '';
    65 
    66   SetConsoleTitle('HL2 Launcher');
    67   Sleep(200); // wait a few ms until the launch program is closed
    68 
    69   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    70   WriteLn(' _   _ _     ____       _                           _               ');
    71   WriteLn('| | | | |   |___ \     | |    __ _ _   _ _ __   ___| |__   ___ _ __ ');
    72   WriteLn('| |_| | |     __) |    | |   / _` | | | | ''_ \ / __| ''_ \ / _ \ ''__|');
    73   WriteLn('|  _  | |___ / __/     | |__| (_| | |_| | | | | (__| | | |  __/ |   ');
    74   WriteLn('|_| |_|_____|_____|    |_____\__,_|\__,_|_| |_|\___|_| |_|\___|_|   ');
    75   WriteLn('  for listen servers using Metamod:Source');
    76   WriteLn('');
    77   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    78   { Check files }
    79   WriteLn('Checking files...');
    80   if not FileExists(ePath + 'hl2.exe') then begin
    81     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    82     WriteLn('Error: hl2.exe is missing! Maybe wrong directory? If not, start your HL2 Mod again via Steam and try again.');
    83     ReadLn;
    84     exit;
    85   end;
    86   { Verify GameInfo.txt ... }
    87   Write('Verifying GameInfo.txt...');
    88   if (FindFirst(ePath + '*.*', faDirectory, eSearchRec) = 0) then begin
    89     repeat
    90       if (FileExists(ePath + eSearchRec.Name + '\GameInfo.txt')) then begin
    91         eModDir := eSearchRec.Name;
    92         break;
    93       end;
    94     until (FindNext(eSearchRec) <> 0);
    95   end;
    96   FindClose(eSearchRec.FindHandle);
    97   if eModDir = '' then begin
    98     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    99     WriteLn('');
   100     WriteLn('Error: Couldn''t find GameInfo.txt!');
   101     ReadLn;
   102     exit;
   103   end
   104   else begin
   105     if Pos('game', LowerCase(eParams)) = 0 then // a small test which isn't worth a notice
   106       eParams := '-game ' + eSearchRec.Name + #32 + eParams;
   107     
   108     eStr := TStringList.Create;
   109     eStr.LoadFromFile(ePath + eModDir + '\GameInfo.txt');
   110     if Pos('|gameinfo_path|addons/metamod/bin', LowerCase(eStr.Text)) = 0 then begin
   111       CheckSuccessful := False;
   112       for i := 0 to eStr.Count -1 do begin
   113         if Pos('searchpaths', LowerCase(Trim(eStr[i]))) = 1 then begin
   114           if i+3 >= eStr.Count then
   115             break;
   116           eStr.Insert(i+2, '			GameBin				|gameinfo_path|addons/metamod/bin');
   117           CheckSuccessful := True;
   118           break;
   119         end;
   120       end;
   121 
   122       if CheckSuccessful then begin
   123         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   124         SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), 0);
   125         eStr.SaveToFile(ePath + eModDir + '\GameInfo.txt');
   126         SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), faReadOnly);
   127         Write(' Registered MM:S sucessfully' + #13#10);
   128       end
   129       else begin
   130         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
   131         Write(' Unexpected EOF, your GameInfo.txt seems to be corrupt' + #13#10);
   132       end;
   133     end
   134     else begin
   135       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   136       Write(' Done' + #13#10);
   137     end;
   138     eStr.Free;
   139   end;
   140   { ... and set it to write-protected }
   141   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   142   Write('Setting GameInfo.txt to write-protected...');
   143   try
   144     eStream := TFileStream.Create(ePath + eModDir + '\GameInfo.txt', fmShareDenyWrite);
   145     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   146     Write(' Done.' + #13#10);
   147   except
   148     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
   149     WriteLn('');
   150     WriteLn('Warning: Couldn''t set GameInfo.txt to write-protected!');
   151     eStream := nil;
   152   end;
   153   { Launch game }
   154   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   155   Write('Starting HL2...');
   156   LaunchFile(ePath + 'hl2.exe', Copy(ePath, 1, Pos('Steam', ePath)+5), eParams);
   157   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   158   { Free GameInfo.txt }
   159   Write('Removing read-only again from GameInfo.txt...');
   160   if Assigned(eStream) then begin
   161     eStream.Free;
   162     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   163     Write(' Done' + #13#10);
   164   end
   165   else begin
   166     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
   167     Write(' Skipped' + #13#10);
   168   end;
   169   { End message }
   170   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   171   WriteLn('');
   172   WriteLn('Thanks for using Metamod:Source! Visit http://www.sourcemm.net/');
   173   Sleep(2500);
   174 end.