SRL Archive - OSR Advanced Scripting Tutorials

How to create a plugin for Simba


Rick

2010-09-05 21:35:29

How to create a plugin for Simba


First, you will need to obtain the 32-Bit version of Lazarus + fpc 2.4.3 (Obviously download a later version if available).

Once you have that installed and setup, your environment should look similar to this:


Now go to File -> New … and select Library.


You should see this:


Next go to File -> Save As and save the file under the name you want to call your plugin.

For example, if I save my plugin as "TestPlugin" you should see the code in the editor change to this:


Notice "library Project1" changed to "library TestPlugin".

Next we need to add "SysUtils" to the uses declaration:


If it is needed you can add other items to the uses declaration, such as "math".

Now we will add your functions. Example:


function Internal_Func: String;
begin
Result := 'Internal_Func';
end;

function Test_Func(var T: String): Integer; register;
begin
T := Internal_Func;
Result := 123;
end;

procedure Test_Proc(var T: String); register;
begin
T := 'Test_Proc';
end;

Internal_Func will be used within the plugin only, whilst Test_Func and Test_Proc will be exported.

Once you have added your functions you need to export them. This can be done with the following three functions.

1:


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

This is the amount of functions we are exporting.

2:


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

Adjust 0..1 to the amount of functions you are exporting. For example, if I had 4 functions I would change it to:


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

3:


function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
begin
case x of
0:
begin
ProcAddr := @Test_Func;
StrPCopy(ProcDef, 'function Test_Func(var T: String): Integer;');
end;
1:
begin
ProcAddr := @Test_Proc;
StrPCopy(ProcDef, 'procedure Test_Proc(var T: String)');
end;
else
x := -1;
end;
Result := x;
end;

When adding your own functions change the value of "ProcAddr" to the name of your function without parametres, and in StrPCopy use the full name of the function with parametres.

Lastly, you need to finish the plugin with:


exports GetFunctionCount;
exports GetFunctionInfo;
exports GetFunctionCallingConv;

begin
end.

Here is an example of what your finished plugin could look like:


library TestPlugin;

{$mode objfpc}{$H+}

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

{$R *.res}

function Internal_Func: String;
begin
Result := 'Internal_Func';
end;

function Test_Func(var T: String): Integer; register;
begin
T := Internal_Func;
Result := 123;
end;

procedure Test_Proc(var T: String); register;
begin
T := 'Test_Proc';
end;

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

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

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
begin
case x of
0:
begin
ProcAddr := @Test_Func;
StrPCopy(ProcDef, 'function Test_Func(var T: String): Integer;');
end;
1:
begin
ProcAddr := @Test_Proc;
StrPCopy(ProcDef, 'procedure Test_Proc(var T: String);');
end;
else
x := -1;
end;
Result := x;
end;

exports GetFunctionCount;
exports GetFunctionInfo;
exports GetFunctionCallingConv;

begin
end.

Once you are finished, go ahead and press Ctrl - F9, or Run -> Build. This will compile the plugin. If everything was correct you should see this:


Finally, browse to the folder where you saved the plugin and locate the dll. Copy and Paste it to Simba/Plugins.

An example of using your plugin in Simba:


program SimbaPluginTest;
{$LoadLib TestPlugin}

var
t: String;

begin
Writeln(Test_Func(t));
Writeln(t);
Test_Proc(t);
Writeln(t);
end.

Replacing "TestPlugin" with the name of your plugin.
Running this produces:


Compiled succesfully in 15 ms.
123
Internal_Func
Test_Proc
Successfully executed.

Congratulations, you have created a basic plugin for Simba :)

Errors
If you receive the following error message in Simba:


Exception in Script: Error loading plugin

This means you have compiled your plugin with a 64-Bit compiler, whilst using a Simba compiled in 32-Bit.

Thanks for reading.

Wizzup?

2010-09-06 02:08:39

Have you tried compiling a 64 bit plugin for Simba 64 bit? I wonder how well it works.

Nice tutorial! :)

Esteban

2010-09-06 12:35:28

Even though it is in the advanced section, it would probably be good to explain why we use plugins

Awkwardsaw

2010-09-06 12:39:11

pics dont work. :)

