diff --git a/Source/Core/DiscIO/CMakeLists.txt b/Source/Core/DiscIO/CMakeLists.txt index 54e437c521..1729f0d959 100644 --- a/Source/Core/DiscIO/CMakeLists.txt +++ b/Source/Core/DiscIO/CMakeLists.txt @@ -11,6 +11,8 @@ add_library(discio DiscExtractor.h DiscScrubber.cpp DiscScrubber.h + DiscUtils.cpp + DiscUtils.h DriveBlob.cpp DriveBlob.h Enums.cpp diff --git a/Source/Core/DiscIO/DiscExtractor.cpp b/Source/Core/DiscIO/DiscExtractor.cpp index 8aff525cec..5cc665f7d9 100644 --- a/Source/Core/DiscIO/DiscExtractor.cpp +++ b/Source/Core/DiscIO/DiscExtractor.cpp @@ -5,50 +5,21 @@ #include "DiscIO/DiscExtractor.h" #include -#include +#include #include - -#include +#include +#include #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Common/IOFile.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/Enums.h" #include "DiscIO/Filesystem.h" #include "DiscIO/Volume.h" namespace DiscIO { -std::string NameForPartitionType(u32 partition_type, bool include_prefix) -{ - switch (partition_type) - { - case PARTITION_DATA: - return "DATA"; - case PARTITION_UPDATE: - return "UPDATE"; - case PARTITION_CHANNEL: - return "CHANNEL"; - case PARTITION_INSTALL: - // wit doesn't recognize the name "INSTALL", so we can't use it when naming partition folders - if (!include_prefix) - return "INSTALL"; - [[fallthrough]]; - default: - const std::string type_as_game_id{static_cast((partition_type >> 24) & 0xFF), - static_cast((partition_type >> 16) & 0xFF), - static_cast((partition_type >> 8) & 0xFF), - static_cast(partition_type & 0xFF)}; - if (std::all_of(type_as_game_id.cbegin(), type_as_game_id.cend(), - [](char c) { return std::isalnum(c, std::locale::classic()); })) - { - return include_prefix ? "P-" + type_as_game_id : type_as_game_id; - } - - return fmt::format(include_prefix ? "P{}" : "{}", partition_type); - } -} - u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, u8* buffer, u64 max_buffer_size, u64 offset_in_file) { @@ -248,17 +219,6 @@ bool ExportBI2Data(const Volume& volume, const Partition& partition, return ExportData(volume, partition, 0x440, 0x2000, export_filename); } -std::optional GetApploaderSize(const Volume& volume, const Partition& partition) -{ - constexpr u64 header_size = 0x20; - const std::optional apploader_size = volume.ReadSwapped(0x2440 + 0x14, partition); - const std::optional trailer_size = volume.ReadSwapped(0x2440 + 0x18, partition); - if (!apploader_size || !trailer_size) - return std::nullopt; - - return header_size + *apploader_size + *trailer_size; -} - bool ExportApploader(const Volume& volume, const Partition& partition, const std::string& export_filename) { @@ -272,51 +232,6 @@ bool ExportApploader(const Volume& volume, const Partition& partition, return ExportData(volume, partition, 0x2440, *apploader_size, export_filename); } -std::optional GetBootDOLOffset(const Volume& volume, const Partition& partition) -{ - const Platform volume_type = volume.GetVolumeType(); - if (!IsDisc(volume_type)) - return std::nullopt; - - std::optional dol_offset = volume.ReadSwappedAndShifted(0x420, partition); - - // Datel AR disc has 0x00000000 as the offset (invalid) and doesn't use it in the AppLoader. - if (dol_offset && *dol_offset == 0) - dol_offset.reset(); - - return dol_offset; -} - -std::optional GetBootDOLSize(const Volume& volume, const Partition& partition, u64 dol_offset) -{ - if (!IsDisc(volume.GetVolumeType())) - return std::nullopt; - - u32 dol_size = 0; - - // Iterate through the 7 code segments - for (u8 i = 0; i < 7; i++) - { - const std::optional offset = volume.ReadSwapped(dol_offset + 0x00 + i * 4, partition); - const std::optional size = volume.ReadSwapped(dol_offset + 0x90 + i * 4, partition); - if (!offset || !size) - return {}; - dol_size = std::max(*offset + *size, dol_size); - } - - // Iterate through the 11 data segments - for (u8 i = 0; i < 11; i++) - { - const std::optional offset = volume.ReadSwapped(dol_offset + 0x1c + i * 4, partition); - const std::optional size = volume.ReadSwapped(dol_offset + 0xac + i * 4, partition); - if (!offset || !size) - return {}; - dol_size = std::max(*offset + *size, dol_size); - } - - return dol_size; -} - bool ExportDOL(const Volume& volume, const Partition& partition, const std::string& export_filename) { if (!IsDisc(volume.GetVolumeType())) @@ -332,24 +247,6 @@ bool ExportDOL(const Volume& volume, const Partition& partition, const std::stri return ExportData(volume, partition, *dol_offset, *dol_size, export_filename); } -std::optional GetFSTOffset(const Volume& volume, const Partition& partition) -{ - const Platform volume_type = volume.GetVolumeType(); - if (!IsDisc(volume_type)) - return std::nullopt; - - return volume.ReadSwappedAndShifted(0x424, partition); -} - -std::optional GetFSTSize(const Volume& volume, const Partition& partition) -{ - const Platform volume_type = volume.GetVolumeType(); - if (!IsDisc(volume_type)) - return std::nullopt; - - return volume.ReadSwappedAndShifted(0x428, partition); -} - bool ExportFST(const Volume& volume, const Partition& partition, const std::string& export_filename) { if (!IsDisc(volume.GetVolumeType())) diff --git a/Source/Core/DiscIO/DiscExtractor.h b/Source/Core/DiscIO/DiscExtractor.h index c7e18f83d6..07ca88adf6 100644 --- a/Source/Core/DiscIO/DiscExtractor.h +++ b/Source/Core/DiscIO/DiscExtractor.h @@ -17,13 +17,6 @@ class FileInfo; struct Partition; class Volume; -constexpr u32 PARTITION_DATA = 0; -constexpr u32 PARTITION_UPDATE = 1; -constexpr u32 PARTITION_CHANNEL = 2; // Mario Kart Wii, Wii Fit, Wii Fit Plus, Rabbids Go Home -constexpr u32 PARTITION_INSTALL = 3; // Dragon Quest X only - -std::string NameForPartitionType(u32 partition_type, bool include_prefix); - u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, u8* buffer, u64 max_buffer_size, u64 offset_in_file = 0); u64 ReadFile(const Volume& volume, const Partition& partition, std::string_view path, u8* buffer, @@ -61,15 +54,10 @@ bool ExportHeader(const Volume& volume, const Partition& partition, const std::string& export_filename); bool ExportBI2Data(const Volume& volume, const Partition& partition, const std::string& export_filename); -std::optional GetApploaderSize(const Volume& volume, const Partition& partition); bool ExportApploader(const Volume& volume, const Partition& partition, const std::string& export_filename); -std::optional GetBootDOLOffset(const Volume& volume, const Partition& partition); -std::optional GetBootDOLSize(const Volume& volume, const Partition& partition, u64 dol_offset); bool ExportDOL(const Volume& volume, const Partition& partition, const std::string& export_filename); -std::optional GetFSTOffset(const Volume& volume, const Partition& partition); -std::optional GetFSTSize(const Volume& volume, const Partition& partition); bool ExportFST(const Volume& volume, const Partition& partition, const std::string& export_filename); diff --git a/Source/Core/DiscIO/DiscScrubber.cpp b/Source/Core/DiscIO/DiscScrubber.cpp index bd39e8bb7a..7ed42f870a 100644 --- a/Source/Core/DiscIO/DiscScrubber.cpp +++ b/Source/Core/DiscIO/DiscScrubber.cpp @@ -16,7 +16,7 @@ #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" -#include "DiscIO/DiscExtractor.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/Filesystem.h" #include "DiscIO/Volume.h" diff --git a/Source/Core/DiscIO/DiscUtils.cpp b/Source/Core/DiscIO/DiscUtils.cpp new file mode 100644 index 0000000000..1919a1d6e5 --- /dev/null +++ b/Source/Core/DiscIO/DiscUtils.cpp @@ -0,0 +1,123 @@ +// Copyright 2021 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "DiscIO/DiscUtils.h" + +#include +#include +#include +#include + +#include + +#include "Common/CommonTypes.h" +#include "DiscIO/Volume.h" + +namespace DiscIO +{ +std::string NameForPartitionType(u32 partition_type, bool include_prefix) +{ + switch (partition_type) + { + case PARTITION_DATA: + return "DATA"; + case PARTITION_UPDATE: + return "UPDATE"; + case PARTITION_CHANNEL: + return "CHANNEL"; + case PARTITION_INSTALL: + // wit doesn't recognize the name "INSTALL", so we can't use it when naming partition folders + if (!include_prefix) + return "INSTALL"; + [[fallthrough]]; + default: + const std::string type_as_game_id{static_cast((partition_type >> 24) & 0xFF), + static_cast((partition_type >> 16) & 0xFF), + static_cast((partition_type >> 8) & 0xFF), + static_cast(partition_type & 0xFF)}; + if (std::all_of(type_as_game_id.cbegin(), type_as_game_id.cend(), + [](char c) { return std::isalnum(c, std::locale::classic()); })) + { + return include_prefix ? "P-" + type_as_game_id : type_as_game_id; + } + + return fmt::format(include_prefix ? "P{}" : "{}", partition_type); + } +} + +std::optional GetApploaderSize(const Volume& volume, const Partition& partition) +{ + constexpr u64 header_size = 0x20; + const std::optional apploader_size = volume.ReadSwapped(0x2440 + 0x14, partition); + const std::optional trailer_size = volume.ReadSwapped(0x2440 + 0x18, partition); + if (!apploader_size || !trailer_size) + return std::nullopt; + + return header_size + *apploader_size + *trailer_size; +} + +std::optional GetBootDOLOffset(const Volume& volume, const Partition& partition) +{ + const Platform volume_type = volume.GetVolumeType(); + if (!IsDisc(volume_type)) + return std::nullopt; + + std::optional dol_offset = volume.ReadSwappedAndShifted(0x420, partition); + + // Datel AR disc has 0x00000000 as the offset (invalid) and doesn't use it in the AppLoader. + if (dol_offset && *dol_offset == 0) + dol_offset.reset(); + + return dol_offset; +} + +std::optional GetBootDOLSize(const Volume& volume, const Partition& partition, u64 dol_offset) +{ + if (!IsDisc(volume.GetVolumeType())) + return std::nullopt; + + u32 dol_size = 0; + + // Iterate through the 7 code segments + for (size_t i = 0; i < 7; i++) + { + const std::optional offset = volume.ReadSwapped(dol_offset + 0x00 + i * 4, partition); + const std::optional size = volume.ReadSwapped(dol_offset + 0x90 + i * 4, partition); + if (!offset || !size) + return {}; + dol_size = std::max(*offset + *size, dol_size); + } + + // Iterate through the 11 data segments + for (size_t i = 0; i < 11; i++) + { + const std::optional offset = volume.ReadSwapped(dol_offset + 0x1c + i * 4, partition); + const std::optional size = volume.ReadSwapped(dol_offset + 0xac + i * 4, partition); + if (!offset || !size) + return {}; + dol_size = std::max(*offset + *size, dol_size); + } + + return dol_size; +} + +std::optional GetFSTOffset(const Volume& volume, const Partition& partition) +{ + const Platform volume_type = volume.GetVolumeType(); + if (!IsDisc(volume_type)) + return std::nullopt; + + return volume.ReadSwappedAndShifted(0x424, partition); +} + +std::optional GetFSTSize(const Volume& volume, const Partition& partition) +{ + const Platform volume_type = volume.GetVolumeType(); + if (!IsDisc(volume_type)) + return std::nullopt; + + return volume.ReadSwappedAndShifted(0x428, partition); +} + +} // namespace DiscIO diff --git a/Source/Core/DiscIO/DiscUtils.h b/Source/Core/DiscIO/DiscUtils.h new file mode 100644 index 0000000000..3a86ff8b0c --- /dev/null +++ b/Source/Core/DiscIO/DiscUtils.h @@ -0,0 +1,29 @@ +// Copyright 2021 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include "Common/CommonTypes.h" + +namespace DiscIO +{ +struct Partition; +class Volume; + +constexpr u32 PARTITION_DATA = 0; +constexpr u32 PARTITION_UPDATE = 1; +constexpr u32 PARTITION_CHANNEL = 2; // Mario Kart Wii, Wii Fit, Wii Fit Plus, Rabbids Go Home +constexpr u32 PARTITION_INSTALL = 3; // Dragon Quest X only + +std::string NameForPartitionType(u32 partition_type, bool include_prefix); + +std::optional GetApploaderSize(const Volume& volume, const Partition& partition); +std::optional GetBootDOLOffset(const Volume& volume, const Partition& partition); +std::optional GetBootDOLSize(const Volume& volume, const Partition& partition, u64 dol_offset); +std::optional GetFSTOffset(const Volume& volume, const Partition& partition); +std::optional GetFSTSize(const Volume& volume, const Partition& partition); +} diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index c706e809a8..cc48fa8f85 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -19,7 +19,7 @@ #include "Common/Logging/Log.h" #include "Common/MsgHandler.h" #include "Common/StringUtil.h" -#include "DiscIO/DiscExtractor.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/FileSystemGCWii.h" #include "DiscIO/Filesystem.h" #include "DiscIO/VolumeDisc.h" diff --git a/Source/Core/DiscIO/VolumeDisc.cpp b/Source/Core/DiscIO/VolumeDisc.cpp index 3ccea4b62f..ec23922274 100644 --- a/Source/Core/DiscIO/VolumeDisc.cpp +++ b/Source/Core/DiscIO/VolumeDisc.cpp @@ -12,7 +12,7 @@ #include #include "Common/CommonTypes.h" -#include "DiscIO/DiscExtractor.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/Enums.h" #include "DiscIO/Filesystem.h" diff --git a/Source/Core/DiscIO/VolumeGC.cpp b/Source/Core/DiscIO/VolumeGC.cpp index e8bab67771..d47f9c5735 100644 --- a/Source/Core/DiscIO/VolumeGC.cpp +++ b/Source/Core/DiscIO/VolumeGC.cpp @@ -23,6 +23,7 @@ #include "DiscIO/Blob.h" #include "DiscIO/DiscExtractor.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/Enums.h" #include "DiscIO/FileSystemGCWii.h" #include "DiscIO/Filesystem.h" diff --git a/Source/Core/DiscIO/VolumeVerifier.cpp b/Source/Core/DiscIO/VolumeVerifier.cpp index 21ac8f5d39..c5fdb0cf91 100644 --- a/Source/Core/DiscIO/VolumeVerifier.cpp +++ b/Source/Core/DiscIO/VolumeVerifier.cpp @@ -41,8 +41,8 @@ #include "Core/IOS/IOS.h" #include "Core/IOS/IOSC.h" #include "DiscIO/Blob.h" -#include "DiscIO/DiscExtractor.h" #include "DiscIO/DiscScrubber.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/Enums.h" #include "DiscIO/Filesystem.h" #include "DiscIO/Volume.h" diff --git a/Source/Core/DiscIO/WIABlob.cpp b/Source/Core/DiscIO/WIABlob.cpp index 8a76b9bd77..07894fa25a 100644 --- a/Source/Core/DiscIO/WIABlob.cpp +++ b/Source/Core/DiscIO/WIABlob.cpp @@ -30,7 +30,7 @@ #include "Common/Swap.h" #include "DiscIO/Blob.h" -#include "DiscIO/DiscExtractor.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/Filesystem.h" #include "DiscIO/LaggedFibonacciGenerator.h" #include "DiscIO/MultithreadedCompressor.h" diff --git a/Source/Core/DolphinLib.props b/Source/Core/DolphinLib.props index 638e6734e2..9d89aee8b9 100644 --- a/Source/Core/DolphinLib.props +++ b/Source/Core/DolphinLib.props @@ -417,6 +417,7 @@ + @@ -991,6 +992,7 @@ + diff --git a/Source/Core/DolphinQt/Config/FilesystemWidget.cpp b/Source/Core/DolphinQt/Config/FilesystemWidget.cpp index ddffe76963..09ab45a6c4 100644 --- a/Source/Core/DolphinQt/Config/FilesystemWidget.cpp +++ b/Source/Core/DolphinQt/Config/FilesystemWidget.cpp @@ -18,6 +18,7 @@ #include #include "DiscIO/DiscExtractor.h" +#include "DiscIO/DiscUtils.h" #include "DiscIO/Filesystem.h" #include "DiscIO/Volume.h"