unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    Image5: TImage;
    player_name_edit: TEdit;
    procedure start_btnClick(Sender: TObject);
    procedure exit_btnClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    procedure SetPlayerName();
  public
  end;

var
  Form1: TForm1;
  PLAYER_UUID: String;
  PLAYER_NAME: String;
  PX, PY: Integer;
  MouseIsDown: Boolean;
const
  Hytale_Dir = '.\package\game\latest\Client\';
  //JAVA_EXE = '.\package\jre\latest\bin\java.exe';

implementation

function GenerateUniqueID: string;
var
  guid: TGUID;
  guidStr: string;
begin
  Result := '';
  if CreateGUID(guid) = 0 then
   begin
     guidStr := GUIDToString(guid); // Получаем GUID со скобками
     guidStr := StringReplace(guidStr, '{', '', [rfReplaceAll]); // Удаляем '{'
     guidStr := StringReplace(guidStr, '}', '', [rfReplaceAll]); // Удаляем '}'
     Result := guidStr;
   end;
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
   begin
     MouseIsDown := True;
     PX := X;
     PY := Y;
   end;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if MouseIsDown then
  begin
    SetBounds(Form1.Left + (X - PX), Form1.Top + (Y - PY), Form1.Width, Form1.Height);
  end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  MouseIsDown := False;
end;

procedure GenerateID();
var
  f: TextFile;
  FileContent: string;
  tmp_uuid: string;
begin
  if not FileExists(Hytale_Dir+'uuid.txt') then
    begin
      tmp_uuid:= GenerateUniqueID();
      AssignFile(f, Hytale_Dir+'uuid.txt');
      Rewrite(f);
      Write(f, AnsiLowerCase(tmp_uuid));
      CloseFile(f);
      PLAYER_UUID:=AnsiLowerCase(tmp_uuid);
    end
    else
    begin
      AssignFile(f, Hytale_Dir+'uuid.txt');
      Reset(f);
      Read(f, FileContent);
      CloseFile(f);
      PLAYER_UUID:=FileContent;
    end;
end;

procedure TForm1.SetPlayerName();
var
  f: TextFile;
  FileContent: string;
begin
  if FileExists(Hytale_Dir+'username.txt') then
    begin
      AssignFile(f,Hytale_Dir+'username.txt');
      Reset(f);
      Read(f, FileContent);
      CloseFile(f);
      player_name_edit.Text:=FileContent;
      PLAYER_NAME:=FileContent;
    end
end;

{$R *.lfm}

{ TForm1 }

procedure TForm1.start_btnClick(Sender: TObject);

var
  f: TextFile;
  Parameters: string;
  APath, JAVA_EXE: String;
begin
  APath:= ExtractFilePath(Application.ExeName);
  JAVA_EXE:= APath+'package\jre\latest\bin\java.exe';
  APath:= APath+'package\game\latest\';

  AssignFile(f, Hytale_Dir+'username.txt');
  Rewrite(f);
  Write(f, player_name_edit.Text);
  CloseFile(f);

  Parameters:= '--app-dir '+APath+' --java-exec '+JAVA_EXE+' --auth-mode offline --uuid '+PLAYER_UUID+' --name '+PLAYER_NAME;
  //ShowMessage(Parameters);
  ExecuteProcess(APath+'Client\HytaleClient.exe', Parameters);
  Close;
end;

procedure TForm1.exit_btnClick(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormShow(Sender: TObject);

begin
  SetPlayerName();
  GenerateID();
end;

end.

