summaryrefslogtreecommitdiff
path: root/src/scripting/lua.cpp
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-14 16:34:34 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-14 16:34:34 +0000
commit2fa455ff7870dc09d75bb89a897c7c1d26eb9020 (patch)
tree8417c6fd49f862f00234683e3a06a8d50a4f281c /src/scripting/lua.cpp
parenta88aa0a9af24962f8eea11e039fcf34dade66037 (diff)
downloadmanaserv-2fa455ff7870dc09d75bb89a897c7c1d26eb9020.tar.gz
manaserv-2fa455ff7870dc09d75bb89a897c7c1d26eb9020.tar.bz2
manaserv-2fa455ff7870dc09d75bb89a897c7c1d26eb9020.tar.xz
manaserv-2fa455ff7870dc09d75bb89a897c7c1d26eb9020.zip
Made it possible to load scripts from strings instead of files.
Diffstat (limited to 'src/scripting/lua.cpp')
-rw-r--r--src/scripting/lua.cpp57
1 files changed, 25 insertions, 32 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 636417f9..0fc3283d 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -29,7 +29,6 @@ extern "C" {
}
#include "defines.h"
-#include "resourcemanager.h"
#include "game-server/buysell.hpp"
#include "game-server/character.hpp"
#include "game-server/gamehandler.hpp"
@@ -50,10 +49,12 @@ class LuaScript: public Script
{
public:
- LuaScript(lua_State *);
+ LuaScript();
~LuaScript();
+ void load(char const *);
+
void prepare(std::string const &);
void push(int);
@@ -329,20 +330,11 @@ static int test_NpcSell(lua_State *s)
return 0;
}
-LuaScript::LuaScript(lua_State *s):
- mState(s),
+LuaScript::LuaScript():
nbArgs(-1)
{
+ mState = luaL_newstate();
luaL_openlibs(mState);
- // A Lua state is like a function, so "execute" it in order to initialize it.
- int res = lua_pcall(mState, 0, 0, 0);
- if (res)
- {
- LOG_ERROR("Failure while initializing Lua script: "
- << lua_tostring(mState, -1));
- lua_settop(s, 0);
- return;
- }
// Put some callback functions in the scripting environment.
static luaL_reg const callbacks[] = {
@@ -363,7 +355,7 @@ LuaScript::LuaScript(lua_State *s):
lua_pushlightuserdata(mState, this);
lua_settable(mState, LUA_REGISTRYINDEX);
- lua_settop(s, 0);
+ lua_settop(mState, 0);
}
LuaScript::~LuaScript()
@@ -411,34 +403,35 @@ int LuaScript::execute()
return res;
}
-static Script *loadScript(std::string const &filename)
+void LuaScript::load(char const *prog)
{
- // Load the file through resource manager.
- ResourceManager *resman = ResourceManager::getInstance();
- int fileSize;
- char *buffer = (char *)resman->loadFile(filename, fileSize);
- if (!buffer) return NULL;
+ int res = luaL_loadstring(mState, prog);
- lua_State *s = luaL_newstate();
- int res = luaL_loadstring(s, buffer);
- free(buffer);
+ if (res == LUA_ERRSYNTAX)
+ {
+ LOG_ERROR("Syntax error while loading Lua script.");
+ return;
+ }
- switch(res)
+ // A Lua chunk is like a function, so "execute" it in order to initialize it.
+ res = lua_pcall(mState, 0, 0, 0);
+ if (res)
{
- case 0:
- LOG_INFO("Successfully loaded script " << filename);
- return new LuaScript(s);
- case LUA_ERRSYNTAX:
- LOG_ERROR("Syntax error while loading script " << filename);
+ LOG_ERROR("Failure while initializing Lua script: "
+ << lua_tostring(mState, -1));
+ lua_settop(mState, 0);
+ return;
}
+}
- lua_close(s);
- return NULL;
+static Script *LuaFactory()
+{
+ return new LuaScript();
}
struct LuaRegister
{
- LuaRegister() { Script::registerEngine("lua", loadScript); }
+ LuaRegister() { Script::registerEngine("lua", LuaFactory); }
};
static LuaRegister dummy;