mirror of
https://github.com/melonDS-emu/melonDS.git
synced 2024-11-14 13:27:41 -07:00
Support loading Zstandard-compressed ROMs
This is different from the archive support in that the compressed ROMs are standalone files, rather than archives, making it possible to use them exactly as if they were regular ROMs, while saving a bunch of space on disk. This is supported both for DS and GBA ROMs, though given GBA ROMs' generally small size it's mostly useful for the former.
This commit is contained in:
parent
3ada5b9bc8
commit
e7d2edd203
@ -39,6 +39,10 @@
|
||||
<string>srl</string>
|
||||
<string>dsi</string>
|
||||
<string>ids</string>
|
||||
<string>nds.zst</string>
|
||||
<string>srl.zst</string>
|
||||
<string>dsi.zst</string>
|
||||
<string>ids.zst</string>
|
||||
</array>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>Viewer</string>
|
||||
@ -50,6 +54,8 @@
|
||||
<array>
|
||||
<string>gba</string>
|
||||
<string>agb</string>
|
||||
<string>gba.zst</string>
|
||||
<string>agb.zst</string>
|
||||
</array>
|
||||
<key>CFBundleTypeRole</key>
|
||||
<string>Viewer</string>
|
||||
|
@ -83,6 +83,9 @@ pkg_check_modules(SDL2 REQUIRED IMPORTED_TARGET sdl2)
|
||||
pkg_check_modules(Slirp REQUIRED IMPORTED_TARGET slirp)
|
||||
pkg_check_modules(LibArchive REQUIRED IMPORTED_TARGET libarchive)
|
||||
|
||||
find_package(zstd CONFIG)
|
||||
cmake_dependent_option(ENABLE_ZSTD "Enable support for Zstandard-compressed ROMs" ON "zstd_FOUND" OFF)
|
||||
|
||||
fix_interface_includes(PkgConfig::SDL2 PkgConfig::Slirp PkgConfig::LibArchive)
|
||||
|
||||
add_compile_definitions(ARCHIVE_SUPPORT_ENABLED)
|
||||
@ -157,6 +160,15 @@ target_link_libraries(melonDS PRIVATE core)
|
||||
target_link_libraries(melonDS PRIVATE PkgConfig::SDL2 PkgConfig::Slirp PkgConfig::LibArchive)
|
||||
target_link_libraries(melonDS PRIVATE ${QT_LINK_LIBS} ${CMAKE_DL_LIBS})
|
||||
|
||||
if (ENABLE_ZSTD)
|
||||
target_compile_definitions(melonDS PRIVATE ZSTD_ENABLED)
|
||||
if (BUILD_STATIC)
|
||||
target_link_libraries(melonDS PRIVATE zstd::libzstd_static)
|
||||
else()
|
||||
target_link_libraries(melonDS PRIVATE zstd::libzstd_shared)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (UNIX)
|
||||
option(PORTABLE "Make a portable build that looks for its configuration in the current directory" OFF)
|
||||
elseif (WIN32)
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <zstd.h>
|
||||
|
||||
#ifdef ARCHIVE_SUPPORT_ENABLED
|
||||
#include "ArchiveUtil.h"
|
||||
@ -478,6 +479,27 @@ bool LoadBIOS()
|
||||
return true;
|
||||
}
|
||||
|
||||
u32 DecompressROM(const u8* inContent, const u32 inSize, u8** outContent)
|
||||
{
|
||||
u64 realSize = ZSTD_getFrameContentSize(inContent, inSize);
|
||||
|
||||
if (realSize == ZSTD_CONTENTSIZE_UNKNOWN || realSize == ZSTD_CONTENTSIZE_ERROR || realSize > 0x40000000)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
u8* realContent = new u8[realSize];
|
||||
u64 decompressed = ZSTD_decompress(realContent, realSize, inContent, inSize);
|
||||
|
||||
if (ZSTD_isError(decompressed))
|
||||
{
|
||||
delete[] realContent;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*outContent = realContent;
|
||||
return realSize;
|
||||
}
|
||||
|
||||
bool LoadROM(QStringList filepath, bool reset)
|
||||
{
|
||||
@ -520,6 +542,27 @@ bool LoadROM(QStringList filepath, bool reset)
|
||||
fclose(f);
|
||||
filelen = (u32)len;
|
||||
|
||||
#if ZSTD_ENABLED
|
||||
if (filename.length() > 4 && filename.substr(filename.length() - 4) == ".zst")
|
||||
{
|
||||
u8* outContent = nullptr;
|
||||
u32 decompressed = DecompressROM(filedata, len, &outContent);
|
||||
|
||||
if (decompressed > 0)
|
||||
{
|
||||
delete[] filedata;
|
||||
filedata = outContent;
|
||||
filelen = decompressed;
|
||||
filename = filename.substr(0, filename.length() - 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
delete[] filedata;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int pos = LastSep(filename);
|
||||
if(pos != -1)
|
||||
basepath = filename.substr(0, pos);
|
||||
@ -530,14 +573,14 @@ bool LoadROM(QStringList filepath, bool reset)
|
||||
{
|
||||
// file inside archive
|
||||
|
||||
s32 lenread = Archive::ExtractFileFromArchive(filepath.at(0), filepath.at(1), &filedata, &filelen);
|
||||
if (lenread < 0) return false;
|
||||
if (!filedata) return false;
|
||||
if (lenread != filelen)
|
||||
{
|
||||
delete[] filedata;
|
||||
return false;
|
||||
}
|
||||
s32 lenread = Archive::ExtractFileFromArchive(filepath.at(0), filepath.at(1), &filedata, &filelen);
|
||||
if (lenread < 0) return false;
|
||||
if (!filedata) return false;
|
||||
if (lenread != filelen)
|
||||
{
|
||||
delete[] filedata;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string std_archivepath = filepath.at(0).toStdString();
|
||||
basepath = std_archivepath.substr(0, LastSep(std_archivepath));
|
||||
@ -682,6 +725,27 @@ bool LoadGBAROM(QStringList filepath)
|
||||
fclose(f);
|
||||
filelen = (u32)len;
|
||||
|
||||
#if ZSTD_ENABLED
|
||||
if (filename.length() > 4 && filename.substr(filename.length() - 4) == ".zst")
|
||||
{
|
||||
u8* outContent = nullptr;
|
||||
u32 decompressed = DecompressROM(filedata, len, &outContent);
|
||||
|
||||
if (decompressed > 0)
|
||||
{
|
||||
delete[] filedata;
|
||||
filedata = outContent;
|
||||
filelen = decompressed;
|
||||
filename = filename.substr(0, filename.length() - 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
delete[] filedata;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int pos = LastSep(filename);
|
||||
basepath = filename.substr(0, pos);
|
||||
romname = filename.substr(pos+1);
|
||||
|
@ -146,7 +146,7 @@ const QStringList ArchiveExtensions
|
||||
".tar.lz",
|
||||
".tar.lzma", ".tlz",
|
||||
".tar.lrz", ".tlrz",
|
||||
".tar.lzo", ".tzo",
|
||||
".tar.lzo", ".tzo"
|
||||
#endif
|
||||
};
|
||||
|
||||
@ -1568,9 +1568,26 @@ static bool SupportedArchiveByMimetype(const QMimeType& mimetype)
|
||||
return MimeTypeInList(mimetype, ArchiveMimeTypes);
|
||||
}
|
||||
|
||||
#ifdef ZSTD_ENABLED
|
||||
static bool ZstdNdsRomByExtension(const QString& filename)
|
||||
{
|
||||
if (filename.endsWith(".zst", Qt::CaseInsensitive))
|
||||
return NdsRomByExtension(filename.left(filename.size() - 4));
|
||||
}
|
||||
|
||||
static bool ZstdGbaRomByExtension(const QString& filename)
|
||||
{
|
||||
if (filename.endsWith(".zst", Qt::CaseInsensitive))
|
||||
return GbaRomByExtension(filename.left(filename.size() - 4));
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool FileIsSupportedFiletype(const QString& filename, bool insideArchive = false)
|
||||
{
|
||||
#ifdef ZSTD_ENABLED
|
||||
if (ZstdNdsRomByExtension(filename) || ZstdGbaRomByExtension(filename))
|
||||
return true;
|
||||
#endif
|
||||
if (NdsRomByExtension(filename) || GbaRomByExtension(filename) || SupportedArchiveByExtension(filename))
|
||||
return true;
|
||||
|
||||
@ -2207,7 +2224,14 @@ void MainWindow::dropEvent(QDropEvent* event)
|
||||
const auto matchMode = romInsideArchive ? QMimeDatabase::MatchExtension : QMimeDatabase::MatchDefault;
|
||||
const QMimeType mimetype = QMimeDatabase().mimeTypeForFile(filename, matchMode);
|
||||
|
||||
if (NdsRomByExtension(filename) || NdsRomByMimetype(mimetype))
|
||||
bool isNdsRom = NdsRomByExtension(filename) || NdsRomByMimetype(mimetype);
|
||||
bool isGbaRom = GbaRomByExtension(filename) || GbaRomByMimetype(mimetype);
|
||||
#ifdef ZSTD_ENABLED
|
||||
isNdsRom |= ZstdNdsRomByExtension(filename);
|
||||
isGbaRom |= ZstdGbaRomByExtension(filename);
|
||||
#endif
|
||||
|
||||
if (isNdsRom)
|
||||
{
|
||||
if (!ROMManager::LoadROM(file, true))
|
||||
{
|
||||
@ -2227,7 +2251,7 @@ void MainWindow::dropEvent(QDropEvent* event)
|
||||
|
||||
updateCartInserted(false);
|
||||
}
|
||||
else if (GbaRomByExtension(filename) || GbaRomByMimetype(mimetype))
|
||||
else if (isGbaRom)
|
||||
{
|
||||
if (!ROMManager::LoadGBAROM(file))
|
||||
{
|
||||
@ -2452,14 +2476,26 @@ QStringList MainWindow::pickROM(bool gba)
|
||||
const QString console = gba ? "GBA" : "DS";
|
||||
const QStringList& romexts = gba ? GbaRomExtensions : NdsRomExtensions;
|
||||
|
||||
static const QString filterSuffix = ArchiveExtensions.empty()
|
||||
? ");;Any file (*.*)"
|
||||
: " *" + ArchiveExtensions.join(" *") + ");;Any file (*.*)";
|
||||
QString rawROMs = romexts.join(" *");
|
||||
QString extraFilters = ";;" + console + " ROMs (*" + rawROMs;
|
||||
QString allROMs = rawROMs;
|
||||
|
||||
#ifdef ZSTD_ENABLED
|
||||
QString zstdROMs = "*" + romexts.join(".zst *") + ".zst";
|
||||
extraFilters += ");;Zstandard-compressed " + console + " ROMs (" + zstdROMs + ")";
|
||||
allROMs += " " + zstdROMs;
|
||||
#endif
|
||||
#ifdef ARCHIVE_SUPPORT_ENABLED
|
||||
QString archives = "*" + ArchiveExtensions.join(" *");
|
||||
extraFilters += ";;Archives (" + archives + ")";
|
||||
allROMs += " " + archives;
|
||||
#endif
|
||||
extraFilters += ";;All files (*.*)";
|
||||
|
||||
const QString filename = QFileDialog::getOpenFileName(
|
||||
this, "Open " + console + " ROM",
|
||||
QString::fromStdString(Config::LastROMFolder),
|
||||
console + " ROMs (*" + romexts.join(" *") + filterSuffix
|
||||
"All supported files (*" + allROMs + ")" + extraFilters
|
||||
);
|
||||
|
||||
if (filename.isEmpty()) return {};
|
||||
|
Loading…
Reference in New Issue
Block a user