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