OK, so I invested some time in SWIG
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!P7ZRRIRPttLk ... az8VTiC7_g