diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/frontend/qt_sdl/ArchiveUtil.cpp | 101 | ||||
-rw-r--r-- | src/frontend/qt_sdl/ArchiveUtil.h | 27 | ||||
-rw-r--r-- | src/frontend/qt_sdl/CMakeLists.txt | 11 | ||||
-rw-r--r-- | src/frontend/qt_sdl/main.cpp | 96 |
4 files changed, 187 insertions, 48 deletions
diff --git a/src/frontend/qt_sdl/ArchiveUtil.cpp b/src/frontend/qt_sdl/ArchiveUtil.cpp new file mode 100644 index 0000000..6457b4f --- /dev/null +++ b/src/frontend/qt_sdl/ArchiveUtil.cpp @@ -0,0 +1,101 @@ +/* + Copyright 2016-2020 Arisotura, WaluigiWare64 + + This file is part of melonDS. + + melonDS is free software: you can redistribute it and/or modify it under + the terms of the GNU General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + melonDS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with melonDS. If not, see http://www.gnu.org/licenses/. +*/ + +#include "ArchiveUtil.h" + +namespace Archive +{ + +QVector<QString> ListArchive(const char* path) +{ + struct archive *a; + struct archive_entry *entry; + int r; + + QVector<QString> fileList = {"OK"}; + + a = archive_read_new(); + archive_read_support_filter_all(a); + archive_read_support_format_all(a); + r = archive_read_open_filename(a, path, 10240); + if (r != ARCHIVE_OK) + { + return QVector<QString> {"Err"}; + } + + while (archive_read_next_header(a, &entry) == ARCHIVE_OK) + { + fileList.push_back(archive_entry_pathname(entry)); + archive_read_data_skip(a); + } + archive_read_close(a); + archive_read_free(a); + if (r != ARCHIVE_OK) + { + return QVector<QString> {"Err"}; + } + + return fileList; +} + +QVector<QString> ExtractFileFromArchive(const char* path, const char* wantedFile) +{ + struct archive *a = archive_read_new(); + struct archive_entry *entry; + int r; + + archive_read_support_format_all(a); + archive_read_support_filter_all(a); + + r = archive_read_open_filename(a, path, 10240); + if (r != ARCHIVE_OK) + { + return QVector<QString> {"Err"}; + } + while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { + if (wantedFile == nullptr) + { + break; + } + if (strcmp(wantedFile, archive_entry_pathname(entry)) == 0) + { + break; + } + } + size_t bytesToWrite = archive_entry_size(entry); + auto archiveBuffer = std::make_unique<u8[]>(bytesToWrite); + ssize_t bytesRead = archive_read_data(a, archiveBuffer.get(), bytesToWrite); + if (bytesRead < 0) + { + printf(archive_error_string(a)); + archiveBuffer.reset(nullptr); + return QVector<QString> {"Err", archive_error_string(a)}; + } + + const char* fileToWrite = archive_entry_pathname(entry); + std::ofstream(fileToWrite, std::ofstream::binary).write((char*)archiveBuffer.get(), bytesToWrite); + + archiveBuffer.reset(nullptr); + archive_read_close(a); + archive_read_free(a); + return QVector<QString> {QDir::toNativeSeparators(QDir::currentPath() + "/" + fileToWrite)}; + +} + + +} diff --git a/src/frontend/qt_sdl/ArchiveUtil.h b/src/frontend/qt_sdl/ArchiveUtil.h new file mode 100644 index 0000000..5b03a59 --- /dev/null +++ b/src/frontend/qt_sdl/ArchiveUtil.h @@ -0,0 +1,27 @@ +#ifndef ARCHIVEUTIL_H +#define ARCHIVEUTIL_H + +#include <stdio.h> + +#include <string> +#include <iostream> +#include <fstream> +#include <memory> + +#include <QVector> +#include <QDir> + +#include <archive.h> +#include <archive_entry.h> + +#include "types.h" + +namespace Archive +{ + +QVector<QString> ListArchive(const char* path); +QVector<QString> ExtractFileFromArchive(const char* path, const char* wantedFile); + +} + +#endif // ARCHIVEUTIL_H diff --git a/src/frontend/qt_sdl/CMakeLists.txt b/src/frontend/qt_sdl/CMakeLists.txt index fcb5c6f..55b8125 100644 --- a/src/frontend/qt_sdl/CMakeLists.txt +++ b/src/frontend/qt_sdl/CMakeLists.txt @@ -16,6 +16,9 @@ SET(SOURCES_QT_SDL font.h Platform.cpp PlatformConfig.cpp + + ArchiveUtil.h + ArchiveUtil.cpp ../Util_ROM.cpp ../Util_Video.cpp @@ -49,7 +52,7 @@ find_package(PkgConfig REQUIRED) find_package(Iconv REQUIRED) pkg_check_modules(SDL2 REQUIRED sdl2) pkg_check_modules(SLIRP REQUIRED slirp) -pkg_check_modules(LIBZIP REQUIRED libzip) +pkg_check_modules(LIBARCHIVE REQUIRED libarchive) if (WIN32 AND (CMAKE_BUILD_TYPE STREQUAL Release)) add_executable(melonDS WIN32 ${SOURCES_QT_SDL}) @@ -61,16 +64,16 @@ target_link_libraries(melonDS ${CMAKE_THREAD_LIBS_INIT}) target_include_directories(melonDS PRIVATE ${SDL2_INCLUDE_DIRS}) target_include_directories(melonDS PRIVATE ${SLIRP_INCLUDE_DIRS}) -target_include_directories(melonDS PRIVATE ${LIBZIP_INCLUDE_DIRS}) +target_include_directories(melonDS PRIVATE ${LIBARCHIVE_INCLUDE_DIRS}) target_include_directories(melonDS PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") target_include_directories(melonDS PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..") target_include_directories(melonDS PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../..") target_link_libraries(melonDS core) if (BUILD_STATIC) - target_link_libraries(melonDS -static ${SDL2_STATIC_LIBRARIES} ${SLIRP_STATIC_LIBRARIES} ${LIBZIP_STATIC_LIBRARIES}) + target_link_libraries(melonDS -static ${SDL2_STATIC_LIBRARIES} ${SLIRP_STATIC_LIBRARIES} ${LIBARCHIVE_STATIC_LIBRARIES}) else() - target_link_libraries(melonDS ${SDL2_LIBRARIES} ${SLIRP_LIBRARIES} ${LIBZIP_LIBRARIES}) + target_link_libraries(melonDS ${SDL2_LIBRARIES} ${SLIRP_LIBRARIES} ${LIBARCHIVE_LIBRARIES}) endif() if (NOT Iconv_IS_BUILT_IN) diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index c48e506..722ebd5 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -21,18 +21,21 @@ #include <stdio.h> #include <string.h> -#include <zip.h> -#include <fstream> -#include <iostream> +#include <vector> +#include <string> +#include <algorithm> #include <QApplication> #include <QMessageBox> #include <QMenuBar> #include <QFileDialog> +#include <QInputDialog> #include <QPaintEvent> #include <QPainter> #include <QKeyEvent> #include <QMimeData> +#include <QSet> +#include <QVector> #include <SDL2/SDL.h> @@ -64,6 +67,7 @@ #include "main_shaders.h" +#include "ArchiveUtil.h" // TODO: uniform variable spelling @@ -1416,54 +1420,58 @@ QString MainWindow::loadErrorStr(int error) } } -std::string extractROM(char* zipName, std::string zipDir){ - //Open the ZIP archive - int err = 0; - zip *z = zip_open(zipName, 0, &err); - - struct zip_stat st; - zip_stat_init(&st); - zip_stat_index(z, 0, 0, &st); //Get information about the file at index 0 - char newName[255]; - strcpy(newName, st.name); //fix for Linux invalid encoding filename - //Allocate memory for its uncompressed contents - u8 *contents = new u8[st.size]; - - //Read the compressed file - zip_file *f = zip_fopen_index(z, 0, 0); //Open file at index 0 - zip_fread(f, contents, st.size); - zip_fclose(f); - - zip_close(z); - - //Write the file (binary mode) - std::ofstream(zipDir + "/" + newName, std::ofstream::binary).write((char*) contents, st.size); - delete[] contents; - return zipDir + "/" + newName; -} - void MainWindow::onOpenFile() { emuThread->emuPause(); - bool romExtracted = false; //No use yet but may be useful later QString filename = QFileDialog::getOpenFileName(this, "Open ROM", Config::LastROMFolder, - "DS ROMs (*.nds *.dsi *.srl *.zip);;GBA ROMs (*.gba *.zip);;Any file (*.*)"); - QFileInfo filenameExtLoc = filename; - - if (filenameExtLoc.completeSuffix().toUtf8() == "zip") - { - printf("Extracting ROM from ZIP...\n"); - std::string extractRomLoc = extractROM(filename.toUtf8().data(), filenameExtLoc.absolutePath().toUtf8().data()); - printf("Done.\n"); - filename = QString::fromUtf8(extractRomLoc.c_str()); - romExtracted = true; - } else if (filenameExtLoc.completeSuffix().toUtf8() == "") { - //do nothing - } else { - romExtracted = false; + "DS ROMs (*.nds *.dsi *.srl *.zip *.7z);;GBA ROMs (*.gba *.zip *.7z);;Other Compressed ROMs (*.zip *.7z *.rar *.tar *.tar.gz *.tar.xz *tar.bz2);;Any file (*.*)"); + + static const QSet<QString> compressedExts = {"zip", "7z", "rar", "tar", "tar.gz", "tar.xz", "tar.bz2"}; + if (compressedExts.contains(QFileInfo(filename).completeSuffix())) + { + printf("Finding list of ROMs...\n"); + QVector<QString> archiveROMList = Archive::ListArchive(filename.toUtf8().constData()); + if (archiveROMList.size() > 2) + { + archiveROMList.removeFirst(); + QString toLoad = QInputDialog::getItem(this, "melonDS", + "The archive was found to have multiple files. Select which ROM you want to load.", archiveROMList.toList(), 0, false); + printf("Extracting '%s'\n", toLoad.toUtf8().constData()); + QVector<QString> extractResult = Archive::ExtractFileFromArchive(filename.toUtf8().constData(), toLoad.toUtf8().constData()); + if (extractResult[0] != QString("Err")) + { + filename = extractResult[0]; + } + else + { + QMessageBox::critical(this, "melonDS", QString("There was an error while trying to extract the ROM from the archive: ") + extractResult[1]); + } + } + else if (archiveROMList.size() == 2) + { + printf("Extracting the only ROM in archive\n"); + QVector<QString> extractResult = Archive::ExtractFileFromArchive(filename.toUtf8().constData(), nullptr); + if (extractResult[0] != QString("Err")) + { + filename = extractResult[0]; + } + else + { + QMessageBox::critical(this, "melonDS", QString("There was an error while trying to extract the ROM from the archive: ") + extractResult[1]); + } + } + else if ((archiveROMList.size() == 1) && (archiveROMList[0] == QString("OK"))) + { + QMessageBox::warning(this, "melonDS", "The archive is intact, but there are no files inside."); + } + else + { + QMessageBox::critical(this, "melonDS", "The archive could not be read. It may be corrupt or you don't have the permissions."); + } + } if (filename.isEmpty()) |