SRL Archive - General

SCAR 4 Plugins


Freddy1990

2011-08-04 09:57:24

I've finished the base for the SCAR 4 plugin system, it's already pretty functional. I've completely changed the design in comparison to the Divi plugin system to hopefully result in a far more flexible system. Also I've tried to make it more compatible with C++ by trying not to use any Delphi specific stuff.

Here's an example for a Delphi plugin:
library SamplePlugin;

uses
TitanPlugin in 'TitanPlugin.pas';

{$R *.res}

type
PTest = ^TTest;
TTest = record
a, b: Integer;
end;

function TestStdCall(i: Integer): Integer; stdcall;
begin
Result := i;
end;

function TestCDecl(i: Integer): Integer; cdecl;
begin
Result := i;
end;

function MakeTest(a, b: Integer): TTest;
begin
Result.a := a;
Result.b := b;
end;

// --

function LoadPlugin(VMajor, VMinor: Integer): Boolean; stdcall;
begin
Result := (VMajor = 4) and (VMinor >= 0);
end;

function UnloadPlugin: Boolean; stdcall;
begin
Result := True;
end;

procedure DoExport(Engine: Integer; EExport: PEExport; ELib: PELib); stdcall;
var
n, t: Integer;
begin
EExport^.RegisterDirectiveA(Engine, 'TEST_DELPHI');
n := EExport^.RegisterNamespaceW(Engine, 'Test');
EExport^.RegisterHeaderW(Engine, n, 'function TestStdCall(i: Integer): Integer; stdcall;', @TestStdCall);
EExport^.RegisterHeaderW(Engine, n, 'function TestCDecl(i: Integer): Integer; cdecl;', @TestCDecl);
t := EExport^.RegisterTypeA(Engine, n, 'TTest = record a, b: Integer; end;');
EExport^.RegisterPointerTypeA(Engine, n, 'PTest', t);
EExport^.RegisterDynArrTypeA(Engine, n, 'TTestArray', t);
EExport^.RegisterDynArrTypeA(Engine, n, 'TMyIntArray', T_Integer);
EExport^.RegisterHeaderA(Engine, n, 'function MakeTest(a, b: Integer): TTest;', @MakeTest);
end;

exports LoadPlugin;
exports UnloadPlugin;
exports DoExport;

end.

I've also created a c++ example with Visual Studio:
#include "Main.h"
#include "TitanPlugin.h"

typedef struct {
int a, b;
} TTest, *PTest;

int __stdcall TestStdCall(int i) {
return i;
}

int TestCDecl(int i) {
return i;
}

PTest __stdcall MakeTest(int a, int b) {
TTest t;
t.a = a;
t.b = b;
return &t;
}

// --

bool DLL_EXPORT LoadPlugin(int VMajor, int VMinor) {
return (VMajor == 4) && (VMinor >= 0);
}

bool DLL_EXPORT UnloadPlugin() {
return true;
}

void DLL_EXPORT DoExport(int Engine, PEExport EExport, PELib ELib) {
EExport->RegisterDirectiveA(Engine, "TEST_CPP");
int n = EExport->RegisterNamespaceA(Engine, "Test");
EExport->RegisterHeaderA(Engine, n, "function TestStdCall(i: Integer): Integer; stdcall;", &TestStdCall);
EExport->RegisterHeaderA(Engine, n, "function TestCDecl(i: Integer): Integer; cdecl;", &TestCDecl);
int t = EExport->RegisterTypeA(Engine, n, "TTest = record a, b: Integer; end;");
EExport->RegisterPointerTypeA(Engine, n, "PTest", t);
EExport->RegisterDynArrTypeA(Engine, n, "TTestArray", t);
EExport->RegisterDynArrTypeA(Engine, n, "TMyIntArray", T_Integer);
EExport->RegisterHeaderA(Engine, n, "function MakeTest(a, b: Integer): PTest; stdcall;", &MakeTest);
}

I know the naming conventions in the c++ example are horrible, but I wanted to keep the plugin as similar to the delphi one as possible for the example. Feel free to rename stuff to your liking :) As long as you don't rename LoadPlugin, UnloadPlugin and DoExport of course :p

