Forum

> > CS2D > Scripts > Regarding some Lua libraries in CS2D
Forums overviewCS2D overview Scripts overviewLog in to reply

English Regarding some Lua libraries in CS2D

18 replies
To the start Previous 1 Next To the start

old Regarding some Lua libraries in CS2D

DannyDeth
User Off Offline

Quote
Hi,

I am busy scratching some code up using LuaSocket to fetch the server data from CS2D servers. I just wanted to know if Cs2D has something such as Alien in it to allow me to write a dynamic library to allow LuaSocket to run within CS2D.
Answers are much appreciated.

Thanks,
Danny

old Re: Regarding some Lua libraries in CS2D

Lee
Moderator Off Offline

Quote
You'll probably have to compile the source from mingw on windows for it to work. You will also need to set the correct library path.

Here's my version:
http://unrealsoftware.de/files_show.php?file=6970

server.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package.cpath = package.cpath .. ";libs/?.dll"
package.path = package.path .. ";libs/lua/?.lua"

require "socket"

foo = socket.protect(function()
	-- connect somewhere
	local c = socket.try(socket.connect("google.com", 80))
	-- create a try function that closes 'c' on error
	local try = socket.newtry(function() c:close() end)
	-- do everything reassured c will be closed 
	try(c:send("hello there?\r\n"))
	local answer = try(c:receive())
	print(answer)
	try(c:send("good bye\r\n"))
	c:close()
end)

foo()

old Re: Regarding some Lua libraries in CS2D

DannyDeth
User Off Offline

Quote
Yeah, just saw your upload, looks pretty neat. I'm busy reading through it as we speak, I found it pretty difficult to get it running and 'm still kinda stuck a little.

But thanks for the example, Lee. Gonna try follow some of ur stuff.

old Re: Regarding some Lua libraries in CS2D

FlooD
GAME BANNED Off Offline

Quote
bla... anyway how can i use c functions in lua?
oh and is it possible to use luasockets on the cs2d server's socket? (i think some hacking would be involved)

old Re: Regarding some Lua libraries in CS2D

DannyDeth
User Off Offline

Quote
@FlooD:
registering C functions for Lua:
1
2
3
4
5
int my_lua_func(luavm){
	lua_pushnumber(luavm,lua_tonumber(1));
	return 1; /* number of arguments pushed onto stack */
}
lua_register(luavm,"lua_name",my_lua_func())

And the LuaSocket made by Lee would appear to be missing UDP support ( no guarantee this is true ). If it does, however, have UDP support, then yes, you can interface with CS2D.
edited 1×, last 07.06.11 01:32:54 pm

old Re: Regarding some Lua libraries in CS2D

FlooD
GAME BANNED Off Offline

Quote
like
if i have this stupid function in c
1
2
3
4
5
6
#include <stdio.h>
int c_func(int x)
{
	printf("x = %d\n", x);
	return (x + 123) * x;
}
so how do i get cs2d to load it?

user DannyDeth has written
If it does, however, have UDP support, then yes, you can interface with CS2D.


i meant having luasocket bind(or hijack) the already bound socket that the cs2d server uses to communicated with clients

old Re: Regarding some Lua libraries in CS2D

archmage
User Off Offline

Quote
@FlooD

You would have to define a c function like this.
1
2
3
int register_cfuncs(lua_State* L) {
	lua_register(L, "smthn" lua_cfunc);
}

then load the module
1
package.loadlib("path to dll", "register_cfuncs")();

Also your code:
1
2
3
4
5
6
#include <stdio.h>
int c_func(int x)
{
     printf("x = %d\n", x);
     return (x + 123) * x;
}

Cannot be used by Lua because it is not a lua c function.
You could use it if were like this:
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int c_func(luad_State* L) {
	if ( lua_gettop(L) >= 1 ) { // gettop returns the the index of the top of the stack (it returns the number of parameters)
		double num = lua_tonumber(L, 1);
		printf("x = %d\n", x);
		
		// push the return value onto the stack
		lua_pushnumber(L, (x+123)*x);
		lua_return 1;
	}
	return 0;
}

old Re: Regarding some Lua libraries in CS2D

Lee
Moderator Off Offline

Quote
Remember, dynamic libraries only export the pointer address of where specific functions begin as well as a memory dump. It doesn't store the type of the function so Lua has to get around that by using a generic function signature, typedefed to
1
typedef int (*lua_CFunction) (lua_State *L);
where the return value depicts how many items are pushed onto the Lua stack (and are to be treated as return values) and the L pointer references the current Lua environment, which contains the stack that the entirety of Lua depends upon.

Note: It is your responsibility to make sure that you leave the Lua stack as it was passed in, otherwise you run the risk of corrupting it.

Refer to DB's code, where its possible to replace the first line with

1
2
3
4
int luaopen_filename(void* l){
	lua_register(l, "func name in lua" c_func);
	return 1;
}

and directly load the module via

1
require "filename"

where filename is the without-extension file name of the dynamic library compiled from the c source.

Furthermore, if you have a raw lua_CFunction embedded in a dynamic library, you can simply call package.loadlib that returns a lua wrapper around the function.


Quote
i meant having luasocket bind(or hijack) the already bound socket that the cs2d server uses to communicated with clients


That's usually OS dependent. On *nix systems, I believe its possible to set up using the flags AF_INET, SOCK_RAW, and IPPROTO_IP. The only problem with this is that:

1. You usually need root
2. This is obviously not crossplatform
3. You may have to roll this out yourself as AFAIK, LuaSocket doesn't support SOCK_RAW nor IP level packets.
4. You'll have to hand craft your packets seeing that you're no longer automating the IP layer. (I suggest taking a look at the scapy/impacket/pcap projects)
5. The incoming packet will still go through to CS2D

old Re: Regarding some Lua libraries in CS2D

Apache uwu
User Off Offline

Quote
FlooD has written
bla... anyway how can i use c functions in lua?
oh and is it possible to use luasockets on the cs2d server's socket? (i think some hacking would be involved)


Sorry mate, cs2d is using windows' fail winsock does not allow reusing ports.

http://support.microsoft.com/kb/173619

If you do get luasocket working on the same port as cs2d please reply, I'd love to know how to do this.

ps. I'm not sure if you can set your local port in luasocket.

old Re: Regarding some Lua libraries in CS2D

Lee
Moderator Off Offline

Quote
This might remedy that however:

http://msdn.microsoft.com/en-us/library/ms740548(v=vs.85).aspx

A raw socket can be bound to the any interface (and hence can serve as a listen socket for all inbound traffic) and can send out crafted packets provided that you manually specify the src/dst within the packet.

If you're up for a challenge, here's the specs for the IP datagram http://www.tcpipguide.com/free/t_IPDatagramGeneralFormat.htm
and the UDP datagram that lays below the IP header (just concatenate the two)

old Re: Regarding some Lua libraries in CS2D

FlooD
GAME BANNED Off Offline

Quote
user Apache uwu has written
Sorry mate, cs2d is using windows' fail winsock does not allow reusing ports.

dont u just love microsoft?

anyway i giv up dont feel like figuring out all that crap, but if someone can get clock_gettime() into cs2d's lua let me know ;p

old Re: Regarding some Lua libraries in CS2D

Apache uwu
User Off Offline

Quote
user FlooD has written
user Apache uwu has written
Sorry mate, cs2d is using windows' fail winsock does not allow reusing ports.

dont u just love microsoft?

anyway i giv up dont feel like figuring out all that crap, but if someone can get clock_gettime() into cs2d's lua let me know ;p



On a related side note, has anyone got multiple lua states working? This might help a lot when udp/tcp ports can be used at the same time!
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview