2010-03-25 20:36:09 -04:00
|
|
|
library libmml;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
uses
|
2010-03-26 10:20:50 -04:00
|
|
|
cmem,Classes,interfaces,graphics,client,sysutils,MufasaTypes,dtmutil;
|
2010-03-25 20:36:09 -04:00
|
|
|
|
|
|
|
{$R *.res}
|
|
|
|
|
2010-03-30 18:24:16 -04:00
|
|
|
Const
|
|
|
|
RESULT_OK = 0;
|
2010-06-01 07:40:51 -04:00
|
|
|
RESULT_FALSE = 1;
|
2010-04-01 19:48:06 -04:00
|
|
|
RESULT_ERROR = -1;
|
|
|
|
|
|
|
|
MOUSE_UP = 0;
|
|
|
|
MOUSE_DOWN = 1;
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-03-25 20:36:09 -04:00
|
|
|
var
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error: String;
|
2010-09-05 14:43:45 -04:00
|
|
|
debug: boolean;
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function init: integer;
|
|
|
|
begin
|
|
|
|
last_error := '';
|
|
|
|
debug := true;
|
|
|
|
result := RESULT_OK;
|
|
|
|
end;
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function validate_client(C: TClient): boolean; inline;
|
2010-03-25 20:36:09 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
result := Assigned(C);
|
|
|
|
if not result then
|
|
|
|
begin
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := 'PClient is NULL';
|
2010-09-05 14:43:45 -04:00
|
|
|
if debug then
|
|
|
|
writeln(last_error);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function create_client: PtrUInt; cdecl;
|
|
|
|
var
|
|
|
|
C: TClient;
|
|
|
|
begin
|
|
|
|
try
|
|
|
|
C := TClient.Create('');
|
|
|
|
Result := PtrUInt(C);
|
|
|
|
except on e : Exception do
|
|
|
|
begin
|
|
|
|
writeln('ERROR');
|
|
|
|
result := PtrUInt(RESULT_ERROR);
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
writeln(format('C: %d, IOManager: %d', [PtrUInt(C), PtrUInt(C.IOManager)]));
|
|
|
|
end;
|
|
|
|
|
|
|
|
function destroy_client(C: TClient): integer;
|
|
|
|
begin
|
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
C.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure set_debug(v: Boolean);
|
|
|
|
begin
|
|
|
|
debug := v;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function get_debug: boolean;
|
|
|
|
begin
|
|
|
|
exit(debug);
|
|
|
|
end;
|
|
|
|
|
2010-09-12 08:28:19 -04:00
|
|
|
{
|
|
|
|
VERY IMPORTANT: If you use get_last_error, you must immediately store the
|
|
|
|
resulting string somewhere else. As soon as you do other calls, the last error
|
|
|
|
may be reset or assigned a different memory position, making your old
|
|
|
|
pointer invalid.
|
|
|
|
}
|
2010-09-05 14:43:45 -04:00
|
|
|
function get_last_error: pchar;
|
|
|
|
begin
|
2010-09-12 08:28:19 -04:00
|
|
|
exit(@last_error[1]);
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
|
|
|
|
|
|
|
function array_to_ptr(ptr: Pointer; size: PtrUInt; objsize: PtrUInt): Pointer;
|
|
|
|
begin
|
|
|
|
result := GetMem(objsize * size);
|
|
|
|
Move(ptr^, result^, objsize * size);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function free_ptr(ptr: pointer): boolean;
|
|
|
|
begin
|
|
|
|
result := Assigned(ptr);
|
|
|
|
if not result then
|
|
|
|
begin
|
|
|
|
last_error := PChar('TClient is NULL');
|
|
|
|
if debug then
|
|
|
|
writeln(last_error);
|
|
|
|
end else
|
2010-09-12 09:06:11 -04:00
|
|
|
FreeMem(ptr);
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
|
|
|
|
|
|
|
function alloc_mem(size, objsize: PtrUInt): Pointer;
|
|
|
|
begin
|
|
|
|
result := GetMem(size * objsize);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function realloc_mem(ptr: Pointer; size, objsize: PtrUInt): Pointer;
|
|
|
|
begin
|
2010-09-12 09:06:11 -04:00
|
|
|
result := ReAllocMem(ptr, size*objsize);
|
2010-03-25 20:36:09 -04:00
|
|
|
end;
|
|
|
|
|
2010-03-30 18:24:16 -04:00
|
|
|
{ Mouse }
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function get_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-03-25 20:36:09 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
|
2010-03-30 18:24:16 -04:00
|
|
|
try
|
|
|
|
C.IOManager.GetMousePos(t.x,t.y);
|
|
|
|
result := RESULT_OK;
|
|
|
|
except on e : Exception do
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-03-30 18:24:16 -04:00
|
|
|
end;
|
2010-03-25 20:36:09 -04:00
|
|
|
end;
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function set_mouse_pos(C: TClient; var t: tpoint): integer; cdecl;
|
2010-04-01 19:48:06 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
|
2010-04-01 19:48:06 -04:00
|
|
|
try
|
2010-05-31 19:59:48 -04:00
|
|
|
C.IOManager.MoveMouse(t.x,t.y);
|
2010-04-01 19:48:06 -04:00
|
|
|
result := RESULT_OK;
|
|
|
|
except on e : Exception do
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-04-01 19:48:06 -04:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function ConvIntClickType(Int : Integer) : TClickType; inline;
|
2010-04-01 19:48:06 -04:00
|
|
|
begin
|
2010-03-30 18:24:16 -04:00
|
|
|
case int of
|
2010-04-01 19:48:06 -04:00
|
|
|
0 : result := mouse_Left;
|
|
|
|
1 : result := mouse_Right;
|
|
|
|
2: result := mouse_Middle;
|
2010-03-30 18:24:16 -04:00
|
|
|
end;
|
2010-04-01 19:48:06 -04:00
|
|
|
end;
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function get_mouse_button_state(C: TClient; But: Integer): Integer;
|
2010-04-01 19:48:06 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
writeln(but);
|
|
|
|
|
2010-04-01 19:48:06 -04:00
|
|
|
try
|
|
|
|
if C.IOManager.IsMouseButtonDown(ConvIntClickType(But)) then
|
|
|
|
result := MOUSE_DOWN;
|
|
|
|
except on e : Exception do
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-04-01 19:48:06 -04:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function set_mouse_button_state(C: TClient; But, State, X, Y: Integer): Integer;
|
2010-04-01 19:48:06 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
|
2010-04-01 19:48:06 -04:00
|
|
|
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
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-04-01 19:48:06 -04:00
|
|
|
end;
|
|
|
|
end;
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function find_color(C: TClient; var x, y: integer; color, x1, y1, x2, y2: integer): integer;
|
2010-05-31 19:59:48 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
|
2010-05-31 20:03:25 -04:00
|
|
|
try
|
2010-06-01 07:40:51 -04:00
|
|
|
if C.MFinder.FindColor(x, y, color, x1, y1, x2, y2) then
|
|
|
|
result := RESULT_OK
|
|
|
|
else
|
|
|
|
result := RESULT_FALSE;
|
2010-05-31 20:03:25 -04:00
|
|
|
except on e : Exception do
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
|
|
|
writeln('last_error: ' + last_error);
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-05-31 20:03:25 -04:00
|
|
|
end;
|
2010-05-31 19:59:48 -04:00
|
|
|
end;
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function find_color_tolerance(C: TClient; var x, y: integer; color, tol, x1, y1, x2, y2: integer): integer;
|
|
|
|
|
2010-03-26 10:20:50 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
|
2010-06-01 07:40:51 -04:00
|
|
|
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
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-06-01 07:40:51 -04:00
|
|
|
end;
|
2010-03-26 10:20:50 -04:00
|
|
|
end;
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function find_colors(C: TClient; var ptr: PPoint; var len: Integer; color, x1, y1, x2, y2: integer): integer;
|
2010-03-30 18:24:16 -04:00
|
|
|
var
|
2010-06-01 07:40:51 -04:00
|
|
|
TPA: TPointArray;
|
2010-03-30 18:24:16 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
|
|
|
|
SetLength(TPA, 0);
|
2010-06-01 07:40:51 -04:00
|
|
|
try
|
|
|
|
C.MFinder.FindColors(TPA, color, x1, y1, x2, y2);
|
|
|
|
except on e : Exception do
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-06-01 07:40:51 -04:00
|
|
|
end;
|
2010-09-05 14:43:45 -04:00
|
|
|
|
|
|
|
len := Length(TPA);
|
|
|
|
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
|
|
|
|
result := RESULT_OK;
|
|
|
|
setlength(tpa, 0);
|
2010-03-30 18:24:16 -04:00
|
|
|
end;
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
function find_colors_tolerance(C: TClient; var ptr: PPoint; var len: Integer; color, tol, x1, y1, x2, y2: integer): integer;
|
2010-03-30 18:24:16 -04:00
|
|
|
var
|
2010-06-01 07:40:51 -04:00
|
|
|
TPA: TPointArray;
|
2010-03-30 18:24:16 -04:00
|
|
|
begin
|
2010-09-05 14:43:45 -04:00
|
|
|
if not validate_client(C) then
|
|
|
|
begin
|
|
|
|
exit(RESULT_ERROR);
|
|
|
|
end;
|
|
|
|
|
2010-06-01 07:40:51 -04:00
|
|
|
try
|
|
|
|
C.MFinder.FindColorsTolerance(TPA, color, x1, y1, x2, y2, tol);
|
|
|
|
except on e : Exception do
|
2010-09-05 14:43:45 -04:00
|
|
|
begin
|
|
|
|
result := RESULT_ERROR;
|
2010-09-12 08:28:19 -04:00
|
|
|
last_error := e.Message;
|
2010-09-05 14:43:45 -04:00
|
|
|
end;
|
2010-06-01 07:40:51 -04:00
|
|
|
end;
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
len := Length(TPA);
|
|
|
|
ptr := array_to_ptr(Pointer(@TPA[0]), len, sizeof(TPoint));
|
|
|
|
result := RESULT_OK;
|
2010-03-30 18:24:16 -04:00
|
|
|
end;
|
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
exports
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
init,
|
|
|
|
create_client,
|
|
|
|
get_last_error,
|
|
|
|
get_debug,
|
|
|
|
set_debug,
|
|
|
|
alloc_mem,
|
|
|
|
realloc_mem,
|
|
|
|
free_ptr,
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
get_mouse_pos, set_mouse_pos,
|
|
|
|
get_mouse_button_state, set_mouse_button_state,
|
2010-03-30 18:24:16 -04:00
|
|
|
|
2010-09-05 14:43:45 -04:00
|
|
|
find_color, find_color_tolerance,
|
|
|
|
|
|
|
|
find_colors, find_colors_tolerance;
|
2010-03-25 20:36:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
end.
|
|
|
|
|