From d9338083192084613e5530b02710b796252d342b Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 12 Aug 2010 10:57:04 +0000 Subject: New scripting system part2. --- client/lua.cc | 100 ++++++++++++++++++++++------------------------------------ 1 file changed, 37 insertions(+), 63 deletions(-) (limited to 'client/lua.cc') diff --git a/client/lua.cc b/client/lua.cc index 19b94c0..7dbdf75 100644 --- a/client/lua.cc +++ b/client/lua.cc @@ -58,7 +58,7 @@ static int get_widget(lua_State *L) Widget *widget = lua->getWidget(name); - printf("FIND: %s (%p)\n", name.toStdString().c_str(), widget); + // printf("FIND: %s (%p)\n", name.toStdString().c_str(), widget); if(widget) { wdg_make_widget(L, widget); @@ -66,15 +66,14 @@ static int get_widget(lua_State *L) lua_pushnil(L); } - printf("DONE\n"); + // printf("DONE\n"); return 1; } -LUA::LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets) +LUA::LUA(Widget **rootwidget) { - this->widgets = widgets; - this->auxwidgets = auxwidgets; + this->rootwidget = rootwidget; L = luaL_newstate(); if(L == NULL) { @@ -97,35 +96,21 @@ LUA::~LUA() lua_close(L); } -bool LUA::runValidator(QString program, Widget *widget, QString name, QString value) +QString LUA::runParser(QString program) { if(L == NULL) { error("LUA state not initialized!"); return false; } - printf("Running %s on %s with value %s\n", - program.toStdString().c_str(), - name.toStdString().c_str(), - value.toStdString().c_str() ); - - lua_pushstring(L, value.toStdString().c_str()); - lua_setglobal(L, "value"); - - lua_pushstring(L, name.toStdString().c_str()); - lua_setglobal(L, "name"); - - if(widget) { - wdg_make_widget(L, widget); - lua_setglobal(L, "this"); - } + printf("Running %s\n", program.toStdString().c_str()); - // int top = lua_gettop(L); + int top = lua_gettop(L); if(luaL_loadbuffer(L, program.toStdString().c_str(), program.size(), - name.toStdString().c_str())) { + "parser")) { error(lua_tostring(L, lua_gettop(L))); return false; } @@ -136,24 +121,43 @@ bool LUA::runValidator(QString program, Widget *widget, QString name, QString va return false; } - return true; + if(top != lua_gettop(L) - 1) { + error("Program did not return a single value.\n"); + return false; + } + + if(lua_isstring(L, lua_gettop(L)) == false) { + error("Program did not return a boolean value.\n"); + return false; + } + + QString res = lua_tostring(L, lua_gettop(L)); + lua_pop(L, 1); + + return res; } -QString LUA::runParser(QString program) +bool LUA::runScript(QString script, Widget *widget, QString name) { if(L == NULL) { error("LUA state not initialized!"); return false; } - printf("Running %s\n", program.toStdString().c_str()); + printf("Running %s script %s on %s widget.\n", + name.toStdString().c_str(), + script.toStdString().c_str(), + widget?widget->name().toStdString().c_str():"NULL"); - int top = lua_gettop(L); + if(widget) { + wdg_make_widget(L, widget); + lua_setglobal(L, "this"); + } if(luaL_loadbuffer(L, - program.toStdString().c_str(), - program.size(), - "parser")) { + script.toStdString().c_str(), + script.size(), + name.toStdString().c_str())) { error(lua_tostring(L, lua_gettop(L))); return false; } @@ -164,20 +168,7 @@ QString LUA::runParser(QString program) return false; } - if(top != lua_gettop(L) - 1) { - error("Program did not return a single value.\n"); - return false; - } - - if(lua_isstring(L, lua_gettop(L)) == false) { - error("Program did not return a boolean value.\n"); - return false; - } - - QString res = lua_tostring(L, lua_gettop(L)); - lua_pop(L, 1); - - return res; + return true; } void LUA::error(QString message) @@ -187,23 +178,6 @@ void LUA::error(QString message) Widget *LUA::getWidget(QString name) { - QVector< Widget* >::iterator i = widgets->begin(); - while (i != widgets->end()) { - Widget* w = *i; - if(name == w->name()) return w; - i++; - } - - if(auxwidgets) { - QVector< Widget* >::iterator j = auxwidgets->begin(); - while (j != auxwidgets->end()) { - Widget* w = *j; - if(name == w->name()) return w; - j++; - } - } - - printf("WARNING: Widget %s not found\n", name.toStdString().c_str()); - - return NULL; + if(*rootwidget) return (*rootwidget)->findWidget(name, true); + else return NULL; } -- cgit v1.2.3