SLikeNet and Lua

General topics regarding SLikeNet
Shando
Baby-Member
Posts: 4
Joined: Thu Jul 26, 2018 7:01 am

SLikeNet and Lua

Postby Shando » Thu Jul 26, 2018 7:06 am

Hi all,

I've just come across SLikeNet (used RakNet previously) and was wondering if anyone has tried to expose SLikeNet functions in Lua?

Thanks in advance

Shando

PS: From what I've seen so far, SLikeNet is looking like a good replacement for RakNet :)

Luke1410
Site Admin
Posts: 43
Joined: Wed Jul 12, 2017 12:42 am

Re: SLikeNet and Lua

Postby Luke1410 » Sat Jul 28, 2018 5:24 pm

Unfortunately that's not on our priority list atm. A start might be to investigate using SWIG to generate Lua wrappers. Since SWIG is used to create C# wrappers already, that might be the easiest start.

An alternative approach could be to use LuaJIT and FFI. That should allow a straight forward integration of SLikeNet in Lua.

If you happen to invest some work on that side yourself and want it being integrated in SLikeNet directly, don't hesitate to speak up. For now, I put the request/suggestion on our issue tracker at least (internal case number: SLNET-235).

Shando
Baby-Member
Posts: 4
Joined: Thu Jul 26, 2018 7:01 am

Re: SLikeNet and Lua

Postby Shando » Wed Aug 01, 2018 1:34 am

Thanks for the reply.

