save also metadata that have default values. they might not had default values on start so they have to be saved

This commit is contained in:
verybadsoldier 2019-08-24 16:22:02 +02:00
parent 723b17ae4c
commit a136179fc9
11 changed files with 62 additions and 28 deletions

View file

@ -448,6 +448,8 @@ void CollectionSystemManager::exitEditMode()
mWindow->setInfoPopup(s); mWindow->setInfoPopup(s);
mIsEditingCustom = false; mIsEditingCustom = false;
mEditingCollection = "Favorites"; mEditingCollection = "Favorites";
mEditingCollectionSystemData->system->onMetaDataSavePoint();
} }
// adds or removes a game from a specific collection // adds or removes a game from a specific collection
@ -521,6 +523,9 @@ bool CollectionSystemManager::toggleGameInCollection(FileData* file)
md->set("favorite", "false"); md->set("favorite", "false");
} }
file->getSourceFileData()->getSystem()->getIndex()->addToIndex(file); file->getSourceFileData()->getSystem()->getIndex()->addToIndex(file);
file->getSourceFileData()->getSystem()->onMetaDataSavePoint();
refreshCollectionSystems(file->getSourceFileData()); refreshCollectionSystems(file->getSourceFileData());
} }
if (adding) if (adding)

View file

@ -309,6 +309,8 @@ void FileData::launchGame(Window* window)
//update last played time //update last played time
gameToUpdate->metadata.set("lastplayed", Utils::Time::DateTime(Utils::Time::now())); gameToUpdate->metadata.set("lastplayed", Utils::Time::DateTime(Utils::Time::now()));
CollectionSystemManager::get()->refreshCollectionSystems(gameToUpdate); CollectionSystemManager::get()->refreshCollectionSystems(gameToUpdate);
gameToUpdate->mSystem->onMetaDataSavePoint();
} }
CollectionFileData::CollectionFileData(FileData* file, SystemData* system) CollectionFileData::CollectionFileData(FileData* file, SystemData* system)

View file

@ -1,5 +1,7 @@
#include "Gamelist.h" #include "Gamelist.h"
#include <chrono>
#include "utils/FileSystemUtil.h" #include "utils/FileSystemUtil.h"
#include "FileData.h" #include "FileData.h"
#include "FileFilterIndex.h" #include "FileFilterIndex.h"
@ -217,11 +219,6 @@ void updateGamelist(SystemData* system)
{ {
const char* tag = ((*fit)->getType() == GAME) ? "game" : "folder"; 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 // do not touch if it wasn't changed anyway
if (!(*fit)->metadata.wasChanged()) if (!(*fit)->metadata.wasChanged())
continue; continue;
@ -255,6 +252,8 @@ void updateGamelist(SystemData* system)
//now write the file //now write the file
if (numUpdated > 0) { 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) //make sure the folders leading up to this path exist (or the write will fail)
std::string xmlWritePath(system->getGamelistPath(true)); std::string xmlWritePath(system->getGamelistPath(true));
Utils::FileSystem::createDirectory(Utils::FileSystem::getParent(xmlWritePath)); Utils::FileSystem::createDirectory(Utils::FileSystem::getParent(xmlWritePath));
@ -264,6 +263,9 @@ void updateGamelist(SystemData* system)
if (!doc.save_file(xmlWritePath.c_str())) { if (!doc.save_file(xmlWritePath.c_str())) {
LOG(LogError) << "Error saving gamelist.xml to \"" << xmlWritePath << "\" (for system " << system->getName() << ")!"; 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<std::chrono::milliseconds>(endTs - startTs).count() << " ms";
} }
}else{ }else{
LOG(LogError) << "Found no root folder for system \"" << system->getName() << "\"!"; LOG(LogError) << "Found no root folder for system \"" << system->getName() << "\"!";

View file

@ -140,17 +140,6 @@ float MetaDataList::getFloat(const std::string& key) const
return (float)atof(get(key).c_str()); return (float)atof(get(key).c_str());
} }
bool MetaDataList::isDefault()
{
const std::vector<MetaDataDecl>& 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 bool MetaDataList::wasChanged() const
{ {
return mWasChanged; return mWasChanged;

View file

@ -55,8 +55,6 @@ public:
int getInt(const std::string& key) const; int getInt(const std::string& key) const;
float getFloat(const std::string& key) const; float getFloat(const std::string& key) const;
bool isDefault();
bool wasChanged() const; bool wasChanged() const;
void resetChangedFlag(); void resetChangedFlag();

View file

@ -50,11 +50,8 @@ SystemData::SystemData(const std::string& name, const std::string& fullName, Sys
SystemData::~SystemData() SystemData::~SystemData()
{ {
//save changed game data back to xml if(Settings::getInstance()->getString("SaveGamelistsMode") == "on exit")
if(!Settings::getInstance()->getBool("IgnoreGamelist") && Settings::getInstance()->getBool("SaveGamelistsOnExit") && !mIsCollectionSystem) writeMetaData();
{
updateGamelist(this);
}
delete mRootFolder; delete mRootFolder;
delete mFilterIndex; delete mFilterIndex;
@ -502,3 +499,18 @@ void SystemData::loadTheme()
mTheme = std::make_shared<ThemeData>(); // reset to empty mTheme = std::make_shared<ThemeData>(); // 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();
}

View file

@ -68,6 +68,7 @@ public:
void loadTheme(); void loadTheme();
FileFilterIndex* getIndex() { return mFilterIndex; }; FileFilterIndex* getIndex() { return mFilterIndex; };
void onMetaDataSavePoint();
private: private:
bool mIsCollectionSystem; bool mIsCollectionSystem;
@ -81,6 +82,7 @@ private:
void populateFolder(FileData* folder); void populateFolder(FileData* folder);
void indexAllGameFilters(const FileData* folder); void indexAllGameFilters(const FileData* folder);
void setIsGameSystemStatus(); void setIsGameSystemStatus();
void writeMetaData();
FileFilterIndex* mFilterIndex; FileFilterIndex* mFilterIndex;

View file

@ -400,10 +400,18 @@ void GuiMenu::openOtherSettings()
}); });
// gamelists // gamelists
auto save_gamelists = std::make_shared<SwitchComponent>(mWindow); auto gamelistsSaveMode = std::make_shared< OptionListComponent<std::string> >(mWindow, "SAVE METADATA", false);
save_gamelists->setState(Settings::getInstance()->getBool("SaveGamelistsOnExit")); std::vector<std::string> saveModes;
s->addWithLabel("SAVE METADATA ON EXIT", save_gamelists); saveModes.push_back("on exit");
s->addSaveFunc([save_gamelists] { Settings::getInstance()->setBool("SaveGamelistsOnExit", save_gamelists->getState()); }); 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<SwitchComponent>(mWindow); auto parse_gamelists = std::make_shared<SwitchComponent>(mWindow);
parse_gamelists->setState(Settings::getInstance()->getBool("ParseGamelistOnly")); parse_gamelists->setState(Settings::getInstance()->getBool("ParseGamelistOnly"));

View file

@ -199,6 +199,8 @@ void GuiMetaDataEd::save()
// update respective Collection Entries // update respective Collection Entries
CollectionSystemManager::get()->refreshCollectionSystems(mScraperParams.game); CollectionSystemManager::get()->refreshCollectionSystems(mScraperParams.game);
mScraperParams.system->onMetaDataSavePoint();
} }
void GuiMetaDataEd::fetch() void GuiMetaDataEd::fetch()

