Skip to content

Commit

Permalink
Merge pull request Aloshi#78 from verybadsoldier/only_save_gamelist_d…
Browse files Browse the repository at this point in the history
…iffs

Instant Shutdown by only writing changes to gamelist.xml
  • Loading branch information
verybadsoldier authored and Your Name committed Aug 9, 2018
1 parent 8591377 commit ffb800c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
33 changes: 24 additions & 9 deletions es-app/src/Gamelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ void parseGamelist(SystemData* system)
//make sure name gets set if one didn't exist
if(file->metadata.get("name").empty())
file->metadata.set("name", defaultName);

file->metadata.resetChangedFlag();
}
}
}
Expand Down Expand Up @@ -202,14 +204,24 @@ void updateGamelist(SystemData* system)
FileData* rootFolder = system->getRootFolder();
if (rootFolder != nullptr)
{
int numUpdated = 0;

//get only files, no folders
std::vector<FileData*> files = rootFolder->getFilesRecursive(GAME | FOLDER);
//iterate through all files, checking if they're already in the XML
std::vector<FileData*>::const_iterator fit = files.cbegin();
while(fit != files.cend())
for(std::vector<FileData*>::const_iterator fit = files.cbegin(); fit != files.cend(); ++fit)
{
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;

// check if the file already exists in the XML
// if it does, remove it before adding
for(pugi::xml_node fileNode = root.child(tag); fileNode; fileNode = fileNode.next_sibling(tag))
Expand All @@ -233,18 +245,21 @@ void updateGamelist(SystemData* system)

// it was either removed or never existed to begin with; either way, we can add it now
addFileDataNode(root, *fit, tag, system);

++fit;
++numUpdated;
}

//now write the file

//make sure the folders leading up to this path exist (or the write will fail)
boost::filesystem::path xmlWritePath(system->getGamelistPath(true));
boost::filesystem::create_directories(xmlWritePath.parent_path());
if (numUpdated > 0) {
//make sure the folders leading up to this path exist (or the write will fail)
boost::filesystem::path xmlWritePath(system->getGamelistPath(true));
boost::filesystem::create_directories(xmlWritePath.parent_path());

if (!doc.save_file(xmlWritePath.c_str())) {
LOG(LogError) << "Error saving gamelist.xml to \"" << xmlWritePath << "\" (for system " << system->getName() << ")!";
LOG(LogInfo) << "Added/Updated " << numUpdated << " entities in '" << xmlReadPath << "'";

if (!doc.save_file(xmlWritePath.c_str())) {
LOG(LogError) << "Error saving gamelist.xml to \"" << xmlWritePath << "\" (for system " << system->getName() << ")!";
}
}
}else{
LOG(LogError) << "Found no root folder for system \"" << system->getName() << "\"!";
Expand Down
24 changes: 22 additions & 2 deletions es-app/src/MetaData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const std::vector<MetaDataDecl>& getMDDByType(MetaDataListType type)


MetaDataList::MetaDataList(MetaDataListType type)
: mType(type)
: mType(type), mWasChanged(false)
{
const std::vector<MetaDataDecl>& mdd = getMDD();
for(auto iter = mdd.begin(); iter != mdd.end(); iter++)
Expand Down Expand Up @@ -107,11 +107,12 @@ void MetaDataList::appendToXML(pugi::xml_node parent, bool ignoreDefaults, const
void MetaDataList::set(const std::string& key, const std::string& value)
{
mMap[key] = value;
mWasChanged = true;
}

void MetaDataList::setTime(const std::string& key, const boost::posix_time::ptime& time)
{
mMap[key] = boost::posix_time::to_iso_string(time);
set(key, boost::posix_time::to_iso_string(time));
}

const std::string& MetaDataList::get(const std::string& key) const
Expand All @@ -133,3 +134,22 @@ boost::posix_time::ptime MetaDataList::getTime(const std::string& key) const
{
return string_to_ptime(get(key), "%Y%m%dT%H%M%S%F%q");
}

bool MetaDataList::isDefault()
{
for (int i = 1; i < mMap.size(); i++) {
if (mMap.at(gameDecls[i].key) != gameDecls[i].defaultValue) return false;
}

return true;
}

bool MetaDataList::wasChanged() const
{
return mWasChanged;
}

void MetaDataList::resetChangedFlag()
{
mWasChanged = false;
}
6 changes: 6 additions & 0 deletions es-app/src/MetaData.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ class MetaDataList
float getFloat(const std::string& key) const;
boost::posix_time::ptime getTime(const std::string& key) const;

bool isDefault();

bool wasChanged() const;
void resetChangedFlag();

inline MetaDataListType getType() const { return mType; }
inline const std::vector<MetaDataDecl>& getMDD() const { return getMDDByType(getType()); }

private:
MetaDataListType mType;
std::map<std::string, std::string> mMap;
bool mWasChanged;
};

0 comments on commit ffb800c

Please sign in to comment.