diff --git a/src/GuestClass/EmuCores/EmuCores.cpp b/src/GuestClass/EmuCores/EmuCores.cpp index 01767cd..69f0b69 100644 --- a/src/GuestClass/EmuCores/EmuCores.cpp +++ b/src/GuestClass/EmuCores/EmuCores.cpp @@ -56,15 +56,11 @@ void Chip8_CoreInterface::instructionErrorML(const u32 HI, const u32 LO) { setInterrupt(Interrupt::ERROR); } -bool Chip8_CoreInterface::copyGameToMemory(u8* dest, const u32 offset) { - std::basic_ifstream ifs(HDM.getFilePath(), std::ios::binary); - ifs.read(reinterpret_cast(dest + offset), HDM.getFileSize()); - if (ifs.fail()) { - blog.newEntry(BLOG::ERROR, "Failed to copy rom data to memory, aborting."); - return false; - } else { - return true; - } +void Chip8_CoreInterface::copyGameToMemory(u8* dest, const u32 offset) { + std::copy_n( + std::execution::unseq, + HDM.getFileData(), HDM.getFileSize(), dest + offset + ); } void Chip8_CoreInterface::copyFontToMemory(u8* dest, const u32 offset, const u32 size) { diff --git a/src/GuestClass/EmuCores/EmuCores.hpp b/src/GuestClass/EmuCores/EmuCores.hpp index 10ddcc4..7a6c4fc 100644 --- a/src/GuestClass/EmuCores/EmuCores.hpp +++ b/src/GuestClass/EmuCores/EmuCores.hpp @@ -120,7 +120,7 @@ class Chip8_CoreInterface : public EmuInterface { void instructionError(const u32 HI, const u32 LO); void instructionErrorML(const u32 HI, const u32 LO); - bool copyGameToMemory(u8* dest, const u32 offset); + void copyGameToMemory(u8* dest, const u32 offset); void copyFontToMemory(u8* dest, const u32 offset, const u32 size); public: diff --git a/src/HostClass/HomeDirManager.cpp b/src/HostClass/HomeDirManager.cpp index b6b7db9..4a1110d 100644 --- a/src/HostClass/HomeDirManager.cpp +++ b/src/HostClass/HomeDirManager.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "HomeDirManager.hpp" #include "../Assistants/BasicLogger.hpp" @@ -29,14 +30,10 @@ HomeDirManager::HomeDirManager(const std::string_view homeDirName) try } void HomeDirManager::clearCachedFileData() noexcept { - //mFilePath.clear(); - //mFileName.clear(); - //mFileStem.clear(); - //mFileExts.clear(); mFilePath.clear(); mFileSHA1.clear(); + mFileData.resize(0); mFileTime = {}; - mFileSize = {}; } void HomeDirManager::addDirectory() { @@ -63,32 +60,29 @@ bool HomeDirManager::validateGameFile(const FilePath gamePath) noexcept { blog.newEntry(BLOG::WARN, "Provided path is not to a file!"); return false; } - - const auto tempSize{ fs::file_size(gamePath, error) }; - if (error) { - blog.newEntry(BLOG::ERROR, "Unable to read file!"); - return false; - } - if (tempSize == 0) { - blog.newEntry(BLOG::WARN, "File must not be empty!"); - return false; - } const auto tempTime{ HDM::getFileTime(gamePath) }; - const auto tempSHA1{ SHA1::from_file(gamePath.string()) }; + + std::ifstream ifs(gamePath, std::ios::binary); + mFileData.assign(std::istreambuf_iterator(ifs), {}); if (tempTime != HDM::getFileTime(gamePath)) { blog.newEntry(BLOG::WARN, "File was modified while reading!"); return false; } - const bool gameApproved{ checkGame(tempSize, gamePath.extension().string(), tempSHA1) }; + if (!getFileSize()) { + blog.newEntry(BLOG::WARN, "File must not be empty!"); + return false; + } + + const auto tempSHA1{ SHA1::from_span(mFileData) }; + const bool gameApproved{ checkGame(getFileSize(), gamePath.extension().string(), tempSHA1)}; if (gameApproved) { mFilePath = gamePath; mFileSHA1 = tempSHA1; mFileTime = tempTime; - mFileSize = tempSize; } if (gameApproved) { diff --git a/src/HostClass/HomeDirManager.hpp b/src/HostClass/HomeDirManager.hpp index 6cbcdd7..f2027d2 100644 --- a/src/HostClass/HomeDirManager.hpp +++ b/src/HostClass/HomeDirManager.hpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -17,15 +18,15 @@ class HomeDirManager final : public BasicHome { using GameValidator = bool (*)(std::uint64_t, std::string_view, std::string_view); using FileModTime = std::filesystem::file_time_type; using FilePath = std::filesystem::path; - using FileSize = std::uintmax_t; FilePath mFilePath{}; std::string mFileSHA1{}; FileModTime mFileTime{}; - FileSize mFileSize{}; GameValidator checkGame{}; + std::vector mFileData{}; + public: HomeDirManager(const std::string_view); @@ -37,9 +38,10 @@ class HomeDirManager final : public BasicHome { auto getFileName() const noexcept { return mFilePath.filename().string(); } auto getFileStem() const noexcept { return mFilePath.stem().string(); } auto getFileExts() const noexcept { return mFilePath.extension().string(); } + auto getFileSize() const noexcept { return mFileData.size(); } + auto getFileData() const noexcept { return mFileData.data(); } auto getFileSHA1() const noexcept { return mFileSHA1; } auto getFileTime() const noexcept { return mFileTime; } - auto getFileSize() const noexcept { return mFileSize; } void setValidator(GameValidator func) noexcept { checkGame = func; } diff --git a/src/HostClass/HostFunctions.cpp b/src/HostClass/HostFunctions.cpp index 2a716da..ffd8405 100644 --- a/src/HostClass/HostFunctions.cpp +++ b/src/HostClass/HostFunctions.cpp @@ -68,7 +68,7 @@ SDL_AppResult VM_Host::runFrame() { return SDL_APP_CONTINUE; } if (kb.isPressed(KEY(BACKSPACE))) { - loadGameFile(HDM->getFullPath()); + replaceGuest(false); return SDL_APP_CONTINUE; } if (kb.isPressed(KEY(RSHIFT))) {