Added a ROM path variable for use with es_systems.cfg and custom collections.

The variable %ROMPATH% now expands to the ROMDirectory setting in es_settings.cfg which removes the need for absolute ROM paths (although absolute paths are still supported). Custom collections with absolute paths can still be loaded for backward compatibility with old custom collections.
This commit is contained in:
Leon Styhre 2020-06-18 17:09:32 +02:00
parent 3c7d86a578
commit fb0ab2f06c
6 changed files with 53 additions and 2 deletions

View file

@ -115,10 +115,12 @@ void CollectionSystemManager::deinit()
void CollectionSystemManager::saveCustomCollection(SystemData* sys)
{
const std::string rompath = FileData::getROMDirectory();
std::string name = sys->getName();
std::unordered_map<std::string, FileData*>
games = sys->getRootFolder()->getChildrenByFilename();
bool found = mCustomCollectionSystemsData.find(name) != mCustomCollectionSystemsData.cend();
if (found) {
CollectionSystemData sysData = mCustomCollectionSystemsData.at(name);
if (sysData.needsSave) {
@ -127,6 +129,11 @@ void CollectionSystemManager::saveCustomCollection(SystemData* sys)
for (std::unordered_map<std::string, FileData*>::const_iterator
iter = games.cbegin(); iter != games.cend(); ++iter) {
std::string path = iter->first;
// If the ROM path of the game begins with the path from the setting
// ROMDirectory (or the default ROM directory), then replace it with %ROMPATH%.
if (path.find(rompath) == 0)
path.replace(0, rompath.size(), "%ROMPATH%/");
configFile << path << std::endl;
}
configFile.close();
@ -850,8 +857,20 @@ void CollectionSystemManager::populateCustomCollection(CollectionSystemData* sys
std::unordered_map<std::string,FileData*>
allFilesMap = getAllGamesCollection()->getRootFolder()->getChildrenByFilename();
// Get the ROM directory, either as configured in es_settings.cfg, or if no value
// is set there, then use the default hardcoded path.
const std::string rompath = FileData::getROMDirectory();
// Iterate list of files in the config file.
for (std::string gameKey; getline(input, gameKey); ) {
// If there is a %ROMPATH% variable set for the game, expand it. By doing this
// it's possible to use either absolute ROM paths in the collection files or using
// the path variable. The absolute ROM paths are only used for backward compatibility
// with old custom collections. All custom collections saved by EmulationStation-DE
// will use the %ROMPATH% variable instead.
gameKey = Utils::String::replace(gameKey, "%ROMPATH%", rompath);
gameKey = Utils::String::replace(gameKey, "//", "/");
std::unordered_map<std::string,FileData*>::const_iterator it = allFilesMap.find(gameKey);
if (it != allFilesMap.cend()) {
CollectionFileData* newGame = new CollectionFileData(it->second, newSys);

View file

@ -98,6 +98,29 @@ const bool FileData::getFavorite()
return false;
}
const std::string FileData::getROMDirectory()
{
std::string romDirSetting = Settings::getInstance()->getString("ROMDirectory");
std::string romDirPath = "";
if (romDirSetting == "") {
romDirPath = Utils::FileSystem::getHomePath() + "/ROMs/";
}
else {
romDirPath = romDirSetting;
// Expand home symbol if the path starts with ~
if (romDirPath[0] == '~') {
romDirPath.erase(0, 1);
romDirPath.insert(0, Utils::FileSystem::getHomePath());
}
if (romDirPath.back() != '/')
romDirPath = romDirPath + "/";
}
return romDirPath;
}
const std::string FileData::getMediaDirectory()
{
std::string mediaDirSetting = Settings::getInstance()->getString("MediaDirectory");

View file

@ -59,6 +59,7 @@ public:
inline SystemEnvironmentData* getSystemEnvData() const { return mEnvData; }
const std::vector<std::string>& getFirstLetterIndex() const
{ return mFirstLetterIndex; };
static const std::string getROMDirectory();
static const std::string getMediaDirectory();
virtual const std::string getMediafilePath(
std::string subdirectory, std::string mediatype) const;

View file

@ -10,6 +10,7 @@
#include "SystemData.h"
#include "utils/FileSystemUtil.h"
#include "utils/StringUtil.h"
#include "CollectionSystemManager.h"
#include "FileFilterIndex.h"
#include "FileSorts.h"
@ -195,6 +196,7 @@ bool SystemData::loadConfig()
deleteSystems();
std::string path = getConfigPath(false);
const std::string rompath = FileData::getROMDirectory();
LOG(LogInfo) << "Loading system config file " << path << "...";
@ -229,6 +231,13 @@ bool SystemData::loadConfig()
fullname = system.child("fullname").text().get();
path = system.child("path").text().get();
// If there is a %ROMPATH% variable set for the system, expand it. By doing this
// it's possible to use either absolute ROM paths in es_systems.cfg or to utilize
// the ROM path configured as ROMDirectory in es_settings.cfg. If it's set to ""
// in this configuration file, the default hardcoded path $HOME/ROMs/ will be used.
path = Utils::String::replace(path, "%ROMPATH%", rompath);
path = Utils::String::replace(path, "//", "/");
// Convert extensions list from a string into a vector of strings.
std::vector<std::string> extensions = readList(system.child("extension").text().get());

View file

@ -233,8 +233,6 @@ void SystemView::onCursorChanged(const CursorState& /*state*/)
// Update help style.
updateHelpPrompts();
// NavigationSounds::getInstance()->playThemeNavigationSound(SYSTEMBROWSESOUND);
float startPos = mCamOffset;
float posMax = (float)mEntries.size();

View file

@ -209,6 +209,7 @@ void Settings::setDefaults()
mStringMap["DefaultSortOrder"] = "filename, ascending";
mStringMap["MediaDirectory"] = "";
mStringMap["ROMDirectory"] = "";
mIntMap["ScraperResizeWidth"] = 600;
mIntMap["ScraperResizeHeight"] = 0;