From edbc1afa5676b5ca60a61e2fd71507edf5f8789d Mon Sep 17 00:00:00 2001 From: emb <> Date: Sun, 22 Nov 2015 22:05:08 -0600 Subject: [PATCH] Fixed table pushing for LuaCollection::getItemAt(). Cleaned up configuration loading. --- RetroFE/Source/Lua/LuaCollection.cpp | 66 ++++++++++++++++++- RetroFE/Source/Lua/LuaCollection.h | 4 ++ RetroFE/Source/Main.cpp | 94 +++++++++++++++++++++++++++- RetroFE/Source/RetroFE.cpp | 5 ++ 4 files changed, 165 insertions(+), 4 deletions(-) diff --git a/RetroFE/Source/Lua/LuaCollection.cpp b/RetroFE/Source/Lua/LuaCollection.cpp index 01f551c..08dc6aa 100644 --- a/RetroFE/Source/Lua/LuaCollection.cpp +++ b/RetroFE/Source/Lua/LuaCollection.cpp @@ -22,6 +22,58 @@ int LuaCollection::load(lua_State *l) return 0; } +int LuaCollection::getSize(lua_State *l) +{ + CollectionInfo *i = (CollectionInfo *)luaL_checkinteger(l, 1); + + lua_pushinteger(l, (int)i->items.size()); + + return 1; +} + +int LuaCollection::getName(lua_State *l) +{ + CollectionInfo *i = (CollectionInfo *)luaL_checkinteger(l, 1); + + lua_pushstring(l, i->name.c_str()); + + return 1; +} + +int LuaCollection::getItemAt(lua_State *l) +{ + CollectionInfo *i = (CollectionInfo *)luaL_checkinteger(l, 1); + int index = (int)luaL_checkinteger(l, 2); + Item *item = i->items.at(index); + + std::string name; + std::string filepath; + std::string title; + std::string fullTitle; + std::string year; + std::string manufacturer; + std::string genre; + std::string cloneof; + std::string numberPlayers; + std::string numberButtons; + + lua_createtable(l, 0, 4); + lua_pushstring(l, "name"); + lua_pushstring(l, item->name.c_str()); + lua_settable(l, -3); + + lua_pushstring(l, "title"); + lua_pushstring(l, item->title.c_str()); + lua_settable(l, -3); + + lua_pushstring(l, "filepath"); + lua_pushstring(l, item->filepath.c_str()); + lua_settable(l, -3); + + return 1; +} + + int LuaCollection::destroy(lua_State *l) { CollectionInfo *i = (CollectionInfo *)luaL_checkinteger(l, 1); @@ -31,9 +83,17 @@ int LuaCollection::destroy(lua_State *l) } +CollectionInfo *loadingCollection = NULL; void LuaCollection::loadCallback(void *context, CollectionInfo *info) { - lua_State *l = (lua_State *)context; - events->trigger(l, "onCollectionLoaded", (void *)info); - + loadingCollection = info; } + +void LuaCollection::update(lua_State *l) { + if(loadingCollection) { + events->trigger(l, "onCollectionLoaded", (void *)loadingCollection); + loadingCollection = NULL; + + } +} + diff --git a/RetroFE/Source/Lua/LuaCollection.h b/RetroFE/Source/Lua/LuaCollection.h index e053b7b..a377330 100644 --- a/RetroFE/Source/Lua/LuaCollection.h +++ b/RetroFE/Source/Lua/LuaCollection.h @@ -9,7 +9,11 @@ namespace LuaCollection { void initialize(Configuration *c, CollectionInfoBuilder *b, LuaEvent *e); int load(lua_State *l); + int getSize(lua_State *l); + int getName(lua_State *l); + int getItemAt(lua_State *l); int destroy(lua_State *l); + void update(lua_State *l); void loadCallback(void *context, CollectionInfo *info); }; \ No newline at end of file diff --git a/RetroFE/Source/Main.cpp b/RetroFE/Source/Main.cpp index 4df020f..3586a5e 100644 --- a/RetroFE/Source/Main.cpp +++ b/RetroFE/Source/Main.cpp @@ -30,7 +30,9 @@ int main(int argc, char **argv) { Configuration::initialize(); Configuration config; - config.import("", "C:/Users/Don/Downloads/RetroFE-FTP/settings.conf"); + if(!ImportConfiguration(&config)) { + return -1; + } if(!StartLogging()) { @@ -46,6 +48,96 @@ int main(int argc, char **argv) return 0; } +bool ImportConfiguration(Configuration *c) +{ + std::string configPath = Configuration::absolutePath; + std::string launchersPath = Utils::combinePath(Configuration::absolutePath, "launchers"); + std::string collectionsPath = Utils::combinePath(Configuration::absolutePath, "collections"); + DIR *dp; + struct dirent *dirp; + + std::string settingsConfPath = Utils::combinePath(configPath, "settings.conf"); + if(!c->import("", settingsConfPath)) + { + Logger::write(Logger::ZONE_ERROR, "RetroFE", "Could not import \"" + settingsConfPath + "\""); + return false; + } + + std::string controlsConfPath = Utils::combinePath(configPath, "controls.conf"); + if(!c->import("controls", controlsConfPath)) + { + Logger::write(Logger::ZONE_ERROR, "RetroFE", "Could not import \"" + controlsConfPath + "\""); + return false; + } + + dp = opendir(launchersPath.c_str()); + + if(dp == NULL) + { + Logger::write(Logger::ZONE_NOTICE, "RetroFE", "Could not read directory \"" + launchersPath + "\""); + return false; + } + + while((dirp = readdir(dp)) != NULL) + { + if (dirp->d_type != DT_DIR && std::string(dirp->d_name) != "." && std::string(dirp->d_name) != "..") + { + std::string basename = dirp->d_name; + + std::string extension = basename.substr(basename.find_last_of("."), basename.size()-1); + basename = basename.substr(0, basename.find_last_of(".")); + + if(extension == ".conf") + { + std::string prefix = "launchers." + basename; + + std::string importFile = Utils::combinePath(launchersPath, std::string(dirp->d_name)); + + if(!c->import(prefix, importFile)) + { + Logger::write(Logger::ZONE_ERROR, "RetroFE", "Could not import \"" + importFile + "\""); + closedir(dp); + return false; + } + } + } + } + + closedir(dp); + + dp = opendir(collectionsPath.c_str()); + + if(dp == NULL) + { + Logger::write(Logger::ZONE_ERROR, "RetroFE", "Could not read directory \"" + collectionsPath + "\""); + return false; + } + + while((dirp = readdir(dp)) != NULL) + { + std::string collection = (dirp->d_name); + if (dirp->d_type == DT_DIR && collection != "." && collection != ".." && collection.length() > 0 && collection[0] != '_') + { + std::string prefix = "collections." + collection; + + std::string settingsFile = Utils::combinePath(collectionsPath, collection, "settings.conf"); + + if(!c->import(collection, prefix, settingsFile)) + { + Logger::write(Logger::ZONE_ERROR, "RetroFE", "Could not import \"" + settingsFile + "\""); + closedir(dp); + return false; + } + } + } + + closedir(dp); + + Logger::write(Logger::ZONE_INFO, "RetroFE", "Imported configuration"); + + return true; +} + bool StartLogging() { std::string logFile = Utils::combinePath(Configuration::absolutePath, "log.txt"); diff --git a/RetroFE/Source/RetroFE.cpp b/RetroFE/Source/RetroFE.cpp index 230cbb3..8b5c62e 100644 --- a/RetroFE/Source/RetroFE.cpp +++ b/RetroFE/Source/RetroFE.cpp @@ -83,6 +83,9 @@ const luaL_Reg RetroFE::luaImageFuncs[] = { const luaL_Reg RetroFE::luaCollectionFuncs[] = { {"load", LuaCollection::load}, {"destroy", LuaCollection::destroy}, + {"getSize", LuaCollection::getSize}, + {"getName", LuaCollection::getName}, + {"getItemAt", LuaCollection::getItemAt}, {NULL, NULL} }; @@ -188,6 +191,8 @@ void RetroFE::run() deltaTime = currentTime - lastTime; + LuaCollection::update(lua_.state); + state.update((float)deltaTime); SDL_LockMutex(SDL::getMutex());