English LuaJIT for Dedicated Server icon

14 comments
11.01.20 01:54:52 pm
like 11 like it!
278 kb, 37 Downloads
MikuAuahDark
User
Offline Off
SUPPORTED CS2D VERSION: 1.0.1.1
IF STABILITY OF YOUR SCRIPT IS CONCERN, DON'T USE
IMG:https://github.com/MikuAuahDark/cs2djit/workflows/CI/badge.svg


∗ Description ∗
This patches the dedicated server to use LuaJIT instead of Lua 5.1. This also allows Lua C (external) modules to be loaded without memory errors. This works for Windows and Linux (patches for macOS are welcome).

So what's better with LuaJIT? This execute your scripts faster, I mean, faster. You can also use one of the best feature in LuaJIT which is FFI. This means you can use file cs2d [LuaJIT/LuaFFI] Fix for os.clock (6) and file cs2d [LuaJIT/LuaFFI] Socket Support (4) again.

∗ Motivation ∗

You may hear LuaJIT before and yes, LuaJIT was used in thread news CS2D Beta 0.1.2.6 but removed in thread news CS2D Beta 0.1.2.7 due to stability issues. I believe the instability was caused by ancient BlitzMax (or whatever CS2D uses) so this method only patches the Lua API inside the dedicated server to use LuaJIT and nothing else.

From UnrealSoftware Discord:
user MikuAuahDark has written:
Is it possible to have custom build of CS2D dedicated server with LuaJIT instead of Lua 5.1 as an option?

user DC has written:
@user MikuAuahDark Last time we tried that everything exploded. Won't go through the pain of trying it again. Not worth it.

user MikuAuahDark has written:
Even by adding notice that "it's unstable"?

user DC has written:
I can't add it as an option. either broken shit or working shit. The decision has already been made.

So, CS2DJIT is born. It provides users an option to use LuaJIT for their server.

∗ Installation ∗
Windows: Extract
cs2djit.dll
,
cs2djitwrapper.exe
, and optionally
lua51.dll
(notice below!) into your CS2D server root directory, beside
cs2d_dedicated.exe

Linux: Extract
cs2djit.sh
and
libcs2djit.so
. Make sure you have LuaJIT library (i386) installed (Debian-based: libluajit-5.1)

If your Linux VPS supports Docker, you can use user EngiN33R's Docker image which additionally support LuaRocks so you can install Lua C modules with ease.

∗ Running ∗
Windows: Run
cs2djitwrapper.exe
in your server directory.
Linux: Run
cs2djit.sh
in your CS2D server directory, example:
bash cs2djit.sh


∗ Source Code ∗
In case you think this steals stuff, it's not. You can compile it yourself if you really paranoid. Here's the source code: https://github.com/MikuAuahDark/cs2djit

∗ Permission ∗
This program is licensed under MIT license. In short:
√ You can use this.
√ Modify.

∗ Notes ∗
• This guaranteed 100% to break when new CS2D is released. Please check this file for up-to-date version! GitHub releases are used for archival-purpose only.
• Executables only tested in Debian 10 and Windows 10.
• In Windows, the LuaJIT lua51.dll is bundled along with the downloads. If you have your own compiled lua51.dll which uses same compiler as your other Lua modules, prefer that. You have to be consistent in using compilers there as mixing DLLs between different (MSVC) compilers can cause problems.
• If you have any issues which directly related to this, please tell me.
• If your script breaks after using this, please tell me in UnrealSoftware Discord server, #cs2d-scripting channel.
• This only benefits server scripters. This is not form of cheat/hack that can give players unfair advantage

∗ Version History ∗
> 20200130
FIXED Error when starting cs2djit.sh somewhere else other than current server folder
CHANGED Windows now has LuaJIT-2.1.0-beta3 DLL bundled. This DLL is compiled under MinGW. Please see above for implications!
CHANGED cs2djit.sh and cs2djitwrapper.exe now pass all arguments to cs2d_dedicated/.exe.
CHANGED libcs2djit.so is now compiled under Ubuntu 16.04 to improve compatibility with older distros.
> 20200111
ADDED Initial release.
imageimage
edited 8×, last 14.03.20 02:26:31 pm
ok This file has been reviewed and approved by Starkkz (11.01.20 10:35:59 pm)

Comments

14 comments
Goto Page
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in
08.02.20 08:03:07 pm
like I like it!
Up
Nekomata
User
Offline Off
amazing stuff as always
04.02.20 03:56:22 pm
Up
MikuAuahDark
User
Offline Off
@user Fraizeraust: Alright let me answer it one by one.

user Fraizeraust has written:
Why do you allocate memory for the module variable? For what? You could simply declare your variable as an array with a fixed size to hold enough number of elements. E.g.:

