2012-08-02 01:43:55 +00:00
|
|
|
#include "XMLReader.h"
|
|
|
|
#include "SystemData.h"
|
|
|
|
#include "GameData.h"
|
|
|
|
#include "pugiXML/pugixml.hpp"
|
|
|
|
#include <boost/filesystem.hpp>
|
2013-01-04 23:31:51 +00:00
|
|
|
#include "Log.h"
|
2013-10-13 19:07:48 +00:00
|
|
|
#include "Settings.h"
|
2012-08-02 01:43:55 +00:00
|
|
|
|
|
|
|
//this is obviously an incredibly inefficient way to go about searching
|
|
|
|
//but I don't think it'll matter too much with the size of most collections
|
|
|
|
GameData* searchFolderByPath(FolderData* folder, std::string const& path)
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < folder->getFileCount(); i++)
|
|
|
|
{
|
|
|
|
FileData* file = folder->getFile(i);
|
|
|
|
|
|
|
|
if(file->isFolder())
|
|
|
|
{
|
|
|
|
GameData* result = searchFolderByPath((FolderData*)file, path);
|
|
|
|
if(result)
|
|
|
|
return (GameData*)result;
|
|
|
|
}else{
|
|
|
|
if(file->getPath() == path)
|
|
|
|
return (GameData*)file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-08 00:50:45 +00:00
|
|
|
GameData* createGameFromPath(std::string gameAbsPath, SystemData* system)
|
2012-08-02 01:43:55 +00:00
|
|
|
{
|
2012-08-08 00:50:45 +00:00
|
|
|
std::string gamePath = gameAbsPath;
|
|
|
|
std::string sysPath = system->getStartPath();
|
|
|
|
|
|
|
|
//strip out the system path stuff so it's relative to the system root folder
|
2013-06-14 15:16:16 +00:00
|
|
|
unsigned int i = 0;
|
|
|
|
while(i < gamePath.length() && i < sysPath.length() && gamePath[i] == sysPath[i])
|
|
|
|
i++;
|
|
|
|
|
|
|
|
gamePath = gamePath.substr(i, gamePath.length() - i);
|
|
|
|
|
2012-08-08 00:50:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
FolderData* folder = system->getRootFolder();
|
|
|
|
|
2012-09-15 21:24:33 +00:00
|
|
|
size_t separator = 0;
|
|
|
|
size_t nextSeparator = 0;
|
2012-09-10 18:10:59 +00:00
|
|
|
unsigned int loops = 0;
|
2012-08-08 00:50:45 +00:00
|
|
|
while(nextSeparator != std::string::npos)
|
|
|
|
{
|
|
|
|
//determine which chunk of the path we're testing right now
|
|
|
|
nextSeparator = gamePath.find('/', separator + 1);
|
|
|
|
if(nextSeparator == std::string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::string checkName = gamePath.substr(separator + 1, nextSeparator - separator - 1);
|
|
|
|
separator = nextSeparator;
|
|
|
|
|
|
|
|
//see if the folder already exists
|
|
|
|
bool foundFolder = false;
|
|
|
|
for(unsigned int i = 0; i < folder->getFileCount(); i++)
|
|
|
|
{
|
|
|
|
FileData* checkFolder = folder->getFile(i);
|
|
|
|
if(checkFolder->isFolder() && checkFolder->getName() == checkName)
|
|
|
|
{
|
|
|
|
folder = (FolderData*)checkFolder;
|
|
|
|
foundFolder = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//the folder didn't already exist, so create it
|
|
|
|
if(!foundFolder)
|
|
|
|
{
|
|
|
|
FolderData* newFolder = new FolderData(system, folder->getPath() + "/" + checkName, checkName);
|
|
|
|
folder->pushFileData(newFolder);
|
|
|
|
folder = newFolder;
|
|
|
|
}
|
2012-09-10 18:10:59 +00:00
|
|
|
|
2012-09-15 21:24:33 +00:00
|
|
|
//if for some reason this function is broken, break out of this while instead of freezing
|
|
|
|
if(loops > gamePath.length() * 2)
|
2012-09-10 18:10:59 +00:00
|
|
|
{
|
2013-01-06 20:33:50 +00:00
|
|
|
LOG(LogError) << "createGameFromPath breaking out of loop for path \"" << gamePath << "\" to prevent infinite loop (please report this)";
|
2012-09-10 18:10:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
loops++;
|
2012-08-08 00:50:45 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 16:16:20 +00:00
|
|
|
GameData* game = new GameData(gameAbsPath, MetaDataList(system->getGameMDD()));
|
2012-08-08 00:50:45 +00:00
|
|
|
folder->pushFileData(game);
|
|
|
|
return game;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parseGamelist(SystemData* system)
|
|
|
|
{
|
2012-12-17 19:29:43 +00:00
|
|
|
std::string xmlpath = system->getGamelistPath();
|
2012-08-08 00:50:45 +00:00
|
|
|
|
2013-10-13 21:40:36 +00:00
|
|
|
if(!boost::filesystem::exists(xmlpath))
|
2012-12-17 19:29:43 +00:00
|
|
|
return;
|
2012-08-08 00:50:45 +00:00
|
|
|
|
2013-01-06 20:33:50 +00:00
|
|
|
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\"...";
|
2012-08-02 01:43:55 +00:00
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
pugi::xml_parse_result result = doc.load_file(xmlpath.c_str());
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
{
|
2013-01-06 20:33:50 +00:00
|
|
|
LOG(LogError) << "Error parsing XML file \"" << xmlpath << "\"!\n " << result.description();
|
2012-08-02 01:43:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-14 12:16:49 +00:00
|
|
|
pugi::xml_node root = doc.child("gameList");
|
2012-08-02 01:43:55 +00:00
|
|
|
if(!root)
|
|
|
|
{
|
2013-08-14 12:16:49 +00:00
|
|
|
LOG(LogError) << "Could not find <gameList> node in gamelist \"" << xmlpath << "\"!";
|
2012-08-02 01:43:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-14 12:16:49 +00:00
|
|
|
for(pugi::xml_node gameNode = root.child("game"); gameNode; gameNode = gameNode.next_sibling("game"))
|
2012-08-02 01:43:55 +00:00
|
|
|
{
|
2013-08-14 12:16:49 +00:00
|
|
|
pugi::xml_node pathNode = gameNode.child("path");
|
2012-08-02 01:43:55 +00:00
|
|
|
if(!pathNode)
|
|
|
|
{
|
2013-08-14 12:16:49 +00:00
|
|
|
LOG(LogError) << "<game> node contains no <path> child!";
|
2013-01-06 20:33:50 +00:00
|
|
|
continue;
|
2012-08-02 01:43:55 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 12:27:06 +00:00
|
|
|
//convert path to generic directory seperators
|
|
|
|
boost::filesystem::path gamePath(pathNode.text().get());
|
|
|
|
std::string path = gamePath.generic_string();
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2013-08-14 12:16:49 +00:00
|
|
|
//expand '.'
|
2012-09-23 21:01:56 +00:00
|
|
|
if(path[0] == '.')
|
|
|
|
{
|
|
|
|
path.erase(0, 1);
|
2013-08-14 12:16:49 +00:00
|
|
|
path.insert(0, boost::filesystem::path(xmlpath).parent_path().generic_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
//expand '~'
|
|
|
|
if(path[0] == '~')
|
|
|
|
{
|
|
|
|
path.erase(0, 1);
|
|
|
|
path.insert(0, getHomePath());
|
2012-09-23 21:01:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 00:50:45 +00:00
|
|
|
if(boost::filesystem::exists(path))
|
2012-08-02 01:43:55 +00:00
|
|
|
{
|
2012-08-08 00:50:45 +00:00
|
|
|
GameData* game = searchFolderByPath(system->getRootFolder(), path);
|
|
|
|
|
|
|
|
if(game == NULL)
|
|
|
|
game = createGameFromPath(path, system);
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2013-08-14 12:16:49 +00:00
|
|
|
//load the metadata
|
2013-09-19 23:41:14 +00:00
|
|
|
*(game->metadata()) = MetaDataList::createFromXML(system->getGameMDD(), gameNode);
|
2012-08-02 01:43:55 +00:00
|
|
|
|
2013-08-14 12:16:49 +00:00
|
|
|
//make sure name gets set if one didn't exist
|
|
|
|
if(game->metadata()->get("name").empty())
|
|
|
|
game->metadata()->set("name", game->getBaseName());
|
|
|
|
}else{
|
2013-01-06 20:33:50 +00:00
|
|
|
LOG(LogWarning) << "Game at \"" << path << "\" does not exist!";
|
2012-08-02 01:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 12:54:14 +00:00
|
|
|
|
2013-09-19 23:41:14 +00:00
|
|
|
void addGameDataNode(pugi::xml_node& parent, const GameData* game, SystemData* system)
|
2013-06-28 12:54:14 +00:00
|
|
|
{
|
|
|
|
//create game and add to parent node
|
2013-08-14 12:16:49 +00:00
|
|
|
pugi::xml_node newGame = parent.append_child("game");
|
|
|
|
|
|
|
|
//write metadata
|
2013-09-19 23:41:14 +00:00
|
|
|
const_cast<GameData*>(game)->metadata()->appendToXML(newGame, system->getGameMDD());
|
2013-08-14 12:16:49 +00:00
|
|
|
|
|
|
|
if(newGame.children().begin() == newGame.child("name") //first element is name
|
|
|
|
&& ++newGame.children().begin() == newGame.children().end() //theres only one element
|
|
|
|
&& newGame.child("name").text().get() == game->getBaseName()) //the name is the default
|
|
|
|
{
|
|
|
|
//if the only info is the default name, don't bother with this node
|
|
|
|
parent.remove_child(newGame);
|
|
|
|
}else{
|
|
|
|
//there's something useful in there so we'll keep the node, add the path
|
|
|
|
newGame.prepend_child("path").text().set(game->getPath().c_str());
|
2013-06-28 12:54:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateGamelist(SystemData* system)
|
|
|
|
{
|
|
|
|
//We do this by reading the XML again, adding changes and then writing it back,
|
|
|
|
//because there might be information missing in our systemdata which would then miss in the new XML.
|
|
|
|
//We have the complete information for every game though, so we can simply remove a game
|
|
|
|
//we already have in the system from the XML, and then add it back from its GameData information...
|
|
|
|
|
2013-10-13 19:07:48 +00:00
|
|
|
if(Settings::getInstance()->getBool("DisableGamelistWrites"))
|
|
|
|
return;
|
|
|
|
|
2013-06-28 12:54:14 +00:00
|
|
|
std::string xmlpath = system->getGamelistPath();
|
|
|
|
|
|
|
|
pugi::xml_document doc;
|
|
|
|
|
2013-10-16 22:05:02 +00:00
|
|
|
if(boost::filesystem::exists(xmlpath))
|
2013-08-14 12:16:49 +00:00
|
|
|
{
|
2013-10-16 22:05:02 +00:00
|
|
|
//parse an existing file first
|
|
|
|
LOG(LogInfo) << "Parsing XML file \"" << xmlpath << "\" before writing...";
|
|
|
|
|
|
|
|
pugi::xml_parse_result result = doc.load_file(xmlpath.c_str());
|
|
|
|
if(!result)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Error parsing XML file \"" << xmlpath << "\"!\n " << result.description();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
//set up an empty gamelist to append to
|
|
|
|
doc.append_child("gameList");
|
2013-10-16 22:49:14 +00:00
|
|
|
|
|
|
|
//make sure the folders leading up to this path exist (or the XML file write will fail later on)
|
|
|
|
boost::filesystem::path path(xmlpath);
|
|
|
|
boost::filesystem::create_directories(path.parent_path());
|
2013-06-28 12:54:14 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 22:05:02 +00:00
|
|
|
|
2013-08-14 12:16:49 +00:00
|
|
|
pugi::xml_node root = doc.child("gameList");
|
|
|
|
if(!root)
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Could not find <gameList> node in gamelist \"" << xmlpath << "\"!";
|
2013-06-28 12:54:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//now we have all the information from the XML. now iterate through all our games and add information from there
|
|
|
|
FolderData * rootFolder = system->getRootFolder();
|
2013-08-14 12:16:49 +00:00
|
|
|
if (rootFolder != nullptr)
|
|
|
|
{
|
2013-06-28 12:54:14 +00:00
|
|
|
//get only files, no folders
|
|
|
|
std::vector<FileData*> files = rootFolder->getFilesRecursive(true);
|
|
|
|
//iterate through all files, checking if they're already in the XML
|
|
|
|
std::vector<FileData*>::const_iterator fit = files.cbegin();
|
2013-08-14 12:16:49 +00:00
|
|
|
while(fit != files.cend())
|
|
|
|
{
|
2013-06-28 12:54:14 +00:00
|
|
|
//try to cast to gamedata
|
|
|
|
const GameData * game = dynamic_cast<const GameData*>(*fit);
|
2013-08-14 12:16:49 +00:00
|
|
|
if (game != nullptr)
|
|
|
|
{
|
2013-06-28 12:54:14 +00:00
|
|
|
//worked. check if this games' path can be found somewhere in the XML
|
2013-08-14 12:16:49 +00:00
|
|
|
for(pugi::xml_node gameNode = root.child("game"); gameNode; gameNode = gameNode.next_sibling("game"))
|
|
|
|
{
|
2013-06-28 12:54:14 +00:00
|
|
|
//get path from game node
|
2013-08-14 12:16:49 +00:00
|
|
|
pugi::xml_node pathNode = gameNode.child("path");
|
2013-06-28 12:54:14 +00:00
|
|
|
if(!pathNode)
|
|
|
|
{
|
2013-08-14 12:16:49 +00:00
|
|
|
LOG(LogError) << "<game> node contains no <path> child!";
|
2013-06-28 12:54:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-14 12:16:49 +00:00
|
|
|
|
2013-07-03 12:27:06 +00:00
|
|
|
//check paths. use the same directory separators
|
|
|
|
boost::filesystem::path nodePath(pathNode.text().get());
|
|
|
|
boost::filesystem::path gamePath(game->getPath());
|
2013-08-14 12:16:49 +00:00
|
|
|
if (nodePath.generic_string() == gamePath.generic_string())
|
|
|
|
{
|
2013-06-28 12:54:14 +00:00
|
|
|
//found the game. remove it. it will be added again later with updated values
|
|
|
|
root.remove_child(gameNode);
|
|
|
|
//break node search loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-14 12:16:49 +00:00
|
|
|
|
2013-06-28 12:54:14 +00:00
|
|
|
//either the game content was removed, because it needs to be updated,
|
|
|
|
//or didn't exist in the first place, so just add it
|
2013-09-19 23:41:14 +00:00
|
|
|
addGameDataNode(root, game, system);
|
2013-06-28 12:54:14 +00:00
|
|
|
}
|
|
|
|
++fit;
|
|
|
|
}
|
|
|
|
//now write the file
|
|
|
|
if (!doc.save_file(xmlpath.c_str())) {
|
2013-08-14 12:16:49 +00:00
|
|
|
LOG(LogError) << "Error saving gamelist.xml file \"" << xmlpath << "\"!";
|
2013-06-28 12:54:14 +00:00
|
|
|
}
|
2013-08-14 12:16:49 +00:00
|
|
|
}else{
|
2013-06-28 12:54:14 +00:00
|
|
|
LOG(LogError) << "Found no root folder for system \"" << system->getName() << "\"!";
|
|
|
|
}
|
|
|
|
}
|