Break out the "Recent ISOs" code into its own file so it's more practical to work on it

This commit is contained in:
Henrik Rydgård
2025-03-26 10:00:27 +01:00
parent 2309226a08
commit a156697822
12 changed files with 217 additions and 144 deletions
+2
View File
@@ -2425,6 +2425,8 @@ add_library(${CoreLibName} ${CoreLinkType}
Core/Util/BlockAllocator.h
Core/Util/PPGeDraw.cpp
Core/Util/PPGeDraw.h
Core/Util/RecentFiles.cpp
Core/Util/RecentFiles.h
${GPU_SOURCES}
ext/disarm.cpp
ext/disarm.h
+1 -1
View File
@@ -328,7 +328,7 @@ std::string ResolvePath(const std::string &path) {
}
if (Android_IsContentUri(path)) {
// Nothing to do?
// Nothing to do? We consider these to only have one canonical form.
return path;
}
+20 -138
View File
@@ -52,11 +52,12 @@
#include "Core/Config.h"
#include "Core/ConfigSettings.h"
#include "Core/ConfigValues.h"
#include "Core/Loaders.h"
#include "Core/KeyMap.h"
#include "Core/System.h"
#include "Core/HLE/sceUtility.h"
#include "Core/Instance.h"
#include "Core/Util/RecentFiles.h"
#include "GPU/Common/FramebufferManagerCommon.h"
// TODO: Find a better place for this.
@@ -66,24 +67,13 @@ Config g_Config;
static bool jitForcedOff;
// Not in Config.h because it's #included a lot.
struct ConfigPrivate {
std::mutex recentIsosLock;
std::mutex recentIsosThreadLock;
std::thread recentIsosThread;
bool recentIsosThreadPending = false;
void ResetRecentIsosThread();
void SetRecentIsosThread(std::function<void()> f);
};
#ifdef _DEBUG
static const char * const logSectionName = "LogDebug";
#else
static const char * const logSectionName = "Log";
#endif
static bool TryUpdateSavedPath(Path *path);
bool TryUpdateSavedPath(Path *path);
std::string GPUBackendToString(GPUBackend backend) {
switch (backend) {
@@ -1091,30 +1081,13 @@ static void IterateSettings(std::function<void(const ConfigSetting &setting)> fu
}
}
void ConfigPrivate::ResetRecentIsosThread() {
std::lock_guard<std::mutex> guard(recentIsosThreadLock);
if (recentIsosThreadPending && recentIsosThread.joinable())
recentIsosThread.join();
}
void ConfigPrivate::SetRecentIsosThread(std::function<void()> f) {
std::lock_guard<std::mutex> guard(recentIsosThreadLock);
if (recentIsosThreadPending && recentIsosThread.joinable())
recentIsosThread.join();
recentIsosThread = std::thread(f);
recentIsosThreadPending = true;
}
Config::Config() {
private_ = new ConfigPrivate();
}
Config::Config() {}
Config::~Config() {
if (bUpdatedInstanceCounter) {
ShutdownInstanceCounter();
}
private_->ResetRecentIsosThread();
delete private_;
ResetRecentIsosThread();
}
void Config::LoadLangValuesMapping() {
@@ -1269,18 +1242,7 @@ void Config::Load(const char *iniFileName, const char *controllerIniFilename) {
}
if (iMaxRecent > 0) {
private_->ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
recentIsos.clear();
for (int i = 0; i < iMaxRecent; i++) {
char keyName[64];
std::string fileName;
snprintf(keyName, sizeof(keyName), "FileName%d", i);
if (recent->Get(keyName, &fileName, "") && !fileName.empty()) {
recentIsos.push_back(fileName);
}
}
LoadRecentIsos(recent, iMaxRecent);
}
// Time tracking
@@ -1417,18 +1379,7 @@ bool Config::Save(const char *saveReason) {
Section *recent = iniFile.GetOrCreateSection("Recent");
recent->Set("MaxRecent", iMaxRecent);
private_->ResetRecentIsosThread();
for (int i = 0; i < iMaxRecent; i++) {
char keyName[64];
snprintf(keyName, sizeof(keyName), "FileName%d", i);
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
if (i < (int)recentIsos.size()) {
recent->Set(keyName, recentIsos[i]);
} else {
recent->Delete(keyName); // delete the nonexisting FileName
}
}
SaveRecentIsos(recent, iMaxRecent);
Section *pinnedPaths = iniFile.GetOrCreateSection("PinnedPaths");
pinnedPaths->Clear();
@@ -1621,44 +1572,30 @@ void Config::DismissUpgrade() {
g_Config.dismissedVersion = g_Config.upgradeVersion;
}
void Config::AddRecent(const std::string &file) {
void Config::AddRecent(const std::string &filename) {
// Don't bother with this if the user disabled recents (it's -1).
if (iMaxRecent <= 0)
return;
// We'll add it back below. This makes sure it's at the front, and only once.
RemoveRecent(file);
private_->ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
const std::string filename = File::ResolvePath(file);
recentIsos.insert(recentIsos.begin(), filename);
if ((int)recentIsos.size() > iMaxRecent)
recentIsos.resize(iMaxRecent);
const std::string resolvedFilename = File::ResolvePath(filename);
AddRecentResolved(resolvedFilename, iMaxRecent);
}
void Config::RemoveRecent(const std::string &file) {
// Don't bother with this if the user disabled recents (it's -1).
if (iMaxRecent <= 0)
void Config::RemoveRecent(const std::string &filename) {
if (iMaxRecent <= 0) {
return;
}
private_->ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
const std::string filename = File::ResolvePath(file);
auto iter = std::remove_if(recentIsos.begin(), recentIsos.end(), [filename](const auto &str) {
const std::string recent = File::ResolvePath(str);
return filename == recent;
});
// remove_if is weird.
recentIsos.erase(iter, recentIsos.end());
std::string resolvedFilename = File::ResolvePath(filename);
RemoveRecentResolved(resolvedFilename);
}
// 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) {
bool TryUpdateSavedPath(Path *path) {
#if PPSSPP_PLATFORM(IOS)
INFO_LOG(Log::Loader, "Original path: %s", path->c_str());
std::string pathStr = path->ToString();
@@ -1683,74 +1620,19 @@ static bool TryUpdateSavedPath(Path *path) {
}
void Config::CleanRecent() {
private_->SetRecentIsosThread([this] {
SetCurrentThreadName("RecentISOs");
AndroidJNIThreadContext jniContext; // destructor detaches
double startTime = time_now_d();
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
std::vector<std::string> cleanedRecent;
if (recentIsos.empty()) {
INFO_LOG(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(Log::Loader, "Exists=%d when checking updated path: %s", exists, path.c_str());
}
}
break;
default:
FileLoader *loader = ConstructFileLoader(path);
exists = loader->ExistsFast();
delete loader;
break;
}
if (exists) {
std::string pathStr = path.ToString();
// Make sure we don't have any redundant items.
auto duplicate = std::find(cleanedRecent.begin(), cleanedRecent.end(), pathStr);
if (duplicate == cleanedRecent.end()) {
cleanedRecent.push_back(pathStr);
}
} else {
DEBUG_LOG(Log::Loader, "Removed %s from recent. errno=%d", path.c_str(), errno);
}
}
double recentTime = time_now_d() - startTime;
if (recentTime > 0.1) {
INFO_LOG(Log::System, "CleanRecent took %0.2f", recentTime);
}
recentIsos = cleanedRecent;
});
CleanRecentIsos();
}
std::vector<std::string> Config::RecentIsos() const {
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
return recentIsos;
return GetRecentIsos();
}
bool Config::HasRecentIsos() const {
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
return !recentIsos.empty();
return ::HasRecentIsos();
}
void Config::ClearRecentIsos() {
private_->ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(private_->recentIsosLock);
recentIsos.clear();
::CleanRecentIsos();
}
void Config::SetSearchPath(const Path &searchPath) {
+1 -3
View File
@@ -35,7 +35,6 @@ namespace http {
}
struct UrlEncoder;
struct ConfigPrivate;
class Section;
@@ -670,7 +669,6 @@ private:
bool reload_ = false;
std::string gameId_;
std::string gameIdTitle_;
std::vector<std::string> recentIsos;
std::map<std::string, std::pair<std::string, int>, std::less<>> langValuesMapping_;
PlayTimeTracker playTimeTracker_;
Path iniFilename_;
@@ -679,10 +677,10 @@ private:
Path appendedConfigFileName_;
// A set make more sense, but won't have many entry, and I dont want to include the whole std::set header here
std::vector<std::string> appendedConfigUpdatedGames_;
ConfigPrivate *private_ = nullptr;
};
std::string CreateRandMAC();
bool TryUpdateSavedPath(Path *path);
// TODO: Find a better place for this.
extern http::RequestManager g_DownloadManager;
+2
View File
@@ -1120,6 +1120,7 @@
<InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">AnySuitable</InlineFunctionExpansion>
<InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">AnySuitable</InlineFunctionExpansion>
</ClCompile>
<ClCompile Include="Util\RecentFiles.cpp" />
<ClCompile Include="WaveFile.cpp" />
<ClCompile Include="WebServer.cpp" />
<ClCompile Include="MIPS\x86\X64IRRegCache.cpp" />
@@ -1478,6 +1479,7 @@
<ClInclude Include="Util\PortManager.h" />
<ClInclude Include="Util\PPGeDraw.h" />
<ClInclude Include="..\ext\xxhash.h" />
<ClInclude Include="Util\RecentFiles.h" />
<ClInclude Include="WaveFile.h" />
<ClInclude Include="WebServer.h" />
<ClInclude Include="MIPS\x86\X64IRRegCache.h" />
+6
View File
@@ -1339,6 +1339,9 @@
<ClCompile Include="Util\AtracTrack.cpp">
<Filter>Util</Filter>
</ClCompile>
<ClCompile Include="Util\RecentFiles.cpp">
<Filter>Util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ELF\ElfReader.h">
@@ -2160,6 +2163,9 @@
<ClInclude Include="Util\AtracTrack.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="Util\RecentFiles.h">
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\LICENSE.TXT" />
+155
View File
@@ -0,0 +1,155 @@
#include <mutex>
#include <algorithm>
#include <condition_variable>
#include "Common/File/FileUtil.h"
#include "Common/Thread/ThreadUtil.h"
#include "Common/Log.h"
#include "Common/TimeUtil.h"
#include "Core/Loaders.h"
#include "Core/Util/RecentFiles.h"
#include "Core/Config.h"
// Not in Config.h because it's #included a lot.
std::mutex recentIsosLock;
std::mutex recentIsosThreadLock;
std::thread recentIsosThread;
bool recentIsosThreadPending = false;
std::vector<std::string> recentIsos;
std::vector<std::string> GetRecentIsos() {
std::lock_guard<std::mutex> guard(recentIsosLock);
return recentIsos;
}
bool HasRecentIsos() {
std::lock_guard<std::mutex> guard(recentIsosLock);
return !recentIsos.empty();
}
void ClearRecentIsos() {
ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(recentIsosLock);
recentIsos.clear();
}
void ResetRecentIsosThread() {
std::lock_guard<std::mutex> guard(recentIsosThreadLock);
if (recentIsosThreadPending && recentIsosThread.joinable())
recentIsosThread.join();
}
void SetRecentIsosThread(std::function<void()> f) {
std::lock_guard<std::mutex> guard(recentIsosThreadLock);
if (recentIsosThreadPending && recentIsosThread.joinable())
recentIsosThread.join();
recentIsosThread = std::thread(f);
recentIsosThreadPending = true;
}
void LoadRecentIsos(const Section *recent, int maxRecent) {
ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(recentIsosLock);
recentIsos.clear();
for (int i = 0; i < maxRecent; i++) {
char keyName[64];
std::string fileName;
snprintf(keyName, sizeof(keyName), "FileName%d", i);
if (recent->Get(keyName, &fileName, "") && !fileName.empty()) {
recentIsos.push_back(fileName);
}
}
}
void SaveRecentIsos(Section *recent, int maxRecent) {
ResetRecentIsosThread();
for (int i = 0; i < maxRecent; i++) {
char keyName[64];
snprintf(keyName, sizeof(keyName), "FileName%d", i);
std::lock_guard<std::mutex> guard(recentIsosLock);
if (i < (int)recentIsos.size()) {
recent->Set(keyName, recentIsos[i]);
} else {
recent->Delete(keyName); // delete the nonexisting FileName
}
}
}
void AddRecentResolved(const std::string &resolvedFilename, int maxRecent) {
RemoveRecentResolved(resolvedFilename);
ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(recentIsosLock);
recentIsos.insert(recentIsos.begin(), resolvedFilename);
if ((int)recentIsos.size() > maxRecent)
recentIsos.resize(maxRecent);
}
void RemoveRecentResolved(const std::string &resolvedFilename) {
ResetRecentIsosThread();
std::lock_guard<std::mutex> guard(recentIsosLock);
auto iter = std::remove_if(recentIsos.begin(), recentIsos.end(), [resolvedFilename](const auto &str) {
const std::string recent = File::ResolvePath(str);
return resolvedFilename == recent;
});
// remove_if is weird.
recentIsos.erase(iter, recentIsos.end());
}
void CleanRecentIsos() {
SetRecentIsosThread([] {
SetCurrentThreadName("RecentISOs");
AndroidJNIThreadContext jniContext; // destructor detaches
double startTime = time_now_d();
std::lock_guard<std::mutex> guard(recentIsosLock);
std::vector<std::string> cleanedRecent;
if (recentIsos.empty()) {
INFO_LOG(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(Log::Loader, "Exists=%d when checking updated path: %s", exists, path.c_str());
}
}
break;
default:
FileLoader *loader = ConstructFileLoader(path);
exists = loader->ExistsFast();
delete loader;
break;
}
if (exists) {
std::string pathStr = path.ToString();
// Make sure we don't have any redundant items.
auto duplicate = std::find(cleanedRecent.begin(), cleanedRecent.end(), pathStr);
if (duplicate == cleanedRecent.end()) {
cleanedRecent.push_back(pathStr);
}
} else {
DEBUG_LOG(Log::Loader, "Removed %s from recent. errno=%d", path.c_str(), errno);
}
}
double recentTime = time_now_d() - startTime;
if (recentTime > 0.1) {
INFO_LOG(Log::System, "CleanRecent took %0.2f", recentTime);
}
recentIsos = cleanedRecent;
});
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <functional>
#include <vector>
#include <string>
#include "Common/Data/Format/IniFile.h"
void ResetRecentIsosThread();
void SetRecentIsosThread(std::function<void()> f);
void LoadRecentIsos(const Section *recent, int maxRecent);
void SaveRecentIsos(Section *recent, int maxRecent);
void AddRecentResolved(const std::string &resolvedFilename, int maxRecent);
void RemoveRecentResolved(const std::string &resolvedFilename);
void CleanRecentIsos();
std::vector<std::string> GetRecentIsos();
bool HasRecentIsos();
void ClearRecentIsos();
+3 -1
View File
@@ -335,6 +335,7 @@
<ClInclude Include="..\..\Core\Util\GameDB.h" />
<ClInclude Include="..\..\Core\Util\MemStick.h" />
<ClInclude Include="..\..\Core\Util\PortManager.h" />
<ClInclude Include="..\..\Core\Util\RecentFiles.h" />
<ClInclude Include="..\..\Core\WebServer.h" />
<ClInclude Include="..\..\Core\Util\AudioFormat.h" />
<ClInclude Include="..\..\Core\Util\BlockAllocator.h" />
@@ -633,6 +634,7 @@
<ClCompile Include="..\..\Core\Util\GameDB.cpp" />
<ClCompile Include="..\..\Core\Util\MemStick.cpp" />
<ClCompile Include="..\..\Core\Util\PortManager.cpp" />
<ClCompile Include="..\..\Core\Util\RecentFiles.cpp" />
<ClCompile Include="..\..\Core\WebServer.cpp" />
<ClCompile Include="..\..\Core\Util\AudioFormat.cpp" />
<ClCompile Include="..\..\Core\Util\BlockAllocator.cpp" />
@@ -1038,4 +1040,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
+7 -1
View File
@@ -1234,6 +1234,9 @@
<ClCompile Include="..\..\Core\Util\AtracTrack.cpp">
<Filter>Util</Filter>
</ClCompile>
<ClCompile Include="..\..\Core\Util\RecentFiles.cpp">
<Filter>Util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
@@ -1952,10 +1955,13 @@
<ClInclude Include="..\..\Core\Util\AtracTrack.h">
<Filter>Util</Filter>
</ClInclude>
<ClInclude Include="..\..\Core\Util\RecentFiles.h">
<Filter>Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\ext\gason\LICENSE">
<Filter>Ext\gason</Filter>
</None>
</ItemGroup>
</Project>
</Project>
+1
View File
@@ -759,6 +759,7 @@ EXEC_AND_LIB_FILES := \
$(SRC)/Core/Util/GameManager.cpp \
$(SRC)/Core/Util/BlockAllocator.cpp \
$(SRC)/Core/Util/PPGeDraw.cpp \
$(SRC)/Core/Util/RecentFiles.cpp \
$(SRC)/git-version.cpp
LOCAL_MODULE := ppsspp_core
+1
View File
@@ -828,6 +828,7 @@ SOURCES_CXX += \
$(COREDIR)/Util/BlockAllocator.cpp \
$(COREDIR)/Util/MemStick.cpp \
$(COREDIR)/Util/PPGeDraw.cpp \
$(COREDIR)/Util/RecentFiles.cpp \
$(COREDIR)/Util/AudioFormat.cpp \
$(COREDIR)/Util/PortManager.cpp \
$(CORE_DIR)/UI/GameInfoCache.cpp