Merge pull request #578 from verybadsoldier/feature/save_metadata_on_change

Added an option to save meta data instantly when changed.
This commit is contained in:
Jools Wills 2019-12-18 04:06:23 +00:00 committed by GitHub
commit 46047894f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 10 deletions

View file

@ -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)

View file

@ -310,6 +310,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)

View file

@ -1,5 +1,7 @@
#include "Gamelist.h"
#include <chrono>
#include "utils/FileSystemUtil.h"
#include "FileData.h"
#include "FileFilterIndex.h"
@ -250,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));
@ -259,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<std::chrono::milliseconds>(endTs - startTs).count() << " ms";
}
}else{
LOG(LogError) << "Found no root folder for system \"" << system->getName() << "\"!";

View file

@ -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<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();
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;

View file

@ -401,10 +401,18 @@ void GuiMenu::openOtherSettings()
});
// gamelists
auto save_gamelists = std::make_shared<SwitchComponent>(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<std::string> >(mWindow, "SAVE METADATA", false);
std::vector<std::string> 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<SwitchComponent>(mWindow);
parse_gamelists->setState(Settings::getInstance()->getBool("ParseGamelistOnly"));

View file

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

View file

@ -75,7 +75,6 @@ void Settings::setDefaults()
mBoolMap["HideConsole"] = true;
mBoolMap["QuickSystemSelect"] = true;
mBoolMap["MoveCarousel"] = true;
mBoolMap["SaveGamelistsOnExit"] = true;
mBoolMap["Debug"] = false;
mBoolMap["DebugGrid"] = false;
@ -96,6 +95,7 @@ void Settings::setDefaults()
mStringMap["ScreenSaverBehavior"] = "dim";
mStringMap["Scraper"] = "TheGamesDB";
mStringMap["GamelistViewStyle"] = "automatic";
mStringMap["SaveGamelistsMode"] = "on exit";
mBoolMap["ScreenSaverControls"] = true;
mStringMap["ScreenSaverGameInfo"] = "never";
@ -225,6 +225,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<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.

View file

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