Trigger build for hl2sdk-l4d changes.
5 uses SysUtils, Classes, Windows, Graphics, Forms, ShellAPI, Controls, Messages,
6 TlHelp32, IdFTPCommon, ComCtrls;
8 type TOS = (osWindows, osLinux);
10 procedure AddStatus(Text: String; Color: TColor; ShowTime: Boolean = True);
11 procedure AddDone(Additional: String = '');
13 procedure AddNotFound;
14 procedure DownloadFile(eFile: String; eDestination: String);
16 procedure BasicInstallation(ePath: String; SteamInstall, ListenInstall: Boolean; OS: TOS; const Source: Boolean);
17 procedure InstallDedicated(eModPath: String; const UseSteam, Source: Boolean);
18 procedure InstallListen(ePath: String; const Source: Boolean);
19 procedure InstallCustom(ePath: String; eOS: TOS; const Source: Boolean);
20 procedure InstallFTP(OS: TOS; const Source: Boolean);
22 var StartTime: TDateTime;
24 StandaloneServer: String;
25 Cancel: Boolean = False;
29 uses UnitfrmMain, UnitfrmProxy, UnitFunctions, UnitPackSystem;
33 function InstallTime: String;
35 Result := Copy(FormatDateTime('HH:MM:SS', Now - StartTime), 4, 5);
38 procedure AddStatus(Text: String; Color: TColor; ShowTime: Boolean = True);
40 frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
41 if ShowTime then begin
42 frmMain.rtfDetails.SelAttributes.Color := clBlack;
43 if frmMain.rtfDetails.Text = '' then
44 frmMain.rtfDetails.SelText := '[' + InstallTime + '] '
46 frmMain.rtfDetails.SelText := #13#10 + '[' + InstallTime + '] ';
47 frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
50 frmMain.rtfDetails.SelText := #13#10;
52 frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
53 frmMain.rtfDetails.SelAttributes.Color := Color;
54 frmMain.rtfDetails.SelText := Text;
55 frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
58 Application.ProcessMessages;
61 procedure AddDone(Additional: String = '');
63 frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
64 frmMain.rtfDetails.SelAttributes.Color := clGreen;
65 if Additional = '' then
66 frmMain.rtfDetails.SelText := ' Done.'
68 frmMain.rtfDetails.SelText := ' Done, ' + Additional + '.';
69 frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
72 Application.ProcessMessages;
77 frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
78 frmMain.rtfDetails.SelAttributes.Color := $004080FF; // orange
79 frmMain.rtfDetails.SelText := ' Skipped.';
80 frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
83 Application.ProcessMessages;
86 procedure AddNotFound;
88 frmMain.rtfDetails.SelStart := Length(frmMain.rtfDetails.Text);
89 frmMain.rtfDetails.SelAttributes.Color := clRed;
90 frmMain.rtfDetails.SelText := ' Not found.';
91 frmMain.rtfDetails.Perform(WM_VSCROLL, SB_BOTTOM, 0);
94 Application.ProcessMessages;
97 procedure FileCopy(Source, Destination: String; CopyConfig: Boolean; AddStatus: Boolean = True);
99 if (not CopyConfig) and (Pos('config', Source) <> 0) then begin
105 if not FileExists(Source) then begin
112 if FileExists(Destination) then
113 DeleteFile(PChar(Destination));
114 CopyFile(PChar(Source), PChar(Destination), False);
116 Application.ProcessMessages;
123 procedure DownloadFile(eFile: String; eDestination: String);
124 var TransferType: TIdFTPTransferType;
126 // There's only one file to download and it's ASCII :]
127 TransferType := ftASCII;
128 if frmMain.IdFTP.TransferType <> TransferType then
129 frmMain.IdFTP.TransferType := TransferType;
131 frmMain.IdFTP.Get(eFile, eDestination, True);
134 procedure UploadFile(eFile: String; eDestination: String; CopyConfig: Boolean = True);
135 var TransferType: TIdFTPTransferType;
137 if (Pos('config', eFile) > 0) and (not CopyConfig) then begin
142 eDestination := StringReplace(eDestination, '\', '/', [rfReplaceAll]);
144 // the same as in DownloadFile()
145 TransferType := ftBinary;
146 if ExtractFileExt(LowerCase(eFile)) = '.txt' then TransferType := ftASCII;
147 if frmMain.IdFTP.TransferType <> TransferType then
148 frmMain.IdFTP.TransferType := TransferType;
150 frmMain.IdFTP.Put(eFile, eDestination);
154 procedure FTPMakeDir(eDir: String);
156 eDir := StringReplace(eDir, '\', '/', [rfReplaceAll]);
158 frmMain.IdFTP.MakeDir(eDir);
160 Application.ProcessMessages;
164 function FSize(eFile: String): Cardinal;
165 var eRec: TSearchRec;
167 if FindFirst(eFile, faAnyFile, eRec) = 0 then
173 // stuff for killing processes
175 function GetProcessID(sProcName: String): Integer;
178 pe32: TProcessEntry32;
181 hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
182 if hProcSnap = INVALID_HANDLE_VALUE then
185 pe32.dwSize := SizeOf(ProcessEntry32);
186 if Process32First(hProcSnap, pe32) = true then begin
187 while Process32Next(hProcSnap, pe32) = true do begin
188 if pos(sProcName, pe32.szExeFile) <> 0then
189 result := pe32.th32ProcessID;
192 CloseHandle(hProcSnap);
195 procedure KillProcess(dwProcID: DWORD);
200 hProcess := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, dwProcID);
201 TerminateProcess(hProcess, 0);
202 dw := WaitForSingleObject(hProcess, 5000);
205 CloseHandle(hProcess);
210 CloseHandle(hProcess);
214 CloseHandle(hProcess);
219 { Basic Installation }
221 procedure BasicInstallation(ePath: String; SteamInstall, ListenInstall: Boolean; OS: TOS; const Source: Boolean);
222 var eStr: TStringList;
227 frmMain.ggeAll.MaxValue := 8;
228 frmMain.ggeAll.Progress := 0;
229 frmMain.ggeItem.MaxValue := 1;
230 frmMain.ggeItem.Progress := 0;
232 {if (GetProcessID('Steam.exe') <> -1) and (SteamInstall) then begin
233 if MessageBox(frmMain.Handle, 'Steam is still running. It is necersarry to shut it down before you install Metamod:Source. Shut it down now?', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
234 AddStatus('Shutting down Steam...', clBlack, False);
235 if GetProcessID('Steam.exe') = -1 then
238 KillProcess(GetProcessID('Steam.exe'));
240 while GetProcessID('Steam.exe') <> -1 do begin // sure is sure...
242 Application.ProcessMessages;
247 Application.Terminate;
251 frmMain.ggeAll.Progress := 1;
252 frmMain.ggeItem.Progress := 1;
254 frmMain.ggeItem.Progress := 0;
255 AddStatus('Unpacking files...', clBlack);
256 if not Unpack(Source) then begin
257 AddStatus('No files attached!', clRed);
258 Screen.Cursor := crDefault;
262 frmMain.ggeAll.Progress := 2;
263 frmMain.ggeItem.Progress := 1;
264 { Check for installation / Create directories }
266 AddStatus('Creating directories...', clBlack);
267 if DirectoryExists(ePath + 'addons\metamod\bin') then begin
268 case MessageBox(frmMain.Handle, 'A Metamod:Source installation was already detected. If you choose to reinstall, your configuration files will be erased. Click Yes to continue, No to Upgrade, or Cancel to abort the install.', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNOCANCEL) of
269 mrNo: CopyConfig := False;
271 Application.Terminate;
278 frmMain.ggeItem.Progress := 0;
279 ForceDirectories(ePath + 'addons\metamod\bin');
282 frmMain.ggeItem.Progress := 1;
283 frmMain.ggeAll.Progress := 3;
286 if not FileExists(ePath + 'gameinfo.txt') then begin
287 if MessageBox(frmMain.Handle, 'The file "gameinfo.txt" couldn''t be found. Continue installation?', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNO) = mrNo then begin
288 AddStatus('Installation canceled by user!', clRed, False);
289 Screen.Cursor := crDefault;
295 eStr := TStringList.Create;
297 frmMain.ggeItem.Progress := 0;
298 AddStatus('Creating metaplugins.ini...', clBlack);
299 if CopyConfig then begin
300 eStr.SaveToFile(ePath + 'addons\metamod\metaplugins.ini');
305 frmMain.ggeItem.Progress := 1;
306 frmMain.ggeAll.Progress := 4;
308 frmMain.ggeItem.Progress := 0;
310 AddStatus('Editing gameinfo.txt...', clBlack);
311 eStr.LoadFromFile(ePath + 'gameinfo.txt');
312 for i := 0 to eStr.Count -1 do begin
313 if Trim(LowerCase(eStr[i])) = 'gamebin |gameinfo_path|addons/metamod/bin' then begin
319 if not eFound then begin
320 for i := 0 to eStr.Count -1 do begin
321 if Trim(eStr[i]) = 'SearchPaths' then begin
322 eStr.Insert(i +2, ' GameBin |gameinfo_path|addons/metamod/bin');
326 SetFileAttributes(PChar(ePath + 'gameinfo.txt'), 0);
327 eStr.SaveToFile(ePath + 'gameinfo.txt');
328 SetFileAttributes(PChar(ePath + 'gameinfo.txt'), faReadOnly); // important for listen servers
334 frmMain.ggeItem.Progress := 1;
335 frmMain.ggeAll.Progress := 5;
338 frmMain.ggeItem.Progress := 0;
339 AddStatus('Copying server.dll...', clBlack);
340 CopyFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'), PChar(ePath + 'addons\metamod\bin\server.dll'), False);
342 frmMain.ggeItem.Progress := 1;
343 frmMain.ggeAll.Progress := 6;
344 if ListenInstall then begin
345 ePath := ExtractFilePath(Copy(ePath, 1, Length(ePath)-1));
346 AddStatus('Copying hl2launch.exe...', clBlack);
347 CopyFile(PChar(ExtractFilePath(ParamStr(0)) + 'hl2launch.exe'), PChar(ePath + 'hl2launch.exe'), False);
351 frmMain.ggeItem.Progress := 0;
352 AddStatus('Removing temporary files...', clBlack);
353 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'hl2launch.exe'));
354 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'));
355 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server_i486.so'));
357 frmMain.ggeItem.Progress := 1;
358 frmMain.ggeAll.Progress := 8;
360 AddStatus('', clBlack, False);
361 AddStatus('Finished installation!', clBlack, False);
362 frmMain.cmdNext.Enabled := True;
363 frmMain.cmdCancel.Hide;
364 Screen.Cursor := crDefault;
366 if ListenInstall then
367 MessageBox(frmMain.Handle, PChar('The Source launcher "hl2launch.exe" has been copied to ' + ePath + '. You can use it to start your game with Metamod:Source enabled.'), PChar(Application.Title), MB_ICONINFORMATION);
372 procedure InstallDedicated(eModPath: String; const UseSteam, Source: Boolean);
375 Screen.Cursor := crHourGlass;
376 AddStatus('Starting Metamod:Source installation on dedicated server...', clBlack, False);
377 BasicInstallation(eModPath, UseSteam, False, osWindows, Source);
382 procedure InstallListen(ePath: String; const Source: Boolean);
385 Screen.Cursor := crHourGlass;
386 AddStatus('Starting Metamod:Source installation on the listen server...', clBlack);
387 BasicInstallation(ePath, True, True, osWindows, Source);
392 procedure InstallCustom(ePath: String; eOS: TOS; const Source: Boolean);
395 Screen.Cursor := crHourGlass;
396 AddStatus('Starting Metamod:Source installation...', clBlack);
397 BasicInstallation(ePath, False, False, eOS, Source);
402 procedure InstallFTP(OS: TOS; const Source: Boolean);
403 function DoReconnect: Boolean;
406 if MessageBox(frmMain.Handle, 'You have been disconnected due to an error. Try to reconnect?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
408 frmMain.IdFTP.Connect;
411 MessageBox(frmMain.Handle, 'Failed to reconnect. Installation aborted.', PChar(Application.Title), MB_ICONSTOP);
418 var eStr: TStringList;
420 CopyConfig, eFound: Boolean;
422 frmMain.cmdCancel.Show;
423 frmMain.cmdNext.Hide;
424 Screen.Cursor := crHourGlass;
426 frmMain.ggeAll.MaxValue := 6;
427 frmMain.ggeAll.Progress := 0;
428 frmMain.ggeItem.MaxValue := 1;
429 frmMain.ggeItem.Progress := 0;
432 frmMain.ggeItem.Progress := 0;
433 AddStatus('Unpacking files...', clBlack);
434 if not Unpack(Source) then begin
435 AddStatus('No files attached!', clRed);
436 Screen.Cursor := crDefault;
440 frmMain.ggeAll.Progress := 2;
441 frmMain.ggeItem.Progress := 1;
442 { Check for installation }
443 AddStatus('Editing gameinfo.txt...', clBlack);
444 eStr := TStringList.Create;
445 DownloadFile('gameinfo.txt', ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
446 eStr.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
450 for i := 0 to eStr.Count -1 do begin
451 if Trim(LowerCase(eStr[i])) = 'gamebin |gameinfo_path|addons/metamod/bin' then begin
453 case MessageBox(frmMain.Handle, 'A Metamod:Source installation was already detected. If you choose to reinstall, your configuration files will be erased. Click Yes to continue, No to Upgrade, or Cancel to abort the install.', PChar(frmMain.Caption), MB_ICONQUESTION + MB_YESNOCANCEL) of
454 mrNo: CopyConfig := False;
456 Application.Terminate;
465 if not eFound then begin
466 for i := 0 to eStr.Count -1 do begin
467 if Trim(eStr[i]) = 'SearchPaths' then begin
468 eStr.Insert(i +2, ' GameBin |gameinfo_path|addons/metamod/bin');
473 eStr.SaveToFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt');
474 UploadFile(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt', 'gameinfo.txt');
476 AddStatus('Trying to set gameinfo.txt to read-only...', clBlack);
477 frmMain.IdFTP.Site('CHMOD 744 gameinfo.txt');
480 AddStatus('Warning: CHMOD not supported.', clMaroon);
482 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'gameinfo.txt'));
486 frmMain.ggeAll.Progress := 3;
487 frmMain.ggeItem.Progress := 1;
489 { Create directories }
490 frmMain.ggeItem.Progress := 0;
491 frmMain.ggeItem.MaxValue := 3;
492 AddStatus('Creating directories...', clBlack);
493 if not eFound then begin
494 FTPMakeDir('addons');
495 frmMain.IdFTP.ChangeDir('addons');
496 frmMain.ggeItem.Progress := 1;
497 FTPMakeDir('metamod');
498 frmMain.IdFTP.ChangeDir('metamod');
499 frmMain.ggeItem.Progress := 2;
501 frmMain.ggeItem.Progress := 3;
506 frmMain.ggeAll.Progress := 4;
507 frmMain.ggeItem.Progress := 3;
508 { Upload metaplugins.ini }
509 frmMain.ggeAll.Progress := 4;
510 frmMain.ggeItem.MaxValue := 1;
511 frmMain.ggeItem.Progress := 0;
512 AddStatus('Uploading metaplugins.ini...', clBlack);
513 if CopyConfig then begin
515 eStr.SaveToFile(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini');
516 UploadFile(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini', 'metaplugins.ini');
517 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'metaplugins.ini'));
521 frmMain.ggeAll.Progress := 5;
522 frmMain.ggeItem.Progress := 1;
523 { Upload server.dll / server_i486.so }
524 frmMain.tmrSpeed.Enabled := True;
525 frmMain.ggeItem.Progress := 0;
526 frmMain.IdFTP.ChangeDir('bin');
527 if OS = osWindows then begin
528 AddStatus('Uploading server.dll...', clBlack);
529 frmMain.ggeItem.MaxValue := FSize(ExtractFilePath(ParamStr(0)) + 'server.dll');
530 UploadFile(ExtractFilePath(ParamStr(0)) + 'server.dll', 'server.dll');
533 AddStatus('Uploading server_i486.so...', clBlack);
534 frmMain.ggeItem.MaxValue := FSize(ExtractFilePath(ParamStr(0)) + 'server_i486.so');
535 UploadFile(ExtractFilePath(ParamStr(0)) + 'server_i486.so', 'server_i486.so');
537 { Remove created files }
538 AddStatus('Removing temporary files...', clBlack);
539 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'hl2launch.exe'));
540 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server.dll'));
541 DeleteFile(PChar(ExtractFilePath(ParamStr(0)) + 'server_i486.so'));
544 frmMain.IdFTP.Disconnect;
545 frmMain.ggeAll.Progress := frmMain.ggeAll.MaxValue;
546 frmMain.ggeItem.Progress := frmMain.ggeItem.MaxValue;
547 AddStatus('', clBlack, False);
548 AddStatus('Finished installation!', clBlack, False);
549 frmMain.tmrSpeed.Enabled := False;
552 Screen.Cursor := crDefault;
553 frmMain.cmdNext.Enabled := True;
554 frmMain.cmdCancel.Hide;
555 frmMain.cmdNext.Show;
556 frmMain.tmrSpeed.Enabled := False;
557 frmMain.Caption := Application.Title;