Files

> > CS2D > Misc. > LuaJIT for Dedicated Server
Files overviewCS2D overviewMisc. overview

English LuaJIT for Dedicated Server >

17 comments370 kb, 830 Downloads

old LuaJIT for Dedicated Server

MikuAuahDark
User Off Offline

SUPPORTED CS2D VERSION: 1.0.1.4
IMG:https://github.com/MikuAuahDark/cs2djit/actions/workflows/main.yml/badge.svg


∗ Notice ∗
This does NOT support closed-beta dedicated server as in thread cs2d CS2D Dedicated Server Closed Beta.

∗ 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 again.

∗ CentOS 7 Users ∗
Unfortunately CentOS 7 stuck with very old glibc. CentOS 7 users must compile from source and not download the prebuilt binaries from this site!

∗ 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
into your CS2D server root directory, beside
cs2d_dedicated
executable. Make sure you have 32-bit LuaJIT library installed (
libluajit-5.1:i386
for Debian and its derivatives)

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.
√ Redistribute modified version under same or different license.
× Hold liable. This program is provided "as-is".

∗ Notes ∗
• This guaranteed 100% to break when new CS2D is released. Please check this file archive 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.
• Windows Defender may flag
cs2djitwrapper.exe
as virus or unwanted software. This maybe caused by antivirus doesn't quite like MinGW-compiled binaries. Rest assured that binaries are compiled by GitHub Actions from Debian or if you really paranoid you can compile this program yourself from source above.
• 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 a form of cheat/hack that can give players unintended/unfair advantage!
• If you got LD_PRELOAD error, please see below.

∗ Fix LD_PRELOAD Error ∗
This is caused if your script uses
os.execute
or
io.popen
. Save this script as
sys/lua/autorun/ld_preload_unset.lua

1
2
3
4
5
6
7
8
9
10
if not jit then return end
local ffi = require("ffi")

ffi.cdef[[
int unsetenv(const char *name);
]]

if os.getenv("LD_PRELOAD") then
	ffi.C.unsetenv("LD_PRELOAD")
end

∗ attempt to index global 'arg' (a nil value) ∗
If you have code something like this:
1
2
3
function foo(...)
	print(arg[1], arg[2], arg[3])
end
Then it won't work. LuaJIT doesn't support this. Please define
arg
yourself:
1
2
3
4
function foo(...)
	local arg = {...} -- this
	print(arg[1], arg[2], arg[3])
end

∗ Version History ∗
> 20240726
INFO
thread news CS2D v1.0.1.4 support.
CHANGED
Windows now has LuaJIT 2.1.1720049189 DLL bundled. This DLL is compiled under MinGW. Please see above for implications!
> 20211127
INFO
thread news CS2D v1.0.1.3 support.
> 20210523
INFO
thread news CS2D v1.0.1.2 support.
> 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.
edited 28×, last 26.07.24 05:18:42 am
Approved by Starkkz

Download Download

370 kb, 830 Downloads

Comments

17 comments
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in

old

MaksDragon
User Off Offline

error on arch linux
edited 1×, last 19.12.22 11:30:30 pm

old

Terracraft
User Off Offline

This is awesome!
I like it!

old

Marcell
Super User Off Offline

Thanks. You are a god Miku.
I like it!

old

Nekomata
User Off Offline

amazing stuff as always
I like it!

old

MikuAuahDark
User Off Offline

@user GeoB99: Alright let me answer it one by one.

user GeoB99 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 GeoB99 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 GeoB99 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.

old

GeoB99
Moderator Off Offline

Some parts of the Windows LuaJIT port code could be simplified. Chunks like this for example.
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.:
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:
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.

old

Starkkz
Moderator Off Offline

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.
I like it!

old

Infinite Rain
Reviewer Off Offline

This is awesome, actually. Lua JIT also supports goto which is amazing as well. Good job.
I like it!

old

VADemon
User Off Offline

There's no reason not to like it. ❤
Thanks for taking your time.
I like it!

old

Gaios
Reviewer Off Offline

Maybe you can record benchmark speed tests between old and new Lua.
I like it!

old

_oops
User Off Offline

Nice job
I like it!

old

mrc
User Off Offline

I'm using it, ty.
I like it!

old

Mami Tomoe
User Off Offline

This should've been integrated.
I like it!

old

Kapachino
User Off Offline

Here, get a like.
I like it!

old

Hajt
User Off Offline

I have tested. Works like a charm.
I like it!
To the start Previous 1 Next To the start