System: Use region from exe/psf when booting

This commit is contained in:
Connor McLaughlin 2021-01-24 14:05:45 +10:00
parent 253b115b11
commit b62f31fd96
2 changed files with 39 additions and 3 deletions

View file

@ -479,8 +479,39 @@ DiscRegion GetRegionForImage(CDImage* cdi)
return GetRegionForCode(code); 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<u32>(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<DiscRegion> GetRegionForPath(const char* image_path) std::optional<DiscRegion> 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<CDImage> cdi = CDImage::Open(image_path); std::unique_ptr<CDImage> cdi = CDImage::Open(image_path);
if (!cdi) if (!cdi)
return {}; return {};
@ -578,11 +609,12 @@ bool Boot(const SystemBootParameters& params)
psf_boot = (!exe_boot && IsPsfFileName(params.filename.c_str())); psf_boot = (!exe_boot && IsPsfFileName(params.filename.c_str()));
if (exe_boot || psf_boot) if (exe_boot || psf_boot)
{ {
// TODO: Pull region from PSF
if (s_region == ConsoleRegion::Auto) if (s_region == ConsoleRegion::Auto)
{ {
Log_InfoPrintf("Defaulting to NTSC-U/C region for executable."); const DiscRegion file_region =
s_region = ConsoleRegion::NTSC_U; (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 else
@ -651,6 +683,8 @@ bool Boot(const SystemBootParameters& params)
s_region = ConsoleRegion::NTSC_U; s_region = ConsoleRegion::NTSC_U;
} }
Log_InfoPrintf("Console Region: %s", Settings::GetConsoleRegionDisplayName(s_region));
// Load BIOS image. // Load BIOS image.
std::optional<BIOS::Image> bios_image = g_host_interface->GetBIOSImage(s_region); std::optional<BIOS::Image> bios_image = g_host_interface->GetBIOSImage(s_region);
if (!bios_image) if (!bios_image)

View file

@ -77,6 +77,8 @@ std::string GetGameCodeForPath(const char* image_path);
DiscRegion GetRegionForCode(std::string_view code); DiscRegion GetRegionForCode(std::string_view code);
DiscRegion GetRegionFromSystemArea(CDImage* cdi); DiscRegion GetRegionFromSystemArea(CDImage* cdi);
DiscRegion GetRegionForImage(CDImage* cdi); DiscRegion GetRegionForImage(CDImage* cdi);
DiscRegion GetRegionForExe(const char* path);
DiscRegion GetRegionForPsf(const char* path);
std::optional<DiscRegion> GetRegionForPath(const char* image_path); std::optional<DiscRegion> GetRegionForPath(const char* image_path);
std::string_view GetTitleForPath(const char* path); std::string_view GetTitleForPath(const char* path);