00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <cassert>
00023
00024 #include "luascript.hpp"
00025
00026 #include "game-server/being.hpp"
00027
00028 #include "utils/logger.h"
00029
00030 LuaScript::~LuaScript()
00031 {
00032 lua_close(mState);
00033 }
00034
00035 void LuaScript::prepare(const std::string &name)
00036 {
00037 assert(nbArgs == -1);
00038 lua_getglobal(mState, name.c_str());
00039 nbArgs = 0;
00040 mCurFunction = name;
00041 }
00042
00043 void LuaScript::push(int v)
00044 {
00045 assert(nbArgs >= 0);
00046 lua_pushinteger(mState, v);
00047 ++nbArgs;
00048 }
00049
00050 void LuaScript::push(const std::string &v)
00051 {
00052 assert(nbArgs >= 0);
00053 lua_pushstring(mState, v.c_str());
00054 ++nbArgs;
00055 }
00056
00057 void LuaScript::push(Thing *v)
00058 {
00059 assert(nbArgs >= 0);
00060 lua_pushlightuserdata(mState, v);
00061 ++nbArgs;
00062 }
00063
00064 int LuaScript::execute()
00065 {
00066 assert(nbArgs >= 0);
00067 int res = lua_pcall(mState, nbArgs, 1, 0);
00068 nbArgs = -1;
00069 if (res || !(lua_isnil(mState, 1) || lua_isnumber(mState, 1)))
00070 {
00071 const char *s = lua_tostring(mState, 1);
00072
00073 LOG_WARN("Lua Script Error" << std::endl
00074 << " Script : " << mScriptFile << std::endl
00075 << " Function: " << mCurFunction << std::endl
00076 << " Error : " << (s ? s : "") << std::endl);
00077 lua_pop(mState, 1);
00078 return 0;
00079 }
00080 res = lua_tointeger(mState, 1);
00081 lua_pop(mState, 1);
00082 return res;
00083 mCurFunction = "";
00084 }
00085
00086 void LuaScript::load(const char *prog)
00087 {
00088 int res = luaL_loadstring(mState, prog);
00089
00090 if (res == LUA_ERRSYNTAX)
00091 {
00092 LOG_ERROR("Syntax error while loading Lua script.");
00093 return;
00094 }
00095
00096
00097
00098 res = lua_pcall(mState, 0, 0, 0);
00099 if (res)
00100 {
00101 LOG_ERROR("Failure while initializing Lua script: "
00102 << lua_tostring(mState, -1));
00103 lua_settop(mState, 0);
00104 return;
00105 }
00106 }
00107
00108 void LuaScript::processDeathEvent(Being *being)
00109 {
00110 prepare("death_notification");
00111 push(being);
00112
00113
00114 execute();
00115 }
00116
00117 void LuaScript::processRemoveEvent(Thing *being)
00118 {
00119 prepare("remove_notification");
00120 push(being);
00121
00122
00123 execute();
00124
00125 being->removeListener(getScriptListener());
00126 }
00127
00131 void LuaScript::getQuestCallback(Character *q, const std::string &name,
00132 const std::string &value, void *data)
00133 {
00134 LuaScript *s = static_cast< LuaScript * >(data);
00135 assert(s->nbArgs == -1);
00136 lua_getglobal(s->mState, "quest_reply");
00137 lua_pushlightuserdata(s->mState, q);
00138 lua_pushstring(s->mState, name.c_str());
00139 lua_pushstring(s->mState, value.c_str());
00140 s->nbArgs = 3;
00141 s->execute();
00142 }
00143
00147 void LuaScript::getPostCallback(Character *q, const std::string &sender,
00148 const std::string &letter, void *data)
00149 {
00150
00151 LuaScript *s = static_cast<LuaScript*>(data);
00152 assert(s->nbArgs == -1);
00153 lua_getglobal(s->mState, "post_reply");
00154 lua_pushlightuserdata(s->mState, q);
00155 lua_pushstring(s->mState, sender.c_str());
00156 lua_pushstring(s->mState, letter.c_str());
00157 s->nbArgs = 3;
00158 s->execute();
00159 }