HostInterface: Pull compatibility database from program directory

Fixes compatibility list not reading on Linux.
This commit is contained in:
Connor McLaughlin 2020-05-16 22:18:02 +10:00
parent 4c9cdbb147
commit f7a2448dfb
2 changed files with 35 additions and 10 deletions

View file

@ -26,7 +26,12 @@ Log_SetChannel(HostInterface);
#include <mmsystem.h> #include <mmsystem.h>
#endif #endif
HostInterface::HostInterface() = default; HostInterface::HostInterface()
{
// we can get the program directory at construction time
const std::string program_path = FileSystem::GetProgramPath();
m_program_directory = FileSystem::GetPathDirectory(program_path.c_str());
}
HostInterface::~HostInterface() HostInterface::~HostInterface()
{ {
@ -45,7 +50,7 @@ bool HostInterface::Initialize()
m_game_list = std::make_unique<GameList>(); m_game_list = std::make_unique<GameList>();
m_game_list->SetCacheFilename(GetUserDirectoryRelativePath("cache/gamelist.cache")); m_game_list->SetCacheFilename(GetUserDirectoryRelativePath("cache/gamelist.cache"));
m_game_list->SetDatabaseFilename(GetUserDirectoryRelativePath("cache/redump.dat")); m_game_list->SetDatabaseFilename(GetUserDirectoryRelativePath("cache/redump.dat"));
m_game_list->SetCompatibilityFilename(GetUserDirectoryRelativePath("database/compatibility.xml")); m_game_list->SetCompatibilityFilename(GetProgramDirectoryRelativePath("database/compatibility.xml"));
return true; return true;
} }
@ -657,22 +662,20 @@ void HostInterface::SetUserDirectory()
if (!m_user_directory.empty()) if (!m_user_directory.empty())
return; return;
const std::string program_path = FileSystem::GetProgramPath(); std::fprintf(stdout, "Program directory \"%s\"\n", m_program_directory.c_str());
const std::string program_directory = FileSystem::GetPathDirectory(program_path.c_str());
std::fprintf(stdout, "Program path: \"%s\" (directory \"%s\")\n", program_path.c_str(), program_directory.c_str());
if (FileSystem::FileExists(StringUtil::StdStringFromFormat("%s%c%s", program_directory.c_str(), if (FileSystem::FileExists(StringUtil::StdStringFromFormat("%s%c%s", m_program_directory.c_str(),
FS_OSPATH_SEPERATOR_CHARACTER, "portable.txt") FS_OSPATH_SEPERATOR_CHARACTER, "portable.txt")
.c_str())) .c_str()))
{ {
std::fprintf(stdout, "portable.txt found, using program directory as user directory.\n"); std::fprintf(stdout, "portable.txt found, using program directory as user directory.\n");
m_user_directory = program_directory; m_user_directory = m_program_directory;
} }
else else
{ {
#ifdef WIN32 #ifdef WIN32
// On Windows, use the path to the program. We might want to use My Documents in the future. // On Windows, use the path to the program. We might want to use My Documents in the future.
m_user_directory = program_directory; m_user_directory = m_program_directory;
#elif __linux__ #elif __linux__
// On Linux, use .local/share/duckstation as a user directory by default. // On Linux, use .local/share/duckstation as a user directory by default.
const char* xdg_data_home = getenv("XDG_DATA_HOME"); const char* xdg_data_home = getenv("XDG_DATA_HOME");
@ -684,7 +687,7 @@ void HostInterface::SetUserDirectory()
{ {
const char* home_path = getenv("HOME"); const char* home_path = getenv("HOME");
if (!home_path) if (!home_path)
m_user_directory = program_directory; m_user_directory = m_program_directory;
else else
m_user_directory = StringUtil::StdStringFromFormat("%s/.local/share/duckstation", home_path); m_user_directory = StringUtil::StdStringFromFormat("%s/.local/share/duckstation", home_path);
} }
@ -692,7 +695,7 @@ void HostInterface::SetUserDirectory()
// On macOS, default to ~/Library/Application Support/DuckStation. // On macOS, default to ~/Library/Application Support/DuckStation.
const char* home_path = getenv("HOME"); const char* home_path = getenv("HOME");
if (!home_path) if (!home_path)
m_user_directory = program_directory; m_user_directory = m_program_directory;
else else
m_user_directory = StringUtil::StdStringFromFormat("%s/Library/Application Support/DuckStation", home_path); m_user_directory = StringUtil::StdStringFromFormat("%s/Library/Application Support/DuckStation", home_path);
#endif #endif
@ -753,6 +756,24 @@ std::string HostInterface::GetUserDirectoryRelativePath(const char* format, ...)
} }
} }
std::string HostInterface::GetProgramDirectoryRelativePath(const char* format, ...) const
{
std::va_list ap;
va_start(ap, format);
std::string formatted_path = StringUtil::StdStringFromFormatV(format, ap);
va_end(ap);
if (m_program_directory.empty())
{
return formatted_path;
}
else
{
return StringUtil::StdStringFromFormat("%s%c%s", m_program_directory.c_str(), FS_OSPATH_SEPERATOR_CHARACTER,
formatted_path.c_str());
}
}
TinyString HostInterface::GetTimestampStringForFileName() TinyString HostInterface::GetTimestampStringForFileName()
{ {
const Timestamp ts(Timestamp::Now()); const Timestamp ts(Timestamp::Now());

View file

@ -145,6 +145,9 @@ public:
/// Returns a path relative to the user directory. /// Returns a path relative to the user directory.
std::string GetUserDirectoryRelativePath(const char* format, ...) const; std::string GetUserDirectoryRelativePath(const char* format, ...) const;
/// Returns a path relative to the application directory (for system files).
std::string GetProgramDirectoryRelativePath(const char* format, ...) const;
/// Returns a string which can be used as part of a filename, based on the current date/time. /// Returns a string which can be used as part of a filename, based on the current date/time.
static TinyString GetTimestampStringForFileName(); static TinyString GetTimestampStringForFileName();
@ -262,6 +265,7 @@ protected:
std::unique_ptr<System> m_system; std::unique_ptr<System> m_system;
std::unique_ptr<GameList> m_game_list; std::unique_ptr<GameList> m_game_list;
Settings m_settings; Settings m_settings;
std::string m_program_directory;
std::string m_user_directory; std::string m_user_directory;
std::deque<OSDMessage> m_osd_messages; std::deque<OSDMessage> m_osd_messages;