2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-05-24 08:29:29 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// MetaData.cpp
|
2020-05-24 08:29:29 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Static data for default metadata values as well as functions
|
|
|
|
// to read and write metadata from the gamelist files.
|
2020-05-24 08:29:29 +00:00
|
|
|
//
|
|
|
|
|
2014-06-25 16:29:58 +00:00
|
|
|
#include "MetaData.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-06-25 16:29:58 +00:00
|
|
|
#include "Log.h"
|
2021-07-07 18:03:42 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
2020-09-21 17:17:34 +00:00
|
|
|
|
2020-06-24 15:38:41 +00:00
|
|
|
#include <pugixml.hpp>
|
2014-06-25 16:29:58 +00:00
|
|
|
|
2021-11-09 21:40:08 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// clang-format off
|
|
|
|
// The statistic entries must be placed at the bottom or otherwise there will be problems with
|
|
|
|
// saving the values in GuiMetaDataEd.
|
2022-01-16 17:18:28 +00:00
|
|
|
MetaDataDecl gameDecls[] {
|
2022-04-11 21:28:18 +00:00
|
|
|
// Key Type Default value Statistic Name in GuiMetaDataEd Prompt in GuiMetaDataEd Scrape
|
2022-01-15 10:50:51 +00:00
|
|
|
{"name", MD_STRING, "", false, "name", "enter name", true},
|
|
|
|
{"sortname", MD_STRING, "", false, "sortname", "enter sortname", false},
|
|
|
|
{"collectionsortname", MD_STRING, "", false, "custom collections sortname", "enter collections sortname", false},
|
|
|
|
{"desc", MD_MULTILINE_STRING, "", false, "description", "enter description", true},
|
|
|
|
{"rating", MD_RATING, "0", false, "rating", "enter rating", true},
|
|
|
|
{"releasedate", MD_DATE, "19700101T000000", false, "release date", "enter release date", true},
|
|
|
|
{"developer", MD_STRING, "unknown", false, "developer", "enter developer", true},
|
|
|
|
{"publisher", MD_STRING, "unknown", false, "publisher", "enter publisher", true},
|
|
|
|
{"genre", MD_STRING, "unknown", false, "genre", "enter genre", true},
|
|
|
|
{"players", MD_STRING, "unknown", false, "players", "enter number of players", true},
|
|
|
|
{"favorite", MD_BOOL, "false", false, "favorite", "enter favorite off/on", false},
|
|
|
|
{"completed", MD_BOOL, "false", false, "completed", "enter completed off/on", false},
|
|
|
|
{"kidgame", MD_BOOL, "false", false, "kidgame", "enter kidgame off/on", false},
|
|
|
|
{"hidden", MD_BOOL, "false", false, "hidden", "enter hidden off/on", false},
|
|
|
|
{"broken", MD_BOOL, "false", false, "broken/not working", "enter broken off/on", false},
|
|
|
|
{"nogamecount", MD_BOOL, "false", false, "exclude from game counter", "enter don't count as game off/on", false},
|
|
|
|
{"nomultiscrape", MD_BOOL, "false", false, "exclude from multi-scraper", "enter no multi-scrape off/on", false},
|
|
|
|
{"hidemetadata", MD_BOOL, "false", false, "hide metadata fields", "enter hide metadata off/on", false},
|
|
|
|
{"playcount", MD_INT, "0", false, "times played", "enter number of times played", false},
|
|
|
|
{"controller", MD_CONTROLLER, "", false, "controller", "select controller", true},
|
|
|
|
{"altemulator", MD_ALT_EMULATOR, "", false, "alternative emulator", "select alternative emulator", false},
|
|
|
|
{"lastplayed", MD_TIME, "0", true, "last played", "enter last played date", false}
|
2021-11-09 21:40:08 +00:00
|
|
|
};
|
2020-08-08 09:36:43 +00:00
|
|
|
|
2022-01-16 17:18:28 +00:00
|
|
|
MetaDataDecl folderDecls[] {
|
2022-04-11 21:28:18 +00:00
|
|
|
// Key Type Default value Statistic Name in GuiMetaDataEd Prompt in GuiMetaDataEd Scrape
|
2021-11-09 21:40:08 +00:00
|
|
|
{"name", MD_STRING, "", false, "name", "enter name", true},
|
|
|
|
{"desc", MD_MULTILINE_STRING, "", false, "description", "enter description", true},
|
|
|
|
{"rating", MD_RATING, "0", false, "rating", "enter rating", true},
|
2021-11-26 19:55:54 +00:00
|
|
|
{"releasedate", MD_DATE, "19700101T000000", false, "release date", "enter release date", true},
|
2021-11-09 21:40:08 +00:00
|
|
|
{"developer", MD_STRING, "unknown", false, "developer", "enter developer", true},
|
|
|
|
{"publisher", MD_STRING, "unknown", false, "publisher", "enter publisher", true},
|
|
|
|
{"genre", MD_STRING, "unknown", false, "genre", "enter genre", true},
|
|
|
|
{"players", MD_STRING, "unknown", false, "players", "enter number of players", true},
|
|
|
|
{"favorite", MD_BOOL, "false", false, "favorite", "enter favorite off/on", false},
|
|
|
|
{"completed", MD_BOOL, "false", false, "completed", "enter completed off/on", false},
|
|
|
|
{"kidgame", MD_BOOL, "false", false, "kidgame (only affects badges)", "enter kidgame off/on", false},
|
|
|
|
{"hidden", MD_BOOL, "false", false, "hidden", "enter hidden off/on", false},
|
|
|
|
{"broken", MD_BOOL, "false", false, "broken/not working", "enter broken off/on", false},
|
|
|
|
{"nomultiscrape", MD_BOOL, "false", false, "exclude from multi-scraper", "enter no multi-scrape off/on", false},
|
|
|
|
{"hidemetadata", MD_BOOL, "false", false, "hide metadata fields", "enter hide metadata off/on", false},
|
|
|
|
{"controller", MD_CONTROLLER, "", false, "controller", "select controller", true},
|
2022-04-12 16:35:57 +00:00
|
|
|
{"folderlink", MD_FOLDER_LINK, "", false, "folder link", "select folder link", false},
|
2021-11-09 21:40:08 +00:00
|
|
|
{"lastplayed", MD_TIME, "0", true, "last played", "enter last played date", false}
|
|
|
|
};
|
|
|
|
// clang-format on
|
2020-08-08 09:36:43 +00:00
|
|
|
|
2022-01-16 17:18:28 +00:00
|
|
|
const std::vector<MetaDataDecl> gameMDD {gameDecls,
|
|
|
|
gameDecls + sizeof(gameDecls) / sizeof(gameDecls[0])};
|
|
|
|
|
|
|
|
const std::vector<MetaDataDecl> folderMDD {
|
|
|
|
folderDecls, folderDecls + sizeof(folderDecls) / sizeof(folderDecls[0])};
|
2021-07-07 18:03:42 +00:00
|
|
|
|
2021-11-09 21:40:08 +00:00
|
|
|
} // namespace
|
2014-06-25 16:29:58 +00:00
|
|
|
|
|
|
|
const std::vector<MetaDataDecl>& getMDDByType(MetaDataListType type)
|
|
|
|
{
|
2021-07-07 18:03:42 +00:00
|
|
|
switch (type) {
|
2020-10-18 17:45:26 +00:00
|
|
|
case GAME_METADATA:
|
|
|
|
return gameMDD;
|
|
|
|
case FOLDER_METADATA:
|
|
|
|
return folderMDD;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(LogError) << "Invalid MDD type";
|
|
|
|
return gameMDD;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MetaDataList::MetaDataList(MetaDataListType type)
|
2021-07-07 18:03:42 +00:00
|
|
|
: mType(type)
|
|
|
|
, mWasChanged(false)
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
const std::vector<MetaDataDecl>& mdd = getMDD();
|
2021-11-17 16:35:34 +00:00
|
|
|
for (auto it = mdd.cbegin(); it != mdd.cend(); ++it)
|
|
|
|
set(it->key, it->defaultValue);
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 08:29:29 +00:00
|
|
|
MetaDataList MetaDataList::createFromXML(MetaDataListType type,
|
2021-07-07 18:03:42 +00:00
|
|
|
pugi::xml_node& node,
|
|
|
|
const std::string& relativeTo)
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
MetaDataList mdl(type);
|
|
|
|
|
|
|
|
const std::vector<MetaDataDecl>& mdd = mdl.getMDD();
|
|
|
|
|
2021-11-17 16:35:34 +00:00
|
|
|
for (auto it = mdd.cbegin(); it != mdd.cend(); ++it) {
|
|
|
|
pugi::xml_node md = node.child(it->key.c_str());
|
2020-06-21 12:25:28 +00:00
|
|
|
if (md && !md.text().empty()) {
|
|
|
|
// If it's a path, resolve relative paths.
|
|
|
|
std::string value = md.text().get();
|
2021-11-17 16:35:34 +00:00
|
|
|
if (it->type == MD_PATH)
|
2020-06-21 12:25:28 +00:00
|
|
|
value = Utils::FileSystem::resolveRelativePath(value, relativeTo, true);
|
2021-11-17 16:35:34 +00:00
|
|
|
mdl.set(it->key, value);
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-11-17 16:35:34 +00:00
|
|
|
mdl.set(it->key, it->defaultValue);
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mdl;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:03:42 +00:00
|
|
|
void MetaDataList::appendToXML(pugi::xml_node& parent,
|
|
|
|
bool ignoreDefaults,
|
|
|
|
const std::string& relativeTo) const
|
2014-06-25 16:29:58 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
const std::vector<MetaDataDecl>& mdd = getMDD();
|
|
|
|
|
2021-11-17 16:35:34 +00:00
|
|
|
for (auto it = mdd.cbegin(); it != mdd.cend(); ++it) {
|
|
|
|
auto mapIter = mMap.find(it->key);
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mapIter != mMap.cend()) {
|
|
|
|
// We have this value!
|
|
|
|
// If it's just the default (and we ignore defaults), don't write it.
|
2021-11-17 16:35:34 +00:00
|
|
|
if (ignoreDefaults && mapIter->second == it->defaultValue)
|
2020-06-21 12:25:28 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Try and make paths relative if we can.
|
|
|
|
std::string value = mapIter->second;
|
2021-11-17 16:35:34 +00:00
|
|
|
if (it->type == MD_PATH)
|
2020-06-21 12:25:28 +00:00
|
|
|
value = Utils::FileSystem::createRelativePath(value, relativeTo, true);
|
|
|
|
|
|
|
|
parent.append_child(mapIter->first.c_str()).text().set(value.c_str());
|
|
|
|
}
|
|
|
|
}
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetaDataList::set(const std::string& key, const std::string& value)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mMap[key] = value;
|
|
|
|
mWasChanged = true;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& MetaDataList::get(const std::string& key) const
|
|
|
|
{
|
2021-07-07 18:03:42 +00:00
|
|
|
// Check that the key actually exists, otherwise return an empty string.
|
2020-06-21 12:25:28 +00:00
|
|
|
if (mMap.count(key) > 0)
|
|
|
|
return mMap.at(key);
|
|
|
|
else
|
2021-07-07 18:03:42 +00:00
|
|
|
return mNoResult;
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int MetaDataList::getInt(const std::string& key) const
|
|
|
|
{
|
2021-07-07 18:03:42 +00:00
|
|
|
// Return integer value.
|
2020-06-21 12:25:28 +00:00
|
|
|
return atoi(get(key).c_str());
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float MetaDataList::getFloat(const std::string& key) const
|
|
|
|
{
|
2021-07-07 18:03:42 +00:00
|
|
|
// Return float value.
|
2020-11-17 22:06:54 +00:00
|
|
|
return static_cast<float>(atof(get(key).c_str()));
|
2014-06-25 16:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 15:59:40 +00:00
|
|
|
bool MetaDataList::wasChanged() const
|
|
|
|
{
|
2021-07-07 18:03:42 +00:00
|
|
|
// Return whether the metadata was changed.
|
2020-06-21 12:25:28 +00:00
|
|
|
return mWasChanged;
|
2016-12-19 15:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MetaDataList::resetChangedFlag()
|
|
|
|
{
|
2021-07-07 18:03:42 +00:00
|
|
|
// Reset the change flag.
|
2020-06-21 12:25:28 +00:00
|
|
|
mWasChanged = false;
|
2016-12-19 15:59:40 +00:00
|
|
|
}
|