From b62f31fd9696a496205326dc02f9d709f1724d74 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 24 Jan 2021 14:05:45 +1000 Subject: [PATCH] System: Use region from exe/psf when booting --- src/core/system.cpp | 40 +++++++++++++++++++++++++++++++++++++--- src/core/system.h | 2 ++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/core/system.cpp b/src/core/system.cpp index acdb6782a..3be755a64 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -479,8 +479,39 @@ DiscRegion GetRegionForImage(CDImage* cdi) return GetRegionForCode(code); } +DiscRegion GetRegionForExe(const char* path) +{ + auto fp = FileSystem::OpenManagedCFile(path, "rb"); + if (!fp) + return DiscRegion::Other; + + std::fseek(fp.get(), 0, SEEK_END); + const u32 file_size = static_cast(std::ftell(fp.get())); + std::fseek(fp.get(), 0, SEEK_SET); + + BIOS::PSEXEHeader header; + if (std::fread(&header, sizeof(header), 1, fp.get()) != 1) + return DiscRegion::Other; + + return BIOS::GetPSExeDiscRegion(header); +} + +DiscRegion GetRegionForPsf(const char* path) +{ + PSFLoader::File psf; + if (!psf.Load(path)) + return DiscRegion::Other; + + return psf.GetRegion(); +} + std::optional GetRegionForPath(const char* image_path) { + if (IsExeFileName(image_path)) + return GetRegionForExe(image_path); + else if (IsPsfFileName(image_path)) + return GetRegionForPsf(image_path); + std::unique_ptr cdi = CDImage::Open(image_path); if (!cdi) return {}; @@ -578,11 +609,12 @@ bool Boot(const SystemBootParameters& params) psf_boot = (!exe_boot && IsPsfFileName(params.filename.c_str())); if (exe_boot || psf_boot) { - // TODO: Pull region from PSF if (s_region == ConsoleRegion::Auto) { - Log_InfoPrintf("Defaulting to NTSC-U/C region for executable."); - s_region = ConsoleRegion::NTSC_U; + const DiscRegion file_region = + (exe_boot ? GetRegionForExe(params.filename.c_str()) : GetRegionForPsf(params.filename.c_str())); + Log_InfoPrintf("EXE/PSF Region: %s", Settings::GetDiscRegionDisplayName(file_region)); + s_region = GetConsoleRegionForDiscRegion(file_region); } } else @@ -651,6 +683,8 @@ bool Boot(const SystemBootParameters& params) s_region = ConsoleRegion::NTSC_U; } + Log_InfoPrintf("Console Region: %s", Settings::GetConsoleRegionDisplayName(s_region)); + // Load BIOS image. std::optional bios_image = g_host_interface->GetBIOSImage(s_region); if (!bios_image) diff --git a/src/core/system.h b/src/core/system.h index 6711a7e1e..710754c03 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -77,6 +77,8 @@ std::string GetGameCodeForPath(const char* image_path); DiscRegion GetRegionForCode(std::string_view code); DiscRegion GetRegionFromSystemArea(CDImage* cdi); DiscRegion GetRegionForImage(CDImage* cdi); +DiscRegion GetRegionForExe(const char* path); +DiscRegion GetRegionForPsf(const char* path); std::optional GetRegionForPath(const char* image_path); std::string_view GetTitleForPath(const char* path);