SRL Archive - Non-RuneScape Scripts

Lazarus + Simba Functions


caused

2012-09-07 08:27:52

Hey guys :D
Is there a Tutorial on how to implement Simba Functions into Lazarus, without crawling all the source :D ?

I'm writing a bot and need to do complex math and database queries, where Simba is just not so good for :x … But I'd love to use some stuff, like dtm detection and so on :D …

Since simba is written in lazarus, things may be easily achieved :).
I'm just not an expert with the Simba Source.

Could someone post an example on how to IE use the FindBItmap Function within lazarus ?

Also it was spoken about a .dll file that would inlcude those functions, but that was ages ago, and I cant remember the Thread :x …

~caused

CynicRus

2012-09-07 08:45:40

See simba source in /Units/MMLCore and /Units/MMLAddon. How to use MML functions just read wrappers sources.

caused

2012-09-07 09:49:48

From: CynicRus
See simba source in /Units/MMLCore and /Units/MMLAddon. How to use MML functions just read wrappers sources.


Thanks for pointing me to the right direction :)

I will have a look and get back if I have more questions :D…

THANKS THANKS THANKS :)

masterBB

2012-09-07 10:53:36

Do you want to create a plugin or add functions to simba?

caused

2012-09-07 10:54:38

From: masterBB
Do you want to create a plugin or add functions to simba?


I Want to "use" simba functions with lazarus :D

masterBB

2012-09-07 10:55:53

Ah, you won't use simba at all? Just the functions?

litoris

2012-09-07 11:02:57

He wants to port it, so he does want to use it, just not with PS.

caused

2012-09-09 12:56:40

From: masterBB
Ah, you won't use simba at all? Just the functions?


Right :)

CynicRus

2012-09-09 12:59:20

If your project is an open license - I'm willing to help with this.

masterBB

2012-09-09 13:08:25

An example of a stand alone project using MML functions is MMLBenchmark: https://github.com/MerlijnWajer/Simba/blob/master/Projects/MMLBenchmark/project1.lpr

program project1;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, CustApp,
interfaces, // to prevent linking errors. :|

client, MufasaTypes,

lclintf;

type

{ TMMLBenchmark }

TMMLBenchmark = class(TCustomApplication)
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
end;

var
bmp: Integer;

MMLClient: TClient;
{ TMMLBenchmark }

procedure init;

begin
MMLClient := TClient.Create('');

bmp := MMLClient.MBitmaps.CreateBMP(0, 0);
MMLClient.MBitmaps.GetBMP(bmp).LoadFromFile('/home/merlijn/Programs/simba/wall.bmp');
MMLClient.IOManager.SetTarget(MMLClient.MBitmaps[bmp]);
end;

procedure free;
begin
MMLClient.IOManager.SetDesktop;
MMLClient.MBitmaps.FreeBMP(bmp);
MMLClient.Free;
end;

procedure Benchmark;

var
t, t2, t3: PtrUInt;
i, c, w, h: integer;
col, tol: integer;

dummytpa: TPointArray;

begin
init;
t := gettickcount;
MMLClient.IOManager.GetDimensions(w, h);

col := 16777215; tol := 30;

for c := 0 to 3 do // benchmark all CTS'es
begin
MMLClient.MFinder.SetToleranceSpeed(c);
t2 := gettickcount;
for i := 0 to 100 do
begin
MMLClient.MFinder.FindColorsTolerance(dummytpa, col, 0, 0, w-1,h-1, tol);
end;
t3 := gettickcount;
writeln(format('FindColorsTolerance'+#10+
'Time spent: %d ms (per %f ms), matches: %d, CTS: %d, Colour: %d, Tolerance %d',
[t3-t2, (t3-t2) / 100.0, length(dummytpa), c, col, tol]));
end;

free;
end;

procedure TMMLBenchmark.DoRun;
var
ErrorMsg: String;
begin
// quick check parameters
ErrorMsg:=CheckOptions('h','help');
if ErrorMsg<>'' then begin
ShowException(Exception.Create(ErrorMsg));
Terminate;
Exit;
end;

// parse parameters
if HasOption('h','help') then begin
WriteHelp;
Terminate;
Exit;
end;

Benchmark;

{ add your program here }

// stop program loop
Terminate;
end;

constructor TMMLBenchmark.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
end;

destructor TMMLBenchmark.Destroy;
begin
inherited Destroy;
end;

procedure TMMLBenchmark.WriteHelp;
begin
{ add your help code here }
writeln('Usage: ',ExeName,' -h');
end;

var
Application: TMMLBenchmark;

{$R *.res}

begin
Application:=TMMLBenchmark.Create(nil);
Application.Run;
Application.Free;
end.

