6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, TFlatRadioButtonUnit, StdCtrls, ComCtrls, mxFlatControls, JvPageList,
8 ExtCtrls, JvExControls, JvComponent, TFlatButtonUnit, jpeg, TFlatEditUnit,
9 TFlatGaugeUnit, ImgList, FileCtrl, Registry, CheckLst, TFlatComboBoxUnit,
10 TFlatCheckBoxUnit, IdBaseComponent, IdComponent, IdTCPConnection,
11 IdTCPClient, IdFTP, IdException, IdAntiFreezeBase, IdAntiFreeze,
12 IdIntercept, IdLogBase, IdLogFile, pngimage;
15 TfrmMain = class(TForm)
16 jplWizard: TJvPageList;
17 jspWelcome: TJvStandardPage;
21 cmdCancel: TFlatButton;
27 jspLicense: TJvStandardPage;
32 freLicense: TmxFlatRichEdit;
33 frbAgree: TFlatRadioButton;
34 ftbDontAgree: TFlatRadioButton;
35 jspInstallMethod: TJvStandardPage;
40 lblInstallMethod: TLabel;
41 pnlInstallMethod: TPanel;
42 frbDedicatedServer: TFlatRadioButton;
43 frbListenServer: TFlatRadioButton;
44 frbSelectMod: TFlatRadioButton;
45 frbFTP: TFlatRadioButton;
47 jspFTP: TJvStandardPage;
57 txtUserName: TFlatEdit;
58 txtPassword: TFlatEdit;
63 cmdConnect: TFlatButton;
65 trvDirectories: TTreeView;
67 jspInstallProgress: TJvStandardPage;
75 rtfDetails: TmxFlatRichEdit;
83 jspSelectMod: TJvStandardPage;
87 lblSelectModInfo: TLabel;
90 chkPassive: TFlatCheckBox;
93 optWindows: TFlatRadioButton;
94 optLinux: TFlatRadioButton;
96 cmdProxySettings: TFlatButton;
97 IdAntiFreeze: TIdAntiFreeze;
98 frbStandaloneServer: TFlatRadioButton;
100 IdLogFile: TIdLogFile;
103 FlatRadioButton1: TFlatRadioButton;
104 procedure jvwStepsCancelButtonClick(Sender: TObject);
105 procedure cmdCancelClick(Sender: TObject);
106 procedure cmdNextClick(Sender: TObject);
107 procedure CheckNext(Sender: TObject);
108 procedure cmdBackClick(Sender: TObject);
109 procedure cmdConnectClick(Sender: TObject);
110 procedure jplWizardChange(Sender: TObject);
111 procedure FormCreate(Sender: TObject);
112 procedure cmdProxySettingsClick(Sender: TObject);
113 procedure txtPortChange(Sender: TObject);
114 procedure trvDirectoriesExpanded(Sender: TObject; Node: TTreeNode);
115 procedure trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
116 procedure IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
117 const AWorkCount: Integer);
118 procedure FormClose(Sender: TObject; var Action: TCloseAction);
119 procedure tmrSpeedTimer(Sender: TObject);
120 procedure trvDirectoriesExpanding(Sender: TObject; Node: TTreeNode;
121 var AllowExpansion: Boolean);
122 procedure trvDirectoriesCollapsing(Sender: TObject; Node: TTreeNode;
123 var AllowCollapse: Boolean);
124 procedure trvModsClick(Sender: TObject);
125 procedure trvDirectoriesMouseDown(Sender: TObject;
126 Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
128 OldProgress: Integer;
129 CurrProgress: Integer;
131 procedure ExceptionHandler(Sender: TObject; E: Exception);
137 var VERSION: String = '<none>';
141 uses UnitFunctions, UnitfrmProxy, UnitInstall, UnitSelectModPath;
145 procedure TfrmMain.jvwStepsCancelButtonClick(Sender: TObject);
150 procedure TfrmMain.cmdCancelClick(Sender: TObject);
155 procedure TfrmMain.cmdNextClick(Sender: TObject);
157 eRegistry: TRegistry;
164 if jplWizard.ActivePage = jspFTP then begin
165 if not IdFTP.Connected then
167 eStr := TStringList.Create;
169 CurNode := trvDirectories.Selected;
170 if (Assigned(CurNode)) then begin
172 ePath := '/' + CurNode.Text + ePath;
173 CurNode := CurNode.Parent;
174 until (not Assigned(CurNode));
176 IdFTP.ChangeDir(ePath);
177 IdFTP.List(eStr, '', False);
178 if eStr.IndexOf('gameinfo.txt') = -1 then begin
179 MessageBox(Handle, 'Invalid directory. Please select your mod directory and try again.', PChar(Application.Title), MB_ICONWARNING);
187 trvDirectories.Enabled := False;
188 cmdConnect.Enabled := False;
189 optWindows.Enabled := False;
190 optLinux.Enabled := False;
191 Screen.Cursor := crHourGlass;
193 if optWindows.Checked then
198 jspInstallProgress.Show;
200 Screen.Cursor := crAppStart;
203 else if jplWizard.ActivePage = jspInstallProgress then
205 else if jplWizard.ActivePage = jspSelectMod then begin
207 if (frbDedicatedServer.Checked) or (frbStandaloneServer.Checked) then begin
208 ePath := trvMods.Selected.Text;
209 if ePath = 'Counter-Strike:Source' then
211 else if ePath = 'Day of Defeat:Source' then
215 ePath := 'SteamApps\' + trvMods.Selected.Parent.Text + '\source dedicated server\' + ePath;
217 if frbDedicatedServer.Checked then begin
218 if DirectoryExists(SteamPath + ePath) then begin
219 jspInstallProgress.Show;
220 InstallDedicated(IncludeTrailingPathDelimiter(SteamPath + ePath), True);
223 MessageBox(Handle, 'Error: The directory of the mod you selected doesn''t exist any more. Run Dedicated Server with the chosen mod and try again.', PChar(Application.Title), MB_ICONERROR);
229 if DirectoryExists(StandaloneServer + ePath) then begin
230 jspInstallProgress.Show;
231 InstallDedicated(IncludeTrailingPathDelimiter(StandaloneServer + ePath), False)
234 MessageBox(Handle, 'Error: The directory of the mod you selected doesn''t exist (any more). Run Half-Life Dedicated Server with the chosen mod again and restart.', PChar(Application.Title), MB_ICONERROR);
241 if frbListenServer.Checked then begin
242 ePath := trvMods.Selected.Text;
243 if ePath = 'Counter-Strike:Source' then
244 ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\counter-strike source\cstrike'
245 else if ePath = 'Half-Life 2 Deathmatch' then
246 ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\half-life 2 deathmatch\hl2mp'
248 ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\day of defeat source\dod';
250 if Pos(SteamPath, ePath) = 0 then
251 MessageBox(Handle, 'An error occured. Please report this bug to the Metamod:Source team and post a new thread on the forums of www.amxmodx.org.', PChar(Application.Title), MB_ICONSTOP)
253 if not FileExists(ePath + '\gameinfo.txt') then begin
254 MessageBox(Handle, 'You have to play this game once before installing Metamod:Source. Do that and try again.', PChar(Application.Title), MB_ICONWARNING);
258 jspInstallProgress.Show;
259 InstallListen(IncludeTrailingPathDelimiter(ePath));
264 else if jplWizard.ActivePage <> jspInstallMethod then
267 if frbDedicatedServer.Checked then begin // Dedicated Server
268 eRegistry := TRegistry.Create(KEY_READ);
270 eRegistry.RootKey := HKEY_CURRENT_USER;
271 if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
272 ePath := eRegistry.ReadString('SteamPath');
273 ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
276 ePath := ePath + 'SteamApps\';
277 if DirectoryExists(ePath) then begin
280 eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
281 for i := 0 to eStr.Count -1 do begin
282 CurNode := trvMods.Items.Add(nil, eStr[i]);
284 if DirectoryExists(ePath + eStr[i] + '\source dedicated server\cstrike') then
285 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
286 if DirectoryExists(ePath + eStr[i] + '\source dedicated server\dod') then
287 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
288 if DirectoryExists(ePath + eStr[i] + '\source dedicated server\hl2mp') then
289 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
291 if CurNode.Count = 0 then
294 CurNode.Expand(False);
298 trvMods.Selected := nil;
299 cmdNext.Enabled := False;
302 MessageBox(Handle, 'You have to run Dedicated Server once before installing Metamod:Source!', 'Error', MB_ICONWARNING);
305 MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
310 else if frbListenServer.Checked then begin // Listen Server
311 eRegistry := TRegistry.Create(KEY_READ);
313 eRegistry.RootKey := HKEY_CURRENT_USER;
314 if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
315 ePath := eRegistry.ReadString('SteamPath');
316 ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
319 ePath := ePath + 'SteamApps\';
320 if DirectoryExists(ePath) then begin
323 eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
324 for i := 0 to eStr.Count -1 do begin
325 CurNode := trvMods.Items.Add(nil, eStr[i]);
327 if DirectoryExists(ePath + eStr[i] + '\counter-strike source') then
328 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
329 if DirectoryExists(ePath + eStr[i] + '\day of defeat source') then
330 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
331 if DirectoryExists(ePath + eStr[i] + '\half-life 2 deathmatch') then
332 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
334 if CurNode.Count = 0 then
337 CurNode.Expand(False);
341 trvMods.Selected := nil;
342 cmdNext.Enabled := False;
345 MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
348 MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
353 else if frbStandaloneServer.Checked then begin // Standalone Server
354 eRegistry := TRegistry.Create;
356 eRegistry.RootKey := HKEY_CURRENT_USER;
357 if eRegistry.OpenKey('Software\Valve\HLServer', False) then begin
358 StandaloneServer := IncludeTrailingPathDelimiter(eRegistry.ReadString('InstallPath'));
359 if DirectoryExists(StandaloneServer + 'cstrike') then
360 trvMods.Items.Add(nil, 'Counter-Strike:Source');
361 if DirectoryExists(StandaloneServer + 'dod') then
362 trvMods.Items.Add(nil, 'Day of Defeat:Source');
363 if DirectoryExists(StandaloneServer + 'hl2mp') then
364 trvMods.Items.Add(nil, 'Half-Life 2 Deatmatch');
366 cmdNext.Enabled := False;
369 MessageBox(Handle, 'You haven''t installed Half-Life Dedicated Server yet!', 'Error', MB_ICONWARNING);
374 else if frbSelectMod.Checked then begin
376 if frmSelectModPath.ShowModal = mrOk then begin
377 jspInstallProgress.Show;
378 InstallCustom(IncludeTrailingPathDelimiter(frmSelectModPath.trvDirectory.SelectedFolder.PathName), osWindows);
381 else if frbFTP.Checked then // FTP
386 procedure TfrmMain.CheckNext(Sender: TObject);
388 cmdNext.Enabled := frbAgree.Checked;
391 procedure TfrmMain.cmdBackClick(Sender: TObject);
393 if jplWizard.ActivePage = jspFTP then
394 jspInstallMethod.Show
397 cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
401 procedure TfrmMain.cmdConnectClick(Sender: TObject);
407 if (Trim(txtHost.Text) = '') or (Trim(txtUsername.Text) = '') then
408 MessageBox(Handle, 'Please fill in each field!', PChar(Application.Title), MB_ICONWARNING)
409 else if cmdConnect.Caption = 'Connect' then begin
410 // ... design stuff ...
411 Screen.Cursor := crHourGlass;
412 cmdConnect.Enabled := False;
413 cmdProxySettings.Enabled := False;
414 txtHost.Enabled := False;
415 txtPort.Enabled := False;
416 txtUsername.Enabled := False;
417 txtPassword.Enabled := False;
418 chkPassive.Enabled := False;
419 cmdConnect.Caption := 'Connecting...';
420 // ... set values ...
421 IdFTP.Host := txtHost.Text;
422 IdFTP.Port := StrToInt(txtPort.Text);
423 IdFTP.Username := txtUsername.Text;
424 IdFTP.Passive := chkPassive.Checked;
425 IdFTP.Password := txtPassword.Text;
426 // ... connect and check values etc ...
428 IdFTP.Connect(True, 15000);
429 // ... get initial directory ...
430 Path := IdFTP.RetrieveCurrentDir;
431 // ... "fix" path ...
432 eStr := TStringList.Create;
433 eStr.Text := StringReplace(Path, '/', #13, [rfReplaceAll]);
434 for i := eStr.Count -1 downto 0 do begin
438 if (Copy(Path, Length(Path) -1, 1) <> '/') then
440 // ... connect successful, change captions ...
441 trvDirectories.Enabled := True;
442 cmdConnect.Enabled := True;
443 cmdConnect.Caption := 'Disconnect';
444 // ... change to / and create all the directories ...
446 if (Path <> '/') then begin
448 IdFTP.ChangeDir('/');
449 with GetAllDirs do begin
450 for i := 0 to Count -1 do begin
451 if (Assigned(CurNode)) then
452 trvDirectories.Items.AddChild(trvDirectories.Items.Add(nil, Strings[i]), 'Scanning...')
454 CurNode := trvDirectories.Items.Add(nil, Strings[i]);
455 trvDirectories.Items.AddChild(CurNode, 'Scanning...');
456 if (Pos('/' + CurNode.Text + '/', Path) = 0) then
462 IdFTP.ChangeDir(Path);
464 if (IdFTP.Connected) then
465 IdFTP.ChangeDir(Path)
470 // ... find directories in start path ...
471 if eStr.Count <> 0 then begin
472 for i := 0 to eStr.Count -1 do begin
473 if (not ((i = 0) and (Assigned(CurNode)))) then
474 CurNode := trvDirectories.Items.AddChild(CurNode, eStr[i]);
477 trvDirectories.Selected := CurNode;
479 // ... scan for directories ...
480 with GetAllDirs do begin
481 for i := 0 to Count -1 do
482 trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(CurNode, Strings[i]), 'Scanning...');
486 if Assigned(CurNode) then
487 CurNode.Expand(False);
489 on E: Exception do begin
490 // reset button properties
491 cmdConnect.Enabled := True;
492 txtHost.Enabled := True;
493 txtPort.Enabled := True;
494 txtUsername.Enabled := True;
495 txtPassword.Enabled := True;
496 chkPassive.Enabled := True;
497 cmdProxySettings.Enabled := True;
498 cmdNext.Enabled := False;
499 cmdConnect.Caption := 'Connect';
501 if Pos('Login incorrect.', E.Message) <> 0 then begin // login failed
502 MessageBox(Handle, 'Login incorrect. Check your FTP settings and try again.', PChar(Application.Title), MB_ICONWARNING);
503 txtUsername.SetFocus;
504 txtUsername.SelectAll;
506 else if Pos('Host not found.', E.Message) <> 0 then begin // host not found
507 MessageBox(Handle, 'The entered host couldn''t be found. Check your settings and try again.', PChar(Application.Title), MB_ICONWARNING);
511 else if Pos('Connection refused.', E.Message) <> 0 then begin // wrong port (?)
512 MessageBox(Handle, 'The host refused the connection. Check your port and try again.', PChar(Application.Title), MB_ICONWARNING);
516 else if E is EIdProtocolReplyError then begin // wrong port
517 MessageBox(Handle, 'The port you entered is definitely wrong. Check it and try again.', PChar(Application.Title), MB_ICONWARNING);
522 MessageBox(Handle, PChar(E.Message), PChar(Application.Title), MB_ICONWARNING); // unknown error
523 // ... connect failed, leave procedure ...
524 Screen.Cursor := crDefault;
529 Screen.Cursor := crDefault;
532 Screen.Cursor := crHourGlass;
534 trvDirectories.Items.Clear;
535 trvDirectories.Enabled := False;
536 cmdConnect.Enabled := True;
537 cmdProxySettings.Enabled := True;
538 txtHost.Enabled := True;
539 txtPort.Enabled := True;
540 txtUsername.Enabled := True;
541 txtPassword.Enabled := True;
542 chkPassive.Enabled := True;
543 cmdConnect.Caption := 'Connect';
544 cmdNext.Enabled := False;
545 Screen.Cursor := crDefault;
549 procedure TfrmMain.jplWizardChange(Sender: TObject);
551 if (jplWizard.ActivePage = jspInstallProgress) then begin
552 cmdNext.Caption := '&Finish';
553 cmdNext.Enabled := False;
554 cmdBack.Visible := False;
557 cmdNext.Caption := '&Next >';
558 cmdNext.Enabled := True;
559 cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
562 if (jplWizard.ActivePage = jspLicense) then
563 cmdNext.Enabled := frbAgree.Checked;
565 if (jplWizard.ActivePage = jspFTP) then
566 cmdNext.Enabled := False;
569 procedure TfrmMain.FormCreate(Sender: TObject);
571 if LowerCase(ParamStr(1)) = '-logftp' then begin
572 MessageBox(Handle, 'FTP installation will be logged to FTP.log!', PChar(Application.Title), MB_ICONINFORMATION);
573 IdLogFile.Filename := ExtractFilePath(ParamStr(0)) + 'FTP.log';
574 IdLogFile.Active := True;
580 procedure TfrmMain.cmdProxySettingsClick(Sender: TObject);
583 // Apply Proxy Settings
584 case frmProxy.cboProxy.ItemIndex of
585 0: IdFTP.ProxySettings.ProxyType := fpcmNone; // none
586 1: IdFTP.ProxySettings.ProxyType := fpcmHttpProxyWithFtp; // HTTP Proxy with FTP
587 2: IdFTP.ProxySettings.ProxyType := fpcmOpen; // Open
588 3: IdFTP.ProxySettings.ProxyType := fpcmSite; // Site
589 4: IdFTP.ProxySettings.ProxyType := fpcmTransparent; // Transparent
590 5: IdFTP.ProxySettings.ProxyType := fpcmUserPass; // User (Password)
591 6: IdFTP.ProxySettings.ProxyType := fpcmUserSite; // User (Site)
594 IdFTP.ProxySettings.Host := frmProxy.txtHost.Text;
595 IdFTP.ProxySettings.UserName := frmProxy.txtPort.Text;
596 IdFTP.ProxySettings.Password := frmProxy.txtPassword.Text;
597 IdFTP.ProxySettings.Port := StrToInt(frmProxy.txtPort.Text);
600 procedure TfrmMain.txtPortChange(Sender: TObject);
603 if txtPort.Text = '' then
606 // check if value is numeric...
607 for i := Length(txtPort.Text) downto 1 do begin
608 if Pos(txtPort.Text[i], '0123456789') = 0 then begin
609 txtPort.Text := '21';
610 txtPort.SelStart := 4;
617 procedure TfrmMain.trvDirectoriesExpanded(Sender: TObject;
623 if Node.Item[0].Text = 'Scanning...' then begin // no directories added yet
624 Screen.Cursor := crHourGlass;
629 ePath := '/' + CurNode.Text + ePath;
630 CurNode := CurNode.Parent;
631 until (not Assigned(CurNode));
632 // change dir and add directories in it
635 IdFTP.ChangeDir(ePath);
636 with GetAllDirs do begin
638 for i := 0 to Count -1 do begin
639 trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(Node, Strings[i]), 'Scanning...');
644 Application.ProcessMessages;
646 Screen.Cursor := crDefault;
650 procedure TfrmMain.trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
652 cmdNext.Enabled := Assigned(trvDirectories.Selected);
655 procedure TfrmMain.IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
656 const AWorkCount: Integer);
658 if AWorkCount > 15 then begin
659 ggeItem.Progress := AWorkCount;
660 CurrProgress := AWorkCount;
666 Application.ProcessMessages;
669 procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
671 if (jplWizard.ActivePage = jspFTP) and (IdFTP.Connected) then
673 if (jplWizard.ActivePage = jspInstallProgress) and (ggeAll.Progress <> ggeAll.MaxValue) and (not Cancel) then begin
674 if MessageBox(Handle, 'Do you really want to cancel the installation?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
675 Screen.Cursor := crDefault;
676 Application.OnException := ExceptionHandler;
678 if IdFTP.Connected then
686 procedure TfrmMain.ExceptionHandler(Sender: TObject; E: Exception);
688 // IF any exceptions were raised after close, nobody would want them so leave this empty
691 procedure TfrmMain.tmrSpeedTimer(Sender: TObject);
693 Caption := CalcSpeed(OldProgress, CurrProgress);
694 OldProgress := CurrProgress;
697 procedure TfrmMain.trvDirectoriesExpanding(Sender: TObject;
698 Node: TTreeNode; var AllowExpansion: Boolean);
700 Node.ImageIndex := 1;
701 Node.SelectedIndex := 1;
704 procedure TfrmMain.trvDirectoriesCollapsing(Sender: TObject;
705 Node: TTreeNode; var AllowCollapse: Boolean);
707 Node.ImageIndex := 0;
708 Node.SelectedIndex := 0;
711 procedure TfrmMain.trvModsClick(Sender: TObject);
713 if Assigned(trvMods.Selected) then
714 cmdNext.Enabled := Assigned(trvMods.Selected.Parent)
716 cmdNext.Enabled := False;
719 procedure TfrmMain.trvDirectoriesMouseDown(Sender: TObject;
720 Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
723 Node := trvDirectories.GetNodeAt(X, Y);
724 if (Assigned(Node)) then begin
725 if (Node.DisplayRect(True).Right < X) then
726 trvDirectories.Selected := nil;