diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-08-14 16:34:34 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-08-14 16:34:34 +0000 |
commit | 2fa455ff7870dc09d75bb89a897c7c1d26eb9020 (patch) | |
tree | 8417c6fd49f862f00234683e3a06a8d50a4f281c | |
parent | a88aa0a9af24962f8eea11e039fcf34dade66037 (diff) | |
download | manaserv-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.
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | src/game-server/testing.cpp | 12 | ||||
-rw-r--r-- | src/scripting/lua.cpp | 57 | ||||
-rw-r--r-- | src/scripting/script.cpp | 9 | ||||
-rw-r--r-- | src/scripting/script.hpp | 12 |
5 files changed, 50 insertions, 43 deletions
@@ -16,6 +16,9 @@ cancelations do not enter an infinite loop of death. * src/defines.h, src/game-server/trade.cpp, src/game-server/trade.hpp, src/game-server/gamehandler.cpp: Involved money in trade handler. + * src/scripting/lua.cpp, src/scripting/script.cpp, + src/scripting/script.hpp, src/game-server/testing.cpp: Made it possible + to load scripts from strings instead of files. 2007-08-13 Eugenio Favalli <elvenprogrammer@gmail.com> diff --git a/src/game-server/testing.cpp b/src/game-server/testing.cpp index 7b34d79a..f32ca68c 100644 --- a/src/game-server/testing.cpp +++ b/src/game-server/testing.cpp @@ -5,6 +5,7 @@ #include <cassert> #include "defines.h" +#include "resourcemanager.h" #include "game-server/gamehandler.hpp" #include "game-server/item.hpp" #include "game-server/itemmanager.hpp" @@ -36,9 +37,18 @@ void testingMap(MapComposite *map) dropItem(map, 58 * 32 + 16, 21 * 32 + 16, 524); // Associate a script - Script *s = Script::create("lua", "test.lua"); + Script *s = Script::create("lua"); if (s) { + ResourceManager *resman = ResourceManager::getInstance(); + int fileSize; + char *buffer = (char *)resman->loadFile("test.lua", fileSize); + if (buffer) + { + s->load(buffer); + free(buffer); + } + map->setScript(s); s->setMap(map); s->prepare("initialize"); 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; diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index 4d917273..12aa0d18 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -43,7 +43,7 @@ void Script::registerEngine(std::string const &name, Factory f) (*engines)[name] = f; } -Script *Script::create(std::string const &engine, std::string const &file) +Script *Script::create(std::string const &engine) { Engines::const_iterator i = engines->find(engine); if (i == engines->end()) @@ -51,12 +51,7 @@ Script *Script::create(std::string const &engine, std::string const &file) LOG_ERROR("No scripting engine named " << engine); return NULL; } - Script *s = i->second(file); - if (!s) - { - LOG_ERROR("Failure while loading script " << file); - } - return s; + return i->second(); } void Script::update() diff --git a/src/scripting/script.hpp b/src/scripting/script.hpp index 8ab89c25..136ae9ff 100644 --- a/src/scripting/script.hpp +++ b/src/scripting/script.hpp @@ -36,7 +36,7 @@ class Script { public: - typedef Script *(*Factory)(std::string const &); + typedef Script *(*Factory)(); /** * Registers a new scripting engine. @@ -44,15 +44,21 @@ class Script static void registerEngine(std::string const &, Factory); /** - * Creates a new script. + * Creates a new script context for a given engine. */ - static Script *create(std::string const &engine, std::string const &file); + static Script *create(std::string const &engine); Script(): mMap(NULL) {} virtual ~Script() {} /** + * Loads a chunk of text into the script context and executes + * its global statements. + */ + virtual void load(char const *) = 0; + + /** * Called every tick for the script to manage its data. * Calls the "update" function of the script by default. */ |