GameDB: Beef up the validation and finish refactor

GameDB: Regenerate with current master's file
This commit is contained in:
Tyler Wilding 2020-11-13 21:26:27 -05:00 committed by refractionpcsx2
parent 9fa484dbab
commit df1a8e8667
4 changed files with 1902 additions and 1397 deletions

File diff suppressed because it is too large Load Diff

View File

@ -35,13 +35,10 @@ std::string GameDatabaseSchema::GameEntry::memcardFiltersAsString()
return filters; return filters;
} }
/// TODO - the following helper functions can realistically be put in some sort of general yaml utility library
// TODO - might be a way to condense this with templates? get it working first
std::string YamlGameDatabaseImpl::safeGetString(const YAML::Node& n, std::string key, std::string def) std::string YamlGameDatabaseImpl::safeGetString(const YAML::Node& n, std::string key, std::string def)
{ {
if (!n[key]) if (!n[key])
return def; return def;
// TODO - test this safety consideration (parse a value that isn't actually a string
return n[key].as<std::string>(); return n[key].as<std::string>();
} }
@ -49,7 +46,6 @@ int YamlGameDatabaseImpl::safeGetInt(const YAML::Node& n, std::string key, int d
{ {
if (!n[key]) if (!n[key])
return def; return def;
// TODO - test this safety consideration (parse a value that isn't actually an int
return n[key].as<int>(); return n[key].as<int>();
} }
@ -57,25 +53,65 @@ std::vector<std::string> YamlGameDatabaseImpl::safeGetStringList(const YAML::Nod
{ {
if (!n[key]) if (!n[key])
return def; return def;
// TODO - test this safety consideration (parse a value that isn't actually a string
return n[key].as<std::vector<std::string>>(); return n[key].as<std::vector<std::string>>();
} }
GameDatabaseSchema::GameEntry YamlGameDatabaseImpl::entryFromYaml(const YAML::Node& node) GameDatabaseSchema::GameEntry YamlGameDatabaseImpl::entryFromYaml(const std::string serial, const YAML::Node& node)
{ {
GameDatabaseSchema::GameEntry entry; GameDatabaseSchema::GameEntry gameEntry;
try try
{ {
entry.name = safeGetString(node, "name"); gameEntry.name = safeGetString(node, "name");
entry.region = safeGetString(node, "region"); gameEntry.region = safeGetString(node, "region");
entry.compat = static_cast<GameDatabaseSchema::Compatibility>(safeGetInt(node, "compat")); gameEntry.compat = static_cast<GameDatabaseSchema::Compatibility>(safeGetInt(node, "compat"));
entry.eeRoundMode = static_cast<GameDatabaseSchema::RoundMode>(safeGetInt(node, "eeRoundMode")); gameEntry.eeRoundMode = static_cast<GameDatabaseSchema::RoundMode>(safeGetInt(node, "eeRoundMode"));
entry.vuRoundMode = static_cast<GameDatabaseSchema::RoundMode>(safeGetInt(node, "vuRoundMode")); gameEntry.vuRoundMode = static_cast<GameDatabaseSchema::RoundMode>(safeGetInt(node, "vuRoundMode"));
entry.eeClampMode = static_cast<GameDatabaseSchema::ClampMode>(safeGetInt(node, "eeClampMode")); gameEntry.eeClampMode = static_cast<GameDatabaseSchema::ClampMode>(safeGetInt(node, "eeClampMode"));
entry.vuClampMode = static_cast<GameDatabaseSchema::ClampMode>(safeGetInt(node, "vuClampMode")); gameEntry.vuClampMode = static_cast<GameDatabaseSchema::ClampMode>(safeGetInt(node, "vuClampMode"));
entry.gameFixes = safeGetStringList(node, "gameFixes");
entry.speedHacks = safeGetStringList(node, "speedHacks"); // Validate game fixes, invalid ones will be dropped!
entry.memcardFilters = safeGetStringList(node, "memcardFilters"); for (std::string fix : gameEntry.gameFixes)
{
std::vector<std::string> rawFixes = safeGetStringList(node, "gameFixes");
bool fixValidated = false;
for (GamefixId id = GamefixId_FIRST; id < pxEnumEnd; ++id)
{
std::string validFix = wxString(EnumToString(id)) + L"Hack";
if (validFix == fix)
{
fixValidated = true;
break;
}
}
if (fixValidated)
{
gameEntry.gameFixes.push_back(fix);
}
else
{
Console.Error("[GameDB] Invalid gamefix: '%s', specified for serial: '%s'. Dropping!", fix, serial);
}
}
if (YAML::Node speedHacksNode = node["speedHacks"])
{
for (YAML::const_iterator entry = speedHacksNode.begin(); entry != speedHacksNode.end(); entry++)
{
// Validate speedhacks, invalid ones will be skipped!
std::string speedHack = entry->first.as<std::string>();
// NOTE - currently only 1 speedhack!
if (speedHack != "mvuFlagSpeedHack")
{
Console.Error("[GameDB] Invalid speedhack: '%s', specified for serial: '%s'. Dropping!", speedHack, serial);
continue;
}
gameEntry.speedHacks[speedHack] = entry->second.as<int>();
}
}
gameEntry.memcardFilters = safeGetStringList(node, "memcardFilters");
if (YAML::Node patches = node["patches"]) if (YAML::Node patches = node["patches"])
{ {
@ -83,18 +119,19 @@ GameDatabaseSchema::GameEntry YamlGameDatabaseImpl::entryFromYaml(const YAML::No
{ {
YAML::Node key = it->first; YAML::Node key = it->first;
YAML::Node val = it->second; YAML::Node val = it->second;
GameDatabaseSchema::PatchCollection patchCol; GameDatabaseSchema::Patch patchCol;
patchCol.author = safeGetString(val, "author"); patchCol.author = safeGetString(val, "author");
patchCol.patchLines = safeGetStringList(val, "patchLines"); patchCol.patchLines = safeGetStringList(val, "patchLines");
entry.patches[key.as<std::string>()] = patchCol; gameEntry.patches[key.as<std::string>()] = patchCol;
} }
} }
} }
catch (std::exception& e) catch (YAML::RepresentationException e)
{ {
entry.isValid = false; Console.Error("[GameDB] Invalid GameDB syntax detected on serial: '%s'. Error Details - %s", serial, e.what());
gameEntry.isValid = false;
} }
return entry; return gameEntry;
} }
GameDatabaseSchema::GameEntry YamlGameDatabaseImpl::findGame(const std::string serial) GameDatabaseSchema::GameEntry YamlGameDatabaseImpl::findGame(const std::string serial)
@ -112,28 +149,31 @@ int YamlGameDatabaseImpl::numGames()
return gameDb.size(); return gameDb.size();
} }
// yaml-cpp definitely takes longer to parse this giant file, but the library feels more mature
// rapidyaml exists and I have it mostly setup in another branch which could be easily dropped in as a replacement if needed
//
// the problem i ran into with rapidyaml is there is a lack of usage/documentation as it's new
// and i didn't like the default way it handles exceptions (seemed to be configurable, but i didn't have much luck)
bool YamlGameDatabaseImpl::initDatabase(const std::string filePath) bool YamlGameDatabaseImpl::initDatabase(const std::string filePath)
{ {
try try
{ {
// yaml-cpp has memory leak issues, convert to a map and throw it away! // yaml-cpp has memory leak issues if you persist and modify a YAML::Node
// convert to a map and throw it away instead!
YAML::Node data = YAML::LoadFile(filePath); YAML::Node data = YAML::LoadFile(filePath);
for (YAML::const_iterator entry = data.begin(); entry != data.end(); entry++) for (YAML::const_iterator entry = data.begin(); entry != data.end(); entry++)
{ {
// TODO - helper function to throw an error gracefully if an entry is malformed // we don't want to throw away the entire GameDB file if a single entry is made, but we do
// want to yell about it so it can be corrected
try
{
std::string serial = entry->first.as<std::string>(); std::string serial = entry->first.as<std::string>();
gameDb[serial] = entryFromYaml(entry->second); gameDb[serial] = entryFromYaml(serial, entry->second);
}
catch (YAML::RepresentationException e)
{
Console.Error("[GameDB] Invalid GameDB syntax detected. Error Details - %s", e.what());
}
} }
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
// TODO - error log Console.Error("Error occured when initializing GameDB: %s", e.what());
return false; return false;
} }

View File

@ -17,13 +17,6 @@
#include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
// TODO - config - is this still required? not needed on our integration branch
// _Target_ is defined by R300A.h and R5900.h and the definition leaks to here.
// The problem, at least with Visual Studio 2019 on Windows,
// is that unordered_map includes xhash which uses _Target_ as a template
// parameter. Unless we undef it here, the build breaks with a cryptic error message.
#undef _Target_
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <string> #include <string>
@ -47,6 +40,7 @@ public:
enum class RoundMode enum class RoundMode
{ {
Undefined = -1,
Nearest = 0, Nearest = 0,
NegativeInfinity, NegativeInfinity,
PositiveInfinity, PositiveInfinity,
@ -55,41 +49,14 @@ public:
enum class ClampMode enum class ClampMode
{ {
Undefined = -1,
Disabled = 0, Disabled = 0,
Normal, Normal,
Extra, Extra,
Full Full
}; };
// No point in using enums because i need to convert from a string then struct Patch
// left here incase i turn these into lists to validate against
/*enum class GameFix
{
VuAddSubHack = 0,
FpuCompareHack,
FpuMulHack,
FpuNegDivHack,
XgKickHack,
IPUWaitHack,
EETimingHack,
SkipMPEGHack,
OPHFLagHack,
DMABusyHack,
VIFFIFOHack,
VIF1StallHack,
GIFFIFOHack,
FMVinSoftwareHack,
ScarfaceIbitHack,
CrashTagTeamRacingIbit,
VU0KickstartHack,
};
enum class SpeedHacks
{
mvuFlagSpeedHack = 0
};*/
struct PatchCollection
{ {
std::string author; std::string author;
std::vector<std::string> patchLines; std::vector<std::string> patchLines;
@ -101,14 +68,14 @@ public:
std::string name; std::string name;
std::string region; std::string region;
Compatibility compat = Compatibility::Unknown; Compatibility compat = Compatibility::Unknown;
RoundMode eeRoundMode = RoundMode::Nearest; RoundMode eeRoundMode = RoundMode::Undefined;
RoundMode vuRoundMode = RoundMode::Nearest; RoundMode vuRoundMode = RoundMode::Undefined;
ClampMode eeClampMode = ClampMode::Disabled; ClampMode eeClampMode = ClampMode::Undefined;
ClampMode vuClampMode = ClampMode::Disabled; ClampMode vuClampMode = ClampMode::Undefined;
std::vector<std::string> gameFixes; std::vector<std::string> gameFixes;
std::vector<std::string> speedHacks; std::unordered_map<std::string, int> speedHacks;
std::vector<std::string> memcardFilters; std::vector<std::string> memcardFilters;
std::unordered_map<std::string, PatchCollection> patches; std::unordered_map<std::string, Patch> patches;
std::string memcardFiltersAsString(); std::string memcardFiltersAsString();
}; };
@ -131,9 +98,9 @@ public:
private: private:
std::unordered_map<std::string, GameDatabaseSchema::GameEntry> gameDb; std::unordered_map<std::string, GameDatabaseSchema::GameEntry> gameDb;
GameDatabaseSchema::GameEntry entryFromYaml(const YAML::Node& node); GameDatabaseSchema::GameEntry entryFromYaml(const std::string serial, const YAML::Node& node);
// TODO move these into a generic library // TODO - config - move these into a generic library
std::string safeGetString(const YAML::Node& n, std::string key, std::string def = ""); std::string safeGetString(const YAML::Node& n, std::string key, std::string def = "");
int safeGetInt(const YAML::Node& n, std::string key, int def = 0); int safeGetInt(const YAML::Node& n, std::string key, int def = 0);
std::vector<std::string> safeGetStringList(const YAML::Node& n, std::string key, std::vector<std::string> def = {}); std::vector<std::string> safeGetStringList(const YAML::Node& n, std::string key, std::vector<std::string> def = {});

View File

@ -245,91 +245,89 @@ void AppCoreThread::OnPauseDebug()
// Returns number of gamefixes set // Returns number of gamefixes set
static int loadGameSettings(Pcsx2Config& dest, const GameDatabaseSchema::GameEntry& game) static int loadGameSettings(Pcsx2Config& dest, const GameDatabaseSchema::GameEntry& game)
{ {
//if (!game.IsOk()) if (!game.isValid)
// return 0;
//int gf = 0;
//if (game.keyExists("eeRoundMode"))
//{
// SSE_RoundMode eeRM = (SSE_RoundMode)game.getInt("eeRoundMode");
// if (EnumIsValid(eeRM))
// {
// PatchesCon->WriteLn("(GameDB) Changing EE/FPU roundmode to %d [%s]", eeRM, EnumToString(eeRM));
// dest.Cpu.sseMXCSR.SetRoundMode(eeRM);
// ++gf;
// }
//}
//if (game.keyExists("vuRoundMode"))
//{
// SSE_RoundMode vuRM = (SSE_RoundMode)game.getInt("vuRoundMode");
// if (EnumIsValid(vuRM))
// {
// PatchesCon->WriteLn("(GameDB) Changing VU0/VU1 roundmode to %d [%s]", vuRM, EnumToString(vuRM));
// dest.Cpu.sseVUMXCSR.SetRoundMode(vuRM);
// ++gf;
// }
//}
//if (game.keyExists("eeClampMode"))
//{
// int clampMode = game.getInt("eeClampMode");
// PatchesCon->WriteLn("(GameDB) Changing EE/FPU clamp mode [mode=%d]", clampMode);
// dest.Cpu.Recompiler.fpuOverflow = (clampMode >= 1);
// dest.Cpu.Recompiler.fpuExtraOverflow = (clampMode >= 2);
// dest.Cpu.Recompiler.fpuFullMode = (clampMode >= 3);
// gf++;
//}
//if (game.keyExists("vuClampMode"))
//{
// int clampMode = game.getInt("vuClampMode");
// PatchesCon->WriteLn("(GameDB) Changing VU0/VU1 clamp mode [mode=%d]", clampMode);
// dest.Cpu.Recompiler.vuOverflow = (clampMode >= 1);
// dest.Cpu.Recompiler.vuExtraOverflow = (clampMode >= 2);
// dest.Cpu.Recompiler.vuSignOverflow = (clampMode >= 3);
// gf++;
//}
//if (game.keyExists("mvuFlagSpeedHack"))
//{
// bool vuFlagHack = game.getInt("mvuFlagSpeedHack") ? 1 : 0;
// PatchesCon->WriteLn("(GameDB) Changing mVU flag speed hack [mode=%d]", vuFlagHack);
// dest.Speedhacks.vuFlagHack = vuFlagHack;
// gf++;
//}
//if (game.keyExists("InstantVU1SpeedHack"))
//{
// bool vu1InstantHack = game.getInt("InstantVU1SpeedHack") ? 1 : 0;
// PatchesCon->WriteLn("(GameDB) Changing Instant VU1 speedhack [mode=%d]", vu1InstantHack);
// dest.Speedhacks.vu1Instant = vu1InstantHack;
// gf++;
//}
//for (GamefixId id = GamefixId_FIRST; id < pxEnumEnd; ++id)
//{
// wxString key(EnumToString(id));
// key += L"Hack";
// if (game.keyExists(key))
// {
// bool enableIt = game.getBool(key);
// dest.Gamefixes.Set(id, enableIt);
// PatchesCon->WriteLn(L"(GameDB) %s Gamefix: " + key, enableIt ? L"Enabled" : L"Disabled");
// gf++;
// // The LUT is only used for 1 game so we allocate it only when the gamefix is enabled (save 4MB)
// if (id == Fix_GoemonTlbMiss && enableIt)
// vtlb_Alloc_Ppmap();
// }
//}
//return gf;
return 0; return 0;
int gf = 0;
if (game.eeRoundMode != GameDatabaseSchema::RoundMode::Undefined)
{
SSE_RoundMode eeRM = (SSE_RoundMode)enum_cast(game.eeRoundMode);
if (EnumIsValid(eeRM))
{
PatchesCon->WriteLn("(GameDB) Changing EE/FPU roundmode to %d [%s]", eeRM, EnumToString(eeRM));
dest.Cpu.sseMXCSR.SetRoundMode(eeRM);
gf++;
}
}
if (game.vuRoundMode != GameDatabaseSchema::RoundMode::Undefined)
{
SSE_RoundMode vuRM = (SSE_RoundMode)enum_cast(game.vuRoundMode);
if (EnumIsValid(vuRM))
{
PatchesCon->WriteLn("(GameDB) Changing VU0/VU1 roundmode to %d [%s]", vuRM, EnumToString(vuRM));
dest.Cpu.sseVUMXCSR.SetRoundMode(vuRM);
gf++;
}
}
if (game.eeClampMode != GameDatabaseSchema::ClampMode::Undefined)
{
int clampMode = enum_cast(game.eeClampMode);
PatchesCon->WriteLn("(GameDB) Changing EE/FPU clamp mode [mode=%d]", clampMode);
dest.Cpu.Recompiler.fpuOverflow = (clampMode >= 1);
dest.Cpu.Recompiler.fpuExtraOverflow = (clampMode >= 2);
dest.Cpu.Recompiler.fpuFullMode = (clampMode >= 3);
gf++;
}
if (game.vuClampMode != GameDatabaseSchema::ClampMode::Undefined)
{
int clampMode = enum_cast(game.vuClampMode);
PatchesCon->WriteLn("(GameDB) Changing VU0/VU1 clamp mode [mode=%d]", clampMode);
dest.Cpu.Recompiler.vuOverflow = (clampMode >= 1);
dest.Cpu.Recompiler.vuExtraOverflow = (clampMode >= 2);
dest.Cpu.Recompiler.vuSignOverflow = (clampMode >= 3);
gf++;
}
if (game.speedHacks.count("mvuFlagSpeedHack") == 1)
{
bool vuFlagHack = game.speedHacks.at("mvuFlagSpeedHack") ? 1 : 0;
PatchesCon->WriteLn("(GameDB) Changing mVU flag speed hack [mode=%d]", vuFlagHack);
dest.Speedhacks.vuFlagHack = vuFlagHack;
gf++;
}
if (game.keyExists("InstantVU1SpeedHack"))
{
bool vu1InstantHack = game.getInt("InstantVU1SpeedHack") ? 1 : 0;
PatchesCon->WriteLn("(GameDB) Changing Instant VU1 speedhack [mode=%d]", vu1InstantHack);
dest.Speedhacks.vu1Instant = vu1InstantHack;
gf++;
}
// TODO - config - this could be simplified with maps instead of bitfields and enums
for (GamefixId id = GamefixId_FIRST; id < pxEnumEnd; ++id)
{
std::string key = wxString(EnumToString(id)) + L"Hack";
// Gamefixes are already guaranteed to be valid, any invalid ones are dropped
if (std::find(game.gameFixes.begin(), game.gameFixes.end(), key) != game.gameFixes.end())
{
// if the fix is present, it is said to be enabled
dest.Gamefixes.Set(id, true);
PatchesCon->WriteLn(L"(GameDB) Enabled Gamefix: " + key);
gf++;
// The LUT is only used for 1 game so we allocate it only when the gamefix is enabled (save 4MB)
if (id == Fix_GoemonTlbMiss && true)
vtlb_Alloc_Ppmap();
}
}
return gf;
} }
// Used to track the current game serial/id, and used to disable verbose logging of // Used to track the current game serial/id, and used to disable verbose logging of