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