Share Pastebin
Guest
Public paste!

luaenv_variables.c

By: a guest | Mar 20th, 2010 | Syntax: C | Size: 4.56 KB | Hits: 66 | Expires: Never
Copy text to clipboard
  1. #include "lua.h"
  2.  
  3. void luaGetVariableInteger(luaScript* script, const char* name, int* ret){
  4.         lua_settop(script->L, 0);
  5.         lua_getglobal(script->L, name);
  6.         if (lua_isnumber(script->L, 1)){
  7.                 *ret = lua_tonumber(script->L,1);
  8.         }
  9.         lua_pop(script->L, 1);
  10. }
  11.  
  12. void luaGetVariableString(luaScript* script, const char* name, char* ret){
  13.         lua_settop(script->L, 0);
  14.         lua_getglobal(script->L, name);
  15.         if (lua_isstring(script->L, 1)){
  16.                 strcpy(ret, lua_tostring(script->L,1));
  17.         }
  18.         lua_pop(script->L, 1);
  19. }
  20.  
  21. void luaGetTableInteger(luaScript* script, const char* table, const char* name, int* ret){
  22.         lua_settop(script->L, 0);
  23.         lua_getglobal(script->L, table);
  24.         if (!lua_istable(script->L, 1)){
  25.                 luaSetError(script, "Variable %s is not a table\n", name);
  26.                 lua_pop(script->L, 1);
  27.         }else{
  28.                 lua_pushstring(script->L, name);
  29.                 lua_gettable(script->L, -2);
  30.                 if (lua_isnumber(script->L, -1)){
  31.                         *ret = lua_tointeger(script->L, -1);
  32.                         lua_pop(script->L, 2);
  33.                 }
  34.         }
  35. }
  36.  
  37. void luaGetTableString(luaScript* script, const char* table, const char* name, char* ret){
  38.         sceKernelDelayThread(10); //anti-freeze
  39.         lua_settop(script->L, 0);
  40.         lua_getglobal(script->L, table);
  41.         if (!lua_istable(script->L, 1)){
  42.                 luaSetError(script, "Variable %s is not a table\n", name);
  43.                 lua_pop(script->L, 1);
  44.         }else{
  45.                 lua_pushstring(script->L, name);
  46.                 lua_gettable(script->L, -2);
  47.                 if (lua_isstring(script->L, -1)){
  48.                         strcpy(ret, lua_tostring(script->L, -1));
  49.                         lua_pop(script->L, 2);
  50.                 }      
  51.         }
  52. }
  53.  
  54. void luaGetTableIntegerByIndex(luaScript* script, const char* name, int i, int* ret){
  55.         lua_settop(script->L, 0);
  56.         lua_getglobal(script->L, name);
  57.         if (!lua_istable(script->L, 1)){
  58.                 luaSetError(script, "Variable %s is not a table\n", name);
  59.                 lua_pop(script->L, 1);
  60.         }else{
  61.                 lua_pushnumber(script->L, i+1);
  62.                 lua_gettable(script->L, -2);
  63.                 if (lua_isnumber(script->L, -1)){
  64.                         *ret = lua_tointeger(script->L, -1);
  65.                         lua_pop(script->L, 2);
  66.                 }      
  67.         }
  68. }
  69.  
  70. void luaGetTableStringByIndex(luaScript* script, const char* name, int i, char* ret){
  71.         lua_settop(script->L, 0);
  72.         lua_getglobal(script->L, name);
  73.         if(!lua_istable(script->L, 1)){
  74.                 luaSetError(script, "Variable %s is not a table\n", name);
  75.                 lua_pop(script->L, 1);
  76.         }else{
  77.                 lua_pushnumber(script->L, i+1);
  78.                 lua_gettable(script->L, -2);
  79.                 if (lua_isstring(script->L, -1)){
  80.                         strcpy(ret, lua_tostring(script->L, -1));
  81.                         lua_pop(script->L, 2);
  82.                 }      
  83.         }
  84. }
  85.  
  86. void luaGetDoubleTableIntegerByIndex(luaScript* script, const char* name, int i, int j, int* ret){
  87.         lua_settop(script->L, 0);
  88.         lua_getglobal(script->L, name);
  89.         if(!lua_istable(script->L, 1)){
  90.                 luaSetError(script, "Variable %s is not a table\n", name);
  91.                 lua_pop(script->L, 1);
  92.         }else{
  93.                 lua_pushnumber(script->L, i+1);
  94.                 lua_gettable(script->L, -2);
  95.                 if(!lua_istable(script->L, -1)){
  96.                         luaSetError(script, "Variable %s is not a simple table\n", name);
  97.                         lua_pop(script->L, 2);
  98.                 }else{
  99.                         lua_pushnumber(script->L, j+1);
  100.                         lua_gettable(script->L, -2);
  101.                         *ret = lua_tointeger(script->L, -1);
  102.                         lua_pop(script->L, 4);
  103.                 }      
  104.         }
  105. }
  106.  
  107. int luaGlobalOpenTable(luaScript* script, const char* name){
  108.         lua_settop(script->L, 0);
  109.         lua_getglobal(script->L, name);
  110.         if(!lua_istable(script->L, 1)){
  111.                 luaSetError(script, "Variable %s is not a table\n", name);
  112.                 lua_pop(script->L, 1);
  113.                 return -1;
  114.         }
  115.         return lua_objlen(script->L, 1);
  116. }
  117.  
  118. int luaOpenTable(luaScript* script, const char* name){
  119.         lua_pushstring(script->L, name);
  120.         lua_gettable(script->L, -2);
  121.         if(!lua_istable(script->L, -1)){
  122.                 luaSetError(script, "Variable %s is not a table\n", name);
  123.                 lua_pop(script->L, 1);
  124.                 return -1;
  125.         }
  126.         return lua_objlen(script->L, -1);
  127. }
  128.  
  129. int luaOpenTableByIndex(luaScript* script, int index){
  130.         lua_pushnumber(script->L, index+1);
  131.         lua_gettable(script->L, -2);
  132.         if(!lua_istable(script->L, -1)){
  133.                 luaSetError(script, "Variable at index %d is not a table\n", index);
  134.                 lua_pop(script->L, 1);
  135.                 return -1;
  136.         }
  137.         return lua_objlen(script->L, -1);
  138. }
  139.  
  140. void luaGetInteger(luaScript* script, const char* name, int* ret){
  141.         lua_pushstring(script->L, name);
  142.         lua_gettable(script->L, -2);
  143.         if (lua_isnumber(script->L, -1)){
  144.                 *ret = lua_tointeger(script->L, -1);
  145.                 lua_pop(script->L, 1);
  146.         }
  147. }
  148.  
  149. void luaGetString(luaScript* script, const char* name, char* ret){
  150.         lua_pushstring(script->L, name);
  151.         lua_gettable(script->L, -2);
  152.         if (lua_isstring(script->L, -1)){
  153.                 strcpy(ret, lua_tostring(script->L, -1));
  154.                 lua_pop(script->L, 1);
  155.         }
  156. }
  157.  
  158. void luaCloseTable(luaScript* script){
  159.         lua_pop(script->L, 1);
  160. }
  161.  
  162. void luaGlobalCloseTable(luaScript* script){
  163.         lua_pop(script->L, -1);
  164. }