Advertisement
Guest User

Untitled

a guest
Jun 21st, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. bool LuaInterface::loadKey(lua_State * L, TVar * var){
  2. if (setjmp(buf) == 0){
  3. int kType = var->getKeyType();
  4. qDebug()<<kType;
  5. if ( kType == LUA_TNUMBER ){
  6. lua_pushnumber(L, var->getName().toInt());
  7. }
  8. else if ( kType == LUA_TTABLE ){
  9. lua_rawgeti(L, LUA_REGISTRYINDEX, var->getName().toInt());
  10. }
  11. else if ( kType == LUA_TBOOLEAN ){
  12. lua_pushboolean(L, var->getName().toLower() == "true" ? 1 : 0 );
  13. }
  14. else{
  15. lua_pushstring(L, var->getName().toLatin1().data());
  16. }
  17. return lua_type(L, -1) == kType;
  18. }
  19. qDebug()<<"panic in loadKey";
  20. return false;
  21. }
  22.  
  23. bool LuaInterface::loadValue(lua_State * L, TVar * var, int index){
  24. //puts a value on stack
  25. if (setjmp(buf) == 0){
  26. if (loadKey(L, var)){
  27. //everything is tabled in lua, we need to just find what table
  28. //we're using, if index == 0, we iterate to the closest table
  29. if (index)
  30. lua_gettable(L, index);
  31. else{
  32. for (int j=1;j<=lua_gettop(L);j++){
  33. if (lua_type(L,j*-1) == LUA_TTABLE){
  34. lua_gettable(L, j*-1);
  35. }
  36. }
  37. }
  38. }
  39. else
  40. return false;
  41. return lua_type(L, -1) == var->getValueType();
  42. }
  43. qDebug()<<"panic error in loadValue";
  44. return false;
  45. }
  46.  
  47. bool LuaInterface::reparentCVariable(TVar * from , TVar * to, TVar * curVar){
  48. //get the old parent on the stack
  49. if (setjmp(buf) == 0){
  50. if ( !from && !to )//moving from global to global
  51. return true;
  52. QList<TVar *> vars = varOrder(curVar);
  53. lua_getglobal(L, (vars[0]->getName()).toLatin1().data());
  54. int i=1;
  55. for( ; i<vars.size(); i++ ){
  56. qDebug()<<loadValue(L, vars[i], -2);
  57. }
  58. qDebug()<<"old parent with value on top of stack";
  59. for (int j=1;j<=lua_gettop(L);j++){
  60. qDebug()<<j<<":"<<lua_type(L,j*-1);
  61. }
  62.  
  63. //redo the parenting in TVar
  64. qDebug()<<"new parent is"<<to->getName();
  65. from->removeChild(curVar);
  66. curVar->setParent(to);
  67. to->addChild(curVar);
  68. vars = varOrder(curVar);
  69. //do the actual reparenting part
  70. if (to == varUnit->getBase()){
  71. //we're going global
  72. lua_setglobal(L, curVar->getName().toLatin1().data());
  73. }
  74. else{
  75. lua_getglobal(L, (vars[0]->getName()).toLatin1().data());
  76. i=1;
  77. for( ; i<vars.size()-1; i++ ){
  78. qDebug()<<"pushing"<<vars[i]->getName();
  79. qDebug()<<loadValue(L, vars[i], -2);
  80. qDebug()<<"removing"<<vars[i-1]->getName();
  81. lua_remove(L, -2);
  82. }
  83. qDebug()<<"new stack fully loaded";
  84. for (int j=1;j<=lua_gettop(L);j++){
  85. qDebug()<<j<<":"<<lua_type(L,j*-1);
  86. }
  87. lua_insert(L, -2);
  88. qDebug()<<loadKey(L, curVar);
  89. lua_insert(L, -2);
  90. lua_settable(L, -3);
  91. lua_pop(L, 1);
  92. }
  93. //delete the old copy
  94. if (from == varUnit->getBase()){
  95. lua_pushnil(L);
  96. lua_setglobal(L, curVar->getName().toLatin1().data());
  97. }
  98. else{
  99. loadKey(L, curVar);
  100. lua_pushnil(L);
  101. lua_settable(L, -3);
  102. }
  103. return true;
  104. }
  105. else{
  106. qDebug()<<"panic in reparentCVariable";
  107. return false;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement