Skip to content
Browse files

initial LUA initialization code, and a hello world script example

  • Loading branch information...
1 parent b4ec2eb commit 3a5f73c52fbf6f71eb1b9fcc63005d02b3f31fa9 @jonlimle jonlimle committed
Showing with 76 additions and 7 deletions.
  1. +2 −1 CMakeLists.txt
  2. +5 −3 engine/server.cpp
  3. +1 −0 scripts/HelloWorld.lua
  4. +11 −2 shared/IRCbot.cpp
  5. +1 −1 shared/IRCbot.h
  6. +26 −0 shared/sauerLua.cpp
  7. +30 −0 shared/sauerLua.h
View
3 CMakeLists.txt
@@ -12,7 +12,8 @@ set(qserv_srcs
shared/tools.cpp
shared/stream.cpp
shared/crypto.cpp
- shared/IRCbot.cpp)
+ shared/IRCbot.cpp
+ shared/sauerLua.cpp)
set(qserv_LIBS
enet
View
8 engine/server.cpp
@@ -4,11 +4,12 @@
#include "engine.h"
#include <pthread.h>
#include "IRCbot.h"
+#include "sauerLua.h"
extern ircBot irc;
-
+luaVM luavm;
#ifdef STANDALONE
void fatal(const char *s, ...)
-{ //
+{
void cleanupserver();
cleanupserver();
defvformatstring(msg,s,s);
@@ -640,7 +641,7 @@ void serverslice(bool dedicated, uint timeout) // main server update, called f
char hn[1024];
copystring(c.hostname, (enet_address_get_host_ip(&c.peer->address, hn, sizeof(hn))==0) ? hn : "unknown");
-
+
printf("incomming connection (%s)\n", c.hostname);
int reason = server::clientconnect(c.num, c.peer->address.host, c.hostname);
if(!reason) nonlocalclients++;
@@ -839,6 +840,7 @@ void *IRCInit(void *x)
}
int main(int argc, char* argv[])
{
+ luavm.qservLuaInit();
pthread_t server_thread, irc_thread;
int iserver,iirc;
iserver = 1;
View
1 scripts/HelloWorld.lua
@@ -0,0 +1 @@
+qserv.toserver("Hello World")
View
13 shared/IRCbot.cpp
@@ -6,6 +6,7 @@
#include <netinet/in.h>
#endif
#include "IRCbot.h"
+#include "sauerLua.h"
SVAR(irchost, "irc.gamesurge.net");
VAR(ircport, 0, 6667, 65535);
@@ -21,7 +22,7 @@ ICOMMAND(login, "s", (char *s), {
if(isloggedin(0)){
irc.notice(irc.lastmsg()->nick, "You are already logged in!");
return;
- }if(!strcmp(s, newstring(ircloginpass))){
+ }if(!strcmp(s, ircloginpass)){
irc.IRCusers.push_back(irc.lastmsg()->host);
irc.speak("%s has logged in", irc.lastmsg()->nick);
}
@@ -33,6 +34,14 @@ ICOMMAND(clearbans, "", (), {
server::clearbans();
});
+ICOMMAND(doscript, "s", (char *s), {
+ if(isloggedin()){
+ if(fileexists(s, "r"))
+ luaL_dofile(luavm.getState(), s);
+ else irc.notice(irc.lastmsg()->nick, "Invalid Script Name");
+ }
+});
+
ICOMMAND(join, "s", (char *s), {
if(isloggedin())
irc.join(s);
@@ -112,7 +121,7 @@ void ircBot::part(char *channel){
send(sock,partchan,strlen(partchan),0);
}
-void ircBot::notice(char *user, char *message){
+void ircBot::notice(char *user, const char *message){
defformatstring(noticeuser)("NOTICE %s :%s\r\n", user, message);
send(sock,noticeuser,strlen(noticeuser),0);
}
View
2 shared/IRCbot.h
@@ -27,7 +27,7 @@ class ircBot
bool IsCommand(char *buff);
void join(char *channel);
void part(char *channel);
- void notice(char *user, char *message);
+ void notice(char *user, const char *message);
IrcMsg *lastmsg();
std::vector<char *> IRCusers;
private:
View
26 shared/sauerLua.cpp
@@ -0,0 +1,26 @@
+#include "sauerLua.h"
+
+lua_State *luaVM::getState()
+{
+ return L;
+}
+
+static int lua_toserver(lua_State *Lua)
+{
+ char *msg = (char *)luaL_checkstring(Lua, 1);
+ server::sendservmsg(msg);
+ return 0;
+}
+
+static const luaL_Reg qservlib[] = {
+ {"toserver", lua_toserver},
+ {NULL, NULL}
+};
+
+void luaVM::qservLuaInit()
+{
+ L=lua_open();
+ luaL_openlibs(L);
+ luaL_register(L, "qserv", qservlib);
+ printf("\33[33mLua Successfully initialized\33[0m\n");
+}
View
30 shared/sauerLua.h
@@ -0,0 +1,30 @@
+/**
+
+ header for the LUA module for the QServ sauerbraten server mod
+
+**/
+
+#ifndef __LUA_INCLUDED
+#define __LUA_INCLUDED
+
+extern "C"
+{
+ #include "lua.h"
+ #include "lualib.h"
+ #include "lauxlib.h"
+}
+
+#include "game.h"
+
+class luaVM
+{
+ public:
+ void qservLuaInit();
+ lua_State *getState();
+ private:
+ lua_State *L;
+};
+
+extern luaVM luavm;
+
+#endif ///__LUA_INCLUDED

0 comments on commit 3a5f73c

Please sign in to comment.
Something went wrong with that request. Please try again.