Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool LuaInterface::loadKey(lua_State * L, TVar * var){
- if (setjmp(buf) == 0){
- int kType = var->getKeyType();
- qDebug()<<kType;
- if ( kType == LUA_TNUMBER ){
- lua_pushnumber(L, var->getName().toInt());
- }
- else if ( kType == LUA_TTABLE ){
- lua_rawgeti(L, LUA_REGISTRYINDEX, var->getName().toInt());
- }
- else if ( kType == LUA_TBOOLEAN ){
- lua_pushboolean(L, var->getName().toLower() == "true" ? 1 : 0 );
- }
- else{
- lua_pushstring(L, var->getName().toLatin1().data());
- }
- return lua_type(L, -1) == kType;
- }
- qDebug()<<"panic in loadKey";
- return false;
- }
- bool LuaInterface::loadValue(lua_State * L, TVar * var, int index){
- //puts a value on stack
- if (setjmp(buf) == 0){
- if (loadKey(L, var)){
- //everything is tabled in lua, we need to just find what table
- //we're using, if index == 0, we iterate to the closest table
- if (index)
- lua_gettable(L, index);
- else{
- for (int j=1;j<=lua_gettop(L);j++){
- if (lua_type(L,j*-1) == LUA_TTABLE){
- lua_gettable(L, j*-1);
- }
- }
- }
- }
- else
- return false;
- return lua_type(L, -1) == var->getValueType();
- }
- qDebug()<<"panic error in loadValue";
- return false;
- }
- bool LuaInterface::reparentCVariable(TVar * from , TVar * to, TVar * curVar){
- //get the old parent on the stack
- if (setjmp(buf) == 0){
- if ( !from && !to )//moving from global to global
- return true;
- QList<TVar *> vars = varOrder(curVar);
- lua_getglobal(L, (vars[0]->getName()).toLatin1().data());
- int i=1;
- for( ; i<vars.size(); i++ ){
- qDebug()<<loadValue(L, vars[i], -2);
- }
- qDebug()<<"old parent with value on top of stack";
- for (int j=1;j<=lua_gettop(L);j++){
- qDebug()<<j<<":"<<lua_type(L,j*-1);
- }
- //redo the parenting in TVar
- qDebug()<<"new parent is"<<to->getName();
- from->removeChild(curVar);
- curVar->setParent(to);
- to->addChild(curVar);
- vars = varOrder(curVar);
- //do the actual reparenting part
- if (to == varUnit->getBase()){
- //we're going global
- lua_setglobal(L, curVar->getName().toLatin1().data());
- }
- else{
- lua_getglobal(L, (vars[0]->getName()).toLatin1().data());
- i=1;
- for( ; i<vars.size()-1; i++ ){
- qDebug()<<"pushing"<<vars[i]->getName();
- qDebug()<<loadValue(L, vars[i], -2);
- qDebug()<<"removing"<<vars[i-1]->getName();
- lua_remove(L, -2);
- }
- qDebug()<<"new stack fully loaded";
- for (int j=1;j<=lua_gettop(L);j++){
- qDebug()<<j<<":"<<lua_type(L,j*-1);
- }
- lua_insert(L, -2);
- qDebug()<<loadKey(L, curVar);
- lua_insert(L, -2);
- lua_settable(L, -3);
- lua_pop(L, 1);
- }
- //delete the old copy
- if (from == varUnit->getBase()){
- lua_pushnil(L);
- lua_setglobal(L, curVar->getName().toLatin1().data());
- }
- else{
- loadKey(L, curVar);
- lua_pushnil(L);
- lua_settable(L, -3);
- }
- return true;
- }
- else{
- qDebug()<<"panic in reparentCVariable";
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement