From b9a47c687c735594aa7d3b818e81abab3c4d1429 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 20 Jan 2023 20:23:08 +0200 Subject: [PATCH] merged methods for extracting fields directly from scripts or tables within --- abstractAppState.cpp | 11 ++++---- luaManager.cpp | 60 ++++++++++++++------------------------------ luaManager.h | 13 ++++------ 3 files changed, 29 insertions(+), 55 deletions(-) diff --git a/abstractAppState.cpp b/abstractAppState.cpp index 23a09d0..0657acd 100644 --- a/abstractAppState.cpp +++ b/abstractAppState.cpp @@ -20,14 +20,13 @@ namespace gameBase { LuaManager *luaManager = LuaManager::getSingleton(); luaManager->buildScript(vector{optionsFile}); - string table = "mappings"; for(int i = firstMapping; i < firstMapping + numMappings; i++){ - Index idIndex = Index(i + 1); - int bind = luaManager->getIntFromTable(table, vector{idIndex, Index("bind")}); - Mapping::BindType type = (Mapping::BindType)luaManager->getIntFromTable(table, vector{idIndex, Index("bindType")}); - bool action = luaManager->getBoolFromTable(table, vector{idIndex, Index("action")}); - int trigger = luaManager->getIntFromTable(table, vector{idIndex, Index("trigger")}); + Index tableInd = Index("mappings"), idIndex = Index(i + 1); + int bind = luaManager->getInt(vector{tableInd, idIndex, Index("bind")}); + Mapping::BindType type = (Mapping::BindType)luaManager->getInt(vector{tableInd, idIndex, Index("bindType")}); + bool action = luaManager->getBool(vector{tableInd, idIndex, Index("action")}); + int trigger = luaManager->getInt(vector{tableInd, idIndex, Index("trigger")}); Mapping *m = new Mapping; m->bind = bind; diff --git a/luaManager.cpp b/luaManager.cpp index b59cd79..9ab15f6 100644 --- a/luaManager.cpp +++ b/luaManager.cpp @@ -32,37 +32,13 @@ namespace gameBase{ cout << errMsg; } - int LuaManager::getInt(string varName){ - lua_getglobal(state, varName.c_str()); - int isNum; - int var = (int)lua_tointegerx(state, -1, &isNum); - lua_pop(state, 1); - return var; - } + void LuaManager::prepareTableForRetrieval(vector &indices){ + lua_getglobal(state, indices[0].index.c_str()); - float LuaManager::getFloat(string varName){ - lua_getglobal(state, varName.c_str()); - int isNum; - float var = (int)lua_tonumberx(state, -1, &isNum); - lua_pop(state, 1); - return var; - } + for(int i = 1; i < indices.size(); i++){ + if(!lua_istable(state, -1)) + cout << indices[i].index << " is not in the " << indices[0].index << " table\n"; - string LuaManager::getString(string varName){ - lua_getglobal(state, varName.c_str()); - string result = lua_tostring(state, -1); - lua_pop(state, 1); - - return result; - } - - void LuaManager::prepareTableForRetrieval(string table, vector &indices){ - lua_getglobal(state, table.c_str()); - - if(!lua_istable(state, -1)) - cout << table << " is not a table\n"; - - for(int i = 0; i < indices.size(); i++){ if(indices[i].string) lua_pushstring(state, indices[i].index.c_str()); else @@ -72,40 +48,42 @@ namespace gameBase{ } } - int LuaManager::getIntFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); + int LuaManager::getInt(vector indices){ + prepareTableForRetrieval(indices); int isNum; int result = lua_tointegerx(state, -1, &isNum); - lua_pop(state, 1 + indices.size()); + lua_pop(state, indices.size()); return result; } - float LuaManager::getFloatFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); + float LuaManager::getFloat(vector indices){ + prepareTableForRetrieval(indices); int isNum; float result = lua_tonumberx(state, -1, &isNum); - lua_pop(state, 1 + indices.size()); + lua_pop(state, indices.size()); return result; } - bool LuaManager::getBoolFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); + bool LuaManager::getBool(vector indices){ + prepareTableForRetrieval(indices); + bool result = lua_toboolean(state, -1); - lua_pop(state, 1 + indices.size()); + lua_pop(state, indices.size()); return result; } - string LuaManager::getStringFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); + string LuaManager::getString(vector indices){ + prepareTableForRetrieval(indices); + string result = lua_tostring(state, -1); - lua_pop(state, 1 + indices.size()); + lua_pop(state, indices.size()); return result; } diff --git a/luaManager.h b/luaManager.h index 790c3a0..2571243 100644 --- a/luaManager.h +++ b/luaManager.h @@ -35,16 +35,13 @@ namespace gameBase{ inline lua_State* getState(){return state;} void buildScript(std::vector); void executeCode(std::string, std::string = "Error executing code\n"); - int getInt(std::string); - float getFloat(std::string); - int getIntFromTable(std::string, std::vector); - bool getBoolFromTable(std::string, std::vector); - float getFloatFromTable(std::string, std::vector); - std::string getStringFromTable(std::string, std::vector); - std::string getString(std::string); + int getInt(std::vector); + bool getBool(std::vector); + float getFloat(std::vector); + std::string getString(std::vector); private: LuaManager(); - void prepareTableForRetrieval(std::string, std::vector&); + void prepareTableForRetrieval(std::vector&); lua_State *state = nullptr; };