1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-21 08:45:06 -05:00

libMML: Some bugfixes + started changing indent.

This commit is contained in:
Merlijn Wajer 2011-02-06 00:14:56 +01:00
parent fa4d89446b
commit 40f6b542c6

View File

@ -5,7 +5,7 @@ library libmml;
uses
cmem,Classes,interfaces,graphics,client,sysutils,MufasaTypes,dtmutil, dtm;
{$R *.res}
//{$R *.res}
Const
RESULT_OK = 0;
@ -23,28 +23,28 @@ var
function init: integer; cdecl;
begin
last_error := '';
debug := true;
result := RESULT_OK;
last_error := '';
debug := true;
result := RESULT_OK;
end;
procedure set_last_error(s: string);
begin
last_error := s;
if debug then
writeln('ERROR: ' + s);
last_error := s;
if debug then
writeln('ERROR: ' + s);
end;
{ Validate the TClient. If it is NULL, set last error and return false }
function validate_client(C: TClient): boolean; inline;
begin
result := Assigned(C);
if not result then
begin
last_error := 'PClient is NULL';
if debug then
writeln(last_error);
end;
result := Assigned(C);
if not result then
begin
last_error := 'PClient is NULL';
if debug then
writeln(last_error);
end;
end;
{
@ -55,40 +55,38 @@ function create_client: PtrUInt; cdecl;
var
C: TClient;
begin
try
C := TClient.Create('');
Result := PtrUInt(C);
except on e : Exception do
begin
// FIXME UINT negative
result := PtrUInt(RESULT_ERROR);
set_last_error(e.Message);
try
C := TClient.Create('');
Result := PtrUInt(C);
except on e : Exception do
// FIXME UINT negative
result := PtrUInt(RESULT_ERROR);
set_last_error(e.Message);
end;
end;
writeln(format('C: %d, IOManager: %d', [PtrUInt(C), PtrUInt(C.IOManager)]));
writeln(format('C: %d, IOManager: %d', [PtrUInt(C), PtrUInt(C.IOManager)]));
end;
{ Destroy a TClient }
function destroy_client(C: TClient): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
C.Free;
try
C.Free;
except on e : Exception do
result := RESULT_ERROR;
set_last_error(e.message):
end;
end;
{ Set (verbose) debug on/off }
procedure set_debug(v: Boolean); cdecl;
begin
debug := v;
debug := v;
end;
{ Get debug }
function get_debug: boolean; cdecl;
begin
exit(debug);
exit(debug);
end;
{
@ -99,40 +97,40 @@ end;
}
function get_last_error: pchar; cdecl;
begin
exit(@last_error[1]);
exit(@last_error[1]);
end;
{ Turn an array into a pointer. The pointer memory is not managed by FPC, so we can pass
it along happily. It'll have to be freed by the external control though }
function array_to_ptr(ptr: Pointer; size: PtrUInt; objsize: PtrUInt): Pointer; cdecl;
begin
result := GetMem(objsize * size);
Move(ptr^, result^, objsize * size);
result := GetMem(objsize * size);
Move(ptr^, result^, objsize * size);
end;
{ Free memory previously allocated by libMML }
function free_ptr(ptr: pointer): boolean; cdecl;
begin
result := Assigned(ptr);
if not result then
begin
set_last_error('TClient is NULL');
if debug then
writeln(last_error);
end else
FreeMem(ptr);
result := Assigned(ptr);
if not result then
begin
set_last_error('TClient is NULL');
if debug then
writeln(last_error);
end else
FreeMem(ptr);
end;
{ Allocate memory with libMML }
function alloc_mem(size, objsize: PtrUInt): Pointer; cdecl;
begin
result := GetMem(size * objsize);
result := GetMem(size * objsize);
end;
{ Reallocate memory with libMML }
function realloc_mem(ptr: Pointer; size, objsize: PtrUInt): Pointer; cdecl;
begin
result := ReAllocMem(ptr, size*objsize);
result := ReAllocMem(ptr, size*objsize);
end;
{ Mouse }
@ -141,39 +139,30 @@ end;
function get_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
C.IOManager.GetMousePos(t.x,t.y);
result := RESULT_OK;
except on e : Exception do
if not validate_client(C) then
begin
result := RESULT_ERROR;
set_last_error(e.Message);
exit(RESULT_ERROR);
end;
try
C.IOManager.GetMousePos(t.x,t.y);
result := RESULT_OK;
except on e : Exception do
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;
{ Set mouse position of client C to point t }
function set_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
C.IOManager.MoveMouse(t.x,t.y);
result := RESULT_OK;
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
try
C.IOManager.MoveMouse(t.x,t.y);
result := RESULT_OK;
except on e : Exception do
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;
@ -183,53 +172,41 @@ begin
case int of
0 : result := mouse_Left;
1 : result := mouse_Right;
2: result := mouse_Middle;
2 : result := mouse_Middle;
end;
end;
{ Return the state of a mouse button given client C }
function get_mouse_button_state(C: TClient; But: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.IOManager.IsMouseButtonDown(ConvIntClickType(But)) then
result := MOUSE_DOWN;
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
try
if C.IOManager.IsMouseButtonDown(ConvIntClickType(But)) then
result := MOUSE_DOWN;
else
result := MOUSE_UP;
except on e : Exception do
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;
{ Set the state of a mouse button given client C }
function set_mouse_button_state(C: TClient; But, State, X, Y: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if State = MOUSE_UP then
begin
C.IOManager.ReleaseMouse(X, Y, ConvIntClickType(But));
result := RESULT_OK;
end else if state = MOUSE_DOWN then
begin
C.IOManager.HoldMouse(X, Y, ConvIntClickType(But));
result := RESULT_OK;
try
if State = MOUSE_UP then
begin
C.IOManager.ReleaseMouse(X, Y, ConvIntClickType(But));
result := RESULT_OK;
end else if state = MOUSE_DOWN then
begin
C.IOManager.HoldMouse(X, Y, ConvIntClickType(But));
result := RESULT_OK;
end;
except on e : Exception do
result := RESULT_ERROR;
set_last_error(e.Message);
end;
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
end;
@ -238,67 +215,46 @@ end;
function get_color(C: TClient; x, y: Integer;
out color: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
color := C.IOManager.GetColor(x, y);
if color > -1 then
result := RESULT_OK
else
result := RESULT_FALSE;
try
color := C.IOManager.GetColor(x, y);
if color > -1 then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
set_last_error(e.message);
result := RESULT_ERROR;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;
{ Find color on client C in area (x1,y1,x2,y2) and return coordinate (if any) in x, y }
function find_color(C: TClient; var x, y: Integer;
color, x1, y1, x2, y2: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColor(x, y, color, x1, y1, x2, y2) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.Message);
result := RESULT_ERROR;
try
if C.MFinder.FindColor(x, y, color, x1, y1, x2, y2) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
set_last_error(e.Message);
result := RESULT_ERROR;
end;
end;
end;
function find_color_tolerance(C: TClient; var x, y: Integer; color: Integer;
tol, x1, y1, x2, y2: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColorTolerance(x, y, color, x1, y1, x2, y2, tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.Message);
result := RESULT_ERROR;
try
if C.MFinder.FindColorTolerance(x, y, color, x1, y1, x2, y2, tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
set_last_error(e.Message);
result := RESULT_ERROR;
end;
end;
end;
function find_color_tolerance_optimised(C: TClient; var x, y: Integer;
@ -306,22 +262,16 @@ function find_color_tolerance_optimised(C: TClient; var x, y: Integer;
x1, y1, x2, y2: Integer;
tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColorToleranceOptimised(x, y, col, x1, y1, x2, y2,
tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
try
if C.MFinder.FindColorToleranceOptimised(x, y, col, x1, y1, x2, y2,
tol) then
result := RESULT_OK
else
result := RESULT_FALSE;
except on e : Exception do
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
end;
function find_colors(C: TClient; var ptr: PPoint; var len: Integer;
@ -329,28 +279,23 @@ function find_colors(C: TClient; var ptr: PPoint; var len: Integer;
var
TPA: TPointArray;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
SetLength(TPA, 0);
try
C.MFinder.FindColors(TPA, color, x1, y1, x2, y2);
except on e : Exception do
begin
set_last_error(e.Message);
result := RESULT_ERROR;
setlength(TPA, 0);
try
C.MFinder.FindColors(TPA, color, x1, y1, x2, y2);
except on e : Exception do
set_last_error(e.Message);
result := RESULT_ERROR;
end;
end;
len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
if len > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
setlength(tpa, 0);
len := Length(TPA);
if len > 0 then
result := RESULT_OK
else
setlength(tpa, 0);
exit(RESULT_FALSE);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
setlength(tpa, 0);
end;
function find_colors_tolerance(C: TClient; var ptr: PPoint; var len: Integer;
@ -358,11 +303,6 @@ function find_colors_tolerance(C: TClient; var ptr: PPoint; var len: Integer;
var
TPA: TPointArray;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
C.MFinder.FindColorsTolerance(TPA, color, x1, y1, x2, y2, tol);
except on e : Exception do
@ -373,11 +313,13 @@ begin
end;
len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
if len > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
setlength(tpa, 0);
exit(RESULT_FALSE);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
setlength(TPA, 0);
end;
@ -388,10 +330,6 @@ function find_colors_tolerance_optimised(C: TClient; var ptr: PPoint;
var
TPA: TPointArray;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
C.MFinder.FindColorsToleranceOptimised(TPA, col, x1, y1, x2, y2, tol);
except on e : Exception do
@ -402,19 +340,18 @@ begin
end;
len := Length(TPA);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
if len > 0 then
result := RESULT_OK
else
result := RESULT_FALSE;
setlength(tpa, 0);
exit(RESULT_FALSE);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
setlength(TPA, 0);
end;
function similar_colors(C: TClient; col1, col2, tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
if C.MFinder.SimilarColors(col1, col2, tol) then
result := RESULT_OK
else
@ -424,10 +361,6 @@ end;
function count_color(C: TClient; out count: Integer;
Color, xs, ys, xe, ye: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
count := C.MFinder.CountColor(Color, xs, ys, xe, ye);
@ -447,10 +380,6 @@ end;
function count_color_tolerance(C: TClient; out count: Integer; col: Integer;
xs, ys, xe, ye, tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
count := C.MFinder.CountColorTolerance(col, xs, ys, xe, ye, tol);
except on e : Exception do
@ -468,10 +397,6 @@ end;
function find_color_spiral(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColorSpiral(x, y, col, xs, ys, xe, ye) then
result := RESULT_OK
@ -489,10 +414,6 @@ function find_color_spiral_tolerance(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye: Integer;
tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColorSpiralTolerance(x, y, col, xs, ys, xe, ye, tol) then
result := RESULT_OK
@ -509,10 +430,6 @@ end;
function find_colored_area(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye, minA: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColoredArea(x, y, col, xs, ys, xe, ye, minA) then
result := RESULT_OK
@ -530,10 +447,6 @@ function find_colored_area_tolerance(C: TClient; var x, y: Integer;
col, xs, ys, xe, ye, minA: Integer;
tol: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
if C.MFinder.FindColoredAreaTolerance(x, y, col,
xs, ys, xe, ye, minA, tol) then
@ -550,10 +463,6 @@ end;
function set_tolerance_speed(C: TClient; nCTS: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.MFinder.SetToleranceSpeed(nCTS);
@ -569,21 +478,21 @@ end;
function get_tolerance_speed(C: TClient; out cts: Integer): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
cts := C.MFinder.GetToleranceSpeed;
result := RESULT_OK;
try
begin
cts := C.MFinder.GetToleranceSpeed;
result := RESULT_OK;
end
except on e: Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
function set_tolerance_speed_2_modifiers(C: TClient;
nHue, nSat: Extended): Integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
begin
C.MFinder.SetToleranceSpeed2Modifiers(nHue, nSat);
@ -602,14 +511,18 @@ function get_tolerance_speed_2_modifiers(C: TClient; out hueMod: Extended;
var
h, s: Extended;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
C.MFinder.GetToleranceSpeed2Modifiers(h, s);
hueMod := h;
satMod := s;
result := RESULT_OK;
try:
begin
C.MFinder.GetToleranceSpeed2Modifiers(h, s);
hueMod := h;
satMod := s;
result := RESULT_OK;
end;
except on e : Exception do
begin
set_last_error(e.message);
result := RESULT_ERROR;
end;
end;
{ DTM }
@ -636,11 +549,6 @@ end;
{ Delete a MDTM. Don't delete it if it is managed! use remove_dtm instead }
function delete_dtm(C: TClient; DTM: TMDTM): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
if not assigned(DTM) then
begin
set_last_error('DTM is NULL');
@ -655,28 +563,26 @@ end;
{ Add a previously created DTM to the DTM Manager }
function add_dtm(C: TClient; DTM: TMDTM; var index: integer): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
if not assigned(DTM) then
begin
set_last_error('DTM is NULL');
exit(RESULT_ERROR);
end;
index := C.MDTMs.AddDTM(DTM);
try:
begin
index := C.MDTMs.AddDTM(DTM);
exit(RESULT_OK);
end;
except on e : Exception do
begin
exit(RESULT_ERROR);
end;
end;
{ Remove a previously added DTM from the DTM manager. This also frees the DTM }
function remove_dtm(C: TClient; DTMi: integer): integer; cdecl;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
C.MDTMs.FreeDTM(DTMi);
end;
@ -685,11 +591,6 @@ function find_dtm(C: TClient; DTMi: integer; var x, y: integer; x1, y1, x2, y2:
var
res: boolean;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
end;
try
res := C.MFinder.FindDTM(C.MDTMs.DTM[DTMi], x, y, x1, y1, x2, y2);
except on e : Exception do
@ -705,13 +606,34 @@ begin
result := RESULT_FALSE;
end;
function set_array_target(C: TClient; Arr: PRGB32; Size: TPoint): integer; cdecl;
{ Find a DTM given DTM index i, client C in area x1,y1,x2,y2. Return coord at x, y. }
function find_dtms(C: TClient; DTMi: integer; ptr: PPoint; x1, y1, x2, y2: integer): integer; cdecl;
var
res: boolean;
TPA: TPointArray;
begin
if not validate_client(C) then
begin
exit(RESULT_ERROR);
try
res := C.MFinder.FindDTMs(C.MDTMs.DTM[DTMi], TPA, x1, y1, x2, y2);
except on e : Exception do
begin
result := RESULT_ERROR;
set_last_error(e.Message);
end;
end;
len := Length(TPA);
if len > 0 then
result := RESULT_OK
else
setlength(tpa, 0);
exit(RESULT_FALSE);
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
setlength(TPA, 0);
end;
function set_array_target(C: TClient; Arr: PRGB32; Size: TPoint): integer; cdecl;
begin
if not assigned(Arr) then
begin
set_last_error('Arr is not assigned');
@ -753,4 +675,4 @@ exports
begin
end.
end.