From 8571dfe7bf09439143e88fe066224b39d97af70b Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Tue, 12 Dec 2023 23:21:27 +0100 Subject: [PATCH] (Android) Implemented a cleaner way of handling the private data directory --- es-app/src/FileData.cpp | 4 +++ es-app/src/main.cpp | 4 +++ es-core/src/ThemeData.cpp | 41 ++++++++++------------- es-core/src/resources/ResourceManager.cpp | 26 +++++++------- es-core/src/resources/ResourceManager.h | 4 --- es-core/src/utils/FileSystemUtil.cpp | 12 ++++--- es-core/src/utils/PlatformUtil.h | 5 --- 7 files changed, 44 insertions(+), 52 deletions(-) diff --git a/es-app/src/FileData.cpp b/es-app/src/FileData.cpp index 15918bcda..889b26b7e 100644 --- a/es-app/src/FileData.cpp +++ b/es-app/src/FileData.cpp @@ -25,6 +25,10 @@ #include "views/GamelistView.h" #include "views/ViewController.h" +#if defined(__ANDROID__) +#include "utils/PlatformUtilAndroid.h" +#endif + #include #include diff --git a/es-app/src/main.cpp b/es-app/src/main.cpp index e6010c332..c61c4d0b5 100644 --- a/es-app/src/main.cpp +++ b/es-app/src/main.cpp @@ -37,6 +37,10 @@ #include #include +#if defined(__ANDROID__) +#include "utils/PlatformUtilAndroid.h" +#endif + #if defined(__EMSCRIPTEN__) #include #endif diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index fed58c4cd..8469b23ca 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -696,32 +696,27 @@ void ThemeData::populateThemes() userThemeDirectory = defaultUserThemeDir; } -#if defined(__unix__) || defined(__APPLE__) -#if defined(APPIMAGE_BUILD) - static const size_t pathCount {2}; -#else - static const size_t pathCount {3}; -#endif -#else - static const size_t pathCount {2}; -#endif - std::string paths[pathCount] = { +#if defined(__ANDROID__) + const std::vector themePaths {Utils::FileSystem::getProgramDataPath() + "/themes", + userThemeDirectory}; +#elif defined(__APPLE__) + const std::vector themePaths { Utils::FileSystem::getExePath() + "/themes", -#if defined(__APPLE__) - Utils::FileSystem::getExePath() + "/../Resources/themes", -#elif defined(__ANDROID__) - ResourceManager::getInstance().getDataDirectory() + "/themes", -#elif defined(__unix__) && !defined(APPIMAGE_BUILD) - Utils::FileSystem::getProgramDataPath() + "/themes", + Utils::FileSystem::getExePath() + "/../Resources/themes", userThemeDirectory}; +#elif defined(_WIN64) || defined(APPIMAGE_BUILD) + const std::vector themePaths {Utils::FileSystem::getExePath() + "/themes", + userThemeDirectory}; +#else + const std::vector themePaths {Utils::FileSystem::getExePath() + "/themes", + Utils::FileSystem::getProgramDataPath() + "/themes", + userThemeDirectory}; #endif - userThemeDirectory - }; - for (size_t i {0}; i < pathCount; ++i) { - if (!Utils::FileSystem::isDirectory(paths[i])) + for (auto path : themePaths) { + if (!Utils::FileSystem::isDirectory(path)) continue; - Utils::FileSystem::StringList dirContent {Utils::FileSystem::getDirContent(paths[i])}; + Utils::FileSystem::StringList dirContent {Utils::FileSystem::getDirContent(path)}; for (Utils::FileSystem::StringList::const_iterator it = dirContent.cbegin(); it != dirContent.cend(); ++it) { @@ -732,7 +727,6 @@ void ThemeData::populateThemes() Utils::String::toLower(themeDirName.substr(themeDirName.length() - 8, 8)) == "disabled")) continue; - #if defined(_WIN64) LOG(LogDebug) << "Loading theme capabilities for \"" << Utils::String::replace(*it, "/", "\\") << "\"..."; @@ -745,9 +739,8 @@ void ThemeData::populateThemes() continue; std::string themeName; - if (capabilities.themeName != "") { + if (capabilities.themeName != "") themeName.append(" (\"").append(capabilities.themeName).append("\")"); - } #if defined(_WIN64) LOG(LogInfo) << "Added theme \"" << Utils::String::replace(*it, "/", "\\") << "\"" diff --git a/es-core/src/resources/ResourceManager.cpp b/es-core/src/resources/ResourceManager.cpp index d4f5e3201..2902cfe01 100644 --- a/es-core/src/resources/ResourceManager.cpp +++ b/es-core/src/resources/ResourceManager.cpp @@ -41,18 +41,22 @@ std::string ResourceManager::getResourcePath(const std::string& path, bool termi if (Utils::FileSystem::exists(applePackagePath)) { return applePackagePath; } -#elif defined(__unix__) && !defined(APPIMAGE_BUILD) -#if defined(__ANDROID__) - std::string testDataPath {mDataDirectory + "/resources/" + &path[2]}; -#else - // Check under the data installation directory (Unix only). +#elif (defined(__unix__) && !defined(APPIMAGE_BUILD)) || defined(__ANDROID__) + // Check in the program data directory. std::string testDataPath {Utils::FileSystem::getProgramDataPath() + "/resources/" + &path[2]}; -#endif if (Utils::FileSystem::exists(testDataPath)) return testDataPath; #endif -#if !defined(__ANDROID__) +#if defined(__ANDROID__) + // Check in the assets directory using AssetManager. + SDL_RWops* resFile {SDL_RWFromFile(path.substr(2).c_str(), "rb")}; + if (resFile != nullptr) { + SDL_RWclose(resFile); + return path.substr(2); + } + else { +#else // Check under the ES executable directory. std::string testExePath {Utils::FileSystem::getExePath() + "/resources/" + &path[2]}; @@ -63,13 +67,6 @@ std::string ResourceManager::getResourcePath(const std::string& path, bool termi // indicate that we have a broken EmulationStation installation. If the argument // terminateOnFailure is set to false though, then skip this step. else { -#else - SDL_RWops* resFile {SDL_RWFromFile(path.substr(2).c_str(), "rb")}; - if (resFile != nullptr) { - SDL_RWclose(resFile); - return path.substr(2); - } - else { #endif if (terminateOnFailure) { LOG(LogError) << "Program resource missing: " << path; @@ -102,6 +99,7 @@ const ResourceData ResourceManager::getFileData(const std::string& path) const const std::string respath {getResourcePath(path)}; #if defined(__ANDROID__) + // Check in the assets directory using AssetManager. SDL_RWops* resFile {SDL_RWFromFile(respath.c_str(), "rb")}; if (resFile != nullptr) { ResourceData data {loadFile(resFile)}; diff --git a/es-core/src/resources/ResourceManager.h b/es-core/src/resources/ResourceManager.h index 526926de6..835f7af21 100644 --- a/es-core/src/resources/ResourceManager.h +++ b/es-core/src/resources/ResourceManager.h @@ -44,9 +44,6 @@ public: void unloadAll(); void reloadAll(); - void setDataDirectory(const std::string& dataDirectory) { mDataDirectory = dataDirectory; } - const std::string& getDataDirectory() const { return mDataDirectory; } - std::string getResourcePath(const std::string& path, bool terminateOnFailure = true) const; const ResourceData getFileData(const std::string& path) const; bool fileExists(const std::string& path) const; @@ -58,7 +55,6 @@ private: ResourceData loadFile(SDL_RWops* resFile) const; std::list> mReloadables; - std::string mDataDirectory; }; #endif // ES_CORE_RESOURCES_RESOURCE_MANAGER_H diff --git a/es-core/src/utils/FileSystemUtil.cpp b/es-core/src/utils/FileSystemUtil.cpp index accc3df82..ba9951ce7 100644 --- a/es-core/src/utils/FileSystemUtil.cpp +++ b/es-core/src/utils/FileSystemUtil.cpp @@ -8,9 +8,9 @@ // remove files etc. // -#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && \ - !defined(__NetBSD__) && !defined(__EMSCRIPTEN__) -#define _FILE_OFFSET_BITS 64 +#if defined(__ANDROID__) +#include "utils/PlatformUtilAndroid.h" +#include #endif #if defined(__APPLE__) @@ -365,10 +365,12 @@ namespace Utils std::string getProgramDataPath() { -#if defined(__unix__) +#if defined(__ANDROID__) + return AndroidVariables::sAndroidDataDirectory; +#elif defined(__unix__) return installPrefix + "/share/emulationstation"; #else - return ""; + return ""; #endif } diff --git a/es-core/src/utils/PlatformUtil.h b/es-core/src/utils/PlatformUtil.h index a5faac134..c1e839571 100644 --- a/es-core/src/utils/PlatformUtil.h +++ b/es-core/src/utils/PlatformUtil.h @@ -15,11 +15,6 @@ #include #endif -#if defined(__ANDROID__) -#include "utils/PlatformUtilAndroid.h" -#endif - - namespace Utils { namespace Platform