Load RTPs from mkxp.json as well in libretro builds

This commit is contained in:
刘皓 2025-05-13 17:26:30 -04:00
parent 714c583b9f
commit 36075a5add
No known key found for this signature in database
GPG key ID: 7901753DB465B711
3 changed files with 50 additions and 7 deletions

View file

@ -775,7 +775,50 @@ static bool init_sandbox() {
PHYSFS_mount(system_root_path.c_str(), "/system", true);
// Mount each RTP needed by the game to the game directory
// Mount each RTP declared in mkxp.json to the game directory
for (const std::string &rtp : conf->rtps) {
std::string path("/system" + fs->normalize(rtp.c_str(), false, true, "/RTP"));
std::string rtp_path(system_root_path.c_str());
#ifdef _WIN32
rtp_path.push_back('\\');
#else
rtp_path.push_back('/');
#endif // _WIN32
rtp_path.append(path.c_str() + sizeof "/system/" - 1);
// Check if this is a file or directory
PHYSFS_Stat stat;
if (!PHYSFS_stat(path.c_str(), &stat) || (stat.filetype != PHYSFS_FILETYPE_DIRECTORY && stat.filetype != PHYSFS_FILETYPE_REGULAR)) {
goto fail;
}
if (stat.filetype == PHYSFS_FILETYPE_DIRECTORY) {
// If it's a directory, just mount the path directly
if (!PHYSFS_mount(rtp_path.c_str(), "/game", true)) {
goto fail;
}
} else {
// If it's a file, try to open it as an archive and then mount it
PHYSFS_File *file = PHYSFS_openRead(path.c_str());
if (file == nullptr) {
goto fail;
}
if (!PHYSFS_mountHandle(file, path.c_str(), "/game", true)) {
PHYSFS_close(file);
goto fail;
}
}
log_printf(RETRO_LOG_INFO, "Mounted RTP \"%s\" from \"%s\"\n", rtp.c_str(), rtp_path.c_str());
continue;
fail:
log_printf(RETRO_LOG_ERROR, "Failed to mount RTP \"%s\" because \"%s\" was not found\n", rtp.c_str(), rtp_path.c_str());
continue;
}
// Mount each RTP declared in Game.ini to the game directory
for (const std::string &rtp : conf->game.rtps) {
struct data {
std::string rtp_root_path;

View file

@ -789,7 +789,7 @@ FileSystem::File::~File() {
}
}
static std::string normalizePath(const char *path, bool absolute) {
static std::string normalizePath(const char *path, bool absolute, const char *current_working_directory = nullptr) {
// Replace backslashes with forward slashes
std::string path_str(path);
for (size_t i = 0; i < path_str.length(); ++i) {
@ -800,9 +800,9 @@ static std::string normalizePath(const char *path, bool absolute) {
// If path doesn't start with a forward slash, prepend the current working directory before normalizing
if (path_str.empty()) {
path_str = mkxp_retro::sandbox.has_value() ? mkxp_retro::sandbox->getcwd() : "/game";
path_str = current_working_directory != nullptr ? current_working_directory : mkxp_retro::sandbox.has_value() ? mkxp_retro::sandbox->getcwd() : "/game";
} else if (path_str.front() != '/') {
path_str = std::string(mkxp_retro::sandbox.has_value() ? mkxp_retro::sandbox->getcwd() : "/game") + '/' + path_str;
path_str = std::string(current_working_directory != nullptr ? current_working_directory : mkxp_retro::sandbox.has_value() ? mkxp_retro::sandbox->getcwd() : "/game") + '/' + path_str;
}
// Lexically normalize the path
@ -860,9 +860,9 @@ static std::string normalizePath(const char *path, bool absolute) {
#endif // MKXPZ_RETRO
std::string FileSystem::normalize(const char *pathname, bool preferred,
bool absolute) {
bool absolute, const char *current_working_directory) {
#ifdef MKXPZ_RETRO
return normalizePath(pathname, absolute);
return normalizePath(pathname, absolute, current_working_directory);
#else
return filesystemImpl::normalizePath(pathname, preferred, absolute);
#endif // MKXPZ_RETRO

View file

@ -140,7 +140,7 @@ public:
bool freeOnClose = false);
#endif // MKXPZ_RETRO
std::string normalize(const char *pathname, bool preferred, bool absolute);
std::string normalize(const char *pathname, bool preferred, bool absolute, const char *current_working_directory = nullptr);
/* Does not perform extension supplementing */
bool exists(const char *filename);