Skip to content

Commit

Permalink
UI: Add threadsafety to cache item file loaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Dec 10, 2017
1 parent 8b665ae commit 472ee12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
28 changes: 12 additions & 16 deletions UI/GameInfoCache.cpp
Expand Up @@ -54,7 +54,7 @@ GameInfo::~GameInfo() {
icon.Clear(); icon.Clear();
pic0.Clear(); pic0.Clear();
pic1.Clear(); pic1.Clear();
delete fileLoader; fileLoader.reset();
} }


bool GameInfo::Delete() { bool GameInfo::Delete() {
Expand Down Expand Up @@ -218,8 +218,7 @@ bool GameInfo::LoadFromPath(const std::string &gamePath) {
std::lock_guard<std::mutex> guard(lock); std::lock_guard<std::mutex> guard(lock);
// No need to rebuild if we already have it loaded. // No need to rebuild if we already have it loaded.
if (filePath_ != gamePath) { if (filePath_ != gamePath) {
delete fileLoader; fileLoader.reset(ConstructFileLoader(gamePath));
fileLoader = ConstructFileLoader(gamePath);
if (!fileLoader) if (!fileLoader)
return false; return false;
filePath_ = gamePath; filePath_ = gamePath;
Expand All @@ -231,16 +230,15 @@ bool GameInfo::LoadFromPath(const std::string &gamePath) {
return fileLoader ? fileLoader->Exists() : true; return fileLoader ? fileLoader->Exists() : true;
} }


FileLoader *GameInfo::GetFileLoader() { std::shared_ptr<FileLoader> GameInfo::GetFileLoader() {
if (!fileLoader) { if (!fileLoader) {
fileLoader = ConstructFileLoader(filePath_); fileLoader.reset(ConstructFileLoader(filePath_));
} }
return fileLoader; return fileLoader;
} }


void GameInfo::DisposeFileLoader() { void GameInfo::DisposeFileLoader() {
delete fileLoader; fileLoader.reset();
fileLoader = nullptr;
} }


bool GameInfo::DeleteAllSaveData() { bool GameInfo::DeleteAllSaveData() {
Expand Down Expand Up @@ -373,24 +371,22 @@ class GameInfoWorkItem : public PrioritizedWorkQueueItem {
{ {
std::lock_guard<std::mutex> lock(info_->lock); std::lock_guard<std::mutex> lock(info_->lock);
info_->working = true; info_->working = true;
info_->fileType = Identify_File(info_->GetFileLoader()); info_->fileType = Identify_File(info_->GetFileLoader().get());
} }


switch (info_->fileType) { switch (info_->fileType) {
case IdentifiedFileType::PSP_PBP: case IdentifiedFileType::PSP_PBP:
case IdentifiedFileType::PSP_PBP_DIRECTORY: case IdentifiedFileType::PSP_PBP_DIRECTORY:
{ {
FileLoader *pbpLoader = info_->GetFileLoader(); auto pbpLoader = info_->GetFileLoader();
std::unique_ptr<FileLoader> altLoader;
if (info_->fileType == IdentifiedFileType::PSP_PBP_DIRECTORY) { if (info_->fileType == IdentifiedFileType::PSP_PBP_DIRECTORY) {
std::string ebootPath = ResolvePBPFile(gamePath_); std::string ebootPath = ResolvePBPFile(gamePath_);
if (ebootPath != gamePath_) { if (ebootPath != gamePath_) {
pbpLoader = ConstructFileLoader(ebootPath); pbpLoader.reset(ConstructFileLoader(ebootPath));
altLoader.reset(pbpLoader);
} }
} }


PBPReader pbp(pbpLoader); PBPReader pbp(pbpLoader.get());
if (!pbp.IsValid()) { if (!pbp.IsValid()) {
if (pbp.IsELF()) { if (pbp.IsELF()) {
goto handleELF; goto handleELF;
Expand Down Expand Up @@ -559,10 +555,10 @@ class GameInfoWorkItem : public PrioritizedWorkQueueItem {
// Let's assume it's an ISO. // Let's assume it's an ISO.
// TODO: This will currently read in the whole directory tree. Not really necessary for just a // TODO: This will currently read in the whole directory tree. Not really necessary for just a
// few files. // few files.
FileLoader *fl = info_->GetFileLoader(); auto fl = info_->GetFileLoader();
if (!fl) if (!fl)
return; // Happens with UWP currently, TODO... return; // Happens with UWP currently, TODO...
BlockDevice *bd = constructBlockDevice(info_->GetFileLoader()); BlockDevice *bd = constructBlockDevice(info_->GetFileLoader().get());
if (!bd) if (!bd)
return; // nothing to do here.. return; // nothing to do here..
ISOFileSystem umd(&handles, bd); ISOFileSystem umd(&handles, bd);
Expand Down Expand Up @@ -700,7 +696,7 @@ void GameInfoCache::Clear() {


void GameInfoCache::CancelAll() { void GameInfoCache::CancelAll() {
for (auto info : info_) { for (auto info : info_) {
FileLoader *fl = info.second->GetFileLoader(); auto fl = info.second->GetFileLoader();
if (fl) { if (fl) {
fl->Cancel(); fl->Cancel();
} }
Expand Down
5 changes: 3 additions & 2 deletions UI/GameInfoCache.h
Expand Up @@ -19,6 +19,7 @@


#include <string> #include <string>
#include <map> #include <map>
#include <memory>
#include <mutex> #include <mutex>
#include <atomic> #include <atomic>


Expand Down Expand Up @@ -93,7 +94,7 @@ class GameInfo {
bool DeleteAllSaveData(); bool DeleteAllSaveData();
bool LoadFromPath(const std::string &gamePath); bool LoadFromPath(const std::string &gamePath);


FileLoader *GetFileLoader(); std::shared_ptr<FileLoader> GetFileLoader();
void DisposeFileLoader(); void DisposeFileLoader();


u64 GetGameSizeInBytes(); u64 GetGameSizeInBytes();
Expand Down Expand Up @@ -148,7 +149,7 @@ class GameInfo {
// Note: this can change while loading, use GetTitle(). // Note: this can change while loading, use GetTitle().
std::string title; std::string title;


FileLoader *fileLoader = nullptr; std::shared_ptr<FileLoader> fileLoader;
std::string filePath_; std::string filePath_;


private: private:
Expand Down

0 comments on commit 472ee12

Please sign in to comment.