I've looked into SWIG, but it seems to generate way too much overhead and isn't human readable :(

In the past I managed to get this working fine:

https://github.com/pabloko/Lua-JIT-RakNet

Which is fairly simple, though I will have to code most new functions by hand, but I suppose that's what the samples are for ;)

Does anyone happen to have a list of functions that would be the minimum set required to get as much functionality (incuding plugins) as possible, without getting into too much detail?

Thanks in advance

Shando

Shando
Baby-Member
Posts: 4
Joined: Thu Jul 26, 2018 7:01 am

Re: SLikeNet and Lua

Postby Shando » Fri Sep 21, 2018 12:23 am

OK, so I invested some time in SWIG :shock:

I have (hopefully) managed to get over 1,500 available, of which I have successfully tested almost 50 in my ‘TestMain.lua’ code.

NOTE: Testing was carried out on Windows 10 using Visual Studio 2017 Community, Lua 5.2 & SWIG 3.0.12.

With the testing I tried to use as many regularly used functions as possible, including creating, writing & reading Bitstreams and running both a Server and a Client.

In the Function Listing by Class section in the 'SLNet_Lua_Funcs.docx' file, C++ functions are in black and the corresponding Lua function is in blue:

    bool = SLNet::AddressOrGUID::__eq ( SLNet::AddressOrGUID * ) = C++ Version
    OUT = AddressOrGUID:_eq ( IN ) = Lua Version

To understand what the Lua function requires as parameter(s), reference will be required back to the C++ version. For example, in the above Lua function OUT = Boolean and IN = a pointer to an SLNet::AddressOrGUID. So, to use this function, the Lua code would look something like this (where inPacket1 and inPacket2 are received packets):

Code: Select all

aog1 = SLNet:AddressOrGUID ( inPacket1 )
aog2 = SLNet:AddressOrGUID ( inPacket2 )
bResult = aog1:_eq ( aog2 )


NOTE: One thing to be careful of is that for any ‘static’ function (and ENUMs) in C++, Lua uses the ‘.’ notation rather than the ‘:’ notation used above:

Code: Select all

testServer = SLNet.RakPeer.GetInstance ( )
*** ‘.’ notation ***

testServer:Startup ( 1, SLNet.SocketDescriptor ( 60001, "127.0.0.1" ), 1 )
*** ‘:’ AND ‘.’ notation ***

testServer:SetMaximumIncomingConnections ( 1 )
*** ‘:’ notation ***

enumVal = SLNet.ID_USER_PACKET_ENUM
*** ‘.’ notation ***


Getting to this point was not an easy task as there are quite a few issues with the SWIG process:

    The generated TYPES TABLE sometimes has 2 entries for the same type. Basically SWIG creates a ‘SWIGTYPE_p_’ and a ‘SWIGTYPE_p_SLNet__’, which leads to some functions not recognising the correct type and requiring manual fixes.
    Marking parameters as ‘OUTPUT’ or ‘INOUT’ only works for “normal” datatypes (bool, int etc.).
    SWIG doesn't work with Structs embedded in Classes. In fact, they are simply ignored.
    SWIG seems to duplicate any ENUMs embedded in Classes. One is created in the Class in 'static swig_lua_const_info swig_CLASSNAME_Sf_SwigStatic_constants[]' and the other in the Module in 'static swig_lua_const_info swig_SwigModule_constants[]'. NOTE: The version in ‘SwigModule’ has 'CLASSNAME_' added as a prefix to the ENUM.

Of course, the above issues could be caused by me not understanding exactly how SWIG works!

In the ‘SLNet_Lua.zip’ file are all the relevant files that I used for testing:

    ‘Source’ folder – contains the required Lua ‘.h’ files
    ‘SWIG I Files’ folder – contains the ‘i’ files required by SWIG to generate the wrapper code. The main ones to generate the basic wrapper are ‘SLNet.i’ and ‘SWIG_Templates.i’.
    ‘inspect.lua’ – an excellent piece of Lua code that allows you to pretty-print the contents of a Lua table (including ‘_G’).
    ‘lua52.dll’ & ‘lua52.lib’ – the main Lua files for dynamic/static linking.
    ‘SLNet_exe.cpp’ – the main code to run the demo/SWIG – NOTE: any Lua file can be passed to this, via the command line, but the code at lines 31 & 32 will need uncommenting / commenting.
    ‘SLNet_Funcs_Lua.docx’ – the documentation.
    ‘SLNet_wrapper.cxx’ – the SWIG-generated wrapper file.
    ‘SLNet_wrapper_plus_extensions.cxx’ – a SWIG-generated wrapper file that includes the RakVoice, Lobby2, PostgreSQL & MySQL extensions. NOTE: This wrapper has NOT been tested at all as it needs quite a few external programs to function, such as speex and PostgreSQL.
    ‘stdafx.cpp’ & ‘stdafx.h’ – a couple of files required to get the tests working in Visual Studio 2017.
    ‘SWIG_Funcs_new.xlsx’ – an Excel spreadsheet that I used to create the Function listing.
    ‘TestMain.lua’ – the Lua file containing my test code.

If you have any questions, comments, issues etc. let me know and I will try to help, with the caveat that I don’t fully understand SWIG! Also, if there are any SWIG gurus out there, maybe they could help fix my ‘.i’ files to allow the auto-generation of the wrapper without any manual fixes afterwards?

Shando

The .zip file is too large to add here, so this is a link to it on Mega:

https://mega.nz/#!35JwDCID!P7ZRRIRPttLkcFRLf-5zTkH-0MI7BEbtLaz8VTiC7_g

Luke1410
Site Admin
Posts: 43
Joined: Wed Jul 12, 2017 12:42 am

Re: SLikeNet and Lua

Postby Luke1410 » Mon Oct 15, 2018 10:08 pm

Great work Shando.

If you like I can put the file onto our server so you can use it as a download mirror.
Would it be fine with you, if we base a later integrated Lua support on your code?
For the time being, I'd add a note in the readme file in the next version to point to your Lua interface package, if you are fine with that (pointing out it's a 3rd-party contribution).

Shando
Baby-Member
Posts: 4
Joined: Thu Jul 26, 2018 7:01 am

Re: SLikeNet and Lua

Postby Shando » Sat Oct 20, 2018 3:54 am

Hey buddy,

No worries. Feel free to use them any way you want.

The only proviso I have is that it clearly states that they have not been fully tested and there is no warranty, though I will try to help any way I can.

Regards

Shando

PS: apologies for not responding sooner


Who is online

Users browsing this forum: No registered users and 1 guest