Results 1 to 21 of 21

Thread: How to create a plugin for Simba

  1. #1
    Join Date
    Oct 2006
    Posts
    500
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default How to create a plugin for Simba

    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:

    Code:
    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:

    Code:
    function GetFunctionCount(): Integer; stdcall; export;
    begin
      Result := 2;
    end;
    This is the amount of functions we are exporting.

    2:

    Code:
    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:

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

    Code:
    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:

    Code:
    exports GetFunctionCount;
    exports GetFunctionInfo;
    exports GetFunctionCallingConv;
    
    begin
    end.
    Here is an example of what your finished plugin could look like:

    Code:
    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:

    Simba Code:
    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:

    Code:
    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:

    Code:
    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

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

    Nice tutorial!



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  3. #3
    Join Date
    Apr 2007
    Location
    The Buckeye State
    Posts
    482
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Even though it is in the advanced section, it would probably be good to explain why we use plugins
    I like my coffee black just like my metal.

  4. #4
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    pics dont work.
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  5. #5
    Join Date
    Apr 2007
    Location
    The Buckeye State
    Posts
    482
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    They work for me.
    I like my coffee black just like my metal.

  6. #6
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by Esteban View Post
    Even though it is in the advanced section, it would probably be good to explain why we use olivine
    What is olivine?



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  7. #7
    Join Date
    Oct 2006
    Posts
    500
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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 ?

  8. #8
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  9. #9
    Join Date
    Oct 2006
    Posts
    500
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Shuttleu View Post
    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.

  10. #10
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

    Default

    Quote Originally Posted by r!ch!e View Post
    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

  11. #11
    Join Date
    Apr 2007
    Location
    The Buckeye State
    Posts
    482
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by r!ch!e View Post
    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
    I like my coffee black just like my metal.

  12. #12
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Hint: Don't export methods that use strings.

    Nice tutorial though.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  13. #13
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  14. #14
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    Quote Originally Posted by Dan's The Man View Post
    Hint: Don't export methods that use strings.

    Nice tutorial though.
    The memory sharing unit should be able to handle this.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  15. #15
    Join Date
    Feb 2006
    Location
    Franklin, Ohio, USA
    Posts
    991
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    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.

  16. #16
    Join Date
    Dec 2006
    Location
    Houston, TX USA
    Posts
    4,791
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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/

  17. #17
    Join Date
    Feb 2006
    Location
    Franklin, Ohio, USA
    Posts
    991
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by IceFire908 View Post
    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.

  18. #18
    Join Date
    Oct 2010
    Posts
    36
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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!

  19. #19
    Join Date
    Aug 2008
    Location
    Finland
    Posts
    2,851
    Mentioned
    3 Post(s)
    Quoted
    2 Post(s)

    Default

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

  20. #20
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    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.

  21. #21
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •