From cc1078d9e8e99221180baf58107214c875f65e52 Mon Sep 17 00:00:00 2001 From: irisz64 Date: Fri, 1 Aug 2025 16:21:18 +0200 Subject: [PATCH] These can be Utility functions --- src/backend/core/Mem.cpp | 57 ++-------------------------------------- src/backend/core/Mem.hpp | 2 -- src/utils/File.hpp | 53 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/src/backend/core/Mem.cpp b/src/backend/core/Mem.cpp index 7b9b8fc7..9717d9e5 100644 --- a/src/backend/core/Mem.cpp +++ b/src/backend/core/Mem.cpp @@ -86,68 +86,15 @@ FORCE_INLINE void SetROMCIC(u32 checksum, ROM &rom) { } } -std::vector Mem::OpenArchive(const std::string &path, size_t &sizeAdjusted) { - const auto stream = ar_open_file(fs::path(path).string().c_str()); - - if (!stream) { - panic("Could not open archive! Are you sure it's an archive?"); - } - - ar_archive *archive = ar_open_zip_archive(stream, false); - - if (!archive) - archive = ar_open_rar_archive(stream); - if (!archive) - archive = ar_open_7z_archive(stream); - if (!archive) - archive = ar_open_tar_archive(stream); - - if (!archive) { - ar_close(stream); - panic("Could not open archive! Are you sure it's a supported archive? (7z, zip, rar and tar are supported)"); - } - - std::vector buf{}; - - std::vector rom_exts{".n64", ".z64", ".v64", ".N64", ".Z64", ".V64"}; - - while (ar_parse_entry(archive)) { - auto filename = ar_entry_get_name(archive); - auto extension = fs::path(filename).extension(); - - if (std::ranges::any_of(rom_exts, [&](const auto &x) { return extension == x; })) { - const auto size = ar_entry_get_size(archive); - sizeAdjusted = Util::NextPow2(size); - buf.resize(sizeAdjusted); - ar_entry_uncompress(archive, buf.data(), size); - break; - } - - ar_close_archive(archive); - ar_close(stream); - panic("Could not find any rom image in the archive!"); - } - - ar_close_archive(archive); - ar_close(stream); - return buf; -} - -std::vector Mem::OpenROM(const std::string &filename, size_t &sizeAdjusted) { - auto buf = Util::ReadFileBinary(filename); - sizeAdjusted = Util::NextPow2(buf.size()); - return buf; -} - void Mem::LoadROM(const bool isArchive, const std::string &filename) { u32 endianness; { size_t sizeAdjusted; std::vector buf{}; if (isArchive) { - buf = OpenArchive(filename, sizeAdjusted); + buf = Util::OpenArchive(filename, sizeAdjusted); } else { - buf = OpenROM(filename, sizeAdjusted); + buf = Util::OpenROM(filename, sizeAdjusted); } endianness = std::byteswap(Util::ReadAccess(buf, 0)); diff --git a/src/backend/core/Mem.hpp b/src/backend/core/Mem.hpp index f254a43a..07cc2cfe 100644 --- a/src/backend/core/Mem.hpp +++ b/src/backend/core/Mem.hpp @@ -82,8 +82,6 @@ struct Mem { Mem(JIT * = nullptr); void Reset(); void LoadSRAM(SaveType, fs::path); - static std::vector OpenROM(const std::string &, size_t &); - static std::vector OpenArchive(const std::string &, size_t &); void LoadROM(bool, const std::string &); [[nodiscard]] auto GetRDRAMPtr() -> u8 * { return mmio.rdp.rdram.data(); } diff --git a/src/utils/File.hpp b/src/utils/File.hpp index d72d9d64..874a0d34 100644 --- a/src/utils/File.hpp +++ b/src/utils/File.hpp @@ -37,4 +37,57 @@ FORCE_INLINE size_t NextPow2(size_t num) { num |= num >> 16; return num + 1; } + +FORCE_INLINE std::vector OpenROM(const std::string &, size_t &) { + auto buf = Util::ReadFileBinary(filename); + sizeAdjusted = Util::NextPow2(buf.size()); + return buf; +} + +FORCE_INLINE std::vector OpenArchive(const std::string &, size_t &) { + const auto stream = ar_open_file(fs::path(path).string().c_str()); + + if (!stream) { + panic("Could not open archive! Are you sure it's an archive?"); + } + + ar_archive *archive = ar_open_zip_archive(stream, false); + + if (!archive) + archive = ar_open_rar_archive(stream); + if (!archive) + archive = ar_open_7z_archive(stream); + if (!archive) + archive = ar_open_tar_archive(stream); + + if (!archive) { + ar_close(stream); + panic("Could not open archive! Are you sure it's a supported archive? (7z, zip, rar and tar are supported)"); + } + + std::vector buf{}; + + std::vector rom_exts{".n64", ".z64", ".v64", ".N64", ".Z64", ".V64"}; + + while (ar_parse_entry(archive)) { + auto filename = ar_entry_get_name(archive); + auto extension = fs::path(filename).extension(); + + if (std::ranges::any_of(rom_exts, [&](const auto &x) { return extension == x; })) { + const auto size = ar_entry_get_size(archive); + sizeAdjusted = Util::NextPow2(size); + buf.resize(sizeAdjusted); + ar_entry_uncompress(archive, buf.data(), size); + break; + } + + ar_close_archive(archive); + ar_close(stream); + panic("Could not find any rom image in the archive!"); + } + + ar_close_archive(archive); + ar_close(stream); + return buf; +} } // namespace Util