From 08ef768e2a794e6ffd08b0a20f191ccf792c41f5 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 20 Jan 2023 19:02:57 +0200 Subject: [PATCH] fixed formatting --- luaManager.cpp | 170 ++++++++++++++++++++++++------------------------- luaManager.h | 82 ++++++++++++------------ 2 files changed, 126 insertions(+), 126 deletions(-) diff --git a/luaManager.cpp b/luaManager.cpp index 2fe81c2..b59cd79 100644 --- a/luaManager.cpp +++ b/luaManager.cpp @@ -5,108 +5,108 @@ using namespace std; namespace gameBase{ - static LuaManager *luaManager = nullptr; + static LuaManager *luaManager = nullptr; - LuaManager* LuaManager::getSingleton(){ - if(!luaManager) luaManager = new LuaManager(); + LuaManager* LuaManager::getSingleton(){ + if(!luaManager) luaManager = new LuaManager(); - return luaManager; - } + return luaManager; + } - LuaManager::LuaManager(){ - state = luaL_newstate(); - luaL_openlibs(state); - } + LuaManager::LuaManager(){ + state = luaL_newstate(); + luaL_openlibs(state); + } - void LuaManager::buildScript(vector files){ - string script = ""; + void LuaManager::buildScript(vector files){ + string script = ""; - for(string f : files) - script += "dofile(\"" + f + "\");"; + for(string f : files) + script += "dofile(\"" + f + "\");"; - executeCode(script, "Error building script(s)\n"); + executeCode(script, "Error building script(s)\n"); + } + + void LuaManager::executeCode(string code, string errMsg){ + if(luaL_dostring(state, code.c_str()) && lua_pcall(state, 0, 0, 0)) + 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; + } + + 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; + } + + 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 + lua_pushnumber(state, atoi(indices[i].index.c_str())); + + lua_gettable(state, -2); } + } - void LuaManager::executeCode(string code, string errMsg){ - if(luaL_dostring(state, code.c_str()) && lua_pcall(state, 0, 0, 0)) - cout << errMsg; - } + int LuaManager::getIntFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); - 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; - } + int isNum; + int result = lua_tointegerx(state, -1, &isNum); - 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; - } + lua_pop(state, 1 + indices.size()); - string LuaManager::getString(string varName){ - lua_getglobal(state, varName.c_str()); - string result = lua_tostring(state, -1); - lua_pop(state, 1); + return result; + } - return result; - } + float LuaManager::getFloatFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); - void LuaManager::prepareTableForRetrieval(string table, vector &indices){ - lua_getglobal(state, table.c_str()); + int isNum; + float result = lua_tonumberx(state, -1, &isNum); - if(!lua_istable(state, -1)) - cout << table << " is not a table\n"; + lua_pop(state, 1 + indices.size()); - for(int i = 0; i < indices.size(); i++){ - if(indices[i].string) - lua_pushstring(state, indices[i].index.c_str()); - else - lua_pushnumber(state, atoi(indices[i].index.c_str())); + return result; + } - lua_gettable(state, -2); - } - } + bool LuaManager::getBoolFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); + bool result = lua_toboolean(state, -1); + lua_pop(state, 1 + indices.size()); - int LuaManager::getIntFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); + return result; + } - int isNum; - int result = lua_tointegerx(state, -1, &isNum); + string LuaManager::getStringFromTable(string table, vector indices){ + prepareTableForRetrieval(table, indices); + string result = lua_tostring(state, -1); + lua_pop(state, 1 + indices.size()); - lua_pop(state, 1 + indices.size()); - - return result; - } - - float LuaManager::getFloatFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); - - int isNum; - float result = lua_tonumberx(state, -1, &isNum); - - lua_pop(state, 1 + indices.size()); - - return result; - } - - bool LuaManager::getBoolFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); - bool result = lua_toboolean(state, -1); - lua_pop(state, 1 + indices.size()); - - return result; - } - - string LuaManager::getStringFromTable(string table, vector indices){ - prepareTableForRetrieval(table, indices); - string result = lua_tostring(state, -1); - lua_pop(state, 1 + indices.size()); - - return result; - } + return result; + } } diff --git a/luaManager.h b/luaManager.h index fcd0826..790c3a0 100644 --- a/luaManager.h +++ b/luaManager.h @@ -9,47 +9,47 @@ #include namespace gameBase{ - class LuaManager{ - public: - struct Index{ - std::string index; - bool string; - - Index(std::string index){ - this->index = index; - this->string = true; - } - - Index(int index){ - this->index = std::to_string(index); - this->string = false; - } - - Index(float index){ - this->index = std::to_string(index); - this->string = false; - } - }; - - static LuaManager* getSingleton(); - 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); - private: - LuaManager(); - void prepareTableForRetrieval(std::string, std::vector&); - - lua_State *state = nullptr; - }; - - typedef LuaManager::Index Index; + class LuaManager{ + public: + struct Index{ + std::string index; + bool string; + + Index(std::string index){ + this->index = index; + this->string = true; + } + + Index(int index){ + this->index = std::to_string(index); + this->string = false; + } + + Index(float index){ + this->index = std::to_string(index); + this->string = false; + } + }; + + static LuaManager* getSingleton(); + 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); + private: + LuaManager(); + void prepareTableForRetrieval(std::string, std::vector&); + + lua_State *state = nullptr; + }; + + typedef LuaManager::Index Index; } #endif