Both of these plugins can be ran with this sample script:
{$PLUGIN SamplePlugin}

uses
Test, Types;

var
tmpTest: TTest;

begin
{$IFDEF TEST_CPP}
tmpTest := MakeTest(7, 8)^;
PrintLn('Using C++ SamplePlugin');
{$ELSE}
tmpTest := MakeTest(7, 8);
PrintLn('Using Delphi SamplePlugin');
{$ENDIF}
PrintLn(TestStdCall(5));
PrintLn(TestCDecl(6));
PrintLn(ToStr(tmpTest.a) + ' ' + ToStr(tmpTest.b));
end.

You can find all of the files for the delphi and c++ projects on here:
http://svn.scar-divi.com/titan_plugin/

The latest copy of SCAR 4 can be downloaded here:
http://svn.scar-divi.com/titan/


As for c++ support, there's some stuff that just might not be compatible with Delphi, so you may not be able to do everything in c++ you can with the Delphi plugin, but I'm trying to keep as much compatible as possible. I haven't succeeded in for example passing structs by value to a script from a function, maybe that's just because my c++ skills aren't as good as my Delphi ones, so if anyone can figure that out, let me know. In the example I've used a pointer to the struct to achieve the same thing. Note that this method is unsafe, the local struct var will be destroyed, that's why i'm copying the contents by value to a variable in the script, it prevents an error, using the pointer will probably not be an option.

EDIT: SCAR 4 will also feature a separated GUI plugin system btw, but I have more important things to focus on first.

Naum

2011-08-04 10:26:09

Looks awesome, good job!

LolL

2011-08-04 15:20:43

Yes, good job on the effort but…………

insult removed by Coh3n

Janilabo

2011-08-04 17:37:28

From: LolL
Yes, good job on the effort but…………

insult removed by Coh3n
Ay, you better try to show some respect here, mr. new guy…

On-topic: Good work, Freddy. Titan's progress is looking good so far..

mormonman

2011-08-04 19:19:51

It looks awesome, but I haven't found a need for a plugin besides something like SMART or APPA yet because paxCompiler is so fast.

Main

2011-08-05 19:37:18

Good job mate:P
do you think you can include a way to download srl without moving the plugin? I personally don't mind but it would be a nice thing to have:)

Brandon

2011-08-05 20:18:20

What's the limits to these plugins?

Drakan

2011-08-05 21:34:48

From: Main Ftw
Good job mate:P
do you think you can include a way to download srl without moving the plugin? I personally don't mind but it would be a nice thing to have:)


Well seeing as SRL will never be supported on titan unless someone rewrote it for titan, thats not really an issue ;)

KingKong

2011-08-05 21:56:10

Freddy, do you plan on adding multithreading(or something similar) in scar, if its possible?

Gu3st...

2011-08-06 03:02:10

From: KingKong
Freddy, do you plan on adding multithreading(or something similar) in scar, if its possible?
He's planned to add that in SCAR 5

Main

2011-08-06 06:04:06

From: drags111
Well seeing as SRL will never be supported on titan unless someone rewrote it for titan, thats not really an issue ;)



What do you mean? I thought srl is still compatible with both scar and simba.

Freddy1990

2011-08-06 06:40:30

From: Main Ftw
Good job mate:P
do you think you can include a way to download srl without moving the plugin? I personally don't mind but it would be a nice thing to have:)


In SCAR 3.25 you don't have to move the plugins, if you use the build-in includes system.

From: ggzz
What's the limits to these plugins?


The only hardcoded limit I imposed is that you can can not add stuff to the global scope, you can only add things to a namespace. Other than that, you will be able to do pretty much anything from adding new types, classes, etc… to adding overloaded operators to existing types or w/e.

Main

2011-08-06 06:48:38

alright, thanks!

Freddy1990

2011-08-06 07:01:26

From: KingKong
Freddy, do you plan on adding multithreading(or something similar) in scar, if its possible?


As stated, officially I'm going to add this in SCAR 5, but some recent changes to my design plans have made me consider to possibly add it in SCAR 4. I will not support procedural threading though, only OOP using the TThread class and descendants.