fixed formatting

This commit is contained in:
devZoGok
2023-01-20 19:02:57 +02:00
parent bbbb748f9d
commit 08ef768e2a
2 changed files with 126 additions and 126 deletions
+85 -85
View File
@@ -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<string> files){
string script = "";
void LuaManager::buildScript(vector<string> 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<Index> &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<Index> 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<Index> indices){
prepareTableForRetrieval(table, indices);
void LuaManager::prepareTableForRetrieval(string table, vector<Index> &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<Index> indices){
prepareTableForRetrieval(table, indices);
bool result = lua_toboolean(state, -1);
lua_pop(state, 1 + indices.size());
int LuaManager::getIntFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
return result;
}
int isNum;
int result = lua_tointegerx(state, -1, &isNum);
string LuaManager::getStringFromTable(string table, vector<Index> 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<Index> 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<Index> indices){
prepareTableForRetrieval(table, indices);
bool result = lua_toboolean(state, -1);
lua_pop(state, 1 + indices.size());
return result;
}
string LuaManager::getStringFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
string result = lua_tostring(state, -1);
lua_pop(state, 1 + indices.size());
return result;
}
return result;
}
}
+41 -41
View File
@@ -9,47 +9,47 @@
#include <vector>
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<std::string>);
void executeCode(std::string, std::string = "Error executing code\n");
int getInt(std::string);
float getFloat(std::string);
int getIntFromTable(std::string, std::vector<Index>);
bool getBoolFromTable(std::string, std::vector<Index>);
float getFloatFromTable(std::string, std::vector<Index>);
std::string getStringFromTable(std::string, std::vector<Index>);
std::string getString(std::string);
private:
LuaManager();
void prepareTableForRetrieval(std::string, std::vector<Index>&);
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<std::string>);
void executeCode(std::string, std::string = "Error executing code\n");
int getInt(std::string);
float getFloat(std::string);
int getIntFromTable(std::string, std::vector<Index>);
bool getBoolFromTable(std::string, std::vector<Index>);
float getFloatFromTable(std::string, std::vector<Index>);
std::string getStringFromTable(std::string, std::vector<Index>);
std::string getString(std::string);
private:
LuaManager();
void prepareTableForRetrieval(std::string, std::vector<Index>&);
lua_State *state = nullptr;
};
typedef LuaManager::Index Index;
}
#endif