mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-04-10 19:15:13 +00:00
Added folder metadata loading/saving.
Rewrote a lot of the gamelist reading/saving code, let me know if I broke it.
This commit is contained in:
parent
ccea2a7e04
commit
0423dc03dd
|
@ -5,82 +5,110 @@
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
|
||||||
//this is obviously an incredibly inefficient way to go about searching
|
namespace fs = boost::filesystem;
|
||||||
//but I don't think it'll matter too much with the size of most collections
|
|
||||||
FileData* searchFolderByPath(FileData* folder, std::string const& path)
|
// example: removeCommonPath("/home/pi/roms/nes/foo/bar.nes", "/home/pi/roms/nes/") returns "foo/bar.nes"
|
||||||
|
fs::path removeCommonPath(const fs::path& path, const fs::path& relativeTo, bool& contains)
|
||||||
{
|
{
|
||||||
for(auto it = folder->getChildren().begin(); it != folder->getChildren().end(); it++)
|
fs::path p = fs::canonical(path);
|
||||||
|
fs::path r = fs::canonical(relativeTo);
|
||||||
|
|
||||||
|
if(p.root_path() != r.root_path())
|
||||||
{
|
{
|
||||||
if((*it)->getType() == FOLDER)
|
contains = false;
|
||||||
{
|
return p;
|
||||||
FileData* result = searchFolderByPath(*it, path);
|
|
||||||
if(result)
|
|
||||||
return result;
|
|
||||||
}else{
|
|
||||||
if((*it)->getPath().generic_string() == path)
|
|
||||||
return *it;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
fs::path result;
|
||||||
|
|
||||||
|
// find point of divergence
|
||||||
|
auto itr_path = p.begin();
|
||||||
|
auto itr_relative_to = r.begin();
|
||||||
|
while(*itr_path == *itr_relative_to && itr_path != p.end() && itr_relative_to != r.end())
|
||||||
|
{
|
||||||
|
++itr_path;
|
||||||
|
++itr_relative_to;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(itr_relative_to != r.end())
|
||||||
|
{
|
||||||
|
contains = false;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(itr_path != p.end())
|
||||||
|
{
|
||||||
|
if(*itr_path != fs::path("."))
|
||||||
|
result = result / *itr_path;
|
||||||
|
|
||||||
|
++itr_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
contains = true;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileData* createGameFromPath(std::string gameAbsPath, SystemData* system)
|
FileData* findOrCreateFile(SystemData* system, const boost::filesystem::path& path, FileType type)
|
||||||
{
|
{
|
||||||
std::string gamePath = gameAbsPath;
|
// first, verify that path is within the system's root folder
|
||||||
std::string sysPath = system->getStartPath();
|
FileData* root = system->getRootFolder();
|
||||||
|
|
||||||
//strip out the system path stuff so it's relative to the system root folder
|
bool contains = false;
|
||||||
unsigned int i = 0;
|
fs::path relative = removeCommonPath(path, root->getPath(), contains);
|
||||||
while(i < gamePath.length() && i < sysPath.length() && gamePath[i] == sysPath[i])
|
if(!contains)
|
||||||
i++;
|
|
||||||
|
|
||||||
gamePath = gamePath.substr(i, gamePath.length() - i);
|
|
||||||
|
|
||||||
|
|
||||||
if(gamePath[0] != '/')
|
|
||||||
gamePath.insert(0, "/");
|
|
||||||
|
|
||||||
|
|
||||||
//make our way through the directory tree finding each folder in our path or creating it if it doesn't exist
|
|
||||||
FileData* folder = system->getRootFolder();
|
|
||||||
|
|
||||||
size_t separator = 0;
|
|
||||||
size_t nextSeparator = 0;
|
|
||||||
while(nextSeparator != std::string::npos)
|
|
||||||
{
|
{
|
||||||
//determine which chunk of the path we're testing right now
|
LOG(LogError) << "File path \"" << path << "\" is outside system path \"" << system->getStartPath() << "\"";
|
||||||
nextSeparator = gamePath.find('/', separator + 1);
|
return NULL;
|
||||||
if(nextSeparator == std::string::npos)
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
std::string checkName = gamePath.substr(separator + 1, nextSeparator - separator - 1);
|
auto path_it = relative.begin();
|
||||||
separator = nextSeparator;
|
FileData* treeNode = root;
|
||||||
|
bool found = false;
|
||||||
//see if the folder already exists
|
while(path_it != relative.end())
|
||||||
bool foundFolder = false;
|
{
|
||||||
for(auto it = folder->getChildren().begin(); it != folder->getChildren().end(); it++)
|
const std::vector<FileData*>& children = treeNode->getChildren();
|
||||||
|
found = false;
|
||||||
|
for(auto child_it = children.begin(); child_it != children.end(); child_it++)
|
||||||
{
|
{
|
||||||
if((*it)->getType() == FOLDER && (*it)->getName() == checkName)
|
if((*child_it)->getPath().filename() == *path_it)
|
||||||
{
|
{
|
||||||
folder = *it;
|
treeNode = *child_it;
|
||||||
foundFolder = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//the folder didn't already exist, so create it
|
// this is the end
|
||||||
if(!foundFolder)
|
if(path_it == --relative.end())
|
||||||
{
|
{
|
||||||
FileData* newFolder = new FileData(FOLDER, folder->getPath() / checkName, system);
|
if(found)
|
||||||
folder->addChild(newFolder);
|
return treeNode;
|
||||||
folder = newFolder;
|
|
||||||
|
FileData* file = new FileData(type, path, system);
|
||||||
|
treeNode->addChild(file);
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!found)
|
||||||
|
{
|
||||||
|
// don't create folders unless it's leading up to a game
|
||||||
|
// if type is a folder it's gonna be empty, so don't bother
|
||||||
|
if(type == FOLDER)
|
||||||
|
{
|
||||||
|
LOG(LogWarning) << "folder doesn't already exist, won't create metadata for folder";
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create missing folder
|
||||||
|
FileData* folder = new FileData(FOLDER, treeNode->getPath().stem() / *path_it, system);
|
||||||
|
treeNode->addChild(folder);
|
||||||
|
treeNode = folder;
|
||||||
|
}
|
||||||
|
|
||||||
|
path_it++;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileData* game = new FileData(GAME, gameAbsPath, system);
|
return NULL;
|
||||||
folder->addChild(game);
|
|
||||||
return game;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void parseGamelist(SystemData* system)
|
void parseGamelist(SystemData* system)
|
||||||
|
@ -108,49 +136,36 @@ void parseGamelist(SystemData* system)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(pugi::xml_node gameNode = root.child("game"); gameNode; gameNode = gameNode.next_sibling("game"))
|
const char* tagList[2] = { "game", "folder" };
|
||||||
|
FileType typeList[2] = { GAME, FOLDER };
|
||||||
|
for(int i = 0; i < 2; i++)
|
||||||
{
|
{
|
||||||
pugi::xml_node pathNode = gameNode.child("path");
|
const char* tag = tagList[i];
|
||||||
if(!pathNode)
|
FileType type = typeList[i];
|
||||||
|
for(pugi::xml_node fileNode = root.child(tag); fileNode; fileNode = fileNode.next_sibling(tag))
|
||||||
{
|
{
|
||||||
LOG(LogError) << "<game> node contains no <path> child!";
|
boost::filesystem::path path = boost::filesystem::path(fileNode.child("path").text().get());
|
||||||
continue;
|
|
||||||
}
|
if(!boost::filesystem::exists(path))
|
||||||
|
{
|
||||||
|
LOG(LogWarning) << "File \"" << path << "\" does not exist! Ignoring.";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
//convert path to generic directory seperators
|
FileData* file = findOrCreateFile(system, path, type);
|
||||||
boost::filesystem::path gamePath(pathNode.text().get());
|
if(!file)
|
||||||
std::string path = gamePath.generic_string();
|
{
|
||||||
|
LOG(LogError) << "Error finding/creating FileData for \"" << path << "\", skipping.";
|
||||||
//expand '.'
|
continue;
|
||||||
if(path[0] == '.')
|
}
|
||||||
{
|
|
||||||
path.erase(0, 1);
|
|
||||||
path.insert(0, boost::filesystem::path(xmlpath).parent_path().generic_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
//expand '~'
|
|
||||||
if(path[0] == '~')
|
|
||||||
{
|
|
||||||
path.erase(0, 1);
|
|
||||||
path.insert(0, getHomePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
if(boost::filesystem::exists(path))
|
|
||||||
{
|
|
||||||
FileData* game = searchFolderByPath(system->getRootFolder(), path);
|
|
||||||
|
|
||||||
if(game == NULL)
|
|
||||||
game = createGameFromPath(path, system);
|
|
||||||
|
|
||||||
//load the metadata
|
//load the metadata
|
||||||
std::string defaultName = game->metadata.get("name");
|
std::string defaultName = file->metadata.get("name");
|
||||||
game->metadata = MetaDataList::createFromXML(GAME_METADATA, gameNode);
|
file->metadata = MetaDataList::createFromXML(GAME_METADATA, fileNode);
|
||||||
|
|
||||||
//make sure name gets set if one didn't exist
|
//make sure name gets set if one didn't exist
|
||||||
if(game->metadata.get("name").empty())
|
if(file->metadata.get("name").empty())
|
||||||
game->metadata.set("name", defaultName);
|
file->metadata.set("name", defaultName);
|
||||||
}else{
|
|
||||||
LOG(LogWarning) << "Game at \"" << path << "\" does not exist!";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,6 +190,27 @@ void addGameDataNode(pugi::xml_node& parent, const FileData* game, SystemData* s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addFileDataNode(pugi::xml_node& parent, const FileData* file, const char* tag, SystemData* system)
|
||||||
|
{
|
||||||
|
//create game and add to parent node
|
||||||
|
pugi::xml_node newNode = parent.append_child(tag);
|
||||||
|
|
||||||
|
//write metadata
|
||||||
|
file->metadata.appendToXML(newNode, true);
|
||||||
|
|
||||||
|
if(newNode.children().begin() == newNode.child("name") //first element is name
|
||||||
|
&& ++newNode.children().begin() == newNode.children().end() //theres only one element
|
||||||
|
&& newNode.child("name").text().get() == getCleanFileName(file->getPath())) //the name is the default
|
||||||
|
{
|
||||||
|
//if the only info is the default name, don't bother with this node
|
||||||
|
//delete it and ultimately do nothing
|
||||||
|
parent.remove_child(newNode);
|
||||||
|
}else{
|
||||||
|
//there's something useful in there so we'll keep the node, add the path
|
||||||
|
newNode.prepend_child("path").text().set(file->getPath().generic_string().c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void updateGamelist(SystemData* system)
|
void updateGamelist(SystemData* system)
|
||||||
{
|
{
|
||||||
//We do this by reading the XML again, adding changes and then writing it back,
|
//We do this by reading the XML again, adding changes and then writing it back,
|
||||||
|
@ -192,9 +228,8 @@ void updateGamelist(SystemData* system)
|
||||||
if(boost::filesystem::exists(xmlpath))
|
if(boost::filesystem::exists(xmlpath))
|
||||||
{
|
{
|
||||||
//parse an existing file first
|
//parse an existing file first
|
||||||
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\" before writing...";
|
|
||||||
|
|
||||||
pugi::xml_parse_result result = doc.load_file(xmlpath.c_str());
|
pugi::xml_parse_result result = doc.load_file(xmlpath.c_str());
|
||||||
|
|
||||||
if(!result)
|
if(!result)
|
||||||
{
|
{
|
||||||
LOG(LogError) << "Error parsing XML file \"" << xmlpath << "\"!\n " << result.description();
|
LOG(LogError) << "Error parsing XML file \"" << xmlpath << "\"!\n " << result.description();
|
||||||
|
@ -222,40 +257,37 @@ void updateGamelist(SystemData* system)
|
||||||
if (rootFolder != nullptr)
|
if (rootFolder != nullptr)
|
||||||
{
|
{
|
||||||
//get only files, no folders
|
//get only files, no folders
|
||||||
std::vector<FileData*> files = rootFolder->getFilesRecursive(GAME);
|
std::vector<FileData*> files = rootFolder->getFilesRecursive(GAME | FOLDER);
|
||||||
//iterate through all files, checking if they're already in the XML
|
//iterate through all files, checking if they're already in the XML
|
||||||
std::vector<FileData*>::const_iterator fit = files.cbegin();
|
std::vector<FileData*>::const_iterator fit = files.cbegin();
|
||||||
while(fit != files.cend())
|
while(fit != files.cend())
|
||||||
{
|
{
|
||||||
if((*fit)->getType() == GAME)
|
const char* tag = ((*fit)->getType() == GAME) ? "game" : "folder";
|
||||||
{
|
|
||||||
//worked. check if this games' path can be found somewhere in the XML
|
|
||||||
for(pugi::xml_node gameNode = root.child("game"); gameNode; gameNode = gameNode.next_sibling("game"))
|
|
||||||
{
|
|
||||||
//get path from game node
|
|
||||||
pugi::xml_node pathNode = gameNode.child("path");
|
|
||||||
if(!pathNode)
|
|
||||||
{
|
|
||||||
LOG(LogError) << "<game> node contains no <path> child!";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//check paths. use the same directory separators
|
// check if the file already exists in the XML
|
||||||
boost::filesystem::path nodePath(pathNode.text().get());
|
// if it does, remove it before adding
|
||||||
boost::filesystem::path gamePath((*fit)->getPath());
|
for(pugi::xml_node fileNode = root.child(tag); fileNode; fileNode = fileNode.next_sibling(tag))
|
||||||
if (nodePath.generic_string() == gamePath.generic_string())
|
{
|
||||||
{
|
pugi::xml_node pathNode = fileNode.child("path");
|
||||||
//found the game. remove it. it will be added again later with updated values
|
if(!pathNode)
|
||||||
root.remove_child(gameNode);
|
{
|
||||||
//break node search loop
|
LOG(LogError) << "<" << tag << "> node contains no <path> child!";
|
||||||
break;
|
continue;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//either the game content was removed, because it needs to be updated,
|
boost::filesystem::path nodePath(pathNode.text().get());
|
||||||
//or didn't exist in the first place, so just add it
|
boost::filesystem::path gamePath((*fit)->getPath());
|
||||||
addGameDataNode(root, *fit, system);
|
if(fs::canonical(nodePath) == fs::canonical(gamePath))
|
||||||
|
{
|
||||||
|
// found it
|
||||||
|
root.remove_child(fileNode);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// it was either removed or never existed to begin with; either way, we can add it now
|
||||||
|
addFileDataNode(root, *fit, tag, system);
|
||||||
|
|
||||||
++fit;
|
++fit;
|
||||||
}
|
}
|
||||||
//now write the file
|
//now write the file
|
||||||
|
|
Loading…
Reference in a new issue