unit uEmulatorsSetup;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls, IniFiles, Buttons, Menus, FileCtrl, ToolWin,
  GR32_Image, uCommon;

type
  TFormEmulatorsSetup = class(TForm)
    ButtonOk: TButton;
    ButtonCancel: TButton;
    LabelSystemDescription: TLabel;
    TopImage: TImage32;
    LabelArcade_exec: TLabel;
    LabelArcade_checksum: TLabel;
    LabelArcade_versioninfo: TLabel;
    ButtonSetOptions_mame: TButton;
    ArcadeSystemSelector: TComboBoxEx;
    Arcade_exec: TEdit;
    ButtonBrowserArcade_exec: TButton;
    ButtonScanArcade_checksum: TButton;
    ButtonScanArcade_versioninfo: TButton;
    Arcade_checksum: TEdit;
    Arcade_versioninfo: TEdit;
    ButtonClearArcade: TButton;
    procedure ButtonCancelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ButtonOkClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure ButtonSetOptions_mameClick(Sender: TObject);
    procedure ArcadeSystemSelectorSelect(Sender: TObject);
    procedure Arcade_execChange(Sender: TObject);
    procedure Arcade_versioninfoChange(Sender: TObject);
    procedure Arcade_checksumChange(Sender: TObject);
    procedure ButtonBrowserArcade_execClick(Sender: TObject);
    procedure ButtonScanArcade_versioninfoClick(Sender: TObject);
    procedure ButtonScanArcade_checksumClick(Sender: TObject);
    procedure ButtonClearArcadeClick(Sender: TObject);
  private
    { Private declarations }
    newEmulatorFile,
    newEmulatorVersion,
    newEmulatorChecksum: packed array[1..MaxArcadeSystems] of String;

    function  VerifyEmulator(SystemID: ShortInt): Boolean;
    procedure GetEmulatorDefaultDescription(SystemID: Integer);
    procedure SetSystemInfo(sysID: ShortInt);
  public
    Folders: THashedStringList;
    { Public declarations }
  end;

var
  FormEmulatorsSetup: TFormEmulatorsSetup;

implementation

uses uMain, uPreferences, uFilesUtil, uStatus;

{$R *.DFM}

procedure TFormEmulatorsSetup.GetEmulatorDefaultDescription(SystemID: Integer);
begin
  FormMain.GetEmulatorVersion(SystemID, newEmulatorFile[SystemID], newEmulatorVersion[SystemID]);
end;

procedure TFormEmulatorsSetup.ButtonCancelClick(Sender: TObject);
begin
  FormMain.MainMenu.Tag:= 1;
  Close;
end;

procedure TFormEmulatorsSetup.FormShow(Sender: TObject);
var
  Loop: Integer;
begin
  FormMain.LoadTopBar(TopImage, 'emulators.png');
  FormMain.FillEmuDescriptionsSelector(ArcadeSystemSelector);

  ButtonCancel.Enabled:= FormMain.AbortExecution;

  for Loop:= 1 to MaxArcadeSystems do
  begin
    newEmulatorFile[Loop]:= FormMain.EmulatorFile[Loop];
    newEmulatorVersion[Loop]:= FormMain.EmulatorVersion[Loop];
    newEmulatorChecksum[Loop]:= FormMain.EmulatorChecksum[Loop];
  end;
  SetComboBoxEx(ArcadeSystemSelector, ArcadeSystemSelector.Tag-1);
  FormEmulatorsSetup.Tag:= 1;
end;

function TFormEmulatorsSetup.VerifyEmulator(SystemID: ShortInt): Boolean;
var
  emuFile: String;
begin
  Application.ProcessMessages;
  emuFile:= newEmulatorFile[SystemID];
  Result:= (emuFile <> '') and (FileExists(emuFile));
  if not Result then
     Exit;
  if FormStatus.Visible then
     begin
       FormStatus.BringToFront;
       FormStatus.LabelMessage.Caption:= 'Verifying '+FormMain.GetSystemIniSection(SystemID)+' info.';
       FormStatus.Refresh;
     end;

  if newEmulatorChecksum[SystemID] <> FormMain.EmulatorChecksum[SystemID] then
     begin
       if SystemID in [1, 4, 5, 6] then
          Result:= FormMain.CheckMAMEIniFile(emuFile, SystemID)
       else
          Result:= True;

       if Result then
          begin
            if (newEmulatorVersion[SystemID] = FormMain.EmulatorVersion[SystemID]) or
               (newEmulatorVersion[SystemID] = '') then
               GetEmulatorDefaultDescription(SystemID);
          end;
     end;
end;

procedure TFormEmulatorsSetup.ButtonOkClick(Sender: TObject);
var
  Loop: Integer;
