1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-29 20:32:18 -05:00
Simba/Projects/SAMufasaGUI/extensionmanager.pas

160 lines
3.8 KiB
ObjectPascal
Raw Normal View History

unit extensionmanager;
{$mode objfpc}
interface
uses
2010-03-22 17:21:08 -04:00
Classes, SysUtils,virtualextension,psextension,mufasabase,mufasatypes;
type
2010-03-22 17:21:08 -04:00
TExtension = TVirtualSimbaExtension;
(**
2010-03-22 17:21:08 -04:00
TExtensionManager holds a list of TExtension, and
has functions to easily handle hooks.
*)
2010-03-22 17:21:08 -04:00
{ TExtensionManager }
TExtensionManager = class(TObject)
2010-03-19 18:11:26 -04:00
private
2010-03-22 17:21:08 -04:00
FOnChange: TNotifyEvent;
procedure SetOnchange(const AValue: TNotifyEvent);
public
constructor Create;
destructor Destroy; override;
2010-03-19 18:11:26 -04:00
public
2010-03-22 17:21:08 -04:00
Extensions: TList;
StartDisabled : boolean;
property OnChange : TNotifyEvent read FOnChange write SetOnchange;
function GetExtensionIndex(Filename : string) : integer;
function LoadPSExtension(Filename : string) : boolean;
function LoadPSExtensionsDir(Directory,ext : string) : boolean;
function HandleHook(HookName: String; Args: Array of Variant): Variant;
end;
2010-03-22 17:21:08 -04:00
var
ExtManager : TExtensionManager;
2010-03-18 19:41:32 -04:00
implementation
2010-03-19 18:11:26 -04:00
uses
2010-03-22 17:21:08 -04:00
TestUnit;
2010-03-22 17:21:08 -04:00
procedure TExtensionManager.SetOnchange(const AValue: TNotifyEvent);
var
i : integer;
begin
for i := 0 to Extensions.Count - 1 do
TExtension(Extensions[i]).OnChange := AValue;;
FOnChange:=AValue;
end;
2010-03-18 19:41:32 -04:00
constructor TExtensionManager.Create;
begin
2010-03-22 17:21:08 -04:00
inherited Create;
2010-03-19 18:11:26 -04:00
Extensions := TList.Create;
2010-03-22 17:21:08 -04:00
StartDisabled := True;
2010-03-18 19:41:32 -04:00
end;
destructor TExtensionManager.Destroy;
2010-03-19 18:11:26 -04:00
var
i: Integer;
2010-03-18 19:41:32 -04:00
begin
2010-03-19 18:11:26 -04:00
for i := 0 to Extensions.Count - 1 do
2010-03-22 17:21:08 -04:00
TExtension(Extensions.Items[i]).Free;
2010-03-19 18:11:26 -04:00
Extensions.Free;
2010-03-22 17:21:08 -04:00
inherited Destroy;
end;
function TExtensionManager.GetExtensionIndex(Filename: string): integer;
var
i : integer;
begin
for i := 0 to Extensions.Count - 1 do
if CompareText(TExtension(Extensions[i]).Filename,filename) = 0 then
exit(i);
result := -1;
end;
function TExtensionManager.LoadPSExtension(Filename: string): boolean;
var
Ext : TExtension;
begin
if GetExtensionIndex(filename) <> -1 then
exit(true);
Result := False;
try
Ext := TSimbaPSExtension.Create(filename,startdisabled);
result := TSimbaPSExtension(ext).Working;
Extensions.Add(ext);
ext.OnChange:= FOnChange;
if assigned(FOnChange) then
FOnChange(Self);
except
on e : exception do
formWritelnex(format('Error in LoadPSExtension(%s): %s',[FileName, e.message]));
end;
end;
function GetFiles(Path, Ext: string): TstringArray;
var
SearchRec : TSearchRec;
c : integer;
begin
c := 0;
if FindFirst(Path + '*.' + ext, faAnyFile, SearchRec) = 0 then
begin
repeat
inc(c);
SetLength(Result,c);
Result[c-1] := SearchRec.Name;
until FindNext(SearchRec) <> 0;
SysUtils.FindClose(SearchRec);
end;
end;
function TExtensionManager.LoadPSExtensionsDir(Directory,ext: string): boolean;
var
Files : TstringArray;
i : integer;
tempevent : TNotifyEvent;
begin
result := false;
if not DirectoryExists(directory) then
exit;
tempevent := FOnChange;
FOnChange := nil;
Directory := IncludeTrailingPathDelimiter(directory);
Files := GetFiles(Directory,ext);
for i := 0 to high(Files) do
result := result or LoadPSExtension(Directory + files[i]);
FOnChange := Tempevent;
if Assigned(FOnChange) then
FOnChange(self);
2010-03-19 18:11:26 -04:00
end;
2010-03-18 19:41:32 -04:00
2010-03-19 18:11:26 -04:00
// How do we return more than one result?
function TExtensionManager.HandleHook(HookName: String; Args: Array of Variant): Variant;
var
i: Integer;
begin
for i := 0 to Extensions.Count -1 do
2010-03-22 17:21:08 -04:00
with TExtension(Extensions[i]) do
if Enabled then
if HookExists(HookName) then
if ExecuteHook(HookName, Args, Result) <> 0 then
begin
// Not succesfull.
end;
2010-03-18 19:41:32 -04:00
end;
2010-03-29 17:01:54 -04:00
initialization
ExtManager := TExtensionManager.Create;
ExtManager.StartDisabled := True;
finalization
if ExtManager <> nil then
FreeAndNil(ExtManager);
end.