32768 is not arbitrary. It's highest length size for paths for unicode version of WinAPI. The reason I allocate it with
malloc
so I won't blow up the stack with 64KB memory. Windows is known to have nasty limit of stack like, around 1MB. I understand that 64KB is nothing but IMO 64KB is small enough that malloc won't return NULL, and even if it does, it's handled.

Other than that, the reason why I allocate such big amount is there's no way to get the required size of GetModuleFileNameA/W(), thus the enormous 32768 buffer size.

user Fraizeraust has written:
I see in your
main()

function you use a mix of Unicode (W) and an ANSI routines. This is bad.

main()
is the only standard entry point, change my mind. Others are compiler-specific/OS-specific non-standard (WinMain counts) and are not supported in (older) MinGW. Not sure how supported
tchar.h
is in MinGW, so I'd rather not take the risk.

If you think about it, it actually sort of makes sense. The only part I use unicode variant is when it involves the module path & name (and CreateProcessW). Other than that I use ANSI variant.

user Fraizeraust has written:
Furthermore, for functions like swprintf() I'd rather recommend that you use the String Safe API, specifically the StringCchPrintfW() function to print out to the output. This is for the sake to avoid any potential buffer overrun.

Interesting. I may consider using it if it doesn't too complicated.

If you still have discussion, please open new GitHub issue instead, to not clutter our discussion in the File Archive comments.
02.02.20 05:57:26 pm
Up
Fraizeraust
Moderator
Offline Off
Some parts of the Windows LuaJIT port code could be simplified. Chunks like this for example.
Code:
1
2
3
4
/* Windows API quirks -_- */
          moduleName = malloc(sizeof(wchar_t) * 32768);
          if (moduleName == NULL)
               return 0;

Why do you allocate memory for the module variable? For what? You could simply declare your variable as an array with a fixed size to hold enough number of elements. E.g.:
Code:
1
2
3
WCHAR szModuleName[256]; // Or a macro define with a fixed size

GetModuleFileNameW(NULL, szModuleName, _countof(szModuleName));

_countof()
calculates the exact amount of elements the array can hold. Please avoid filling fixed size numbers to size parameters.

I see in your
main()
function you use a mix of Unicode (W) and an ANSI routines. This is bad. You either choose to use Unicode entirely, or ANSI, or just be agnostic of what encoding you use. In the latter case, the correct way to define your function is:
Code:
int _tmain(int argc, const TCHAR *argv[])

Where
TCHAR
is converted to a WCHAR if Unicode is defined, CHAR otherwise.

Furthermore, for functions like swprintf() I'd rather recommend that you use the String Safe API, specifically the StringCchPrintfW() function to print out to the output (BEAR IN MIND this takes the buffer size in character counts!). This is for the sake to avoid any potential buffer overrun. Check the documentation for further information.
02.02.20 12:34:32 pm
Up
MikuAuahDark
User
Offline Off
@user Starkkz: Exactly why I wasn't shipping lua51.dll in previous releases. Windows binaries I provide are compiled with i686-w64-mingw64 toolchain under Linux. I've updated the changelog to clear this up.
02.02.20 05:26:21 am
like I like it!
Up
Starkkz
Moderator
Offline Off
On Windows it's best to use the binaries provided at luapower.com (which is compiled with MinGW just like CS2D), other binaries could've have been compiled with Microsoft Visual C++, which might cause runtime errors.
17.01.20 10:07:49 am
like I like it!
Up
Infinite Rain
Reviewer
Offline Off
This is awesome, actually. Lua JIT also supports goto which is amazing as well. Good job.
15.01.20 12:05:25 pm
like I like it!
Up
VADemon
User
Offline Off
There's no reason not to like it. ❤
Thanks for taking your time.
13.01.20 09:10:29 am
like I like it!
Up
Gaios
Security Supporter
Offline Off
Maybe you can record benchmark speed tests between old and new Lua.
12.01.20 02:38:12 am
like I like it!
Up
Oops321
User
Offline Off
Nice job
11.01.20 08:27:03 pm
like I like it!
Up
mrc
User
Offline Off
I'm using it, ty.
11.01.20 08:10:14 pm
like I like it!
Up
Goo
User
Offline Off
This should've been integrated.
11.01.20 07:48:27 pm
like I like it!
Up
TrialAndError
User
Offline Off
Nice.
11.01.20 02:01:08 pm
like I like it!
Up
Kapachino
User
Offline Off
Here, get a like.
11.01.20 01:58:42 pm
like I like it!
Up
Hajt
User
Offline Off
I have tested. Works like a charm.
To the start Previous 1 Next To the start