From adb5cb666465dda2eeb90a6df805e26407f79e65 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Thu, 9 Jul 2020 19:26:48 +0200 Subject: [PATCH] Made the es_systems.cfg template install to the home directory during the first application startup. --- es-app/src/SystemData.cpp | 68 ++++++++++------------------ es-app/src/SystemData.h | 2 +- es-app/src/main.cpp | 42 ++++++++--------- es-core/src/utils/FileSystemUtil.cpp | 49 ++++++++++++++++++++ es-core/src/utils/FileSystemUtil.h | 2 + 5 files changed, 96 insertions(+), 67 deletions(-) diff --git a/es-app/src/SystemData.cpp b/es-app/src/SystemData.cpp index dc9e28e17..2866603c5 100644 --- a/es-app/src/SystemData.cpp +++ b/es-app/src/SystemData.cpp @@ -9,6 +9,7 @@ #include "SystemData.h" +#include "resources/ResourceManager.h" #include "utils/FileSystemUtil.h" #include "utils/StringUtil.h" #include "CollectionSystemManager.h" @@ -218,16 +219,17 @@ bool SystemData::loadConfig() LOG(LogInfo) << "Loading system config file " << path << "..."; if (!Utils::FileSystem::exists(path)) { - LOG(LogError) << "Error - es_systems.cfg file does not exist!"; - writeExampleConfig(getConfigPath(true)); - return false; + LOG(LogWarning) << "Warning - es_systems.cfg does not exist."; + if (copyConfigTemplate(getConfigPath(true))) + return false; + path = getConfigPath(false); } pugi::xml_document doc; pugi::xml_parse_result res = doc.load_file(path.c_str()); if (!res) { - LOG(LogError) << "Error - Could not parse es_systems.cfg file!"; + LOG(LogError) << "Error - Could not parse es_systems.cfg"; LOG(LogError) << res.description(); return false; } @@ -331,50 +333,28 @@ bool SystemData::loadConfig() return true; } -void SystemData::writeExampleConfig(const std::string& path) +bool SystemData::copyConfigTemplate(const std::string& path) { - std::ofstream file(path.c_str()); + std::string systemsTemplateFile;; - file << "\n" - "\n" - "\n" - " \n" - " \n" - "\n" - " \n" - " nes\n" - "\n" - " \n" - " Nintendo Entertainment System\n" - "\n" - " \n" - " ~/roms/nes\n" - "\n" - " \n" - " .nes .NES\n" - "\n" - " \n" - " retroarch -L ~/cores/libretro-fceumm.so %ROM%\n" - "\n" - " \n" - " nes\n" - "\n" - " \n" - " nes\n" - " \n" - "\n"; + LOG(LogInfo) << "Attempting to copy template es_systems.cfg file from the resources directory."; - file.close(); + #if defined (_WIN64) + systemsTemplateFile = ResourceManager::getInstance()-> + getResourcePath(":/templates/es_systems.cfg_windows"); + #elif defined(__unix__) + systemsTemplateFile = ResourceManager::getInstance()-> + getResourcePath(":/templates/es_systems.cfg_unix"); + #endif - LOG(LogError) << "Example config written! Go read it at \"" << path << "\"!"; + if (Utils::FileSystem::copyFile(systemsTemplateFile, path, false)) { + LOG(LogError) << "Error - Copying of es_systems.cfg template file failed."; + return true; + } + + LOG(LogInfo) << "Template es_systems.cfg file copied successfully."; + + return false; } void SystemData::deleteSystems() diff --git a/es-app/src/SystemData.h b/es-app/src/SystemData.h index fc68b850c..076911919 100644 --- a/es-app/src/SystemData.h +++ b/es-app/src/SystemData.h @@ -70,7 +70,7 @@ public: // Returns true if no errors were encountered. // An example will be written if the file doesn't exist. static bool loadConfig(); - static void writeExampleConfig(const std::string& path); + static bool copyConfigTemplate(const std::string& path); static std::string getConfigPath(bool forWrite); static std::vector sSystemVector; diff --git a/es-app/src/main.cpp b/es-app/src/main.cpp index 6d03ee107..bd0ea7993 100644 --- a/es-app/src/main.cpp +++ b/es-app/src/main.cpp @@ -50,13 +50,6 @@ #include #include -enum eErrorCodes { - NO_ERRORS, - NO_SYSTEMS_FILE, - NO_ROMS -}; - - #ifdef _WIN64 enum eConsoleType { NO_CONSOLE, @@ -336,31 +329,36 @@ bool verifyHomeFolderExists() // Returns NO_ERRORS if everything is OK. // Otherwise returns either NO_SYSTEMS_FILE or NO_ROMS. -unsigned int loadSystemConfigFile(std::string& errorMsg) +bool loadSystemConfigFile(std::string& errorMsg) { if (!SystemData::loadConfig()) { - LOG(LogError) << "Error while parsing systems configuration file!"; + LOG(LogError) << "Error - Could not parse systems configuration file."; errorMsg = "COULDN'T FIND THE SYSTEMS CONFIGURATION FILE.\n" - "WILL ATTEMPT TO INSTALL A TEMPLATE ES_SYSTEMS.CFG FILE FROM " - "THE EMULATIONSTATION RESOURCES DIRECTORY.\n" - "PLEASE RESTART THE APPLICATION."; - return NO_SYSTEMS_FILE; + "ATTEMPTED TO COPY A TEMPLATE ES_SYSTEMS.CFG FILE\n" + "FROM THE EMULATIONSTATION RESOURCES DIRECTORY,\n" + "BUT THIS FAILED. HAS EMULATIONSTATION BEEN PROPERLY\n" + "INSTALLED AND DO YOU HAVE WRITE PERMISSIONS TO \n" + "YOUR HOME DIRECTORY?"; + return true; } if (SystemData::sSystemVector.size() == 0) { - LOG(LogError) << "No systems found! Does at least one system have a game present? (check " - "that extensions match!)\n"; - errorMsg = "THE SYSTEMS CONFIGURATION FILE EXISTS BUT NO GAME " - "ROM FILES WERE FOUND. PLEASE MAKE SURE THAT THE 'ROMDIRECTORY' " - "SETTING IN ES_SYSTEMS.CFG IS POINTING TO YOUR ROM DIRECTORY " - "AND THAT YOUR GAME ROMS ARE USING SUPPORTED FILE EXTENSIONS. " + LOG(LogError) << "Error - No systems found, does at least one system have a game present? " + "(Check that the file extensions are supported.)"; + errorMsg = "THE SYSTEMS CONFIGURATION FILE EXISTS, BUT NO\n" + "GAME FILES WERE FOUND. PLEASE MAKE SURE THAT\n" + "THE 'ROMDIRECTORY' SETTING IN ES_SETTINGS.CFG IS\n" + "POINTING TO YOUR ROM DIRECTORY AND THAT YOUR\n" + "GAME FILES ARE USING SUPPORTED FILE EXTENSIONS.\n" + "THE GAME SYSTEMS SUBDIRECTORIES ALSO NEED TO\n" + "MATCH THE PLATFORM TAGS IN ES_SYSTEMS.CFG.\n" "THIS IS THE CURRENTLY CONFIGURED ROM DIRECTORY:\n"; errorMsg += FileData::getROMDirectory(); - return NO_ROMS; + return true; } - return NO_ERRORS; + return false; } // Called on exit, assuming we get far enough to have the log initialized. @@ -437,7 +435,7 @@ int main(int argc, char* argv[]) std::string errorMsg; - if (loadSystemConfigFile(errorMsg) != NO_ERRORS) { + if (loadSystemConfigFile(errorMsg)) { // Something went terribly wrong. if (errorMsg == "") { LOG(LogError) << "Unknown error occured while parsing system config file."; diff --git a/es-core/src/utils/FileSystemUtil.cpp b/es-core/src/utils/FileSystemUtil.cpp index 271536cef..c6e21325e 100644 --- a/es-core/src/utils/FileSystemUtil.cpp +++ b/es-core/src/utils/FileSystemUtil.cpp @@ -9,8 +9,10 @@ #define _FILE_OFFSET_BITS 64 #include "utils/FileSystemUtil.h" +#include "Log.h" #include +#include #include #if defined(_WIN64) @@ -516,6 +518,53 @@ namespace Utils return resolved; } + bool copyFile(const std::string& _source_path, + const std::string& _destination_path, bool _overwrite) + { + if (!exists(_source_path)) { + LOG(LogError) << "Error - Can't copy file, source file does not exist:"; + LOG(LogError) << _source_path; + return true; + } + + if(isDirectory(_destination_path)) { + LOG(LogError) << "Error - Destination file is actually a directory:"; + LOG(LogError) << _destination_path; + return true; + } + + if (!_overwrite && exists(_destination_path)) { + LOG(LogError) << "Error - Destination file exists and the overwrite flag " + "has not been set."; + return true; + } + + std::ifstream sourceFile(_source_path, std::ios::binary); + + if (sourceFile.fail()) { + LOG(LogError) << "Error - Couldn't read from source file (" << _source_path << + "), permission problems?"; + sourceFile.close(); + return true; + } + + std::ofstream targetFile(_destination_path, std::ios::binary); + + if (targetFile.fail()) { + LOG(LogError) << "Error - Couldn't write to target file (" << _destination_path << + "), permission problems?"; + targetFile.close(); + return true; + } + + targetFile << sourceFile.rdbuf(); + + sourceFile.close(); + targetFile.close(); + + return false; + } + bool removeFile(const std::string& _path) { std::string path = getGenericPath(_path); diff --git a/es-core/src/utils/FileSystemUtil.h b/es-core/src/utils/FileSystemUtil.h index a431afcdc..548683958 100644 --- a/es-core/src/utils/FileSystemUtil.h +++ b/es-core/src/utils/FileSystemUtil.h @@ -45,6 +45,8 @@ namespace Utils std::string removeCommonPath(const std::string& _path, const std::string& _common, bool& _contains); std::string resolveSymlink(const std::string& _path); + bool copyFile(const std::string& _source_path, + const std::string& _destination_path, bool _overwrite); bool removeFile(const std::string& _path); bool createDirectory(const std::string& _path); bool exists(const std::string& _path);