From a136179fc976e8a19e9fee365e9b61388e934ea1 Mon Sep 17 00:00:00 2001 From: verybadsoldier Date: Sat, 24 Aug 2019 16:22:02 +0200 Subject: [PATCH] save also metadata that have default values. they might not had default values on start so they have to be saved --- es-app/src/CollectionSystemManager.cpp | 5 +++++ es-app/src/FileData.cpp | 2 ++ es-app/src/Gamelist.cpp | 12 +++++++----- es-app/src/MetaData.cpp | 11 ----------- es-app/src/MetaData.h | 2 -- es-app/src/SystemData.cpp | 22 +++++++++++++++++----- es-app/src/SystemData.h | 2 ++ es-app/src/guis/GuiMenu.cpp | 16 ++++++++++++---- es-app/src/guis/GuiMetaDataEd.cpp | 2 ++ es-core/src/Settings.cpp | 15 ++++++++++++++- es-core/src/Settings.h | 1 + 11 files changed, 62 insertions(+), 28 deletions(-) diff --git a/es-app/src/CollectionSystemManager.cpp b/es-app/src/CollectionSystemManager.cpp index ea3f629da..0315b5cc2 100644 --- a/es-app/src/CollectionSystemManager.cpp +++ b/es-app/src/CollectionSystemManager.cpp @@ -448,6 +448,8 @@ void CollectionSystemManager::exitEditMode() mWindow->setInfoPopup(s); mIsEditingCustom = false; mEditingCollection = "Favorites"; + + mEditingCollectionSystemData->system->onMetaDataSavePoint(); } // adds or removes a game from a specific collection @@ -521,6 +523,9 @@ bool CollectionSystemManager::toggleGameInCollection(FileData* file) md->set("favorite", "false"); } file->getSourceFileData()->getSystem()->getIndex()->addToIndex(file); + + file->getSourceFileData()->getSystem()->onMetaDataSavePoint(); + refreshCollectionSystems(file->getSourceFileData()); } if (adding) diff --git a/es-app/src/FileData.cpp b/es-app/src/FileData.cpp index 2238c60f9..1b98a35cb 100644 --- a/es-app/src/FileData.cpp +++ b/es-app/src/FileData.cpp @@ -309,6 +309,8 @@ void FileData::launchGame(Window* window) //update last played time gameToUpdate->metadata.set("lastplayed", Utils::Time::DateTime(Utils::Time::now())); CollectionSystemManager::get()->refreshCollectionSystems(gameToUpdate); + + gameToUpdate->mSystem->onMetaDataSavePoint(); } CollectionFileData::CollectionFileData(FileData* file, SystemData* system) diff --git a/es-app/src/Gamelist.cpp b/es-app/src/Gamelist.cpp index 14af11757..313670420 100644 --- a/es-app/src/Gamelist.cpp +++ b/es-app/src/Gamelist.cpp @@ -1,5 +1,7 @@ #include "Gamelist.h" +#include + #include "utils/FileSystemUtil.h" #include "FileData.h" #include "FileFilterIndex.h" @@ -217,11 +219,6 @@ void updateGamelist(SystemData* system) { const char* tag = ((*fit)->getType() == GAME) ? "game" : "folder"; - // check if current file has metadata, if no, skip it as it wont be in the gamelist anyway. - if ((*fit)->metadata.isDefault()) { - continue; - } - // do not touch if it wasn't changed anyway if (!(*fit)->metadata.wasChanged()) continue; @@ -255,6 +252,8 @@ void updateGamelist(SystemData* system) //now write the file if (numUpdated > 0) { + const auto startTs = std::chrono::system_clock::now(); + //make sure the folders leading up to this path exist (or the write will fail) std::string xmlWritePath(system->getGamelistPath(true)); Utils::FileSystem::createDirectory(Utils::FileSystem::getParent(xmlWritePath)); @@ -264,6 +263,9 @@ void updateGamelist(SystemData* system) if (!doc.save_file(xmlWritePath.c_str())) { LOG(LogError) << "Error saving gamelist.xml to \"" << xmlWritePath << "\" (for system " << system->getName() << ")!"; } + + const auto endTs = std::chrono::system_clock::now(); + LOG(LogInfo) << "Saved gamelist.xml for system \"" << system->getName() << "\" in " << std::chrono::duration_cast(endTs - startTs).count() << " ms"; } }else{ LOG(LogError) << "Found no root folder for system \"" << system->getName() << "\"!"; diff --git a/es-app/src/MetaData.cpp b/es-app/src/MetaData.cpp index 8a977e950..0851fbdf4 100644 --- a/es-app/src/MetaData.cpp +++ b/es-app/src/MetaData.cpp @@ -140,17 +140,6 @@ float MetaDataList::getFloat(const std::string& key) const return (float)atof(get(key).c_str()); } -bool MetaDataList::isDefault() -{ - const std::vector& mdd = getMDD(); - - for (unsigned int i = 1; i < mMap.size(); i++) { - if (mMap.at(mdd[i].key) != mdd[i].defaultValue) return false; - } - - return true; -} - bool MetaDataList::wasChanged() const { return mWasChanged; diff --git a/es-app/src/MetaData.h b/es-app/src/MetaData.h index 6ed8f6d44..52e1eddc1 100644 --- a/es-app/src/MetaData.h +++ b/es-app/src/MetaData.h @@ -55,8 +55,6 @@ public: int getInt(const std::string& key) const; float getFloat(const std::string& key) const; - bool isDefault(); - bool wasChanged() const; void resetChangedFlag(); diff --git a/es-app/src/SystemData.cpp b/es-app/src/SystemData.cpp index d6e72cfd3..3769d2a41 100644 --- a/es-app/src/SystemData.cpp +++ b/es-app/src/SystemData.cpp @@ -50,11 +50,8 @@ SystemData::SystemData(const std::string& name, const std::string& fullName, Sys SystemData::~SystemData() { - //save changed game data back to xml - if(!Settings::getInstance()->getBool("IgnoreGamelist") && Settings::getInstance()->getBool("SaveGamelistsOnExit") && !mIsCollectionSystem) - { - updateGamelist(this); - } + if(Settings::getInstance()->getString("SaveGamelistsMode") == "on exit") + writeMetaData(); delete mRootFolder; delete mFilterIndex; @@ -502,3 +499,18 @@ void SystemData::loadTheme() mTheme = std::make_shared(); // reset to empty } } + +void SystemData::writeMetaData() { + if(Settings::getInstance()->getBool("IgnoreGamelist") || mIsCollectionSystem) + return; + + //save changed game data back to xml + updateGamelist(this); +} + +void SystemData::onMetaDataSavePoint() { + if(Settings::getInstance()->getString("SaveGamelistsMode") != "always") + return; + + writeMetaData(); +} diff --git a/es-app/src/SystemData.h b/es-app/src/SystemData.h index ad0456b3c..bc8005c38 100644 --- a/es-app/src/SystemData.h +++ b/es-app/src/SystemData.h @@ -68,6 +68,7 @@ public: void loadTheme(); FileFilterIndex* getIndex() { return mFilterIndex; }; + void onMetaDataSavePoint(); private: bool mIsCollectionSystem; @@ -81,6 +82,7 @@ private: void populateFolder(FileData* folder); void indexAllGameFilters(const FileData* folder); void setIsGameSystemStatus(); + void writeMetaData(); FileFilterIndex* mFilterIndex; diff --git a/es-app/src/guis/GuiMenu.cpp b/es-app/src/guis/GuiMenu.cpp index 59d8ae415..c8b4e73d8 100644 --- a/es-app/src/guis/GuiMenu.cpp +++ b/es-app/src/guis/GuiMenu.cpp @@ -400,10 +400,18 @@ void GuiMenu::openOtherSettings() }); // gamelists - auto save_gamelists = std::make_shared(mWindow); - save_gamelists->setState(Settings::getInstance()->getBool("SaveGamelistsOnExit")); - s->addWithLabel("SAVE METADATA ON EXIT", save_gamelists); - s->addSaveFunc([save_gamelists] { Settings::getInstance()->setBool("SaveGamelistsOnExit", save_gamelists->getState()); }); + auto gamelistsSaveMode = std::make_shared< OptionListComponent >(mWindow, "SAVE METADATA", false); + std::vector saveModes; + saveModes.push_back("on exit"); + saveModes.push_back("always"); + saveModes.push_back("never"); + + for(auto it = saveModes.cbegin(); it != saveModes.cend(); it++) + gamelistsSaveMode->add(*it, *it, Settings::getInstance()->getString("SaveGamelistsMode") == *it); + s->addWithLabel("SAVE METADATA", gamelistsSaveMode); + s->addSaveFunc([gamelistsSaveMode] { + Settings::getInstance()->setString("SaveGamelistsMode", gamelistsSaveMode->getSelected()); + }); auto parse_gamelists = std::make_shared(mWindow); parse_gamelists->setState(Settings::getInstance()->getBool("ParseGamelistOnly")); diff --git a/es-app/src/guis/GuiMetaDataEd.cpp b/es-app/src/guis/GuiMetaDataEd.cpp index f07a5061e..606dd203a 100644 --- a/es-app/src/guis/GuiMetaDataEd.cpp +++ b/es-app/src/guis/GuiMetaDataEd.cpp @@ -199,6 +199,8 @@ void GuiMetaDataEd::save() // update respective Collection Entries CollectionSystemManager::get()->refreshCollectionSystems(mScraperParams.game); + + mScraperParams.system->onMetaDataSavePoint(); } void GuiMetaDataEd::fetch() diff --git a/es-core/src/Settings.cpp b/es-core/src/Settings.cpp index 455bea453..4b0f63c08 100644 --- a/es-core/src/Settings.cpp +++ b/es-core/src/Settings.cpp @@ -76,7 +76,6 @@ void Settings::setDefaults() mBoolMap["HideConsole"] = true; mBoolMap["QuickSystemSelect"] = true; mBoolMap["MoveCarousel"] = true; - mBoolMap["SaveGamelistsOnExit"] = true; mBoolMap["Debug"] = false; mBoolMap["DebugGrid"] = false; @@ -97,6 +96,7 @@ void Settings::setDefaults() mStringMap["ScreenSaverBehavior"] = "dim"; mStringMap["Scraper"] = "TheGamesDB"; mStringMap["GamelistViewStyle"] = "automatic"; + mStringMap["SaveGamelistsMode"] = "on exit"; mBoolMap["ScreenSaverControls"] = true; mStringMap["ScreenSaverGameInfo"] = "never"; @@ -228,6 +228,19 @@ void Settings::loadFile() setFloat(node.attribute("name").as_string(), node.attribute("value").as_float()); for(pugi::xml_node node = doc.child("string"); node; node = node.next_sibling("string")) setString(node.attribute("name").as_string(), node.attribute("value").as_string()); + + processBackwardCompatibility(); +} + +void Settings::processBackwardCompatibility() +{ + { // SaveGamelistsOnExit -> SaveGamelistsMode + std::map::const_iterator it = mBoolMap.find("SaveGamelistsOnExit"); + if (it != mBoolMap.end()) { + mStringMap["SaveGamelistsMode"] = it->second ? "on exit" : "never"; + mBoolMap.erase(it); + } + } } //Print a warning message if the setting we're trying to get doesn't already exist in the map, then return the value in the map. diff --git a/es-core/src/Settings.h b/es-core/src/Settings.h index 413694b71..e9b9f7f03 100644 --- a/es-core/src/Settings.h +++ b/es-core/src/Settings.h @@ -31,6 +31,7 @@ private: //Clear everything and load default values. void setDefaults(); + void processBackwardCompatibility(); std::map mBoolMap; std::map mIntMap;