Skip to content

Commit

Permalink
GameList: Avoid double fopen/read of EXEs
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Oct 23, 2024
1 parent 2d04f2e commit 7d2216c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/core/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1187,8 +1187,11 @@ bool Bus::SideloadEXE(const std::string& path, Error* error)
return false;
}

// Stupid Android...
std::string filename = FileSystem::GetDisplayNameFromPath(path);

bool okay = true;
if (StringUtil::EndsWithNoCase(path, ".cpe"))
if (StringUtil::EndsWithNoCase(filename, ".cpe"))
{
okay = InjectCPE(exe_data->cspan(), true, error);
}
Expand Down
16 changes: 13 additions & 3 deletions src/core/game_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,13 @@ bool GameList::GetExeListEntry(const std::string& path, GameList::Entry* entry)
if (entry->file_size < 0)
return false;

entry->title = Path::GetFileTitle(FileSystem::GetDisplayNameFromPath(path));
// Stupid Android...
const std::string filename = FileSystem::GetDisplayNameFromPath(path);

entry->title = Path::GetFileTitle(filename);
entry->type = EntryType::PSExe;

if (StringUtil::EndsWithNoCase(path, ".cpe"))
if (StringUtil::EndsWithNoCase(filename, ".cpe"))
{
u32 magic;
if (std::fread(&magic, sizeof(magic), 1, fp.get()) != 1 || magic != BIOS::CPE_MAGIC)
Expand All @@ -207,7 +210,14 @@ bool GameList::GetExeListEntry(const std::string& path, GameList::Entry* entry)
entry->region = BIOS::GetPSExeDiscRegion(header);
}

const GameHash hash = System::GetGameHashFromFile(path.c_str());
const auto data = FileSystem::ReadBinaryFile(fp.get());
if (!data.has_value())
{
WARNING_LOG("Failed to read {}", path);
return false;
}

const GameHash hash = System::GetGameHashFromBuffer(filename, data->cspan());
entry->serial = hash ? System::GetGameHashId(hash) : std::string();
return true;
}
Expand Down
8 changes: 6 additions & 2 deletions src/core/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,12 @@ GameHash System::GetGameHashFromFile(const char* path)
if (!data)
return 0;

const std::string display_name = FileSystem::GetDisplayNameFromPath(path);
return GetGameHashFromBuffer(display_name, data->cspan(), IsoReader::ISOPrimaryVolumeDescriptor{}, 0);
return GetGameHashFromBuffer(FileSystem::GetDisplayNameFromPath(path), data->cspan());
}

GameHash System::GetGameHashFromBuffer(const std::string_view filename, const std::span<const u8> data)
{
return GetGameHashFromBuffer(filename, data, IsoReader::ISOPrimaryVolumeDescriptor{}, 0);
}

std::string System::GetExecutableNameForImage(IsoReader& iso, bool strip_subdirectories)
Expand Down
2 changes: 2 additions & 0 deletions src/core/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <memory>
#include <optional>
#include <span>
#include <string>

class ByteStream;
Expand Down Expand Up @@ -143,6 +144,7 @@ bool ReadExecutableFromImage(CDImage* cdi, std::string* out_executable_name, std
std::string GetGameHashId(GameHash hash);
bool GetGameDetailsFromImage(CDImage* cdi, std::string* out_id, GameHash* out_hash);
GameHash GetGameHashFromFile(const char* path);
GameHash GetGameHashFromBuffer(const std::string_view filename, const std::span<const u8> data);
DiscRegion GetRegionForSerial(const std::string_view serial);
DiscRegion GetRegionFromSystemArea(CDImage* cdi);
DiscRegion GetRegionForImage(CDImage* cdi);
Expand Down

0 comments on commit 7d2216c

Please sign in to comment.