apophysis/2.10/Source/varGenericPlugin.pas

278 lines
9.7 KiB
ObjectPascal
Raw Normal View History

2007-06-27 18:45:40 -04:00
{
Apophysis Copyright (C) 2001-2004 Mark Townsend
Apophysis Copyright (C) 2005-2006 Ronald Hordijk, Piotr Borys, Peter Sdobnov
Apophysis Copyright (C) 2007-2008 Piotr Borys, Peter Sdobnov
2007-06-27 18:45:40 -04:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
2007-06-27 18:45:40 -04:00
{
Variation Plugin DLL support for Apophysis:
Generic Plugin Support Unit
Started by Jed Kelsey, June 2007
2008-02-24 07:43:30 -05:00
Portions Copyright (C) 2008 Joel Faber
February 2008:
- Remove 30 plugin limit
- Reset variables
}
2007-06-27 18:45:40 -04:00
unit varGenericPlugin;
interface
uses
BaseVariation, XFormMan,
Classes, //TStrings/TStringList
SysUtils, //FindFirst/FindNext/FindClose
2008-02-24 07:43:30 -05:00
Forms; //MessageBox
2007-06-27 18:45:40 -04:00
type
TPluginVariationClass = class of TPluginVariation;
TPluginData = record
Instance: Integer;
PluginHandle: THandle;
PluginClass: TPluginVariationClass;
PluginVarGetName: function: PChar; cdecl;
PluginVarGetNrVariables: function: Integer; cdecl;
PluginVarGetVariableNameAt: function(const Index: integer): PChar; cdecl;
2008-02-24 07:43:30 -05:00
PluginVarCreate: function: Pointer; cdecl;
PluginVarDestroy: function(var MyVariation: Pointer): LongBool; cdecl;
PluginVarInit: function(MyVariation, FPx, FPy, FTx, FTy: Pointer; vvar: double): LongBool; cdecl;
PluginVarInit3D: function(MyVariation, FPx, FPy, FPz, FTx, FTy, FTz: Pointer; vvar: double): LongBool; cdecl;
PluginVarPrepare: function(MyVariation: Pointer): LongBool; cdecl;
PluginVarCalc: function(MyVariation: Pointer): LongBool; cdecl;
PluginVarGetVariable: function(MyVariation: Pointer; const Name: PChar; var value: double): LongBool; cdecl;
PluginVarSetVariable: function(MyVariation: Pointer; const Name: PChar; var value: double): LongBool; cdecl;
PluginVarResetVariable:function(MyVariation: Pointer; const Name: PChar) : LongBool; cdecl;
2007-06-27 18:45:40 -04:00
end;
PPluginData = ^TPluginData;
// This class serves as a proxy for the plugin variations.
TPluginVariation = class(TBaseVariation)
private
2008-02-24 07:43:30 -05:00
PluginData : TPluginData;
2007-06-27 18:45:40 -04:00
MyVariation : Pointer;
public
2008-02-24 07:43:30 -05:00
constructor Create(varData : TPluginData);
2007-06-27 18:45:40 -04:00
destructor Destroy; override;
class function GetName: string; override;
class function GetInstance: TBaseVariation; override;
2008-02-28 10:46:59 -05:00
function GetNrVariables: integer; override;
function GetVariableNameAt(const Index: integer): string; override;
2007-06-27 18:45:40 -04:00
function SetVariable(const Name: string; var value: double): boolean; override;
function GetVariable(const Name: string; var value: double): boolean; override;
2008-02-24 07:43:30 -05:00
function ResetVariable(const Name: string): boolean; override;
2007-06-27 18:45:40 -04:00
procedure Prepare; override;
procedure CalcFunction; override;
end;
2008-02-24 07:43:30 -05:00
type
TVariationPluginLoader = class (TVariationLoader)
public
constructor Create(varData : TPluginData);
destructor Destroy; override;
function GetName: string; override;
function GetInstance: TBaseVariation; override;
function GetNrVariables: integer; override;
function GetVariableNameAt(const Index: integer): string; override;
2008-02-24 07:43:30 -05:00
private
PluginData : TPluginData;
end;
2007-06-27 18:45:40 -04:00
//////////////////////////////////////////////////////////////////////
implementation
uses
Windows, //LoadLibrary
Math;
{ TPluginVariation }
///////////////////////////////////////////////////////////////////////////////
2008-02-24 07:43:30 -05:00
constructor TVariationPluginLoader.Create(varData : TPluginData);
begin
2008-02-24 07:43:30 -05:00
PluginData := varData;
end;
2008-02-24 07:43:30 -05:00
destructor TVariationPluginLoader.Destroy;
begin
2008-02-24 07:43:30 -05:00
FreeLibrary(PluginData.PluginHandle);
end;
2008-02-24 07:43:30 -05:00
function TVariationPluginLoader.GetName : string;
begin
2008-02-24 07:43:30 -05:00
Result := PluginData.PluginVarGetName;
end;
2008-02-24 07:43:30 -05:00
function TVariationPluginLoader.GetInstance: TBaseVariation;
begin
2008-02-24 07:43:30 -05:00
Result := TPluginVariation.Create(PluginData);
end;
2008-02-24 07:43:30 -05:00
function TVariationPluginLoader.GetNrVariables: integer;
begin
2008-02-24 07:43:30 -05:00
Result := PluginData.PluginVarGetNrVariables;
end;
2008-02-24 07:43:30 -05:00
function TVariationPluginLoader.GetVariableNameAt(const Index: integer): string;
begin
2008-02-24 07:43:30 -05:00
Result := PluginData.PluginVarGetVariableNameAt(Index);
end;
2008-02-24 07:43:30 -05:00
///////////////////////////////////////////////////////////////////////////////
2007-06-27 18:45:40 -04:00
procedure TPluginVariation.Prepare;
begin
2008-02-24 07:43:30 -05:00
PluginData.PluginVarInit(MyVariation, Pointer(FPx), Pointer(FPy), Pointer(FTx), Pointer(FTy), vvar);
PluginData.PluginVarPrepare(MyVariation);
2007-06-27 18:45:40 -04:00
end;
///////////////////////////////////////////////////////////////////////////////
procedure TPluginVariation.CalcFunction;
begin
2008-02-24 07:43:30 -05:00
PluginData.PluginVarCalc(MyVariation);
2007-06-27 18:45:40 -04:00
end;
///////////////////////////////////////////////////////////////////////////////
2008-02-24 07:43:30 -05:00
constructor TPluginVariation.Create(varData : TPluginData);
2007-06-27 18:45:40 -04:00
begin
2008-02-24 07:43:30 -05:00
PluginData := varData;
MyVariation := PluginData.PluginVarCreate;
2007-06-27 18:45:40 -04:00
end;
///////////////////////////////////////////////////////////////////////////////
destructor TPluginVariation.Destroy;
begin
2008-02-24 07:43:30 -05:00
PluginData.PluginVarDestroy(MyVariation);
2007-06-27 18:45:40 -04:00
inherited;
end;
///////////////////////////////////////////////////////////////////////////////
class function TPluginVariation.GetInstance: TBaseVariation;
begin
2008-02-24 07:43:30 -05:00
Result := nil;
2007-06-27 18:45:40 -04:00
end;
///////////////////////////////////////////////////////////////////////////////
class function TPluginVariation.GetName: string;
begin
2008-02-24 07:43:30 -05:00
Result := '';
2007-06-27 18:45:40 -04:00
end;
2008-02-28 10:46:59 -05:00
///////////////////////////////////////////////////////////////////////////////
function TPluginVariation.GetNrVariables: integer;
begin
Result := PluginData.PluginVarGetNrVariables;
end;
///////////////////////////////////////////////////////////////////////////////
function TPluginVariation.GetVariableNameAt(const Index: integer): string;
begin
Result := PluginData.PluginVarGetVariableNameAt(Index);
end;
2007-06-27 18:45:40 -04:00
///////////////////////////////////////////////////////////////////////////////
2008-02-24 07:43:30 -05:00
function TPluginVariation.SetVariable(const Name: string; var value: double): boolean;
2007-06-27 18:45:40 -04:00
begin
2008-02-24 07:43:30 -05:00
Result := PluginData.PluginVarSetVariable(MyVariation,PChar(Name),value);
2007-06-27 18:45:40 -04:00
end;
///////////////////////////////////////////////////////////////////////////////
2008-02-24 07:43:30 -05:00
function TPluginVariation.GetVariable(const Name: string; var value: double): boolean;
2007-06-27 18:45:40 -04:00
begin
2008-02-24 07:43:30 -05:00
Result := PluginData.PluginVarGetVariable(MyVariation,PChar(Name),value);
2007-06-27 18:45:40 -04:00
end;
///////////////////////////////////////////////////////////////////////////////
2008-02-24 07:43:30 -05:00
function TPluginVariation.ResetVariable(const Name: string) : boolean;
2007-06-27 18:45:40 -04:00
begin
2008-02-24 07:43:30 -05:00
Result := PluginData.PluginVarResetVariable(MyVariation, PChar(Name));
2007-06-27 18:45:40 -04:00
end;
///////////////////////////////////////////////////////////////////////////////
procedure InitializePlugins;
var
searchResult: TSearchRec;
2008-02-24 07:43:30 -05:00
name, msg: string;
PluginData : TPluginData;
2007-06-27 18:45:40 -04:00
begin
// Try to find regular files matching *.dll in the plugins dir
if FindFirst('.\Plugins\*.dll', faAnyFile, searchResult) = 0 then
begin
repeat
2008-02-24 07:43:30 -05:00
with PluginData do begin
2007-06-27 18:45:40 -04:00
//Load DLL and initialize plugins!
PluginHandle := LoadLibrary(PChar('.\Plugins\'+searchResult.Name));
if PluginHandle<>0 then begin
@PluginVarGetName := GetProcAddress(PluginHandle,'PluginVarGetName');
if @PluginVarGetName = nil then begin // Must not be a valid plugin!
FreeLibrary(PluginHandle);
2008-02-28 10:46:59 -05:00
msg := msg + 'Invalid plugin type: "' + searchResult.Name + '" is not a plugin' + #13#10;
continue;
end;
name := PluginVarGetName;
if GetVariationIndex(name) >= 0 then begin
FreeLibrary(PluginHandle);
2008-02-24 07:43:30 -05:00
msg := msg + 'Cannot load plugin from ' + searchResult.Name + ': variation "' + name + '" already exists!' + #13#10;
end
else begin
2007-06-27 18:45:40 -04:00
@PluginVarGetNrVariables := GetProcAddress(PluginHandle,'PluginVarGetNrVariables');
@PluginVarGetVariableNameAt := GetProcAddress(PluginHandle,'PluginVarGetVariableNameAt');
@PluginVarCreate := GetProcAddress(PluginHandle,'PluginVarCreate');
@PluginVarDestroy := GetProcAddress(PluginHandle,'PluginVarDestroy');
@PluginVarInit := GetProcAddress(PluginHandle,'PluginVarInit');
@PluginVarInit3D := GetProcAddress(PluginHandle,'PluginVarInit3D');
2007-06-27 18:45:40 -04:00
@PluginVarPrepare := GetProcAddress(PluginHandle,'PluginVarPrepare');
@PluginVarCalc := GetProcAddress(PluginHandle,'PluginVarCalc');
@PluginVarGetVariable := GetProcAddress(PluginHandle,'PluginVarGetVariable');
@PluginVarSetVariable := GetProcAddress(PluginHandle,'PluginVarSetVariable');
2008-02-24 07:43:30 -05:00
@PluginVarResetVariable := GetProcAddress(PluginHandle,'PluginVarResetVariable');
2008-02-24 07:43:30 -05:00
RegisterVariation(TVariationPluginLoader.Create(PluginData));
2007-06-27 18:45:40 -04:00
end;
end else
2008-02-28 10:46:59 -05:00
msg := msg + 'Cannot open plugin file: ' + searchResult.Name + #13#10;
2007-06-27 18:45:40 -04:00
end;
until (FindNext(searchResult) <> 0);
SysUtils.FindClose(searchResult); //Since we use Windows unit (LoadLibrary)
2008-02-28 10:46:59 -05:00
if msg <> '' then
Application.MessageBox(
PChar('There were problems with some of the plugins:' + #13#10#13#10 + msg),
'Warning', MB_ICONWARNING or MB_OK);
2007-06-27 18:45:40 -04:00
end;
end;
///////////////////////////////////////////////////////////////////////////////
initialization
InitializePlugins;
end.