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;
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);
126 OldProgress: Integer;
127 CurrProgress: Integer;
129 procedure ExceptionHandler(Sender: TObject; E: Exception);
135 var VERSION: String = '1.2.4';
139 uses UnitFunctions, UnitfrmProxy, UnitInstall, UnitSelectModPath;
143 procedure TfrmMain.jvwStepsCancelButtonClick(Sender: TObject);
148 procedure TfrmMain.cmdCancelClick(Sender: TObject);
153 procedure TfrmMain.cmdNextClick(Sender: TObject);
155 eRegistry: TRegistry;
162 if jplWizard.ActivePage = jspFTP then begin
163 if not IdFTP.Connected then
165 eStr := TStringList.Create;
167 CurNode := trvDirectories.Selected;
169 ePath := '/' + CurNode.Text + ePath;
170 CurNode := CurNode.Parent;
171 until (not Assigned(CurNode));
172 IdFTP.ChangeDir(ePath);
173 IdFTP.List(eStr, '', False);
174 if eStr.IndexOf('gameinfo.txt') = -1 then begin
175 MessageBox(Handle, 'Invalid directory. Please select your mod directory and try again.', PChar(Application.Title), MB_ICONWARNING);
183 trvDirectories.Enabled := False;
184 cmdConnect.Enabled := False;
185 optWindows.Enabled := False;
186 optLinux.Enabled := False;
187 Screen.Cursor := crHourGlass;
189 if optWindows.Checked then
194 jspInstallProgress.Show;
196 Screen.Cursor := crAppStart;
199 else if jplWizard.ActivePage = jspInstallProgress then
201 else if jplWizard.ActivePage = jspSelectMod then begin
203 if (frbDedicatedServer.Checked) or (frbStandaloneServer.Checked) then begin
204 ePath := trvMods.Selected.Text;
205 if ePath = 'Counter-Strike:Source' then
207 else if ePath = 'Day of Defeat:Source' then
211 ePath := 'SteamApps\' + trvMods.Selected.Parent.Text + '\source dedicated server\' + ePath;
213 if frbDedicatedServer.Checked then begin
214 if DirectoryExists(SteamPath + ePath) then begin
215 jspInstallProgress.Show;
216 InstallDedicated(IncludeTrailingPathDelimiter(SteamPath + ePath), True);
219 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);
225 if DirectoryExists(StandaloneServer + ePath) then begin
226 jspInstallProgress.Show;
227 InstallDedicated(IncludeTrailingPathDelimiter(StandaloneServer + ePath), False)
230 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);
237 if frbListenServer.Checked then begin
238 ePath := trvMods.Selected.Text;
239 if ePath = 'Counter-Strike:Source' then
240 ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\counter-strike source\cstrike'
241 else if ePath = 'Half-Life 2 Deathmatch' then
242 ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\half-life 2 deathmatch\hl2mp'
244 ePath := SteamPath + 'SteamApps\' + trvMods.Selected.Parent.Text + '\day of defeat source\dod';
246 if Pos(SteamPath, ePath) = 0 then
247 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)
249 if not FileExists(ePath + '\gameinfo.txt') then begin
250 MessageBox(Handle, 'You have to play this game once before installing Metamod:Source. Do that and try again.', PChar(Application.Title), MB_ICONWARNING);
254 jspInstallProgress.Show;
255 InstallListen(IncludeTrailingPathDelimiter(ePath));
260 else if jplWizard.ActivePage <> jspInstallMethod then
263 if frbDedicatedServer.Checked then begin // Dedicated Server
264 eRegistry := TRegistry.Create(KEY_READ);
266 eRegistry.RootKey := HKEY_CURRENT_USER;
267 if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
268 ePath := eRegistry.ReadString('SteamPath');
269 ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
272 ePath := ePath + 'SteamApps\';
273 if DirectoryExists(ePath) then begin
276 eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
277 for i := 0 to eStr.Count -1 do begin
278 CurNode := trvMods.Items.Add(nil, eStr[i]);
280 if DirectoryExists(ePath + eStr[i] + '\source dedicated server\cstrike') then
281 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
282 if DirectoryExists(ePath + eStr[i] + '\source dedicated server\dod') then
283 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
284 if DirectoryExists(ePath + eStr[i] + '\source dedicated server\hl2mp') then
285 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
287 if CurNode.Count = 0 then
290 CurNode.Expand(False);
294 trvMods.Selected := nil;
295 cmdNext.Enabled := False;
298 MessageBox(Handle, 'You have to run Dedicated Server once before installing Metamod:Source!', 'Error', MB_ICONWARNING);
301 MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
306 else if frbListenServer.Checked then begin // Listen Server
307 eRegistry := TRegistry.Create(KEY_READ);
309 eRegistry.RootKey := HKEY_CURRENT_USER;
310 if eRegistry.OpenKey('Software\Valve\Steam', False) then begin
311 ePath := eRegistry.ReadString('SteamPath');
312 ePath := IncludeTrailingPathDelimiter(StringReplace(ePath, '/', '\', [rfReplaceAll]));
315 ePath := ePath + 'SteamApps\';
316 if DirectoryExists(ePath) then begin
319 eStr := GetAllFiles(ePath + '*.*', faDirectory, False, True, False);
320 for i := 0 to eStr.Count -1 do begin
321 CurNode := trvMods.Items.Add(nil, eStr[i]);
323 if DirectoryExists(ePath + eStr[i] + '\counter-strike source') then
324 trvMods.Items.AddChild(CurNode, 'Counter-Strike:Source');
325 if DirectoryExists(ePath + eStr[i] + '\day of defeat source') then
326 trvMods.Items.AddChild(CurNode, 'Day of Defeat:Source');
327 if DirectoryExists(ePath + eStr[i] + '\half-life 2 deathmatch') then
328 trvMods.Items.AddChild(CurNode, 'Half-Life 2 Deatmatch');
330 if CurNode.Count = 0 then
333 CurNode.Expand(False);
337 trvMods.Selected := nil;
338 cmdNext.Enabled := False;
341 MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
344 MessageBox(Handle, 'You haven''t installed Steam yet! Download it at www.steampowered.com, install Dedicated Server and try again.', 'Error', MB_ICONWARNING);
349 else if frbStandaloneServer.Checked then begin // Standalone Server
350 eRegistry := TRegistry.Create;
352 eRegistry.RootKey := HKEY_CURRENT_USER;
353 if eRegistry.OpenKey('Software\Valve\HLServer', False) then begin
354 StandaloneServer := IncludeTrailingPathDelimiter(eRegistry.ReadString('InstallPath'));
355 if DirectoryExists(StandaloneServer + 'cstrike') then
356 trvMods.Items.Add(nil, 'Counter-Strike:Source');
357 if DirectoryExists(StandaloneServer + 'dod') then
358 trvMods.Items.Add(nil, 'Day of Defeat:Source');
359 if DirectoryExists(StandaloneServer + 'hl2mp') then
360 trvMods.Items.Add(nil, 'Half-Life 2 Deatmatch');
362 cmdNext.Enabled := False;
365 MessageBox(Handle, 'You haven''t installed Half-Life Dedicated Server yet!', 'Error', MB_ICONWARNING);
370 else if frbSelectMod.Checked then begin
372 if frmSelectModPath.ShowModal = mrOk then begin
373 jspInstallProgress.Show;
374 InstallCustom(IncludeTrailingPathDelimiter(frmSelectModPath.trvDirectory.SelectedFolder.PathName), osWindows);
377 else if frbFTP.Checked then // FTP
382 procedure TfrmMain.CheckNext(Sender: TObject);
384 cmdNext.Enabled := frbAgree.Checked;
387 procedure TfrmMain.cmdBackClick(Sender: TObject);
389 if jplWizard.ActivePage = jspFTP then
390 jspInstallMethod.Show
393 cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
397 procedure TfrmMain.cmdConnectClick(Sender: TObject);
402 if (Trim(txtHost.Text) = '') or (Trim(txtUsername.Text) = '') then
403 MessageBox(Handle, 'Please fill in each field!', PChar(Application.Title), MB_ICONWARNING)
404 else if cmdConnect.Caption = 'Connect' then begin
405 // ... design stuff ...
406 Screen.Cursor := crHourGlass;
407 cmdConnect.Enabled := False;
408 cmdProxySettings.Enabled := False;
409 txtHost.Enabled := False;
410 txtPort.Enabled := False;
411 txtUsername.Enabled := False;
412 txtPassword.Enabled := False;
413 chkPassive.Enabled := False;
414 cmdConnect.Caption := 'Connecting...';
415 // ... set values ...
416 IdFTP.Host := txtHost.Text;
417 IdFTP.Port := StrToInt(txtPort.Text);
418 IdFTP.Username := txtUsername.Text;
419 IdFTP.Passive := chkPassive.Checked;
420 IdFTP.Password := txtPassword.Text;
421 // ... connect and check values etc ...
423 IdFTP.Connect(True, 15000);
424 // ... scan for initial directory ...
425 eStr := TStringList.Create;
426 eStr.Text := StringReplace(IdFTP.RetrieveCurrentDir, '/', #13, [rfReplaceAll]);
427 for i := eStr.Count -1 downto 0 do begin
431 // ... connect successful, change captions ...
432 trvDirectories.Enabled := True;
433 cmdConnect.Enabled := True;
434 cmdConnect.Caption := 'Disconnect';
437 if eStr.Count <> 0 then begin
438 for i := 0 to eStr.Count -1 do
439 CurNode := trvDirectories.Items.AddChild(CurNode, eStr[i]);
441 if trvDirectories.Items.Count <> 0 then
442 trvDirectories.Items.Item[0].Expand(True);
445 // ... scan for directories ...
446 with GetAllDirs do begin
447 for i := 0 to Count -1 do
448 trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(CurNode, Strings[i]), 'Scanning...');
452 if Assigned(CurNode) then
453 CurNode.Expand(False);
455 on E: Exception do begin
456 // reset button properties
457 cmdConnect.Enabled := True;
458 txtHost.Enabled := True;
459 txtPort.Enabled := True;
460 txtUsername.Enabled := True;
461 txtPassword.Enabled := True;
462 chkPassive.Enabled := True;
463 cmdProxySettings.Enabled := True;
464 cmdNext.Enabled := False;
465 cmdConnect.Caption := 'Connect';
467 if Pos('Login incorrect.', E.Message) <> 0 then begin // login failed
468 MessageBox(Handle, 'Login incorrect. Check your FTP settings and try again.', PChar(Application.Title), MB_ICONWARNING);
469 txtUsername.SetFocus;
470 txtUsername.SelectAll;
472 else if Pos('Host not found.', E.Message) <> 0 then begin // host not found
473 MessageBox(Handle, 'The entered host couldn''t be found. Check your settings and try again.', PChar(Application.Title), MB_ICONWARNING);
477 else if Pos('Connection refused.', E.Message) <> 0 then begin // wrong port (?)
478 MessageBox(Handle, 'The host refused the connection. Check your port and try again.', PChar(Application.Title), MB_ICONWARNING);
482 else if E is EIdProtocolReplyError then begin // wrong port
483 MessageBox(Handle, 'The port you entered is definitely wrong. Check it and try again.', PChar(Application.Title), MB_ICONWARNING);
488 MessageBox(Handle, PChar(E.Message), PChar(Application.Title), MB_ICONWARNING); // unknown error
489 // ... connect failed, leave procedure ...
490 Screen.Cursor := crDefault;
495 Screen.Cursor := crDefault;
498 Screen.Cursor := crHourGlass;
500 trvDirectories.Items.Clear;
501 trvDirectories.Enabled := False;
502 cmdConnect.Enabled := True;
503 cmdProxySettings.Enabled := True;
504 txtHost.Enabled := True;
505 txtPort.Enabled := True;
506 txtUsername.Enabled := True;
507 txtPassword.Enabled := True;
508 chkPassive.Enabled := True;
509 cmdConnect.Caption := 'Connect';
510 cmdNext.Enabled := False;
511 Screen.Cursor := crDefault;
515 procedure TfrmMain.jplWizardChange(Sender: TObject);
517 if (jplWizard.ActivePage = jspInstallProgress) then begin
518 cmdNext.Caption := '&Finish';
519 cmdNext.Enabled := False;
520 cmdBack.Visible := False;
523 cmdNext.Caption := '&Next >';
524 cmdNext.Enabled := True;
525 cmdBack.Visible := jplWizard.ActivePageIndex <> 0;
528 if (jplWizard.ActivePage = jspLicense) then
529 cmdNext.Enabled := frbAgree.Checked;
531 if (jplWizard.ActivePage = jspFTP) then
532 cmdNext.Enabled := False;
535 procedure TfrmMain.FormCreate(Sender: TObject);
537 if LowerCase(ParamStr(1)) = '-logftp' then begin
538 MessageBox(Handle, 'FTP installation will be logged to FTP.log!', PChar(Application.Title), MB_ICONINFORMATION);
539 IdLogFile.Filename := ExtractFilePath(ParamStr(0)) + 'FTP.log';
540 IdLogFile.Active := True;
546 procedure TfrmMain.cmdProxySettingsClick(Sender: TObject);
549 // Apply Proxy Settings
550 case frmProxy.cboProxy.ItemIndex of
551 0: IdFTP.ProxySettings.ProxyType := fpcmNone; // none
552 1: IdFTP.ProxySettings.ProxyType := fpcmHttpProxyWithFtp; // HTTP Proxy with FTP
553 2: IdFTP.ProxySettings.ProxyType := fpcmOpen; // Open
554 3: IdFTP.ProxySettings.ProxyType := fpcmSite; // Site
555 4: IdFTP.ProxySettings.ProxyType := fpcmTransparent; // Transparent
556 5: IdFTP.ProxySettings.ProxyType := fpcmUserPass; // User (Password)
557 6: IdFTP.ProxySettings.ProxyType := fpcmUserSite; // User (Site)
560 IdFTP.ProxySettings.Host := frmProxy.txtHost.Text;
561 IdFTP.ProxySettings.UserName := frmProxy.txtPort.Text;
562 IdFTP.ProxySettings.Password := frmProxy.txtPassword.Text;
563 IdFTP.ProxySettings.Port := StrToInt(frmProxy.txtPort.Text);
566 procedure TfrmMain.txtPortChange(Sender: TObject);
569 if txtPort.Text = '' then
572 // check if value is numeric...
573 for i := Length(txtPort.Text) downto 1 do begin
574 if Pos(txtPort.Text[i], '0123456789') = 0 then begin
575 txtPort.Text := '21';
576 txtPort.SelStart := 4;
583 procedure TfrmMain.trvDirectoriesExpanded(Sender: TObject;
589 if Node.Item[0].Text = 'Scanning...' then begin // no directories added yet
590 Screen.Cursor := crHourGlass;
595 ePath := '/' + CurNode.Text + ePath;
596 CurNode := CurNode.Parent;
597 until (not Assigned(CurNode));
598 // change dir and add directories in it
601 IdFTP.ChangeDir(ePath);
602 with GetAllDirs do begin
604 for i := 0 to Count -1 do begin
605 trvDirectories.Items.AddChild(trvDirectories.Items.AddChild(Node, Strings[i]), 'Scanning...');
610 Application.ProcessMessages;
612 Screen.Cursor := crDefault;
616 procedure TfrmMain.trvDirectoriesChange(Sender: TObject; Node: TTreeNode);
618 cmdNext.Enabled := Assigned(trvDirectories.Selected);
621 procedure TfrmMain.IdFTPWork(Sender: TObject; AWorkMode: TWorkMode;
622 const AWorkCount: Integer);
624 if AWorkCount > 15 then begin
625 ggeItem.Progress := AWorkCount;
626 CurrProgress := AWorkCount;
632 Application.ProcessMessages;
635 procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
637 if (jplWizard.ActivePage = jspFTP) and (IdFTP.Connected) then
639 if (jplWizard.ActivePage = jspInstallProgress) and (ggeAll.Progress <> ggeAll.MaxValue) and (not Cancel) then begin
640 if MessageBox(Handle, 'Do you really want to cancel the installation?', PChar(Application.Title), MB_ICONQUESTION + MB_YESNO) = mrYes then begin
641 Screen.Cursor := crDefault;
642 Application.OnException := ExceptionHandler;
644 if IdFTP.Connected then
652 procedure TfrmMain.ExceptionHandler(Sender: TObject; E: Exception);
654 // IF any exceptions were raised after close, nobody would want them so leave this empty
657 procedure TfrmMain.tmrSpeedTimer(Sender: TObject);
659 Caption := CalcSpeed(OldProgress, CurrProgress);
660 OldProgress := CurrProgress;
663 procedure TfrmMain.trvDirectoriesExpanding(Sender: TObject;
664 Node: TTreeNode; var AllowExpansion: Boolean);
666 Node.ImageIndex := 1;
667 Node.SelectedIndex := 1;
670 procedure TfrmMain.trvDirectoriesCollapsing(Sender: TObject;
671 Node: TTreeNode; var AllowCollapse: Boolean);
673 Node.ImageIndex := 0;
674 Node.SelectedIndex := 0;
677 procedure TfrmMain.trvModsClick(Sender: TObject);
679 if Assigned(trvMods.Selected) then
680 cmdNext.Enabled := Assigned(trvMods.Selected.Parent)
682 cmdNext.Enabled := False;