begin
  if (newEmulatorFile[1] = '') and (newEmulatorFile[2] = '') and
     (newEmulatorFile[3] = '') and (newEmulatorFile[4] = '') and
     (newEmulatorFile[5] = '') and (newEmulatorFile[6] = '') then
     begin
       GenerateMessage('Error', 'No emulator filenames were selected. '+
                       'Please select at least one emulator or hit cancel button to terminate '+
                       'the application...', 2);
       Exit;
     end;
  FormMain.MainMenu.Tag:= 0;
  FormMain.NoScreenshotLoaded:= False;

  FormEmulatorsSetup.Hide;
  FormStatus.Show;
  FormStatus.LabelStatusType.Caption:= 'Analizing Emulators';

  FormMain.MenuMAMEDefaultSettings.Enabled:= VerifyEmulator(1);
  FormMain.MenuZiNcDefaultSettings.Enabled:= VerifyEmulator(2);
  FormMain.MenuDaphneDefaultSettings.Enabled:= VerifyEmulator(3);
  FormMain.MenuAGEMAMEDefaultSettings.Enabled:= VerifyEmulator(4);
  FormMain.MenuPinMAMEDefaultSettings.Enabled:= VerifyEmulator(5);
  FormMain.MenuHazeMDDefaultSettings.Enabled:= VerifyEmulator(6);

  for Loop:= 1 to MaxArcadeSystems do
  begin
    FormMain.EmulatorFile[Loop]:= newEmulatorFile[Loop];
    FormMain.EmulatorVersion[Loop]:= newEmulatorVersion[Loop];
    FormMain.EmulatorChecksum[Loop]:= newEmulatorChecksum[Loop];
  end;
  FormStatus.Close;
  Close;
end;

procedure TFormEmulatorsSetup.FormKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #27 then
     ButtonCancel.OnClick(Self);
end;

procedure TFormEmulatorsSetup.ButtonSetOptions_mameClick(Sender: TObject);
begin
  case ArcadeSystemSelector.Tag of
    1, 4, 5, 6: FormMain.CallEmulatorOptions(newEmulatorFile[ArcadeSystemSelector.Tag], ArcadeSystemSelector.Tag);
    2: FormMain.MenuZiNcDefaultSettings.Click;
    3: FormMain.MenuDaphneDefaultSettings.Click;
  end;
end;

procedure TFormEmulatorsSetup.SetSystemInfo(sysID: ShortInt);
begin
  LabelSystemDescription.Caption:= FormMain.GetEmuFullDescription(sysID)+
                                   #13#10+LabelSystemDescription.Hint;
  Arcade_exec.Text:= newEmulatorFile[sysID];
  Arcade_versioninfo.Text:= newEmulatorVersion[sysID];
  Arcade_checksum.Text:= newEmulatorChecksum[sysID];
  ButtonSetOptions_mame.Visible:= sysID in [1, 2, 4, 5, 6];
end;

procedure TFormEmulatorsSetup.ArcadeSystemSelectorSelect(Sender: TObject);
begin
  ArcadeSystemSelector.Tag:= ArcadeSystemSelector.ItemsEx[ArcadeSystemSelector.ItemIndex].ImageIndex;
  SetSystemInfo(ArcadeSystemSelector.Tag);
end;

procedure TFormEmulatorsSetup.Arcade_execChange(Sender: TObject);
begin
  newEmulatorFile[ArcadeSystemSelector.Tag]:= Arcade_exec.Text;
end;

procedure TFormEmulatorsSetup.Arcade_versioninfoChange(Sender: TObject);
begin
  newEmulatorVersion[ArcadeSystemSelector.Tag]:= Arcade_versioninfo.Text;
end;

procedure TFormEmulatorsSetup.Arcade_checksumChange(Sender: TObject);
begin
  newEmulatorChecksum[ArcadeSystemSelector.Tag]:= Arcade_checksum.Text;
end;

procedure TFormEmulatorsSetup.ButtonBrowserArcade_execClick(
  Sender: TObject);
begin
  FormMain.DialogOpenFile(0, 'Select a '+FormMain.GetSystemIniSection(ArcadeSystemSelector.Tag)+
                          ' filename', Arcade_exec, False, False);
  ButtonScanArcade_versioninfo.Click;
  ButtonScanArcade_checksum.Click;
end;

procedure TFormEmulatorsSetup.ButtonScanArcade_versioninfoClick(
  Sender: TObject);
begin
  if FileExists(Arcade_exec.Text) then
     begin
       GetEmulatorDefaultDescription(ArcadeSystemSelector.Tag);
       Arcade_versioninfo.Text:= newEmulatorVersion[ArcadeSystemSelector.Tag];
     end;
end;

procedure TFormEmulatorsSetup.ButtonScanArcade_checksumClick(
  Sender: TObject);
begin
  Arcade_checksum.Text:= GetMD5_Value(Arcade_exec.Text);
end;

procedure TFormEmulatorsSetup.ButtonClearArcadeClick(Sender: TObject);
begin
  Arcade_exec.Clear;
  Arcade_versioninfo.Clear;
  Arcade_checksum.Clear;
end;

end.

