1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-16 22:35:05 -05:00

Updated + Added plugins

git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@59 3f818213-9676-44b0-a9b4-5e4c4e03d09d
This commit is contained in:
Raymond 2009-09-13 23:22:23 +00:00
parent 9d6c4ee850
commit fafda03ad0
6 changed files with 296 additions and 66 deletions

View File

@ -40,7 +40,12 @@ end;
function CreateMirroredBitmap(Bmp : integer) : integer; function CreateMirroredBitmap(Bmp : integer) : integer;
begin; begin;
Result := CurrThread.Client.MBitmaps.CreateMirroredBitmap(Bmp); Result := CurrThread.Client.MBitmaps.CreateMirroredBitmap(Bmp, MirrorWidth);
end;
function CreateMirroredBitmapEx(Bmp : integer; MirrorStyle : TBmpMirrorStyle) : integer;
begin;
Result := CurrThread.Client.MBitmaps.CreateMirroredBitmap(Bmp,MirrorStyle);
end; end;
function FastGetPixel(bmp,x,y : integer) : LongWord; function FastGetPixel(bmp,x,y : integer) : LongWord;

View File

@ -1,6 +1,7 @@
Sender.Comp.AddTypeS('TIntegerArray', 'Array of integer'); Sender.Comp.AddTypeS('TIntegerArray', 'Array of integer');
Sender.Comp.AddTypeS('TPointArray','Array of TPoint'); Sender.Comp.AddTypeS('TPointArray','Array of TPoint');
Sender.Comp.AddTypeS('TBmpMirrorStyle','(MirrorWidth,MirrorHeight,MirrorLine)');
Sender.AddFunction(@ThreadSafeCall,'function ThreadSafeCall(ProcName: string; var V: TVariantArray): Variant;'); Sender.AddFunction(@ThreadSafeCall,'function ThreadSafeCall(ProcName: string; var V: TVariantArray): Variant;');
@ -27,6 +28,7 @@ Sender.AddFunction(@LoadBitmap,'function LoadBitmap(Path : string) : integer;');
Sender.AddFunction(@SetBitmapSize,'procedure SetBitmapSize(Bmp,NewW,NewH : integer);'); Sender.AddFunction(@SetBitmapSize,'procedure SetBitmapSize(Bmp,NewW,NewH : integer);');
Sender.AddFunction(@GetBitmapSize,'procedure GetBitmapSize(Bmp : integer; Var BmpW,BmpH : integer);'); Sender.AddFunction(@GetBitmapSize,'procedure GetBitmapSize(Bmp : integer; Var BmpW,BmpH : integer);');
Sender.AddFunction(@CreateMirroredBitmap,'function CreateMirroredBitmap(Bmp : integer) : integer;'); Sender.AddFunction(@CreateMirroredBitmap,'function CreateMirroredBitmap(Bmp : integer) : integer;');
Sender.AddFunction(@CreateMirroredBitmapEx,'function CreateMirroredBitmapEx(Bmp : integer; MirrorStyle : TBmpMirrorStyle) : integer;');
Sender.AddFunction(@FastSetPixel,'procedure FastSetPixel(bmp,x,y : integer; Color : TColor);'); Sender.AddFunction(@FastSetPixel,'procedure FastSetPixel(bmp,x,y : integer; Color : TColor);');
Sender.AddFunction(@FastSetPixels,'procedure FastSetPixels(bmp : integer; TPA : TPointArray; Colors : TIntegerArray);'); Sender.AddFunction(@FastSetPixels,'procedure FastSetPixels(bmp : integer; TPA : TPointArray; Colors : TIntegerArray);');
Sender.AddFunction(@FastGetPixel,'function FastGetPixel(bmp, x,y : integer) : TColor;'); Sender.AddFunction(@FastGetPixel,'function FastGetPixel(bmp, x,y : integer) : TColor;');

View File

