unit MainU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  ComCtrls, Menus, XPMan, jpeg, ExtCtrls;

type
  TfrmMain = class(TForm)
    StatusBar1: TStatusBar;
    mnMain: TMainMenu;
    mmFile: TMenuItem;
    mmExit: TMenuItem;
    mmHelp: TMenuItem;
    mmAbout: TMenuItem;
    mmInstruction: TMenuItem;
    mmRegister: TMenuItem;
    N1: TMenuItem;
    XPManifest1: TXPManifest;
    mmAdditional: TMenuItem;
    mmInlineMe: TMenuItem;
    Image1: TImage;
    mmInlineMeVM: TMenuItem;
    N2: TMenuItem;
    mmDebugCheck: TMenuItem;
    mmVMPresent: TMenuItem;
    mmValidImageCRC: TMenuItem;
    N3: TMenuItem;
    mmCryptedString: TMenuItem;
    procedure mmExitClick(Sender: TObject);
    procedure mmRegisterClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure mmInstructionClick(Sender: TObject);
    procedure mmAboutClick(Sender: TObject);
    procedure mmInlineMeClick(Sender: TObject);
    procedure mmDebugCheckClick(Sender: TObject);
    procedure mmVMPresentClick(Sender: TObject);
    procedure mmValidImageCRCClick(Sender: TObject);
    procedure mmCryptedStringClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

uses RegisterU, frmAboutUnit, VMProtectSDK;

{$R *.dfm}

procedure TfrmMain.mmExitClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.mmRegisterClick(Sender: TObject);
begin
  frmRegistration.ShowModal;
end;

procedure TfrmMain.mmAboutClick(Sender: TObject);
begin
  Application.CreateForm(TfrmAbout, frmAbout);
  frmMain.Visible := False;
  frmAbout.ShowModal;
  frmMain.Visible := True;
  frmAbout.Free;
end;

procedure TfrmMain.mmInstructionClick(Sender: TObject);
begin
  MessageBox(Handle, 'Please see file Readme.txt!', 'Instruction', MB_OK+MB_ICONINFORMATION);
end;

procedure TfrmMain.mmInlineMeClick(Sender: TObject);
begin
  MessageBox(Handle, 'uhhhh....You cool brother!', 'InlineMe', MB_OK+MB_ICONINFORMATION);
end;

procedure TfrmMain.mmDebugCheckClick(Sender: TObject);
begin
  if VMProtectIsDebuggerPresent(True) then
    MessageBox(Handle, 'WARNING: Debugger Detected!', 'Debugger Present', MB_OK+MB_ICONERROR)
  else
    MessageBox(Handle, 'Debugger not Detected', 'Instruction', MB_OK+MB_ICONINFORMATION)
end;

procedure TfrmMain.mmVMPresentClick(Sender: TObject);
begin
  if VMProtectIsVirtualMachinePresent then
    MessageBox(Handle, 'WARNING: Virtual Machine Detected!', 'VM Present', MB_OK+MB_ICONERROR)
  else
    MessageBox(Handle, 'Virtual Machine not Detected', 'Instruction', MB_OK+MB_ICONINFORMATION)
end;

procedure TfrmMain.mmValidImageCRCClick(Sender: TObject);
begin
  if not VMProtectIsValidImageCRC then
    MessageBox(Handle, 'WARNING: Memory Changed!', 'CRC Check', MB_OK+MB_ICONERROR)
  else
    MessageBox(Handle, 'Memory is Ok', 'Instruction', MB_OK+MB_ICONINFORMATION)
end;

procedure TfrmMain.mmCryptedStringClick(Sender: TObject);
const
  a = 'Im stupid crypted string :)';
begin
  MessageBox(Handle, VMProtectDecryptStringA(PAnsiChar(a)), 'Crypted String', MB_OK+MB_ICONINFORMATION)
end;

procedure TfrmMain.FormCreate(Sender: TObject);
var
  RegList: TStringList;
  Name, Key: String;
  Hash: array [0..1] of string;
  HashString: String;
begin

  // additional menu InlineMe
  VMProtectBeginMutation('VM Mutation'); // mark start VM (Mutation)
  if FileExists(ExtractFilePath(Application.ExeName) + 'InlineMe.txt') then
  begin
    mmInlineMe.Enabled := True;
  end;
  VMProtectEnd;

  // check code
  if FileExists(ExtractFilePath(Application.ExeName) + 'Key.ini') then
  begin
    RegList := TStringList.Create;
    try
      RegList.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Key.ini');
      if (Length(RegList.Strings[0]) >= 4) and (RegList.Strings[1] <> '') then
      begin
        Name := RegList.Strings[0];
        Key := RegList.Strings[1];
        RegList.Clear;
        RegList.Delimiter := '-';
        RegList.DelimitedText := Key;
        if (UpperCase(Name[1]) = Copy(RegList.Strings[0], 3,1)) and
           (UpperCase(Name[3]) = Copy(RegList.Strings[1], 3,1))then
        begin
          Hash[0] := IntToStr(frmRegistration.CalcHash(pointer(Name), Length(Name)));
          HashString:= RegList.Strings[0]+ '-' + RegList.Strings[1] + '-' +RegList.Strings[2] + '-' + RegList.Strings[3] + '-';
          Hash[1] :=  IntToStr(frmRegistration.CalcHash(pointer(HashString), Length(HashString)));
          if (Hash[0] = RegList[2]) and (Hash[1] = RegList[4]) and (Key <> BLACKLIST_KEY) then
            StatusBar1.SimpleText := 'Registered to: ' + Name;
        end;   
      end;
    finally
      RegList.Free;
    end;
  end;

  // aditional menu InlineMe
  VMProtectBeginVirtualization('VM Virtualization'); // mark start VM (Virtualization)
  if FileExists(ExtractFilePath(Application.ExeName) + 'InlineMe.txt') then
  begin
    mmInlineMeVM.Enabled := True;
  end;
  VMProtectEnd;

end;

end.