View file

@ -76,7 +76,6 @@ void Settings::setDefaults()
mBoolMap["HideConsole"] = true; mBoolMap["HideConsole"] = true;
mBoolMap["QuickSystemSelect"] = true; mBoolMap["QuickSystemSelect"] = true;
mBoolMap["MoveCarousel"] = true; mBoolMap["MoveCarousel"] = true;
mBoolMap["SaveGamelistsOnExit"] = true;
mBoolMap["Debug"] = false; mBoolMap["Debug"] = false;
mBoolMap["DebugGrid"] = false; mBoolMap["DebugGrid"] = false;
@ -97,6 +96,7 @@ void Settings::setDefaults()
mStringMap["ScreenSaverBehavior"] = "dim"; mStringMap["ScreenSaverBehavior"] = "dim";
mStringMap["Scraper"] = "TheGamesDB"; mStringMap["Scraper"] = "TheGamesDB";
mStringMap["GamelistViewStyle"] = "automatic"; mStringMap["GamelistViewStyle"] = "automatic";
mStringMap["SaveGamelistsMode"] = "on exit";
mBoolMap["ScreenSaverControls"] = true; mBoolMap["ScreenSaverControls"] = true;
mStringMap["ScreenSaverGameInfo"] = "never"; mStringMap["ScreenSaverGameInfo"] = "never";
@ -228,6 +228,19 @@ void Settings::loadFile()
setFloat(node.attribute("name").as_string(), node.attribute("value").as_float()); 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")) 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()); setString(node.attribute("name").as_string(), node.attribute("value").as_string());
processBackwardCompatibility();
}
void Settings::processBackwardCompatibility()
{
{ // SaveGamelistsOnExit -> SaveGamelistsMode
std::map<std::string, bool>::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. //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.

View file

@ -31,6 +31,7 @@ private:
//Clear everything and load default values. //Clear everything and load default values.
void setDefaults(); void setDefaults();
void processBackwardCompatibility();
std::map<std::string, bool> mBoolMap; std::map<std::string, bool> mBoolMap;
std::map<std::string, int> mIntMap; std::map<std::string, int> mIntMap;