2010-01-25 08:59:44 -05:00
|
|
|
unit libloader;
|
|
|
|
|
2010-04-02 11:55:54 -04:00
|
|
|
{$mode objfpc}{$H+}
|
2010-01-25 08:59:44 -05:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, dynlibs;
|
|
|
|
type
|
|
|
|
TGenericLib = record
|
|
|
|
filename: string;
|
|
|
|
handle: TLibHandle;
|
|
|
|
end;
|
|
|
|
TGenericLibArray = array of TGenericLib;
|
|
|
|
|
2010-02-07 10:59:03 -05:00
|
|
|
{ TGenericLoader }
|
|
|
|
|
2010-01-25 08:59:44 -05:00
|
|
|
TGenericLoader = class(TObject)
|
|
|
|
private
|
|
|
|
PluginLen : integer;
|
|
|
|
Loaded: TGenericLibArray;
|
|
|
|
PluginDirs : TStringList;
|
|
|
|
procedure FreePlugins;
|
|
|
|
procedure LoadPluginsDir(DirIndex : integer);
|
2010-02-07 10:59:03 -05:00
|
|
|
function VerifyPath(Path : string) : string;
|
2010-01-25 08:59:44 -05:00
|
|
|
protected
|
|
|
|
function InitPlugin(plugin: TLibHandle): boolean; virtual; abstract;
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
procedure ValidateDirs;
|
2010-01-25 23:25:11 -05:00
|
|
|
procedure AddPath(path: string);
|
2010-01-25 08:59:44 -05:00
|
|
|
function LoadPlugin(PluginName : string) : integer;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2010-05-09 16:54:34 -04:00
|
|
|
MufasaTypes,MufasaBase,FileUtil, strutils;
|
2010-01-25 08:59:44 -05:00
|
|
|
|
2010-01-25 23:25:11 -05:00
|
|
|
procedure TGenericLoader.AddPath(path: string);
|
2010-01-25 08:59:44 -05:00
|
|
|
var
|
|
|
|
idx: integer;
|
2010-02-07 10:59:03 -05:00
|
|
|
verified : string;
|
2010-01-25 08:59:44 -05:00
|
|
|
begin
|
2010-02-07 10:59:03 -05:00
|
|
|
verified := VerifyPath(path);
|
2010-01-27 00:44:43 -05:00
|
|
|
//IDK who changed this to loading a dir, but DON'T
|
2010-02-07 10:59:03 -05:00
|
|
|
if not PluginDirs.Find(verified,idx) then
|
2010-01-27 00:44:43 -05:00
|
|
|
begin
|
2010-03-07 10:57:10 -05:00
|
|
|
mDebugLn('Adding Plugin Path: ' + verified);
|
2010-02-07 10:59:03 -05:00
|
|
|
PluginDirs.Add(verified);
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TGenericLoader.FreePlugins;
|
|
|
|
var
|
|
|
|
I : integer;
|
|
|
|
begin
|
|
|
|
for i := 0 to PluginLen - 1 do
|
|
|
|
begin;
|
|
|
|
if (Loaded[i].handle > 0) then
|
|
|
|
try
|
2010-03-07 10:57:10 -05:00
|
|
|
mDebugLn('Freeing plugin[%d]',[i]);
|
2010-01-25 08:59:44 -05:00
|
|
|
FreeLibrary(Loaded[i].handle);
|
|
|
|
except
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
SetLength(Loaded,0);
|
|
|
|
PluginLen:= 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TGenericLoader.ValidateDirs;
|
|
|
|
var
|
|
|
|
i : integer;
|
|
|
|
begin
|
|
|
|
for i := 0 to PluginDirs.Count - 1 do
|
|
|
|
begin;
|
2010-02-07 10:59:03 -05:00
|
|
|
if DirectoryExists(PluginDirs[i]) = false then
|
|
|
|
raise Exception.createFMT('Directory(%s) does not exist',[PluginDirs[i]]);
|
|
|
|
PluginDirs[i] := VerifyPath(PluginDirs[i]);
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TGenericLoader.LoadPluginsDir(DirIndex: integer);
|
|
|
|
var
|
|
|
|
PlugExt: String = {$IFDEF LINUX}'*.so';{$ELSE}'*.dll';{$ENDIF}
|
|
|
|
FileSearcher : TSearchRec;
|
|
|
|
begin
|
|
|
|
if (DirIndex < 0) or (DirIndex >= PluginDirs.Count) then
|
|
|
|
Exit;
|
|
|
|
if FindFirst(PluginDirs.Strings[DirIndex] + PlugExt, faAnyFile, FileSearcher) <> 0 then
|
|
|
|
begin;
|
|
|
|
FindClose(FileSearcher);
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
repeat
|
|
|
|
LoadPlugin(FileSearcher.Name);
|
|
|
|
until FindNext(FileSearcher) <> 0;
|
|
|
|
FindClose(FileSearcher);
|
|
|
|
end;
|
|
|
|
|
2010-02-07 10:59:03 -05:00
|
|
|
function TGenericLoader.VerifyPath(Path: string): string;
|
|
|
|
begin
|
2010-05-09 16:54:34 -04:00
|
|
|
if (@path = nil) or (path = '') then
|
|
|
|
exit('');
|
2010-02-07 10:59:03 -05:00
|
|
|
Result := Path;
|
|
|
|
if (Result[Length(Result)] <> DS) then
|
|
|
|
begin;
|
|
|
|
if (Result[Length(Result)] = '\') or (Result[Length(Result)] = '/') then
|
|
|
|
Result[Length(Result)] := DS
|
|
|
|
else
|
|
|
|
Result := Result + DS;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2010-01-25 08:59:44 -05:00
|
|
|
|
|
|
|
function TGenericLoader.LoadPlugin(PluginName: string): Integer;
|
|
|
|
var
|
|
|
|
i, ii : integer;
|
|
|
|
PlugExt: String = {$IFDEF LINUX}'.so';{$ELSE}'.dll';{$ENDIF}
|
|
|
|
begin
|
|
|
|
ii := -1;
|
|
|
|
result := -1;
|
|
|
|
if PluginDirs.Count = 0 then
|
|
|
|
Exit;
|
|
|
|
ValidateDirs;
|
|
|
|
PluginName := ExtractFileNameWithoutExt(PluginName);
|
|
|
|
for i := 0 to PluginDirs.Count - 1 do
|
2010-05-15 07:57:55 -04:00
|
|
|
if FileExistsUTF8(PluginDirs.Strings[i] + Pluginname + PlugExt) then
|
2010-01-25 08:59:44 -05:00
|
|
|
begin;
|
|
|
|
if ii <> -1 then
|
|
|
|
Raise Exception.CreateFmt('Plugin(%s) has been found multiple times',[PluginName]);
|
|
|
|
ii := i;
|
|
|
|
end;
|
|
|
|
if ii = -1 then
|
|
|
|
raise Exception.CreateFMT('Plugins(%s) has not been found',[PluginName]);
|
|
|
|
for i := 0 to PluginLen - 1 do
|
|
|
|
if Loaded[i].filename = (PluginDirs.Strings[ii] + PluginName + PlugExt) then
|
|
|
|
Exit(i);
|
|
|
|
SetLength(Loaded,PluginLen + 1);
|
2010-03-07 10:57:10 -05:00
|
|
|
mDebugLn(Format('Loading plugin %s at %s',[PluginName,PluginDirs.Strings[ii]]));
|
2010-01-25 08:59:44 -05:00
|
|
|
Loaded[PluginLen].filename:= PluginDirs.Strings[ii] + Pluginname + PlugExt;
|
|
|
|
Loaded[PluginLen].handle:= LoadLibrary(Loaded[PluginLen].filename);
|
|
|
|
if Loaded[PluginLen].handle = 0 then
|
|
|
|
Raise Exception.CreateFMT('Error loading plugin %s',[Loaded[PluginLen].filename]);
|
|
|
|
if InitPlugin(Loaded[PluginLen].handle) then
|
|
|
|
inc(PluginLen)
|
|
|
|
else
|
|
|
|
FreeLibrary(Loaded[PluginLen].handle);
|
2010-01-25 23:25:11 -05:00
|
|
|
Result:= PluginLen - 1;
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
constructor TGenericLoader.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
PluginLen := 0;
|
|
|
|
PluginDirs := TStringList.Create;
|
|
|
|
PluginDirs.CaseSensitive:= {$IFDEF LINUX}true{$ELSE}false{$ENDIF};
|
2010-02-07 10:59:03 -05:00
|
|
|
PluginDirs.Duplicates:= dupIgnore;
|
2010-01-25 08:59:44 -05:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TGenericLoader.Destroy;
|
|
|
|
begin
|
|
|
|
FreePlugins;
|
|
|
|
PluginDirs.Free;
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|