mirror of
https://github.com/moparisthebest/Simba
synced 2024-11-13 12:55:05 -05:00
b8b8c5125c
Added a lil bar for functionlist (easier to drag) and close button Made the updatetimer go to 30 mins, after first call Made the versioncheck have a different thread (made it pretty thread safe, afaik) Made about form show the version Fixed lil bug with FunctionList(where it would get big borders after selecint an item). git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@367 3f818213-9676-44b0-a9b4-5e4c4e03d09d
188 lines
4.7 KiB
ObjectPascal
188 lines
4.7 KiB
ObjectPascal
unit updateform;
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
interface
|
|
|
|
uses
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
|
ComCtrls, StdCtrls, updater;
|
|
|
|
type
|
|
|
|
{ TSimbaUpdateForm }
|
|
|
|
{ TSimbaVersionThread }
|
|
|
|
TSimbaVersionThread = class(TThread)
|
|
public
|
|
ResultStr : string;
|
|
InputURL : string;
|
|
Done : boolean;
|
|
procedure Execute; override;
|
|
end;
|
|
TSimbaUpdateForm = class(TForm)
|
|
UpdateLog: TMemo;
|
|
UpdateButton: TButton;
|
|
OkButton: TButton;
|
|
CancelButton: TButton;
|
|
DownloadProgress: TProgressBar;
|
|
procedure CancelButtonClick(Sender: TObject);
|
|
procedure CleanUpdateForm(Sender: TObject);
|
|
procedure OkButtonClick(Sender: TObject);
|
|
procedure UpdateButtonClick(Sender: TObject);
|
|
function CanUpdate: Boolean;
|
|
|
|
private
|
|
{ private declarations }
|
|
|
|
Updater: TMMLFileDownloader;
|
|
FCancelling: Boolean;
|
|
FSimbaVersion: Integer;
|
|
SimbaVersionThread : TSimbaVersionThread;
|
|
private
|
|
function OnUpdateBeat: Boolean;
|
|
function GetLatestSimbaVersion: Integer;
|
|
public
|
|
{ public declarations }
|
|
procedure PerformUpdate;
|
|
protected
|
|
FCancelled: Boolean;
|
|
end;
|
|
|
|
|
|
var
|
|
SimbaUpdateForm: TSimbaUpdateForm;
|
|
|
|
implementation
|
|
|
|
uses
|
|
internets, TestUnit;
|
|
|
|
function TSimbaUpdateForm.CanUpdate: Boolean;
|
|
begin
|
|
GetLatestSimbaVersion;
|
|
|
|
Writeln('Latest Simba Version: ' + IntToStr(FSimbaVersion));
|
|
Exit(testunit.SimbaVersion < FSimbaVersion);
|
|
end;
|
|
|
|
function TSimbaUpdateForm.GetLatestSimbaVersion: Integer;
|
|
begin
|
|
if SimbaVersionThread = nil then//Create thread (only if no-other one is already running)
|
|
begin
|
|
SimbaVersionThread := TSimbaVersionThread.Create(true);
|
|
SimbaVersionThread.InputURL:= 'http://old.villavu.com/merlijn/Simba'{$IFDEF WINDOWS} +'.exe'{$ENDIF} + '.version';
|
|
SimbaVersionThread.Resume;
|
|
while SimbaVersionThread.Done = false do//Wait till thread is done
|
|
begin
|
|
Application.ProcessMessages;
|
|
Sleep(50);
|
|
end;
|
|
FSimbaVersion := StrToIntDef(Trim(SimbaVersionThread.ResultStr), -1);//Read output
|
|
FreeAndNil(SimbaVersionThread);//Free the thread
|
|
end else
|
|
//Another thread is already running, lets wait for it! (When it's nil, it means that the result is written!)
|
|
while SimbaVersionThread = nil do
|
|
begin;
|
|
Application.ProcessMessages;
|
|
Sleep(50);
|
|
end;
|
|
Exit(FSimbaVersion);
|
|
end;
|
|
|
|
procedure TSimbaUpdateForm.UpdateButtonClick(Sender: TObject);
|
|
begin
|
|
Self.PerformUpdate;
|
|
end;
|
|
|
|
procedure TSimbaUpdateForm.CancelButtonClick(Sender: TObject);
|
|
begin
|
|
if FCancelled then
|
|
begin
|
|
Self.ModalResult:=mrCancel;
|
|
Self.Hide;
|
|
end else
|
|
begin
|
|
FCancelling := True;
|
|
end;
|
|
end;
|
|
|
|
procedure TSimbaUpdateForm.CleanUpdateForm(Sender: TObject);
|
|
begin
|
|
Self.DownloadProgress.Position:=0;
|
|
Self.UpdateLog.Clear;
|
|
Self.UpdateLog.Lines.Add('---------- Update Session ----------');
|
|
end;
|
|
|
|
procedure TSimbaUpdateForm.OkButtonClick(Sender: TObject);
|
|
begin
|
|
Self.ModalResult:=mrOK;
|
|
Self.Hide;
|
|
end;
|
|
|
|
{ Return true if we have to cancel }
|
|
function TSimbaUpdateForm.OnUpdateBeat: Boolean;
|
|
var
|
|
Percentage: Integer;
|
|
begin
|
|
Application.ProcessMessages;
|
|
|
|
Percentage := Updater.GetPercentage();
|
|
if Percentage <> -1 then
|
|
DownloadProgress.Position:=Percentage;
|
|
|
|
Result := FCancelling;
|
|
end;
|
|
|
|
procedure TSimbaUpdateForm.PerformUpdate;
|
|
begin
|
|
Updater := TMMLFileDownloader.Create;
|
|
|
|
FCancelling := False;
|
|
FCancelled := False;
|
|
|
|
// Make this a setting later
|
|
Updater.FileURL := 'http://old.villavu.com/merlijn/Simba'{$IFDEF WINDOWS} +'.exe'{$ENDIF};
|
|
|
|
//ApplicationName{$IFDEF WINDOWS} +'.exe'{$ENDIF};
|
|
|
|
// Should work on Windows as well
|
|
Updater.ReplacementFile := ExtractFileName(Application.ExeName);
|
|
Updater.OnBeat := @Self.OnUpdateBeat;
|
|
Updater.BasePath := ExtractFilePath(Application.ExeName);
|
|
|
|
Self.UpdateLog.Lines.Add('Starting download of ' + Updater.FileURL + ' ...');
|
|
try
|
|
Updater.DownloadAndSave;
|
|
Self.UpdateLog.Lines.Add('Downloaded to ' + Updater.ReplacementFile + '_ ...');
|
|
Updater.Replace;
|
|
Self.UpdateLog.Lines.Add('Renaming ' + Updater.ReplacementFile + ' to ' + Updater.ReplacementFile + '_old_');
|
|
Self.UpdateLog.Lines.Add('Renaming ' + Updater.ReplacementFile + '_ to ' + Updater.ReplacementFile);
|
|
Self.UpdateLog.Lines.Add('Deleting ' + Updater.ReplacementFile + '_old_');
|
|
Updater.Free;
|
|
except
|
|
FCancelling := False;
|
|
FCancelled := True;
|
|
Self.UpdateLog.Lines.Add('Download stopped ...');
|
|
// more detailed info
|
|
writeln('EXCEPTION IN UPDATEFORM: We either hit Cancel, or something went wrong with files');
|
|
end;
|
|
Self.UpdateLog.Lines.Add('Done ... ');
|
|
Self.UpdateLog.Lines.Add('Please restart all currently running Simba binaries.');
|
|
end;
|
|
|
|
{ TSimbaVersionThread }
|
|
|
|
procedure TSimbaVersionThread.Execute;
|
|
begin
|
|
ResultStr:= GetPage(InputURL);
|
|
done := true;
|
|
end;
|
|
|
|
initialization
|
|
{$I updateform.lrs}
|
|
|
|
end.
|
|
|