@ -5,20 +5,20 @@ unit mmlpsthread;
interface interface
uses uses
Classes, SysUtils, client, uPSComponent,uPSCompiler,uPSRuntime,SynMemo; Classes, SysUtils, client, uPSComponent,uPSCompiler,uPSRuntime,SynMemo,Plugins,uPSPreProcessor;
type type
{ TMMLPSThread } { TMMLPSThread }
TMMLPSThread = class(TThread) TMMLPSThread = class(TThread)
procedure PSScriptProcessUnknowDirective(Sender: TPSPreProcessor;
Parser: TPSPascalPreProcessorParser; const Active: Boolean;
const DirectiveName, DirectiveParam: string; var Continue: Boolean);
protected protected
// PSScript : TPSScript;
// PSClient : TPSScript;
// Client: TClient;
// DebugTo : TStrings;
PSScript : TPSScript; PSScript : TPSScript;
DebugTo : TSynMemo; DebugTo : TSynMemo;
PluginsToload : Array of integer;
procedure OnCompile(Sender: TPSScript); procedure OnCompile(Sender: TPSScript);
procedure AfterExecute(Sender : TPSScript); procedure AfterExecute(Sender : TPSScript);
function RequireFile(Sender: TObject; const OriginFileName: String; function RequireFile(Sender: TObject; const OriginFileName: String;
@ -29,11 +29,10 @@ type
procedure OnThreadTerminate(Sender: TObject); procedure OnThreadTerminate(Sender: TObject);
procedure Execute; override; procedure Execute; override;
public public
Plugins : TMPlugins;
Client : TClient; Client : TClient;
procedure SetPSScript(Script : string); procedure SetPSScript(Script : string);
procedure SetDebug( Strings : TSynMemo ); procedure SetDebug( Strings : TSynMemo );
// function CompilePSScript : boolean;
// function
constructor Create(CreateSuspended: Boolean); constructor Create(CreateSuspended: Boolean);
destructor Destroy; override; destructor Destroy; override;
end; end;
@ -99,12 +98,15 @@ end;
constructor TMMLPSThread.Create(CreateSuspended : boolean); constructor TMMLPSThread.Create(CreateSuspended : boolean);
begin begin
SetLength(PluginsToLoad,0);
Client := TClient.Create; Client := TClient.Create;
Plugins := TMPlugins.Create;
Plugins.PluginDirs.Add(ExpandFileName(MainDir + DS + '..' + DS + '..'+ DS + 'Plugins'+ DS));
PSScript := TPSScript.Create(nil); PSScript := TPSScript.Create(nil);
PSScript.UsePreProcessor:= True; PSScript.UsePreProcessor:= True;
PSScript.OnNeedFile := @RequireFile; PSScript.OnNeedFile := @RequireFile;
PSScript.OnProcessUnknowDirective:=@PSScriptProcessUnknowDirective;
PSScript.OnCompile:= @OnCompile; PSScript.OnCompile:= @OnCompile;
PSScript.OnCompImport:= @OnCompImport; PSScript.OnCompImport:= @OnCompImport;
PSScript.OnExecImport:= @OnExecImport; PSScript.OnExecImport:= @OnExecImport;
@ -126,8 +128,10 @@ end;
destructor TMMLPSThread.Destroy; destructor TMMLPSThread.Destroy;
begin begin
SetLength(PluginsToLoad,0);
Client.Free; Client.Free;
PSScript.Free; PSScript.Free;
Plugins.Free;
inherited; inherited;
end; end;
@ -139,13 +143,39 @@ end;
procedure TMMLPSThread.PSScriptProcessUnknowDirective(Sender: TPSPreProcessor;
Parser: TPSPascalPreProcessorParser; const Active: Boolean;
const DirectiveName, DirectiveParam: string; var Continue: Boolean);
var
TempNum : integer;
I,II : integer;
begin
if DirectiveName= 'LOADDLL' then
if DirectiveParam <> '' then
begin;
TempNum := Plugins.LoadPlugin(DirectiveParam);
if TempNum < 0 then
Writeln(Format('Your DLL %s has not been found',[DirectiveParam]))
else
begin;
for i := High(PluginsToLoad) downto 0 do
if PluginsToLoad[i] = TempNum then
Exit;
SetLength(PluginsToLoad,Length(PluginsToLoad)+1);
PluginsToLoad[High(PluginsToLoad)] := TempNum;
end;
end;
Continue:= True;
end;
procedure TMMLPSThread.OnCompile(Sender: TPSScript); procedure TMMLPSThread.OnCompile(Sender: TPSScript);
var
i,ii : integer;
begin begin
//Here we add all the initalizing, of BMPArray etc for i := high(PluginsToLoad) downto 0 do
for ii := 0 to Plugins.MPlugins[PluginsToLoad[i]].MethodLen - 1 do
// ^ This will all be done with Client.Create; PSScript.AddFunction(Plugins.MPlugins[PluginsToLoad[i]].Methods[i].FuncPtr,
Plugins.MPlugins[PluginsToLoad[i]].Methods[i].FuncStr);
// Here we add all the functions to the engine. // Here we add all the functions to the engine.
{$I PSInc/pscompile.inc} {$I PSInc/pscompile.inc}
end; end;

178
Units/MMLAddon/plugins.pas Normal file
View File

@ -0,0 +1,178 @@
unit plugins;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,dynlibs;
type
TMPluginMethod = record
FuncPtr : pointer;
FuncStr : string;
end;
TMPlugin = record
Methods : Array of TMPluginMethod;
dllHandle : TLibHandle;
filename : string;
MethodLen : integer;
end;
TMPluginArray = array of TMPlugin;
{ TMPlugins }
TMPlugins = class (TObject)
private
Plugins : TMPluginArray;
PluginLen : integer;
procedure FreePlugins;
public
PluginDirs : TStringList;
procedure ValidateDirs;
procedure LoadPluginsDir( DirIndex : integer);
function LoadPlugin(PluginName : string) : integer;
property Count : integer read PluginLen;
property MPlugins : TMPluginArray read Plugins;
constructor Create;
destructor Destroy;override;
end;
implementation
uses
MufasaTypes,FileUtil;
{ TMPlugins }
procedure TMPlugins.FreePlugins;
var
I : integer;
begin
for i := 0 to PluginLen - 1 do
begin;
if (Plugins[i].dllHandle > 0) then
try
FreeLibrary(Plugins[i].dllHandle);
except
end;
end;
SetLength(Plugins,0);
PluginLen:= 0;
end;
procedure TMPlugins.ValidateDirs;
var
i : integer;
TempStr : string;
begin
for i := 0 to PluginDirs.Count - 1 do
begin;
if DirectoryExists(PluginDirs.Strings[i]) = false then
raise Exception.createFMT('Directory(%s) does not exist',[PluginDirs[i]]);
TempStr := PluginDirs.Strings[i];
if (TempStr[Length(TempStr)] <> DS) then
begin;
if (TempStr[Length(TempStr)] = '\') or (TempStr[Length(TempStr)] = '/') then
TempStr[Length(TempStr)] := DS
else
TempStr := TempStr + DS;
PluginDirs.Strings[i] := TempStr;
end;
end;
end;
procedure TMPlugins.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;
function TMPlugins.LoadPlugin(PluginName: string): Integer;
var
i, ii : integer;
pntrArrc : function : integer; stdcall;
GetFuncInfo : function (x: Integer; var ProcAddr: Pointer; var ProcDef: PChar) : Integer; stdcall;
GetTypeCount : function : Integer; stdcall;
GetTypeInfo : function (x: Integer; var sType, sTypeDef: string): Integer; stdcall;
PD : PChar;
pntr : Pointer;
arrc : integer;
Status : LongInt;
PlugExt: String = {$IFDEF LINUX}'.so';{$ELSE}'.dll';{$ENDIF}
begin
ii := -1;
Result := -1;
ValidateDirs;
PluginName := ExtractFileNameWithoutExt(PluginName);
for i := 0 to PluginDirs.Count - 1 do
if FileExists(PluginDirs.Strings[i] + Pluginname + PlugExt) then
begin;
if ii <> -1 then
Raise Exception.CreateFmt('Plugin(%s) has been found multiple times',[PluginName]);
ii := i;
end;
for i := 0 to PluginLen - 1 do
if Plugins[i].filename = (PluginDirs.Strings[ii] + PluginName + PlugExt) then
Exit(i);
pd := StrAlloc(255);
SetLength(Plugins,PluginLen + 1);
Writeln(Format('Loading plugin %s at %s',[PluginName,PluginDirs.Strings[ii]]));
Plugins[PluginLen].filename:= PluginDirs.Strings[ii] + Pluginname + PlugExt;
Plugins[PluginLen].dllHandle:= LoadLibrary(PChar(Plugins[PluginLen].filename));
if Plugins[PluginLen].dllHandle = 0 then
Raise Exception.CreateFMT('Error loading plugin %s',[Plugins[PluginLen].filename]);
Pointer(pntrArrc) := GetProcAddress(Plugins[PluginLen].dllHandle, PChar('GetFunctionCount'));
if @pntrArrc = nil then
Raise Exception.CreateFMT('Error loading plugin %s',[Plugins[PluginLen].filename]);
arrc := pntrArrc();
SetLength(Plugins[PluginLen].Methods, ArrC);
Pointer(GetFuncInfo) := GetProcAddress(Plugins[PluginLen].dllHandle, PChar('GetFunctionInfo'));
if @GetFuncInfo = nil then
Raise Exception.CreateFMT('Error loading plugin %s',[Plugins[PluginLen].filename]);
Plugins[PluginLen].MethodLen := Arrc;
for ii := 0 to ArrC-1 do
begin;
if (GetFuncInfo(ii, pntr, pd) < 0) then
Continue;
Plugins[Pluginlen].Methods[ii].FuncPtr := pntr;
Plugins[Pluginlen].Methods[ii].FuncStr := pd;
end;
Result := PluginLen;
inc(PluginLen);
StrDispose(pd);
end;
constructor TMPlugins.Create;
begin
inherited Create;
PluginLen := 0;
PluginDirs := TStringList.Create;
end;
destructor TMPlugins.Destroy;
begin
FreePlugins;
PluginDirs.Free;
inherited Destroy;
end;
end.

View File

@ -49,7 +49,7 @@ type
function GetBMP(Index : integer) : TMufasaBitmap; function GetBMP(Index : integer) : TMufasaBitmap;
property Bmp[Index : integer]: TMufasaBitmap read GetBMP; property Bmp[Index : integer]: TMufasaBitmap read GetBMP;
function CreateBMP(w, h: integer): Integer; function CreateBMP(w, h: integer): Integer;
function CreateMirroredBitmap(bitmap: Integer): Integer; function CreateMirroredBitmap(bitmap: Integer; MirrorStyle : TBmpMirrorStyle): Integer;
function CreateBMPFromFile(const Path : string) : integer; function CreateBMPFromFile(const Path : string) : integer;
function CreateBMPFromString(width,height : integer; Data : string) : integer; function CreateBMPFromString(width,height : integer; Data : string) : integer;
procedure FreeBMP( Number : integer); procedure FreeBMP( Number : integer);
@ -106,7 +106,8 @@ begin
BmpArray[Result].Index:= Result; BmpArray[Result].Index:= Result;
end; end;
function TMBitmaps.CreateMirroredBitmap(bitmap: Integer): Integer; function TMBitmaps.CreateMirroredBitmap(bitmap: Integer;
MirrorStyle: TBmpMirrorStyle): Integer;
var var
w,h : integer; w,h : integer;
y,x : integer; y,x : integer;
@ -115,11 +116,22 @@ begin
Source := Bmp[Bitmap].FData; Source := Bmp[Bitmap].FData;
w := BmpArray[Bitmap].Width; w := BmpArray[Bitmap].Width;
h := BmpArray[Bitmap].Height; h := BmpArray[Bitmap].Height;
if MirrorStyle = MirrorLine then
Result := CreateBMP(h,w)
else
Result := CreateBMP(w,h); Result := CreateBMP(w,h);
Dest := BmpArray[Result].FData; Dest := BmpArray[Result].FData;
for y := (h-1) downto 0 do case MirrorStyle of
MirrorWidth : for y := (h-1) downto 0 do
for x := (w-1) downto 0 do for x := (w-1) downto 0 do
Dest[y*w+x] := Source[y*w+w-1-x]; Dest[y*w+x] := Source[y*w+w-1-x];
MirrorHeight : for y := (h-1) downto 0 do
Move(Source[y*w],Dest[(h-1 - y) * w],w*SizeOf(TRGB32));
MirrorLine : for y := (h-1) downto 0 do
for x := (w-1) downto 0 do
Dest[x*h+y] := Source[y*w+x];
end;
//Can be optmized, this is just proof of concept //Can be optmized, this is just proof of concept
end; end;

View File

@ -7,7 +7,10 @@ interface
uses uses
Classes, SysUtils; Classes, SysUtils;
const
DS = DirectorySeparator;
var
MainDir : string;
type type
TRGB32 = packed record TRGB32 = packed record
B, G, R, A: Byte; B, G, R, A: Byte;
@ -18,7 +21,7 @@ type
Ptr : PRGB32; Ptr : PRGB32;
IncPtrWith : integer; IncPtrWith : integer;
end; end;
TBmpMirrorStyle = (MirrorWidth,MirrorHeight,MirrorLine); //LineMirror is in line x=y;
TTargetWindowMode = (w_BMP, w_Window, w_HDC, w_ArrayPtr, w_XWindow); TTargetWindowMode = (w_BMP, w_Window, w_HDC, w_ArrayPtr, w_XWindow);
TClickType = (mouse_Left, mouse_Right, mouse_Middle); TClickType = (mouse_Left, mouse_Right, mouse_Middle);
TMousePress = (mouse_Down, mouse_Up); TMousePress = (mouse_Down, mouse_Up);