#include "lua.h"
void luaGetVariableInteger(luaScript* script, const char* name, int* ret){
lua_settop(script->L, 0);
lua_getglobal(script->L, name);
if (lua_isnumber(script->L, 1)){
*ret = lua_tonumber(script->L,1);
}
lua_pop(script->L, 1);
}
void luaGetVariableString(luaScript* script, const char* name, char* ret){
lua_settop(script->L, 0);
lua_getglobal(script->L, name);
if (lua_isstring(script->L, 1)){
strcpy(ret, lua_tostring(script->L,1));
}
lua_pop(script->L, 1);
}
void luaGetTableInteger(luaScript* script, const char* table, const char* name, int* ret){
lua_settop(script->L, 0);
lua_getglobal(script->L, table);
if (!lua_istable(script->L, 1)){
luaSetError(script, "Variable %s is not a table\n", name);
lua_pop(script->L, 1);
}else{
lua_pushstring(script->L, name);
lua_gettable(script->L, -2);
if (lua_isnumber(script->L, -1)){
*ret = lua_tointeger(script->L, -1);
lua_pop(script->L, 2);
}
}
}
void luaGetTableString(luaScript* script, const char* table, const char* name, char* ret){
sceKernelDelayThread(10); //anti-freeze
lua_settop(script->L, 0);
lua_getglobal(script->L, table);
if (!lua_istable(script->L, 1)){
luaSetError(script, "Variable %s is not a table\n", name);
lua_pop(script->L, 1);
}else{
lua_pushstring(script->L, name);
lua_gettable(script->L, -2);
if (lua_isstring(script->L, -1)){
strcpy(ret, lua_tostring(script->L, -1));
lua_pop(script->L, 2);
}
}
}
void luaGetTableIntegerByIndex(luaScript* script, const char* name, int i, int* ret){
lua_settop(script->L, 0);
lua_getglobal(script->L, name);
if (!lua_istable(script->L, 1)){
luaSetError(script, "Variable %s is not a table\n", name);
lua_pop(script->L, 1);
}else{
lua_pushnumber(script->L, i+1);
lua_gettable(script->L, -2);
if (lua_isnumber(script->L, -1)){
*ret = lua_tointeger(script->L, -1);
lua_pop(script->L, 2);
}
}
}
void luaGetTableStringByIndex(luaScript* script, const char* name, int i, char* ret){
lua_settop(script->L, 0);
lua_getglobal(script->L, name);
if(!lua_istable(script->L, 1)){
luaSetError(script, "Variable %s is not a table\n", name);
lua_pop(script->L, 1);
}else{
lua_pushnumber(script->L, i+1);
lua_gettable(script->L, -2);
if (lua_isstring(script->L, -1)){
strcpy(ret, lua_tostring(script->L, -1));
lua_pop(script->L, 2);
}
}
}
void luaGetDoubleTableIntegerByIndex(luaScript* script, const char* name, int i, int j, int* ret){
lua_settop(script->L, 0);
lua_getglobal(script->L, name);
if(!lua_istable(script->L, 1)){
luaSetError(script, "Variable %s is not a table\n", name);
lua_pop(script->L, 1);
}else{
lua_pushnumber(script->L, i+1);
lua_gettable(script->L, -2);
if(!lua_istable(script->L, -1)){
luaSetError(script, "Variable %s is not a simple table\n", name);
lua_pop(script->L, 2);
}else{
lua_pushnumber(script->L, j+1);
lua_gettable(script->L, -2);
*ret = lua_tointeger(script->L, -1);
lua_pop(script->L, 4);
}
}
}
int luaGlobalOpenTable(luaScript* script, const char* name){
lua_settop(script->L, 0);
lua_getglobal(script->L, name);
if(!lua_istable(script->L, 1)){
luaSetError(script, "Variable %s is not a table\n", name);
lua_pop(script->L, 1);
return -1;
}
return lua_objlen(script->L, 1);
}
int luaOpenTable(luaScript* script, const char* name){
lua_pushstring(script->L, name);
lua_gettable(script->L, -2);
if(!lua_istable(script->L, -1)){
luaSetError(script, "Variable %s is not a table\n", name);
lua_pop(script->L, 1);
return -1;
}
return lua_objlen(script->L, -1);
}
int luaOpenTableByIndex(luaScript* script, int index){
lua_pushnumber(script->L, index+1);
lua_gettable(script->L, -2);
if(!lua_istable(script->L, -1)){
luaSetError(script, "Variable at index %d is not a table\n", index);
lua_pop(script->L, 1);
return -1;
}
return lua_objlen(script->L, -1);
}
void luaGetInteger(luaScript* script, const char* name, int* ret){
lua_pushstring(script->L, name);
lua_gettable(script->L, -2);
if (lua_isnumber(script->L, -1)){
*ret = lua_tointeger(script->L, -1);
lua_pop(script->L, 1);
}
}
void luaGetString(luaScript* script, const char* name, char* ret){
lua_pushstring(script->L, name);
lua_gettable(script->L, -2);
if (lua_isstring(script->L, -1)){
strcpy(ret, lua_tostring(script->L, -1));
lua_pop(script->L, 1);
}
}
void luaCloseTable(luaScript* script){
lua_pop(script->L, 1);
}
void luaGlobalCloseTable(luaScript* script){
lua_pop(script->L, -1);
}