bmasterc@168: program HL2Launch; bmasterc@168: bmasterc@168: {$APPTYPE CONSOLE} bmasterc@168: bmasterc@168: uses bmasterc@168: SysUtils, bmasterc@168: ShellApi, bmasterc@168: Windows, bmasterc@168: Classes; bmasterc@168: bmasterc@168: procedure LaunchFile(eFile, eStartDir, eParams: String); bmasterc@168: var eStartInfo: TStartupInfo; bmasterc@168: eProcInfo: TProcessInformation; bmasterc@168: begin bmasterc@168: FillChar(eStartInfo, SizeOf(TStartupInfo), 0); bmasterc@168: with eStartInfo do begin bmasterc@168: cb := SizeOf(eStartInfo); bmasterc@168: dwFlags := STARTF_USESHOWWINDOW; bmasterc@168: end; bmasterc@168: bmasterc@168: if (CreateProcess(nil, PChar(eFile + #32 + eParams), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, PChar(eStartDir), eStartInfo, eProcInfo)) then begin bmasterc@168: try bmasterc@168: WaitForSingleObject(eProcInfo.hProcess, INFINITE); bmasterc@168: finally bmasterc@168: CloseHandle(eProcInfo.hProcess); bmasterc@168: CloseHandle(eProcInfo.hThread); bmasterc@168: end; bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2); bmasterc@168: Write(' Done.' + #13#10); bmasterc@168: end bmasterc@168: else begin bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); bmasterc@168: Write(' Couldn''t start hl2.exe!' + #13#10); bmasterc@168: end; bmasterc@168: end; bmasterc@168: bmasterc@168: function GetFileSize(eFile: String): Int64; bmasterc@168: var eFindHandle: THandle; bmasterc@168: eFindData: TWIN32FINDDATA; bmasterc@168: begin bmasterc@168: Result := 0; bmasterc@168: if not FileExists(eFile) then exit; bmasterc@168: eFindHandle := FindFirstFile(PChar(eFile), eFindData); bmasterc@168: if eFindHandle = INVALID_HANDLE_VALUE then exit; bmasterc@168: Result := (eFindData.nFileSizeHigh * (Int64(MAXDWORD) + 1)) + eFindData.nFileSizeLow; bmasterc@168: FindClose(eFindHandle); bmasterc@168: end; bmasterc@168: bmasterc@168: var eStream: TFileStream; bmasterc@168: ePath, eParams: String; bmasterc@168: eModDir: String; bmasterc@168: eSearchRec: TSearchRec; bmasterc@168: eStr: TStringList; bmasterc@168: i: integer; bmasterc@168: CheckSuccessful: Boolean; bmasterc@168: begin bmasterc@182: WriteLn(FindWindow(nil, 'STEAM')); bmasterc@182: readln; bmasterc@182: exit; bmasterc@182: bmasterc@168: ePath := ExtractFilePath(ParamStr(0)); bmasterc@168: for i := 1 to ParamCount do bmasterc@168: eParams := eParams + #32 + ParamStr(i); bmasterc@168: Delete(eParams, 1, 1); bmasterc@168: if Pos('console', LowerCase(eParams)) = 0 then bmasterc@168: eParams := eParams + ' -console'; bmasterc@168: eStream := nil; bmasterc@168: eModDir := ''; bmasterc@168: bmasterc@168: SetConsoleTitle('HL2 Launcher'); bmasterc@168: Sleep(200); // wait a few ms until the launch program is closed bmasterc@168: bmasterc@172: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2); bmasterc@168: WriteLn(' _ _ _ ____ _ _ '); bmasterc@168: WriteLn('| | | | | |___ \ | | __ _ _ _ _ __ ___| |__ ___ _ __ '); bmasterc@168: WriteLn('| |_| | | __) | | | / _` | | | | ''_ \ / __| ''_ \ / _ \ ''__|'); bmasterc@168: WriteLn('| _ | |___ / __/ | |__| (_| | |_| | | | | (__| | | | __/ | '); bmasterc@168: WriteLn('|_| |_|_____|_____| |_____\__,_|\__,_|_| |_|\___|_| |_|\___|_| '); bmasterc@173: WriteLn(' for listen servers using Metamod:Source'); bmasterc@168: WriteLn(''); bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); bmasterc@168: { Check files } bmasterc@168: WriteLn('Checking files...'); bmasterc@168: if not FileExists(ePath + 'hl2.exe') then begin bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); bmasterc@172: WriteLn('Error: hl2.exe is missing! Maybe wrong directory? If not, start your HL2 Mod again via Steam and try again.'); bmasterc@168: ReadLn; bmasterc@168: exit; bmasterc@168: end; bmasterc@182: if not FileExists(Copy(ePath, 1, Pos('\SteamApps\', ePath)) + 'steam.exe') then begin bmasterc@182: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); bmasterc@182: WriteLn('Error: Cannot find steam.exe! Make sure this application is located in your listen server''s directory.'); bmasterc@182: ReadLn; bmasterc@182: exit; bmasterc@182: end; bmasterc@168: { Verify GameInfo.txt ... } bmasterc@168: Write('Verifying GameInfo.txt...'); bmasterc@168: if (FindFirst(ePath + '*.*', faDirectory, eSearchRec) = 0) then begin bmasterc@168: repeat bmasterc@168: if (FileExists(ePath + eSearchRec.Name + '\GameInfo.txt')) then begin bmasterc@168: eModDir := eSearchRec.Name; bmasterc@168: break; bmasterc@168: end; bmasterc@168: until (FindNext(eSearchRec) <> 0); bmasterc@168: end; bmasterc@168: FindClose(eSearchRec.FindHandle); bmasterc@168: if eModDir = '' then begin bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); bmasterc@168: WriteLn(''); bmasterc@168: WriteLn('Error: Couldn''t find GameInfo.txt!'); bmasterc@168: ReadLn; bmasterc@168: exit; bmasterc@168: end bmasterc@168: else begin bmasterc@168: if Pos('game', LowerCase(eParams)) = 0 then // a small test which isn't worth a notice bmasterc@168: eParams := '-game ' + eSearchRec.Name + #32 + eParams; bmasterc@168: bmasterc@168: eStr := TStringList.Create; bmasterc@168: eStr.LoadFromFile(ePath + eModDir + '\GameInfo.txt'); bmasterc@168: if Pos('|gameinfo_path|addons/metamod/bin', LowerCase(eStr.Text)) = 0 then begin bmasterc@168: CheckSuccessful := False; bmasterc@168: for i := 0 to eStr.Count -1 do begin bmasterc@168: if Pos('searchpaths', LowerCase(Trim(eStr[i]))) = 1 then begin bmasterc@168: if i+3 >= eStr.Count then bmasterc@168: break; bmasterc@168: eStr.Insert(i+2, ' GameBin |gameinfo_path|addons/metamod/bin'); bmasterc@168: CheckSuccessful := True; bmasterc@168: break; bmasterc@168: end; bmasterc@168: end; bmasterc@168: bmasterc@168: if CheckSuccessful then begin bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2); bmasterc@168: SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), 0); bmasterc@168: eStr.SaveToFile(ePath + eModDir + '\GameInfo.txt'); bmasterc@168: SetFileAttributes(PChar(ePath + eModDir + '\GameInfo.txt'), faReadOnly); bmasterc@168: Write(' Registered MM:S sucessfully' + #13#10); bmasterc@168: end bmasterc@168: else begin bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4); bmasterc@168: Write(' Unexpected EOF, your GameInfo.txt seems to be corrupt' + #13#10); bmasterc@168: end; bmasterc@168: end bmasterc@168: else begin bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2); bmasterc@168: Write(' Done' + #13#10); bmasterc@168: end; bmasterc@168: eStr.Free; bmasterc@168: end; bmasterc@168: { ... and set it to write-protected } bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); bmasterc@168: Write('Setting GameInfo.txt to write-protected...'); bmasterc@168: try bmasterc@168: eStream := TFileStream.Create(ePath + eModDir + '\GameInfo.txt', fmShareDenyWrite); bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2); bmasterc@168: Write(' Done.' + #13#10); bmasterc@168: except bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); bmasterc@168: WriteLn(''); bmasterc@168: WriteLn('Warning: Couldn''t set GameInfo.txt to write-protected!'); bmasterc@168: eStream := nil; bmasterc@168: end; bmasterc@182: { Launch Steam if not opened } bmasterc@182: bmasterc@182: ShellExecute(0, 'open', PChar(Copy(ePath, 1, Pos('\SteamApps\', ePath)) + 'steam.exe'), nil, PChar(Copy(ePath, 1, Pos('\SteamApps\', ePath))), SW_SHOW); bmasterc@182: //ShellExecute(0, 'open', bmasterc@168: { Launch game } bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); bmasterc@168: Write('Starting HL2...'); bmasterc@168: LaunchFile(ePath + 'hl2.exe', Copy(ePath, 1, Pos('Steam', ePath)+5), eParams); bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); bmasterc@168: { Free GameInfo.txt } bmasterc@168: Write('Removing read-only again from GameInfo.txt...'); bmasterc@168: if Assigned(eStream) then begin bmasterc@168: eStream.Free; bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2); bmasterc@168: Write(' Done' + #13#10); bmasterc@168: end bmasterc@168: else begin bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4); bmasterc@168: Write(' Skipped' + #13#10); bmasterc@168: end; bmasterc@168: { End message } bmasterc@168: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); bmasterc@168: WriteLn(''); bmasterc@168: WriteLn('Thanks for using Metamod:Source! Visit http://www.sourcemm.net/'); bmasterc@168: Sleep(2500); bmasterc@168: end.