2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// BLOB
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// Blobs in Dolphin are read only Binary Large OBjects. For example, a typical DVD image.
|
|
|
|
// Often, you may want to store these things in a highly compressed format, but still
|
|
|
|
// allow random access. Or you may store them on an odd device, like raw on a DVD.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
// Always read your BLOBs using an interface returned by CreateBlobReader(). It will
|
2009-02-22 16:48:54 +00:00
|
|
|
// detect whether the file is a compressed blob, or just a big hunk of data, or a drive, and
|
2008-09-17 23:25:35 +00:00
|
|
|
// automatically do the right thing.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2015-12-08 00:53:42 +00:00
|
|
|
#include <array>
|
2020-06-26 16:35:09 +00:00
|
|
|
#include <functional>
|
2015-12-07 04:15:51 +00:00
|
|
|
#include <memory>
|
2017-06-04 08:33:14 +00:00
|
|
|
#include <optional>
|
2014-03-12 19:33:41 +00:00
|
|
|
#include <string>
|
2017-01-15 20:46:32 +00:00
|
|
|
#include <vector>
|
2017-03-03 19:43:52 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2008-07-12 17:40:22 +00:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2020-05-17 13:34:50 +00:00
|
|
|
enum class WIARVZCompressionType : u32;
|
2020-04-17 18:38:33 +00:00
|
|
|
|
2017-12-31 19:33:36 +00:00
|
|
|
// Increment CACHE_REVISION (GameFileCache.cpp) if the enum below is modified
|
2015-09-27 12:01:12 +00:00
|
|
|
enum class BlobType
|
|
|
|
{
|
|
|
|
PLAIN,
|
|
|
|
DRIVE,
|
|
|
|
DIRECTORY,
|
|
|
|
GCZ,
|
|
|
|
CISO,
|
2016-12-17 13:48:49 +00:00
|
|
|
WBFS,
|
2018-09-28 15:04:16 +00:00
|
|
|
TGC,
|
2020-05-04 10:41:14 +00:00
|
|
|
WIA,
|
|
|
|
RVZ,
|
2021-10-26 00:01:16 +00:00
|
|
|
MOD_DESCRIPTOR,
|
2022-07-31 08:14:03 +00:00
|
|
|
NFS,
|
2023-02-12 21:08:21 +00:00
|
|
|
SPLIT_PLAIN,
|
2015-09-27 12:01:12 +00:00
|
|
|
};
|
|
|
|
|
2022-08-01 09:53:30 +00:00
|
|
|
// If you convert an ISO file to another format and then call GetDataSize on it, what is the result?
|
|
|
|
enum class DataSizeType
|
|
|
|
{
|
|
|
|
// The result is the same as for the ISO.
|
|
|
|
Accurate,
|
|
|
|
// The result is not larger than for the ISO. (It's usually a little smaller than for the ISO.)
|
|
|
|
// Reads to offsets that are larger than the result will return some kind of "blank" data.
|
|
|
|
LowerBound,
|
|
|
|
// The result is not smaller than for the ISO. (It's usually much larger than for the ISO.)
|
|
|
|
UpperBound,
|
|
|
|
};
|
|
|
|
|
2020-06-21 18:41:50 +00:00
|
|
|
std::string GetName(BlobType blob_type, bool translate);
|
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
class BlobReader
|
2008-07-12 17:40:22 +00:00
|
|
|
{
|
2008-09-17 23:25:35 +00:00
|
|
|
public:
|
2017-06-06 09:49:01 +00:00
|
|
|
virtual ~BlobReader() {}
|
2020-04-04 18:56:20 +00:00
|
|
|
|
2015-09-27 12:01:12 +00:00
|
|
|
virtual BlobType GetBlobType() const = 0;
|
2023-07-07 12:31:26 +00:00
|
|
|
virtual std::unique_ptr<BlobReader> CopyReader() const = 0;
|
2020-04-04 18:56:20 +00:00
|
|
|
|
2015-09-26 13:24:29 +00:00
|
|
|
virtual u64 GetRawSize() const = 0;
|
2008-09-17 23:25:35 +00:00
|
|
|
virtual u64 GetDataSize() const = 0;
|
2022-08-01 09:53:30 +00:00
|
|
|
virtual DataSizeType GetDataSizeType() const = 0;
|
2017-05-19 19:23:00 +00:00
|
|
|
|
2020-04-04 18:56:20 +00:00
|
|
|
// Returns 0 if the format does not use blocks
|
2020-06-07 12:11:00 +00:00
|
|
|
virtual u64 GetBlockSize() const = 0;
|
|
|
|
virtual bool HasFastRandomAccessInBlock() const = 0;
|
2020-06-21 18:41:50 +00:00
|
|
|
virtual std::string GetCompressionMethod() const = 0;
|
2022-02-24 10:51:52 +00:00
|
|
|
virtual std::optional<int> GetCompressionLevel() const = 0;
|
2020-04-04 18:56:20 +00:00
|
|
|
|
2010-01-11 23:27:02 +00:00
|
|
|
// NOT thread-safe - can't call this from multiple threads.
|
2008-09-17 23:25:35 +00:00
|
|
|
virtual bool Read(u64 offset, u64 size, u8* out_ptr) = 0;
|
2017-05-19 19:23:00 +00:00
|
|
|
template <typename T>
|
2017-06-04 08:33:14 +00:00
|
|
|
std::optional<T> ReadSwapped(u64 offset)
|
2017-05-19 19:23:00 +00:00
|
|
|
{
|
|
|
|
T temp;
|
|
|
|
if (!Read(offset, sizeof(T), reinterpret_cast<u8*>(&temp)))
|
2019-12-27 18:17:56 +00:00
|
|
|
return std::nullopt;
|
2017-06-04 08:33:14 +00:00
|
|
|
return Common::FromBigEndian(temp);
|
2017-05-19 19:23:00 +00:00
|
|
|
}
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2020-08-29 13:12:02 +00:00
|
|
|
virtual bool SupportsReadWiiDecrypted(u64 offset, u64 size, u64 partition_data_offset) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-27 21:04:51 +00:00
|
|
|
virtual bool ReadWiiDecrypted(u64 offset, u64 size, u8* out_ptr, u64 partition_data_offset)
|
2017-06-07 12:30:55 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-17 23:25:35 +00:00
|
|
|
protected:
|
2017-06-06 09:49:01 +00:00
|
|
|
BlobReader() {}
|
2008-09-17 23:25:35 +00:00
|
|
|
};
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Provides caching and byte-operation-to-block-operations facilities.
|
|
|
|
// Used for compressed blob and direct drive reading.
|
|
|
|
// NOTE: GetDataSize() is expected to be evenly divisible by the sector size.
|
2017-06-06 09:49:01 +00:00
|
|
|
class SectorReader : public BlobReader
|
2008-09-17 23:25:35 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-04-26 11:24:08 +00:00
|
|
|
virtual ~SectorReader() = 0;
|
2009-02-22 16:48:54 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
bool Read(u64 offset, u64 size, u8* out_ptr) override;
|
2014-09-01 19:48:02 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void SetSectorSize(int blocksize);
|
2016-04-26 11:24:08 +00:00
|
|
|
int GetSectorSize() const { return m_block_size; }
|
|
|
|
// Set the chunk size -> the number of blocks to read at a time.
|
|
|
|
// Default value is 1 but that is too low for physical devices
|
|
|
|
// like CDROMs. Setting this to a higher value helps reduce seeking
|
|
|
|
// and IO overhead by batching reads. Do not set it too high either
|
|
|
|
// as large reads are slow and will take too long to resolve.
|
|
|
|
void SetChunkSize(int blocks);
|
|
|
|
int GetChunkSize() const { return m_chunk_blocks; }
|
|
|
|
// Read a single block/sector.
|
|
|
|
virtual bool GetBlock(u64 block_num, u8* out) = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Read multiple contiguous blocks.
|
|
|
|
// Default implementation just calls GetBlock in a loop, it should be
|
|
|
|
// overridden in derived classes where possible.
|
|
|
|
virtual bool ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8* out_ptr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Cache
|
|
|
|
{
|
|
|
|
std::vector<u8> data;
|
|
|
|
u64 block_idx = 0;
|
|
|
|
u32 num_blocks = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// [Pseudo-] Least Recently Used Shift Register
|
|
|
|
// When an empty cache line is needed, the line with the lowest value
|
|
|
|
// is taken and reset; the LRU register is then shifted down 1 place
|
|
|
|
// on all lines (low bit discarded). When a line is used, the high bit
|
|
|
|
// is set marking it as most recently used.
|
|
|
|
u32 lru_sreg = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
block_idx = 0;
|
|
|
|
num_blocks = 0;
|
|
|
|
lru_sreg = 0;
|
|
|
|
}
|
|
|
|
void Fill(u64 block, u32 count)
|
|
|
|
{
|
|
|
|
block_idx = block;
|
|
|
|
num_blocks = count;
|
|
|
|
// NOTE: Setting only the high bit means the newest line will
|
|
|
|
// be selected for eviction if every line in the cache was
|
|
|
|
// touched. This gives MRU behavior which is probably
|
|
|
|
// desirable in that case.
|
|
|
|
MarkUsed();
|
|
|
|
}
|
|
|
|
bool Contains(u64 block) const { return block >= block_idx && block - block_idx < num_blocks; }
|
|
|
|
void MarkUsed() { lru_sreg |= 0x80000000; }
|
|
|
|
void ShiftLRU() { lru_sreg >>= 1; }
|
|
|
|
bool IsLessRecentlyUsedThan(const Cache& other) const { return lru_sreg < other.lru_sreg; }
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Gets the cache line that contains the given block, or nullptr.
|
|
|
|
// NOTE: The cache record only lasts until it expires (next GetEmptyCacheLine)
|
|
|
|
const Cache* FindCacheLine(u64 block_num);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Finds the least recently used cache line, resets and returns it.
|
|
|
|
Cache* GetEmptyCacheLine();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Combines FindCacheLine with GetEmptyCacheLine and ReadChunk.
|
|
|
|
// Always returns a valid cache line (loading the data if needed).
|
|
|
|
// May return nullptr only if the cache missed and the read failed.
|
|
|
|
const Cache* GetCacheLine(u64 block_num);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
// Read all bytes from a chunk of blocks into a buffer.
|
|
|
|
// Returns the number of blocks read (may be less than m_chunk_blocks
|
|
|
|
// if chunk_num is the last chunk on the disk and the disk size is not
|
|
|
|
// evenly divisible into chunks). Returns zero if it fails.
|
|
|
|
u32 ReadChunk(u8* buffer, u64 chunk_num);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-26 11:24:08 +00:00
|
|
|
static constexpr int CACHE_LINES = 32;
|
|
|
|
u32 m_block_size = 0; // Bytes in a sector/block
|
|
|
|
u32 m_chunk_blocks = 1; // Number of sectors/blocks in a chunk
|
|
|
|
std::array<Cache, CACHE_LINES> m_cache;
|
2008-07-12 17:40:22 +00:00
|
|
|
};
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2017-06-06 09:49:01 +00:00
|
|
|
// Factory function - examines the path to choose the right type of BlobReader, and returns one.
|
|
|
|
std::unique_ptr<BlobReader> CreateBlobReader(const std::string& filename);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2020-06-26 16:35:09 +00:00
|
|
|
using CompressCB = std::function<bool(const std::string& text, float percent)>;
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2020-04-10 15:40:07 +00:00
|
|
|
bool ConvertToGCZ(BlobReader* infile, const std::string& infile_path,
|
2020-06-26 16:35:09 +00:00
|
|
|
const std::string& outfile_path, u32 sub_type, int sector_size,
|
|
|
|
CompressCB callback);
|
2020-04-10 15:40:07 +00:00
|
|
|
bool ConvertToPlain(BlobReader* infile, const std::string& infile_path,
|
2020-06-26 16:35:09 +00:00
|
|
|
const std::string& outfile_path, CompressCB callback);
|
2020-05-17 13:34:50 +00:00
|
|
|
bool ConvertToWIAOrRVZ(BlobReader* infile, const std::string& infile_path,
|
|
|
|
const std::string& outfile_path, bool rvz,
|
|
|
|
WIARVZCompressionType compression_type, int compression_level,
|
2020-06-26 16:35:09 +00:00
|
|
|
int chunk_size, CompressCB callback);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace DiscIO
|