installer/HL2Launch.dpr
author David Anderson <dvander@alliedmods.net>
Sun Oct 07 03:12:19 2007 +0000 (2007-10-07)
changeset 379 e8a3f2617a3e
parent 269 f241c05cb154
permissions -rw-r--r--
initial import of unfinished new sample plugin
this one is more based on valve's sample...
     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     StartTime: Cardinal;
    57 begin
    58   ePath := ExtractFilePath(ParamStr(0));
    59   for i := 1 to ParamCount do
    60     eParams := eParams + #32 + ParamStr(i);
    61   Delete(eParams, 1, 1);
    62   if Pos('console', LowerCase(eParams)) = 0 then
    63     eParams := eParams + ' -console';
    64   eStream := nil;
    65   eModDir := '';
    66 
    67   SetConsoleTitle('HL2 Launcher');
    68   Sleep(200); // wait a few ms until the launch program is closed
    69 
    70   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    71   WriteLn(' _   _ _     ____       _                           _               ');
    72   WriteLn('| | | | |   |___ \     | |    __ _ _   _ _ __   ___| |__   ___ _ __ ');
    73   WriteLn('| |_| | |     __) |    | |   / _` | | | | ''_ \ / __| ''_ \ / _ \ ''__|');
    74   WriteLn('|  _  | |___ / __/     | |__| (_| | |_| | | | | (__| | | |  __/ |   ');
    75   WriteLn('|_| |_|_____|_____|    |_____\__,_|\__,_|_| |_|\___|_| |_|\___|_|   ');
    76   WriteLn('  for listen servers using Metamod:Source');
    77   WriteLn('');
    78   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    79   { Check files }
    80   WriteLn('Checking files...');
    81   if not FileExists(ePath + 'hl2.exe') then begin
    82     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    83     WriteLn('Error: hl2.exe is missing! Maybe wrong directory? If not, start your HL2 Mod again via Steam and try again.');
    84     ReadLn;
    85     exit;
    86   end;
    87   if not FileExists(Copy(ePath, 1, Pos('\steamapps\', LowerCase(ePath))) + 'steam.exe') then begin
    88     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    89     WriteLn('Error: Cannot find steam.exe! Make sure this application is located in your listen server''s directory.');
    90     ReadLn;
    91     exit;
    92   end;
    93   { Verify GameInfo.txt ... }
    94   Write('Verifying GameInfo.txt...');
    95   if (FindFirst(ePath + '*.*', faDirectory, eSearchRec) = 0) then begin
    96     repeat
    97       if (FileExists(ePath + eSearchRec.Name + '\GameInfo.txt')) then begin
    98         eModDir := eSearchRec.Name;
    99         break;
   100       end;
   101     until (FindNext(eSearchRec) <> 0);
   102   end;
   103   FindClose(eSearchRec.FindHandle);
   104   if eModDir = '' then begin
   105     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
   106     WriteLn('');
   107     WriteLn('Error: Couldn''t find GameInfo.txt!');
   108     ReadLn;
   109     exit;
   110   end
   111   else begin
   112     if Pos('game', LowerCase(eParams)) = 0 then // a small test which isn't worth a notice
   113       eParams := '-game ' + eSearchRec.Name + #32 + eParams;
   114     
   115     eStr := TStringList.Create;
   116     eStr.LoadFromFile(ePath + eModDir + '\GameInfo.txt');
   117     if Pos('|gameinfo_path|addons/metamod/bin', LowerCase(eStr.Text)) = 0 then begin
   118       CheckSuccessful := False;
   119       for i := 0 to eStr.Count -1 do begin
   120         if Pos('searchpaths', LowerCase(Trim(eStr[i]))) = 1 then begin
   121           if i+3 >= eStr.Count then
   122             break;
   123           eStr.Insert(i+2, '			GameBin				|gameinfo_path|addons/metamod/bin');
   124           CheckSuccessful := True;
   125           break;
   126         end;
   127       end;
   128 
   129       if CheckSuccessful then begin
   130         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   131         SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), 0);
   132         eStr.SaveToFile(ePath + eModDir + '\GameInfo.txt');
   133         SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), faReadOnly);
   134         Write(' Registered MM:S sucessfully' + #13#10);
   135       end
   136       else begin
   137         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
   138         Write(' Unexpected EOF, your GameInfo.txt seems to be corrupt' + #13#10);
   139       end;
   140     end
   141     else begin
   142       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   143       Write(' Done' + #13#10);
   144     end;
   145     eStr.Free;
   146   end;
   147   { ... and set it to write-protected }
   148   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   149   Write('Setting GameInfo.txt to write-protected...');
   150   try
   151     eStream := TFileStream.Create(ePath + eModDir + '\GameInfo.txt', fmShareDenyWrite);
   152     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   153     Write(' Done.' + #13#10);
   154   except
   155     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
   156     WriteLn('');
   157     WriteLn('Warning: Couldn''t set GameInfo.txt to write-protected!');
   158     eStream := nil;
   159   end;
   160   { Launch Steam if not opened }
   161   ShellExecute(0, 'open', PChar(Copy(ePath, 1, Pos('\steamapps\', LowerCase(ePath))) + 'steam.exe'), nil, PChar(Copy(ePath, 1, Pos('\steamapps\', LowerCase(ePath)))), SW_SHOW);
   162   { Launch game }
   163   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   164   Write('Starting HL2...');
   165   StartTime := GetTickCount;
   166   LaunchFile(ePath + 'hl2.exe', Copy(ePath, 1, Pos('Steam', ePath)+5), eParams);
   167   if (GetTickCount - StartTime < 10000) then begin
   168     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
   169     WriteLn('Important: If you experience any problems starting HL2 using this program, please start it once via Steam and try again.');
   170     ReadLn;
   171   end;
   172   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   173   { Free GameInfo.txt }
   174   Write('Removing read-only again from GameInfo.txt...');
   175   if Assigned(eStream) then begin
   176     eStream.Free;
   177     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   178     Write(' Done' + #13#10);
   179   end
   180   else begin
   181     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
   182     Write(' Skipped' + #13#10);
   183   end;
   184   { End message }
   185   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   186   WriteLn('');
   187   WriteLn('Thanks for using Metamod:Source! Visit http://www.sourcemm.net/');
   188   Sleep(2500);
   189 end.