caused

2012-09-09 13:42:08

This is interesting.

It Opens a Bitmap and uses FindColorsTolerance on that Source.

I'll try to see if I can use the example to "convert" the finder functions :D

caused

2012-09-09 17:42:56

I cant Compile the Example…

There are sooo many missing dependencies…. I already added so much stuff to the project.. but still more stuff missing.. Is there an easy way, to implement the simba includes completely in lazarus ?

masterBB

2012-09-09 18:03:24

I am able to run it. I know how to fix all the errors. Re download it and I will help you step by step.

caused

2012-09-09 19:08:14

From: masterBB
I am able to run it. I know how to fix all the errors. Re download it and I will help you step by step.


Thats very kind :)

I re-downloaded the whole source.

What now :D !?

caused

2012-09-10 20:49:57

anyone D: ?

CynicRus

2012-09-11 00:50:02

now you need include MML to you project:)

masterBB

2012-09-11 04:36:07

What time zone do you live in, is it possible to go on a chat today?

Brandon

2012-09-11 08:26:16

You can just compile the MML Library into a DLL no? Then load that DLL and GetProcAddress of every function in it. Have your bot use that stuff to access functions?

OR do what you're doing I guess and compile that stuff into your bot..
Example:


unit dtm;

{$mode objfpc}{$H+}

interface

uses
Classes, SysUtils, MufasaTypes;

You see the part that says uses? Under it is all the includes needed to use that file. Remember that other files might also have includes too so just make sure to grab those as well. When done, it should not be asking for anymore dependencies.

For example, one of my oldest plugins was:

library SSecurity;

{$mode objfpc}{$H+}

uses
Classes, SysUtils, DCPbase64, DCPconst, DCPcrypt2, DCPmd5, StringFuncs,
MufasaTypes, stringutil, Forms, Dialogs, LCLType, Controls, Interfaces, IniFiles, XTea;

{$R *.res}

type
TCharArray = array of Char;

Function StringToCharArray(TheStr: string): TCharArray; register;
var
i, l: Integer;
begin
l := Length(TheStr);
SetLength(Result, l + 1);
for i := l downto 1 do
Result[i - 1] := TheStr[i];
end;

Function CharArrayToString(TheStr: TCharArray): String; register;
var
i, l: Integer;
begin
l := Length(TheStr) - 1;
for i:= 0 To l do
Result := Result + TheStr[i];
end;

Function ToASCII(AChar: char): Integer; register;
begin
result:= ord(AChar);
end;

Function ToChar(AChar: Byte): Char; register;
begin
result:= chr(AChar);
end;


Function DecToBin(Num: Int64): Int64; register;
var
bin, pos: Cardinal;
begin
bin := 0;
pos := 1;

while (Num > 0) do
begin
bin := bin + (Num mod 2) * pos;
Num := Num Div 2;
pos := pos * 10;
end;

Result:= Bin;
end;


Function Encrypt(StrToEncrypt, Key: String): String; register;
begin
Result:= Trim(Base64Encode(XTeaCryptStr(Trim(StrToEncrypt), Key)));
end;


Function Decrypt(StrToDecrypt, Key: String): String; register;
begin
Result:= Trim(XTeaDecryptStr(Base64Decode(Trim(StrToDecrypt)), Key));
end;


Procedure WriteToFile(StrToWrite: String); register;
var
FS: TOpenDialog;
Buffer, FName: String;
F: TFileStream;
begin
FS := TOpenDialog.Create(nil);
FS.Options := [ofFileMustExist];
FS.Filter := ' Simba Scripts (*.simba;*.Simba)|*.simba;*.Simba|All files (*.*)|*.*|';
FS.FilterIndex := 1;

if FS.Execute then
begin
FName:= FS.FileName;
F:= TFileStream.Create(FName, fmOpenReadWrite);
try
F.Position:= 0;
SetLength(Buffer, F.Size);
F.ReadBuffer(Buffer, F.Size);
Finally
F.Free;
end;

F:= TFileStream.Create(FName, fmOpenReadWrite);
try
F.Position:= 0;
Buffer := StringReplace(Buffer, Between('Pass', ';', Buffer), StrToWrite, [rfReplaceAll, rfIgnoreCase]);
F.WriteBuffer(Buffer, Length(Buffer));
writeln(Buffer);
Finally
F.Free;
end;
FS.Free;
end;
end;

{ EXPORTS }


procedure SetPluginMemoryManager(MemMgr : TMemoryManager); stdcall; export;
begin
SetMemoryManager(MemMgr);
end;


{ EXPORT TYPES }


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

