Log in

View Full Version : Access Violation in Plugin



ReadySteadyGo
11-09-2011, 05:54 AM
library Dungeon;

{$mode objfpc}{$H+}

uses
Classes, math, sysutils
{ you can add units after this };

{type
TPoint = record
x: Integer;
y: Integer;
end; }

type
TPointArray = Array of TPoint;

type
T2DPointArray = Array of TPointArray;

type
TTarget_Exported = record
Target : Pointer;

FindColorsTolerance: function(target: pointer; var pts: TPointArray; col, x1, y1, x2, y2, tol: Integer): Boolean; stdcall;
SortTPAFrom: procedure(target: pointer; var a: TPointArray; const From: TPoint); stdcall;
SplitTPA: function(target: pointer; const arr: TPointArray; Dist: Integer): T2DPointArray; stdcall;
end;

{$R *.res}

{function Point(x, y: LongInt): TPoint;
begin
Result.x := x;
Result.y := y;
end; }

function Plugin_RoomBounds(ImageClient: TTarget_Exported): TPointArray; Register;
var
Rooms: TPointArray;
RoomsSplit: T2DPointArray;
begin
SetLength(Rooms, 0);
if ImageClient.FindColorsTolerance(ImageClient.Target , Rooms, 16777215 , 550{MMX1}, 8{MMY1}, 703{MMX2}, 161{MMY2}, 400) then
begin
ImageClient.SortTPAFrom(ImageClient.Target, Rooms, Point(627{MMCX}, 85{MMCY}));
RoomsSplit := ImageClient.SplitTPA(ImageClient.Target, Rooms, 1);

Result := RoomsSplit[0];

//DebugTPA(Result, '');
end;
end;

function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 1;
end;


function GetFunctionCallingConv(x : integer) : integer; stdcall;
begin
Result := 0;
case x of
0..0 : Result := 1;
end;
end;

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
begin
case x of
0:
begin
ProcAddr := @Plugin_RoomBounds;
StrPCopy(ProcDef, 'function Plugin_RoomBounds(ImageClient: TTarget_Exported): TPointArray;');
end;
else
x := -1;
end;
Result := x;
end;

exports GetFunctionCount;
exports GetFunctionInfo;
exports GetFunctionCallingConv;


end.


Getting an access violation when Plugin_RoomBounds is called. Can anyone work out why?

Markus
11-09-2011, 07:23 AM
Because TTarget_Exported just seems to fail. You have to assign it to a variable first then pass that variable, else Pascalscript will complain.
Most likely you will still get AV after that because the memorymanager is being stupid. You can try this 5-line memory manager patch to fix that:
procedure SetPluginMemManager(MemMgr : TMemoryManager); stdcall; export;
begin
SetMemoryManager(MemMgr);
end;
And add the bottom add:
exports SetPluginMemManager;
This only works with Simba 974, which can be found at Jenkins and is not officially released yet.

Also I can guarantee you that your type definition of TTarget_Exported is wrong. add mufasatypes to your uses to fix that and don't redefine it!

ReadySteadyGo
11-09-2011, 10:10 AM
I've just looked at the TTarget_Exported Type and I didn't realise how incorrectly I was using it. I want to be able to use Simba's colour finding routines. Having looking in the finder Unit, I see it uses a TMFinder, is it possible to use this?

Markus
11-09-2011, 10:16 AM
Well you can but it's messed up. I'm not familiar with Simba's internals, but if you could get the actual IO manager from Simba you're a huge step further. MMLFinder might help then if you set the correct IO manager.