installer/HL2Launch.dpr
branchsourcemm-1.6.0
changeset 347 e82968c21606
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/installer/HL2Launch.dpr	Tue Sep 18 20:03:47 2007 +0000
     1.3 @@ -0,0 +1,189 @@
     1.4 +program HL2Launch;
     1.5 +
     1.6 +{$APPTYPE CONSOLE}
     1.7 +
     1.8 +uses
     1.9 +  SysUtils,
    1.10 +  ShellApi,
    1.11 +  Windows,
    1.12 +  Classes;
    1.13 +
    1.14 +procedure LaunchFile(eFile, eStartDir, eParams: String);
    1.15 +var eStartInfo: TStartupInfo;
    1.16 +    eProcInfo: TProcessInformation;
    1.17 +begin
    1.18 +   FillChar(eStartInfo, SizeOf(TStartupInfo), 0);
    1.19 +   with eStartInfo do begin
    1.20 +     cb := SizeOf(eStartInfo);
    1.21 +     dwFlags := STARTF_USESHOWWINDOW;
    1.22 +   end;
    1.23 +
    1.24 +   if (CreateProcess(nil, PChar(eFile + #32 + eParams), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, PChar(eStartDir), eStartInfo, eProcInfo)) then begin
    1.25 +     try
    1.26 +       WaitForSingleObject(eProcInfo.hProcess, INFINITE);
    1.27 +     finally
    1.28 +       CloseHandle(eProcInfo.hProcess);
    1.29 +       CloseHandle(eProcInfo.hThread);
    1.30 +     end;
    1.31 +     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    1.32 +     Write(' Done.' + #13#10);
    1.33 +   end
    1.34 +   else begin
    1.35 +     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    1.36 +     Write(' Couldn''t start hl2.exe!' + #13#10);
    1.37 +   end;
    1.38 +end;
    1.39 +
    1.40 +function GetFileSize(eFile: String): Int64;
    1.41 +var eFindHandle: THandle;
    1.42 +    eFindData: TWIN32FINDDATA;
    1.43 +begin
    1.44 +  Result := 0;
    1.45 +  if not FileExists(eFile) then exit;
    1.46 +  eFindHandle := FindFirstFile(PChar(eFile), eFindData);
    1.47 +  if eFindHandle = INVALID_HANDLE_VALUE then exit;
    1.48 +  Result := (eFindData.nFileSizeHigh * (Int64(MAXDWORD) + 1)) + eFindData.nFileSizeLow;
    1.49 +  FindClose(eFindHandle);
    1.50 +end;
    1.51 +
    1.52 +var eStream: TFileStream;
    1.53 +    ePath, eParams: String;
    1.54 +    eModDir: String;
    1.55 +    eSearchRec: TSearchRec;
    1.56 +    eStr: TStringList;
    1.57 +    i: integer;
    1.58 +    CheckSuccessful: Boolean;
    1.59 +    StartTime: Cardinal;
    1.60 +begin
    1.61 +  ePath := ExtractFilePath(ParamStr(0));
    1.62 +  for i := 1 to ParamCount do
    1.63 +    eParams := eParams + #32 + ParamStr(i);
    1.64 +  Delete(eParams, 1, 1);
    1.65 +  if Pos('console', LowerCase(eParams)) = 0 then
    1.66 +    eParams := eParams + ' -console';
    1.67 +  eStream := nil;
    1.68 +  eModDir := '';
    1.69 +
    1.70 +  SetConsoleTitle('HL2 Launcher');
    1.71 +  Sleep(200); // wait a few ms until the launch program is closed
    1.72 +
    1.73 +  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    1.74 +  WriteLn(' _   _ _     ____       _                           _               ');
    1.75 +  WriteLn('| | | | |   |___ \     | |    __ _ _   _ _ __   ___| |__   ___ _ __ ');
    1.76 +  WriteLn('| |_| | |     __) |    | |   / _` | | | | ''_ \ / __| ''_ \ / _ \ ''__|');
    1.77 +  WriteLn('|  _  | |___ / __/     | |__| (_| | |_| | | | | (__| | | |  __/ |   ');
    1.78 +  WriteLn('|_| |_|_____|_____|    |_____\__,_|\__,_|_| |_|\___|_| |_|\___|_|   ');
    1.79 +  WriteLn('  for listen servers using Metamod:Source');
    1.80 +  WriteLn('');
    1.81 +  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    1.82 +  { Check files }
    1.83 +  WriteLn('Checking files...');
    1.84 +  if not FileExists(ePath + 'hl2.exe') then begin
    1.85 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    1.86 +    WriteLn('Error: hl2.exe is missing! Maybe wrong directory? If not, start your HL2 Mod again via Steam and try again.');
    1.87 +    ReadLn;
    1.88 +    exit;
    1.89 +  end;
    1.90 +  if not FileExists(Copy(ePath, 1, Pos('\steamapps\', LowerCase(ePath))) + 'steam.exe') then begin
    1.91 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    1.92 +    WriteLn('Error: Cannot find steam.exe! Make sure this application is located in your listen server''s directory.');
    1.93 +    ReadLn;
    1.94 +    exit;
    1.95 +  end;
    1.96 +  { Verify GameInfo.txt ... }
    1.97 +  Write('Verifying GameInfo.txt...');
    1.98 +  if (FindFirst(ePath + '*.*', faDirectory, eSearchRec) = 0) then begin
    1.99 +    repeat
   1.100 +      if (FileExists(ePath + eSearchRec.Name + '\GameInfo.txt')) then begin
   1.101 +        eModDir := eSearchRec.Name;
   1.102 +        break;
   1.103 +      end;
   1.104 +    until (FindNext(eSearchRec) <> 0);
   1.105 +  end;
   1.106 +  FindClose(eSearchRec.FindHandle);
   1.107 +  if eModDir = '' then begin
   1.108 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
   1.109 +    WriteLn('');
   1.110 +    WriteLn('Error: Couldn''t find GameInfo.txt!');
   1.111 +    ReadLn;
   1.112 +    exit;
   1.113 +  end
   1.114 +  else begin
   1.115 +    if Pos('game', LowerCase(eParams)) = 0 then // a small test which isn't worth a notice
   1.116 +      eParams := '-game ' + eSearchRec.Name + #32 + eParams;
   1.117 +    
   1.118 +    eStr := TStringList.Create;
   1.119 +    eStr.LoadFromFile(ePath + eModDir + '\GameInfo.txt');
   1.120 +    if Pos('|gameinfo_path|addons/metamod/bin', LowerCase(eStr.Text)) = 0 then begin
   1.121 +      CheckSuccessful := False;
   1.122 +      for i := 0 to eStr.Count -1 do begin
   1.123 +        if Pos('searchpaths', LowerCase(Trim(eStr[i]))) = 1 then begin
   1.124 +          if i+3 >= eStr.Count then
   1.125 +            break;
   1.126 +          eStr.Insert(i+2, '			GameBin				|gameinfo_path|addons/metamod/bin');
   1.127 +          CheckSuccessful := True;
   1.128 +          break;
   1.129 +        end;
   1.130 +      end;
   1.131 +
   1.132 +      if CheckSuccessful then begin
   1.133 +        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   1.134 +        SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), 0);
   1.135 +        eStr.SaveToFile(ePath + eModDir + '\GameInfo.txt');
   1.136 +        SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), faReadOnly);
   1.137 +        Write(' Registered MM:S sucessfully' + #13#10);
   1.138 +      end
   1.139 +      else begin
   1.140 +        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
   1.141 +        Write(' Unexpected EOF, your GameInfo.txt seems to be corrupt' + #13#10);
   1.142 +      end;
   1.143 +    end
   1.144 +    else begin
   1.145 +      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   1.146 +      Write(' Done' + #13#10);
   1.147 +    end;
   1.148 +    eStr.Free;
   1.149 +  end;
   1.150 +  { ... and set it to write-protected }
   1.151 +  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   1.152 +  Write('Setting GameInfo.txt to write-protected...');
   1.153 +  try
   1.154 +    eStream := TFileStream.Create(ePath + eModDir + '\GameInfo.txt', fmShareDenyWrite);
   1.155 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   1.156 +    Write(' Done.' + #13#10);
   1.157 +  except
   1.158 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
   1.159 +    WriteLn('');
   1.160 +    WriteLn('Warning: Couldn''t set GameInfo.txt to write-protected!');
   1.161 +    eStream := nil;
   1.162 +  end;
   1.163 +  { Launch Steam if not opened }
   1.164 +  ShellExecute(0, 'open', PChar(Copy(ePath, 1, Pos('\steamapps\', LowerCase(ePath))) + 'steam.exe'), nil, PChar(Copy(ePath, 1, Pos('\steamapps\', LowerCase(ePath)))), SW_SHOW);
   1.165 +  { Launch game }
   1.166 +  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   1.167 +  Write('Starting HL2...');
   1.168 +  StartTime := GetTickCount;
   1.169 +  LaunchFile(ePath + 'hl2.exe', Copy(ePath, 1, Pos('Steam', ePath)+5), eParams);
   1.170 +  if (GetTickCount - StartTime < 10000) then begin
   1.171 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
   1.172 +    WriteLn('Important: If you experience any problems starting HL2 using this program, please start it once via Steam and try again.');
   1.173 +    ReadLn;
   1.174 +  end;
   1.175 +  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   1.176 +  { Free GameInfo.txt }
   1.177 +  Write('Removing read-only again from GameInfo.txt...');
   1.178 +  if Assigned(eStream) then begin
   1.179 +    eStream.Free;
   1.180 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
   1.181 +    Write(' Done' + #13#10);
   1.182 +  end
   1.183 +  else begin
   1.184 +    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
   1.185 +    Write(' Skipped' + #13#10);
   1.186 +  end;
   1.187 +  { End message }
   1.188 +  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
   1.189 +  WriteLn('');
   1.190 +  WriteLn('Thanks for using Metamod:Source! Visit http://www.sourcemm.net/');
   1.191 +  Sleep(2500);
   1.192 +end.