function GetTypeInfo(x: Integer; var sType, sTypeDef: string): integer; stdcall;
begin
case x of
0: begin
sType := 'TCharArray';
sTypeDef := 'Array of Char;';
end;
else
Result := -1;
end;
end;



{ EXPORT FUNCS }

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

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

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
begin
case x of
0:
begin
ProcAddr := @StringToCharArray;
StrPCopy(ProcDef, 'Function StringToCharArray(TheStr: string): TCharArray;');
end;
1:
begin
ProcAddr := @CharArrayToString;
StrPCopy(ProcDef, 'Function CharArrayToString(TheStr: TCharArray): String;');
end;
2:
begin
ProcAddr := @ToASCII;
StrPCopy(ProcDef, 'Function ToASCII(AChar: char): Integer;');
end;
3:
begin
ProcAddr := @ToChar;
StrPCopy(ProcDef, 'Function ToChar(AChar: Byte): Char;');
end;
4:
begin
ProcAddr := @DecToBin;
StrPCopy(ProcDef, 'Function DecToBin(Num: Int64): Int64;');
end;
5:
begin
ProcAddr := @Encrypt;
StrPCopy(ProcDef, 'Function Encrypt(StrToEncrypt, Key: String): String;');
end;
6:
begin
ProcAddr := @Decrypt;
StrPCopy(ProcDef, 'Function Decrypt(StrToDecrypt, Key: String): String;');
end;
7:
begin
ProcAddr := @WriteToFile;
StrPCopy(ProcDef, 'Procedure WriteToFile(StrToWrite: String);');
end;
else
x := -1;
end;
Result := x;
end;


//Exports
exports SetPluginMemoryManager;

//Types
exports GetTypeCount;
exports GetTypeInfo;

//Functions
exports GetFunctionCount;
exports GetFunctionInfo;
exports GetFunctionCallingConv;

begin
end.


Right where it says uses is everything I needed to include inorder to use some Simba functions.

caused

2012-09-11 09:18:05

From: masterBB
What time zone do you live in, is it possible to go on a chat today?


UTC+1.. That would be nice.

caused

2012-09-11 09:32:29

@ Brandon:
Thanks for the example. I managed to compile and run the benchmark source now. :x .. stupid.. it was only 1 file missing.

I'll try if I can use the FindBitmap Function from lazarus :D

Thanks so far guys :))) &#9829;

caused

2012-09-11 09:50:18

I'm a little stuck again using the Bitmap Finding Function.
The Function requires a variable of type TMufasaBitmap.

I found the definition of that in bitmaps.pas, but cant figure out how to convert an actual bitmap to Tmufasabitmap ?

Anyone ?

//EDIT: NVM got it working :D.. woohow. Just dont know how to use bitmap strings now :x …

masterBB

2012-09-11 10:53:07

https://github.com/MerlijnWajer/Simba/blob/master/Units/MMLCore/bitmaps.pas#L429

caused

2012-09-11 11:20:39

From: masterBB
https://github.com/MerlijnWajer/Simba/blob/master/Units/MMLCore/bitmaps.pas#L429


yup figured that.. Weird that it needs width height though… In Simba it seems to work without that.

Got another Question.

Do you always need to execute MMLClient.MBitmaps.GetBMP(bmp).LoadFromTBitmap(testbmp);
MMLClient.IOManager.SetTarget(MMLClient.MBitmaps[bmp]);

when the target canvas refreshes ?
Or can you "hook" it to a target canvas ? If so.. how ???

CynicRus

2012-09-11 12:21:11

If i understand your question, you must read iomanager.pas line from 555 to 582.

caused

2012-09-11 13:30:13

ahh I dont get it :( ..

Can we set a HWND as target ?

Brandon

2012-09-11 14:15:27

From: caused
ahh I dont get it :( ..

Can we set a HWND as target ?


Tprocsys is the class you're looking for. It is a process info class. Look at FindAndSetTarget function to see how to use it..

Basically give the window name.. Partial name works too then it'll find a process with that name and also contains the handle to that process's window if it has one.


P.S. I think it might be tsysproc instead of Tprocsys but iunno can't remember..

Sent from phone..

masterBB

2012-09-11 14:33:27

type

TNativeWindow = Hwnd;

e:

Just use SetTarget(YourHwnd);

CynicRus

2012-09-11 14:59:16

See SetTarget code.

Wizzup?

2012-09-13 05:48:23

From: Brandon
You can just compile the MML Library into a DLL no? Then load that DLL and GetProcAddress of every function in it. Have your bot use that stuff to access functions?


You could do that, but you could also just include the units since you're writing in Lazarus/FreePascal. Several plugins also use Simba units so it shouldn't be too hard to figure out how to use it. But yeah, the documentation on doing this is pretty much void.