Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: Prevent the Recents list from disappearing a lot #19213

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Common/File/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ std::string ResolvePath(const std::string &path) {
delete [] buf;
return output;

#elif PPSSPP_PLATFORM(IOS)
// Resolving has wacky effects on documents paths.
return path;
#else
std::unique_ptr<char[]> buf(new char[PATH_MAX + 32768]);
if (realpath(path.c_str(), buf.get()) == nullptr)
Expand Down Expand Up @@ -404,8 +407,8 @@ bool Exists(const Path &path) {
SetErrorMode(OldMode);
#endif
return true;
#else
struct stat file_info;
#else // !WIN32
struct stat file_info{};
return stat(path.c_str(), &file_info) == 0;
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion Common/GPU/Vulkan/VulkanLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ void VulkanSetAvailable(bool available) {
bool VulkanMayBeAvailable() {
#if PPSSPP_PLATFORM(IOS)
g_vulkanAvailabilityChecked = true;
// MoltenVK does no longer seem to support iOS <= 12, despite what the docs say.
g_vulkanMayBeAvailable = System_GetPropertyInt(SYSPROP_SYSTEMVERSION) >= 13;
INFO_LOG(SYSTEM, "VulkanMayBeAvailable: Detected version: %d", (int)System_GetPropertyInt(SYSPROP_SYSTEMVERSION));
return g_vulkanMayBeAvailable;
#else
// Unsupported in VR at the moment
Expand Down
52 changes: 49 additions & 3 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ static const char * const logSectionName = "LogDebug";
static const char * const logSectionName = "Log";
#endif

static bool TryUpdateSavedPath(Path *path);

std::string GPUBackendToString(GPUBackend backend) {
switch (backend) {
case GPUBackend::OPENGL:
Expand Down Expand Up @@ -1147,6 +1149,9 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {

iRunCount++;

// For iOS, issue #19211
TryUpdateSavedPath(&currentDirectory);

// This check is probably not really necessary here anyway, you can always
// press Home or Browse if you're in a bad directory.
if (!File::Exists(currentDirectory))
Expand Down Expand Up @@ -1367,7 +1372,7 @@ bool Config::Save(const char *saveReason) {
playTimeTracker_.Save(playTime);

if (!iniFile.Save(iniFilename_)) {
ERROR_LOG(LOADER, "Error saving config (%s)- can't write ini '%s'", saveReason, iniFilename_.c_str());
ERROR_LOG(LOADER, "Error saving config (%s) - can't write ini '%s'", saveReason, iniFilename_.c_str());
return false;
}
INFO_LOG(LOADER, "Config saved (%s): '%s' (%0.1f ms)", saveReason, iniFilename_.c_str(), (time_now_d() - startTime) * 1000.0);
Expand Down Expand Up @@ -1546,6 +1551,34 @@ void Config::RemoveRecent(const std::string &file) {
recentIsos.erase(iter, recentIsos.end());
}

// On iOS, the path to the app documents directory changes on each launch.
// Example path:
// /var/mobile/Containers/Data/Application/0E0E89DE-8D8E-485A-860C-700D8BC87B86/Documents/PSP/GAME/SuicideBarbie
// The GUID part changes on each launch.
static bool TryUpdateSavedPath(Path *path) {
#if PPSSPP_PLATFORM(IOS)
INFO_LOG(LOADER, "Original path: %s", path->c_str());
std::string pathStr = path->ToString();

const std::string_view applicationRoot = "/var/mobile/Containers/Data/Application/";
if (startsWith(pathStr, applicationRoot)) {
size_t documentsPos = pathStr.find("/Documents/");
if (documentsPos == std::string::npos) {
return false;
}
std::string memstick = g_Config.memStickDirectory.ToString();
size_t memstickDocumentsPos = memstick.find("/Documents"); // Note: No trailing slash, or we won't find it.
*path = Path(memstick.substr(0, memstickDocumentsPos) + pathStr.substr(documentsPos));
return true;
} else {
// Path can't be auto-updated.
return false;
}
#else
return false;
#endif
}

void Config::CleanRecent() {
private_->SetRecentIsosThread([this] {
SetCurrentThreadName("RecentISOs");
Expand All @@ -1556,13 +1589,23 @@ void Config::CleanRecent() {

std::lock_guard<std::mutex> guard(private_->recentIsosLock);
std::vector<std::string> cleanedRecent;
if (recentIsos.empty()) {
INFO_LOG(LOADER, "No recents list found.");
}

for (size_t i = 0; i < recentIsos.size(); i++) {
bool exists = false;
Path path = Path(recentIsos[i]);
switch (path.Type()) {
case PathType::CONTENT_URI:
case PathType::NATIVE:
exists = File::Exists(path);
if (!exists) {
if (TryUpdateSavedPath(&path)) {
exists = File::Exists(path);
INFO_LOG(LOADER, "Exists=%d when checking updated path: %s", exists, path.c_str());
}
}
break;
default:
FileLoader *loader = ConstructFileLoader(path);
Expand All @@ -1572,11 +1615,14 @@ void Config::CleanRecent() {
}

if (exists) {
std::string pathStr = path.ToString();
// Make sure we don't have any redundant items.
auto duplicate = std::find(cleanedRecent.begin(), cleanedRecent.end(), recentIsos[i]);
auto duplicate = std::find(cleanedRecent.begin(), cleanedRecent.end(), pathStr);
if (duplicate == cleanedRecent.end()) {
cleanedRecent.push_back(recentIsos[i]);
cleanedRecent.push_back(pathStr);
}
} else {
DEBUG_LOG(LOADER, "Removed %s from recent. errno=%d", path.c_str(), errno);
}
}

Expand Down
2 changes: 2 additions & 0 deletions ios/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ - (void)shutdown
{
INFO_LOG(SYSTEM, "shutdown GL");

g_Config.Save("shutdown GL");

_dbg_assert_(graphicsContext);
_dbg_assert_(sharedViewController != nil);
sharedViewController = nil;
Expand Down
2 changes: 2 additions & 0 deletions ios/ViewControllerMetal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ - (void)shutdown
{
INFO_LOG(SYSTEM, "shutdown VK");

g_Config.Save("shutdown vk");

_dbg_assert_(sharedViewController != nil);
sharedViewController = nil;

Expand Down
Loading