Esteban

2010-09-06 13:04:37

They work for me.

Wizzup?

2010-09-06 13:41:29

From: Esteban
Even though it is in the advanced section, it would probably be good to explain why we use olivine


What is olivine?

Rick

2010-09-06 13:45:01

Wizzup?: Thanks, I'll try that out, although I'm having trouble compiling Simba in 64-Bit.

Awkwardsaw: What image host works for you ? I'll upload again and post alternative image links.

Esteban: What is olivine ?

Shuttleu

2010-09-06 13:48:14

From: r!ch!e
Wizzup?: Thanks, I'll try that out, although I'm having trouble compiling Simba in 64-Bit.


Simba has already been compiled in 64 bit, but there is no 64 bit smart which means no smart for 64 bit windows

http://villavu.com/forum/showthread.php?t=58775

~shut

Rick

2010-09-07 17:53:23

From: Shuttleu
Simba has already been compiled in 64 bit, but there is no 64 bit smart which means no smart for 64 bit windows

http://villavu.com/forum/showthread.php?t=58775

~shut


Yes, but I wanted to compile a 64-Bit Simba myself, which I have now been able thanks to Wizzup? :)

Also, I'm not interested in using SMART, I was going to check how well plugins compiled with the 64-Bit Lazarus compiler work with the 64-Bit Simba.

Shuttleu

2010-09-07 17:57:42

From: r!ch!e
Yes, but I wanted to compile a 64-Bit Simba myself, which I have now been able thanks to Wizzup? :)

Also, I'm not interested in using SMART, I was going to check how well plugins compiled with the 64-Bit Lazarus compiler work with the 64-Bit Simba.


ah, ok, my bad
congratz tho

~shut

Esteban

2010-09-07 21:46:11

From: r!ch!e
Wizzup?: Thanks, I'll try that out, although I'm having trouble compiling Simba in 64-Bit.

Awkwardsaw: What image host works for you ? I'll upload again and post alternative image links.

Esteban: What is olivine ?


I think my ipod auto-corrected the word plugins into olivine? fixed my post

Daniel

2010-09-08 02:34:46

Hint: Don't export methods that use strings.

Nice tutorial though. :)

Shuttleu

2010-09-08 02:49:15

From: r!ch!e

This means you have compiled your plugin with a 64-Bit compiler, whilst using a Simba compiled in 32-Bit.

or vice versa?

~shut

Wizzup?

2010-09-08 09:51:50

[QUOTE=Dan's The Man;735762]Hint: Don't export methods that use strings.

Nice tutorial though. :)

The memory sharing unit should be able to handle this.

moparisthebest

2010-09-22 01:08:03

Are there any plugins that currently work with Simba besides irokiplugin, skyplugin, and SMART as included in the SRL repo? I've gotten them to compile, but the other ones like boxplugin and wizzyplugin all complain about not having the unit 'FastSharemem', which google leads me to believe isn't available with FPC.

Wanted

2010-09-22 01:09:59

Boxplugin is no longer needed (I think) and simba doesn't use wizzyplugin, those functions are hard coded in simba. I think it only uses the plugins in SRL/place inside plugins folder/simba/

moparisthebest

2010-09-22 01:28:34

From: IceFire908
Boxplugin is no longer needed (I think) and simba doesn't use wizzyplugin, those functions are hard coded in simba. I think it only uses the plugins in SRL/place inside plugins folder/simba/


Thanks, then in that case, we are good to go. :)

EGM

2010-11-09 06:51:27

Can you provide an example (or point me to one) on how to create a plugin that imports a type? Specifically, some talk in the forums regarding json and xml config files made me curious about creating a plugin for reading/writing different config files and I would like to import TXMLConfig to use in my scripts.

Thanks!

marpis

2010-12-11 14:04:49

Waking up the thread!
How do I import functions from Simba?

i luffs yeww

2010-12-11 17:48:49

Into the plugin? Uhh.. wouldn't the functions already be in Simba so you could use them normally? :x Not sure but it makes sense to me. Unless it's like libmml then it's still easy.

Shuttleu

2010-12-11 17:50:30

but if its in simba and not the extension, then lazarus wont compile it into a DLL

~shut