From e20bfd00ecfc4376e39048942c15a55463f065d0 Mon Sep 17 00:00:00 2001 From: goeiecool9999 Date: Wed, 7 Sep 2022 02:42:25 +0200 Subject: [PATCH] Separate filestream.h into OS specific implementation files (#190) --- src/Cafe/Account/Account.cpp | 2 +- src/Cafe/CafeSystem.cpp | 2 +- src/Cafe/Filesystem/FST/FST.cpp | 2 +- src/Cafe/Filesystem/FST/KeyCache.cpp | 2 +- src/Cafe/Filesystem/WUD/wud.cpp | 2 +- src/Cafe/Filesystem/fscDeviceHostFS.cpp | 2 +- src/Cafe/GameProfile/GameProfile.cpp | 2 +- src/Cafe/GraphicPack/GraphicPack2Patches.cpp | 2 +- .../GraphicPack/GraphicPack2PatchesApply.cpp | 2 +- .../GraphicPack/GraphicPack2PatchesParser.cpp | 2 +- .../HW/Latte/Common/RegisterSerializer.cpp | 2 +- src/Cafe/IOSU/PDM/iosu_pdm.cpp | 2 +- src/Cafe/IOSU/legacy/iosu_crypto.cpp | 2 +- src/Cafe/OS/libs/nlibcurl/nlibcurl.cpp | 2 +- src/Cafe/OS/libs/nn_acp/nn_acp.cpp | 2 +- src/Cafe/OS/libs/nn_aoc/nn_aoc.cpp | 2 +- src/Cafe/OS/libs/nn_nfp/nn_nfp.cpp | 2 +- src/Cafe/TitleList/SaveInfo.cpp | 2 +- src/Cafe/TitleList/TitleInfo.cpp | 2 +- src/Cafe/TitleList/TitleList.cpp | 2 +- src/Cemu/FileCache/FileCache.cpp | 2 +- .../Tools/DownloadManager/DownloadManager.cpp | 2 +- src/Common/CMakeLists.txt | 2 + src/Common/FileStream.h | 8 + src/Common/filestream.h | 441 ------------------ src/Common/unix/FileStream_unix.cpp | 214 +++++++++ src/Common/unix/FileStream_unix.h | 56 +++ src/Common/windows/FileStream_win32.cpp | 187 ++++++++ src/Common/windows/FileStream_win32.h | 53 +++ src/config/XMLConfig.h | 2 +- src/gui/CemuUpdateWindow.cpp | 2 +- src/gui/DownloadGraphicPacksWindow.cpp | 2 +- src/gui/MemorySearcherTool.cpp | 2 +- src/gui/components/wxTitleManagerList.cpp | 2 +- src/mainLLE.cpp | 2 +- src/util/ImageWriter/bmp.h | 2 +- src/util/ImageWriter/tga.h | 2 +- 37 files changed, 550 insertions(+), 471 deletions(-) create mode 100644 src/Common/FileStream.h delete mode 100644 src/Common/filestream.h create mode 100644 src/Common/unix/FileStream_unix.cpp create mode 100644 src/Common/unix/FileStream_unix.h create mode 100644 src/Common/windows/FileStream_win32.cpp create mode 100644 src/Common/windows/FileStream_win32.h diff --git a/src/Cafe/Account/Account.cpp b/src/Cafe/Account/Account.cpp index f370c815..2cf424f1 100644 --- a/src/Cafe/Account/Account.cpp +++ b/src/Cafe/Account/Account.cpp @@ -5,7 +5,7 @@ #include "config/ActiveSettings.h" #include "Cafe/IOSU/legacy/iosu_crypto.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include #include diff --git a/src/Cafe/CafeSystem.cpp b/src/Cafe/CafeSystem.cpp index bfa5d822..22ff67c5 100644 --- a/src/Cafe/CafeSystem.cpp +++ b/src/Cafe/CafeSystem.cpp @@ -24,7 +24,7 @@ #include "Cafe/Filesystem/FST/FST.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "GamePatch.h" diff --git a/src/Cafe/Filesystem/FST/FST.cpp b/src/Cafe/Filesystem/FST/FST.cpp index 0a28c251..a4bbfeed 100644 --- a/src/Cafe/Filesystem/FST/FST.cpp +++ b/src/Cafe/Filesystem/FST/FST.cpp @@ -1,5 +1,5 @@ #include "Common/precompiled.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "Cemu/ncrypto/ncrypto.h" #include "Cafe/Filesystem/WUD/wud.h" #include "util/crypto/aes128.h" diff --git a/src/Cafe/Filesystem/FST/KeyCache.cpp b/src/Cafe/Filesystem/FST/KeyCache.cpp index 7fedc3dd..587a5dd6 100644 --- a/src/Cafe/Filesystem/FST/KeyCache.cpp +++ b/src/Cafe/Filesystem/FST/KeyCache.cpp @@ -3,7 +3,7 @@ #include "config/ActiveSettings.h" #include "util/crypto/aes128.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "util/helpers/StringHelpers.h" std::mutex mtxKeyCache; diff --git a/src/Cafe/Filesystem/WUD/wud.cpp b/src/Cafe/Filesystem/WUD/wud.cpp index 205a5db0..07f6b7a3 100644 --- a/src/Cafe/Filesystem/WUD/wud.cpp +++ b/src/Cafe/Filesystem/WUD/wud.cpp @@ -2,7 +2,7 @@ #include #include #include "wud.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" wud_t* wud_open(const fs::path& path) { diff --git a/src/Cafe/Filesystem/fscDeviceHostFS.cpp b/src/Cafe/Filesystem/fscDeviceHostFS.cpp index 43ae6b4f..cb54cacf 100644 --- a/src/Cafe/Filesystem/fscDeviceHostFS.cpp +++ b/src/Cafe/Filesystem/fscDeviceHostFS.cpp @@ -2,7 +2,7 @@ #include "Cafe/Filesystem/fsc.h" #include "Cafe/Filesystem/fscDeviceHostFS.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" /* FSCVirtualFile implementation for HostFS */ diff --git a/src/Cafe/GameProfile/GameProfile.cpp b/src/Cafe/GameProfile/GameProfile.cpp index d3e930ee..9f2550c2 100644 --- a/src/Cafe/GameProfile/GameProfile.cpp +++ b/src/Cafe/GameProfile/GameProfile.cpp @@ -4,7 +4,7 @@ #include "boost/nowide/convert.hpp" #include "config/ActiveSettings.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "util/IniParser/IniParser.h" #include "util/helpers/StringHelpers.h" #include "Cafe/CafeSystem.h" diff --git a/src/Cafe/GraphicPack/GraphicPack2Patches.cpp b/src/Cafe/GraphicPack/GraphicPack2Patches.cpp index 3e4015c9..5a3c85b9 100644 --- a/src/Cafe/GraphicPack/GraphicPack2Patches.cpp +++ b/src/Cafe/GraphicPack/GraphicPack2Patches.cpp @@ -1,5 +1,5 @@ #include "Cafe/GraphicPack/GraphicPack2.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "util/helpers/StringParser.h" #include "Cemu/PPCAssembler/ppcAssembler.h" #include "Cafe/OS/RPL/rpl_structs.h" diff --git a/src/Cafe/GraphicPack/GraphicPack2PatchesApply.cpp b/src/Cafe/GraphicPack/GraphicPack2PatchesApply.cpp index ec94689e..98a5b693 100644 --- a/src/Cafe/GraphicPack/GraphicPack2PatchesApply.cpp +++ b/src/Cafe/GraphicPack/GraphicPack2PatchesApply.cpp @@ -1,5 +1,5 @@ #include "Cafe/GraphicPack/GraphicPack2.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "Cemu/PPCAssembler/ppcAssembler.h" #include "Cafe/OS/RPL/rpl_structs.h" #include "Cafe/OS/RPL/rpl_symbol_storage.h" diff --git a/src/Cafe/GraphicPack/GraphicPack2PatchesParser.cpp b/src/Cafe/GraphicPack/GraphicPack2PatchesParser.cpp index bb052bc2..12f97ef7 100644 --- a/src/Cafe/GraphicPack/GraphicPack2PatchesParser.cpp +++ b/src/Cafe/GraphicPack/GraphicPack2PatchesParser.cpp @@ -1,5 +1,5 @@ #include "Cafe/GraphicPack/GraphicPack2.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "util/helpers/StringParser.h" #include "Cemu/PPCAssembler/ppcAssembler.h" #include "Cafe/OS/RPL/rpl_structs.h" diff --git a/src/Cafe/HW/Latte/Common/RegisterSerializer.cpp b/src/Cafe/HW/Latte/Common/RegisterSerializer.cpp index 3810d354..368c9d94 100644 --- a/src/Cafe/HW/Latte/Common/RegisterSerializer.cpp +++ b/src/Cafe/HW/Latte/Common/RegisterSerializer.cpp @@ -1,7 +1,7 @@ #include "Cafe/HW/Latte/ISA/RegDefines.h" #include "Cafe/HW/Latte/Core/Latte.h" #include "Cafe/HW/Latte/Common/RegisterSerializer.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include #include diff --git a/src/Cafe/IOSU/PDM/iosu_pdm.cpp b/src/Cafe/IOSU/PDM/iosu_pdm.cpp index 626eba40..89be4de2 100644 --- a/src/Cafe/IOSU/PDM/iosu_pdm.cpp +++ b/src/Cafe/IOSU/PDM/iosu_pdm.cpp @@ -1,6 +1,6 @@ #include "iosu_pdm.h" #include "config/ActiveSettings.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "util/helpers/Semaphore.h" #if BOOST_OS_LINUX diff --git a/src/Cafe/IOSU/legacy/iosu_crypto.cpp b/src/Cafe/IOSU/legacy/iosu_crypto.cpp index 0433995c..e74a93e2 100644 --- a/src/Cafe/IOSU/legacy/iosu_crypto.cpp +++ b/src/Cafe/IOSU/legacy/iosu_crypto.cpp @@ -9,7 +9,7 @@ #include "openssl/ecdsa.h" #include "util/crypto/aes128.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" uint8 otpMem[1024]; bool hasOtpMem = false; diff --git a/src/Cafe/OS/libs/nlibcurl/nlibcurl.cpp b/src/Cafe/OS/libs/nlibcurl/nlibcurl.cpp index 907ed67c..9078ecc2 100644 --- a/src/Cafe/OS/libs/nlibcurl/nlibcurl.cpp +++ b/src/Cafe/OS/libs/nlibcurl/nlibcurl.cpp @@ -16,7 +16,7 @@ #include "util/helpers/ConcurrentQueue.h" #include "Cafe/OS/common/PPCConcurrentQueue.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "config/ActiveSettings.h" namespace nlibcurl diff --git a/src/Cafe/OS/libs/nn_acp/nn_acp.cpp b/src/Cafe/OS/libs/nn_acp/nn_acp.cpp index 48aa3c00..23d21434 100644 --- a/src/Cafe/OS/libs/nn_acp/nn_acp.cpp +++ b/src/Cafe/OS/libs/nn_acp/nn_acp.cpp @@ -14,7 +14,7 @@ #include "Cafe/IOSU/legacy/iosu_ioctl.h" #include "Cafe/OS/libs/sysapp/sysapp.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "Cafe/CafeSystem.h" #define acpPrepareRequest() \ diff --git a/src/Cafe/OS/libs/nn_aoc/nn_aoc.cpp b/src/Cafe/OS/libs/nn_aoc/nn_aoc.cpp index 93564536..aa81fa58 100644 --- a/src/Cafe/OS/libs/nn_aoc/nn_aoc.cpp +++ b/src/Cafe/OS/libs/nn_aoc/nn_aoc.cpp @@ -6,7 +6,7 @@ #include "Cafe/TitleList/TitleId.h" #include "Cemu/ncrypto/ncrypto.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" namespace nn { diff --git a/src/Cafe/OS/libs/nn_nfp/nn_nfp.cpp b/src/Cafe/OS/libs/nn_nfp/nn_nfp.cpp index 93e7f7ad..0c567b5a 100644 --- a/src/Cafe/OS/libs/nn_nfp/nn_nfp.cpp +++ b/src/Cafe/OS/libs/nn_nfp/nn_nfp.cpp @@ -3,7 +3,7 @@ #include "Cafe/OS/libs/nn_common.h" #include "nn_nfp.h" #include "Cafe/OS/libs/coreinit/coreinit_Thread.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "Cafe/CafeSystem.h" std::recursive_mutex g_nfpMutex; diff --git a/src/Cafe/TitleList/SaveInfo.cpp b/src/Cafe/TitleList/SaveInfo.cpp index 27411619..f5665ea5 100644 --- a/src/Cafe/TitleList/SaveInfo.cpp +++ b/src/Cafe/TitleList/SaveInfo.cpp @@ -1,6 +1,6 @@ #include "SaveInfo.h" #include "config/ActiveSettings.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "ParsedMetaXml.h" SaveInfo::SaveInfo(TitleId titleId) : m_titleId(titleId) diff --git a/src/Cafe/TitleList/TitleInfo.cpp b/src/Cafe/TitleList/TitleInfo.cpp index a3d6aaf9..6606c130 100644 --- a/src/Cafe/TitleList/TitleInfo.cpp +++ b/src/Cafe/TitleList/TitleInfo.cpp @@ -4,7 +4,7 @@ #include "Cafe/Filesystem/FST/FST.h" #include "pugixml.hpp" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include #include "config/ActiveSettings.h" diff --git a/src/Cafe/TitleList/TitleList.cpp b/src/Cafe/TitleList/TitleList.cpp index 7e1bf883..315103dd 100644 --- a/src/Cafe/TitleList/TitleList.cpp +++ b/src/Cafe/TitleList/TitleList.cpp @@ -1,5 +1,5 @@ #include "TitleList.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "util/helpers/helpers.h" diff --git a/src/Cemu/FileCache/FileCache.cpp b/src/Cemu/FileCache/FileCache.cpp index 8979e416..0165f0c4 100644 --- a/src/Cemu/FileCache/FileCache.cpp +++ b/src/Cemu/FileCache/FileCache.cpp @@ -4,7 +4,7 @@ #include #include #include "zlib.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" struct FileCacheAsyncJob { diff --git a/src/Cemu/Tools/DownloadManager/DownloadManager.cpp b/src/Cemu/Tools/DownloadManager/DownloadManager.cpp index f953844a..33af7433 100644 --- a/src/Cemu/Tools/DownloadManager/DownloadManager.cpp +++ b/src/Cemu/Tools/DownloadManager/DownloadManager.cpp @@ -4,7 +4,7 @@ #include "gui/CemuApp.h" #include "util/crypto/md5.h" #include "Cafe/TitleList/TitleId.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "Cemu/FileCache/FileCache.h" #include "Cemu/ncrypto/ncrypto.h" #include "config/ActiveSettings.h" diff --git a/src/Common/CMakeLists.txt b/src/Common/CMakeLists.txt index 3634527d..f1654f31 100644 --- a/src/Common/CMakeLists.txt +++ b/src/Common/CMakeLists.txt @@ -10,6 +10,7 @@ if(WIN32) target_sources(CemuCommon PRIVATE windows/platform.cpp windows/platform.h + windows/FileStream_win32.cpp ExceptionHandler/ExceptionHandler_win32.cpp ) else() @@ -17,6 +18,7 @@ target_sources(CemuCommon PRIVATE unix/platform.cpp unix/platform.h + unix/FileStream_unix.cpp ExceptionHandler/ExceptionHandler_posix.cpp ) endif() diff --git a/src/Common/FileStream.h b/src/Common/FileStream.h new file mode 100644 index 00000000..f5d8ff0e --- /dev/null +++ b/src/Common/FileStream.h @@ -0,0 +1,8 @@ +#pragma once +#include "Common/precompiled.h" + +#ifdef _WIN32 +#include "Common/windows/FileStream_win32.h" +#else +#include "Common/unix/FileStream_unix.h" +#endif diff --git a/src/Common/filestream.h b/src/Common/filestream.h deleted file mode 100644 index 64b04c70..00000000 --- a/src/Common/filestream.h +++ /dev/null @@ -1,441 +0,0 @@ -#pragma once -#include "Common/precompiled.h" -#include - -#ifdef _WIN32 - -class FileStream -{ -public: - static FileStream* openFile(std::string_view path) - { - HANDLE hFile = CreateFileW(boost::nowide::widen(path.data(), path.size()).c_str(), FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); - if (hFile == INVALID_HANDLE_VALUE) - return nullptr; - return new FileStream(hFile); - } - - static FileStream* openFile(const wchar_t* path, bool allowWrite = false) - { - HANDLE hFile = CreateFileW(path, allowWrite ? (FILE_GENERIC_READ | FILE_GENERIC_WRITE) : FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); - if (hFile == INVALID_HANDLE_VALUE) - return nullptr; - return new FileStream(hFile); - } - - static FileStream* openFile2(const fs::path& path, bool allowWrite = false) - { - return openFile(path.generic_wstring().c_str(), allowWrite); - } - - static FileStream* createFile(const wchar_t* path) - { - HANDLE hFile = CreateFileW(path, FILE_GENERIC_READ | FILE_GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0); - if (hFile == INVALID_HANDLE_VALUE) - return nullptr; - return new FileStream(hFile); - } - - static FileStream* createFile(std::string_view path) - { - auto w = boost::nowide::widen(path.data(), path.size()); - HANDLE hFile = CreateFileW(w.c_str(), FILE_GENERIC_READ | FILE_GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0); - if (hFile == INVALID_HANDLE_VALUE) - return nullptr; - return new FileStream(hFile); - } - - static FileStream* createFile2(const fs::path& path) - { - return createFile(path.generic_wstring().c_str()); - } - - // helper function to load a file into memory - static std::optional> LoadIntoMemory(const fs::path& path) - { - FileStream* fs = openFile2(path); - if (!fs) - return std::nullopt; - uint64 fileSize = fs->GetSize(); - if(fileSize > 0xFFFFFFFFull) - { - delete fs; - return std::nullopt; - } - std::optional> v(fileSize); - if (fs->readData(v->data(), (uint32)fileSize) != (uint32)fileSize) - { - delete fs; - return std::nullopt; - } - delete fs; - return v; - } - - // size and seek - void SetPosition(uint64 pos) - { - LONG posHigh = (LONG)(pos >> 32); - LONG posLow = (LONG)(pos); - SetFilePointer(m_hFile, posLow, &posHigh, FILE_BEGIN); - } - - uint64 GetSize() - { - DWORD fileSizeHigh = 0; - DWORD fileSizeLow = 0; - fileSizeLow = GetFileSize(m_hFile, &fileSizeHigh); - return ((uint64)fileSizeHigh << 32) | (uint64)fileSizeLow; - } - - bool SetEndOfFile() - { - return ::SetEndOfFile(m_hFile) != 0; - } - - // reading - void extract(std::vector& data) - { - DWORD fileSize = GetFileSize(m_hFile, nullptr); - data.resize(fileSize); - SetFilePointer(m_hFile, 0, 0, FILE_BEGIN); - DWORD bt; - ReadFile(m_hFile, data.data(), fileSize, &bt, nullptr); - } - - uint32 readData(void* data, uint32 length) - { - DWORD bytesRead = 0; - ReadFile(m_hFile, data, length, &bytesRead, NULL); - return bytesRead; - } - - bool readU64(uint64& v) - { - return readData(&v, sizeof(uint64)) == sizeof(uint64); - } - - bool readU32(uint32& v) - { - return readData(&v, sizeof(uint32)) == sizeof(uint32); - } - - bool readU8(uint8& v) - { - return readData(&v, sizeof(uint8)) == sizeof(uint8); - } - - bool readLine(std::string& line) - { - line.clear(); - uint8 c; - bool isEOF = true; - while (readU8(c)) - { - isEOF = false; - if(c == '\r') - continue; - if (c == '\n') - break; - line.push_back((char)c); - } - return !isEOF; - } - - // writing (binary) - sint32 writeData(const void* data, sint32 length) - { - DWORD bytesWritten = 0; - WriteFile(m_hFile, data, length, &bytesWritten, NULL); - return bytesWritten; - } - - void writeU64(uint64 v) - { - writeData(&v, sizeof(uint64)); - } - - void writeU32(uint32 v) - { - writeData(&v, sizeof(uint32)); - } - - void writeU8(uint8 v) - { - writeData(&v, sizeof(uint8)); - } - - // writing (strings) - void writeStringFmt(const char* format, ...) - { - char buffer[2048]; - va_list args; - va_start(args, format); - vsnprintf(buffer, sizeof(buffer), format, args); - writeData(buffer, (sint32)strlen(buffer)); - } - - void writeString(const char* str) - { - writeData(str, (sint32)strlen(str)); - } - - void writeLine(const char* str) - { - writeData(str, (sint32)strlen(str)); - writeData("\r\n", 2); - } - - ~FileStream() - { - if(m_isValid) - CloseHandle(m_hFile); - } - - FileStream() {}; - -private: - FileStream(HANDLE hFile) - { - m_hFile = hFile; - m_isValid = true; - } - - bool m_isValid{}; - HANDLE m_hFile; -}; - -#else - -#include - -class FileStream -{ -public: - static FileStream* openFile(std::string_view path) - { - return openFile2(path, false); - } - - static FileStream* openFile(const wchar_t* path, bool allowWrite = false) - { - return openFile2(path, allowWrite); - } - - static FileStream* openFile2(const fs::path& path, bool allowWrite = false) - { - //return openFile(path.generic_wstring().c_str(), allowWrite); - FileStream* fs = new FileStream(path, true, allowWrite); - if (fs->m_isValid) - return fs; - delete fs; - return nullptr; - } - - static FileStream* createFile(const wchar_t* path) - { - return createFile2(path); - } - - static FileStream* createFile(std::string_view path) - { - return createFile2(path); - } - - static FileStream* createFile2(const fs::path& path) - { - FileStream* fs = new FileStream(path, false, false); - if (fs->m_isValid) - return fs; - delete fs; - return nullptr; - } - - // helper function to load a file into memory - static std::optional> LoadIntoMemory(const fs::path& path) - { - FileStream* fs = openFile2(path); - if (!fs) - return std::nullopt; - uint64 fileSize = fs->GetSize(); - if (fileSize > 0xFFFFFFFFull) - { - delete fs; - return std::nullopt; - } - std::optional> v(fileSize); - if (fs->readData(v->data(), (uint32)fileSize) != (uint32)fileSize) - { - delete fs; - return std::nullopt; - } - delete fs; - return v; - } - - // size and seek - void SetPosition(uint64 pos) - { - cemu_assert(m_isValid); - if (m_prevOperationWasWrite) - m_fileStream.seekp((std::streampos)pos); - else - m_fileStream.seekg((std::streampos)pos); - } - - uint64 GetSize() - { - cemu_assert(m_isValid); - auto currentPos = m_fileStream.tellg(); - m_fileStream.seekg(0, std::ios::end); - auto fileSize = m_fileStream.tellg(); - m_fileStream.seekg(currentPos, std::ios::beg); - uint64 fs = (uint64)fileSize; - return fs; - } - - bool SetEndOfFile() - { - assert_dbg(); - return true; - //return ::SetEndOfFile(m_hFile) != 0; - } - - // reading - void extract(std::vector& data) - { - uint64 fileSize = GetSize(); - SetPosition(0); - data.resize(fileSize); - readData(data.data(), fileSize); - } - - uint32 readData(void* data, uint32 length) - { - SyncReadWriteSeek(false); - m_fileStream.read((char*)data, length); - size_t bytesRead = m_fileStream.gcount(); - return (uint32)bytesRead; - } - - bool readU64(uint64& v) - { - return readData(&v, sizeof(uint64)) == sizeof(uint64); - } - - bool readU32(uint32& v) - { - return readData(&v, sizeof(uint32)) == sizeof(uint32); - } - - bool readU8(uint8& v) - { - return readData(&v, sizeof(uint8)) == sizeof(uint8); - } - - bool readLine(std::string& line) - { - line.clear(); - uint8 c; - bool isEOF = true; - while (readU8(c)) - { - isEOF = false; - if (c == '\r') - continue; - if (c == '\n') - break; - line.push_back((char)c); - } - return !isEOF; - } - - // writing (binary) - sint32 writeData(const void* data, sint32 length) - { - SyncReadWriteSeek(true); - m_fileStream.write((const char*)data, length); - return length; - } - - void writeU64(uint64 v) - { - writeData(&v, sizeof(uint64)); - } - - void writeU32(uint32 v) - { - writeData(&v, sizeof(uint32)); - } - - void writeU8(uint8 v) - { - writeData(&v, sizeof(uint8)); - } - - // writing (strings) - void writeStringFmt(const char* format, ...) - { - char buffer[2048]; - va_list args; - va_start(args, format); - vsnprintf(buffer, sizeof(buffer), format, args); - writeData(buffer, (sint32)strlen(buffer)); - } - - void writeString(const char* str) - { - writeData(str, (sint32)strlen(str)); - } - - void writeLine(const char* str) - { - writeData(str, (sint32)strlen(str)); - writeData("\r\n", 2); - } - - ~FileStream() - { - if (m_isValid) - { - m_fileStream.close(); - } - // CloseHandle(m_hFile); - } - - FileStream() {}; - -private: - FileStream(const fs::path& path, bool isOpen, bool isWriteable) - { - if (isOpen) - { - m_fileStream.open(path, isWriteable ? (std::ios_base::in | std::ios_base::out | std::ios_base::binary) : (std::ios_base::in | std::ios_base::binary)); - m_isValid = m_fileStream.is_open(); - } - else - { - m_fileStream.open(path, std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); - m_isValid = m_fileStream.is_open(); - } - } - - void SyncReadWriteSeek(bool nextOpIsWrite) - { - // nextOpIsWrite == false -> read. Otherwise write - if (nextOpIsWrite == m_prevOperationWasWrite) - return; - if (nextOpIsWrite) - m_fileStream.seekp(m_fileStream.tellg(), std::ios::beg); - else - m_fileStream.seekg(m_fileStream.tellp(), std::ios::beg); - - m_prevOperationWasWrite = nextOpIsWrite; - } - - bool m_isValid{}; - std::fstream m_fileStream; - bool m_prevOperationWasWrite{false}; -}; - -#endif \ No newline at end of file diff --git a/src/Common/unix/FileStream_unix.cpp b/src/Common/unix/FileStream_unix.cpp new file mode 100644 index 00000000..b1e95ae9 --- /dev/null +++ b/src/Common/unix/FileStream_unix.cpp @@ -0,0 +1,214 @@ +#include "Common/unix/FileStream_unix.h" + +FileStream* FileStream::openFile(std::string_view path) +{ + return openFile2(path, false); +} + +FileStream* FileStream::openFile(const wchar_t* path, bool allowWrite) +{ + return openFile2(path, allowWrite); +} + +FileStream* FileStream::openFile2(const fs::path& path, bool allowWrite) +{ + //return openFile(path.generic_wstring().c_str(), allowWrite); + FileStream* fs = new FileStream(path, true, allowWrite); + if (fs->m_isValid) + return fs; + delete fs; + return nullptr; +} + +FileStream* FileStream::createFile(const wchar_t* path) +{ + return createFile2(path); +} + +FileStream* FileStream::createFile(std::string_view path) +{ + return createFile2(path); +} + +FileStream* FileStream::createFile2(const fs::path& path) +{ + FileStream* fs = new FileStream(path, false, false); + if (fs->m_isValid) + return fs; + delete fs; + return nullptr; +} + +std::optional> FileStream::LoadIntoMemory(const fs::path& path) +{ + FileStream* fs = openFile2(path); + if (!fs) + return std::nullopt; + uint64 fileSize = fs->GetSize(); + if (fileSize > 0xFFFFFFFFull) + { + delete fs; + return std::nullopt; + } + std::optional> v(fileSize); + if (fs->readData(v->data(), (uint32)fileSize) != (uint32)fileSize) + { + delete fs; + return std::nullopt; + } + delete fs; + return v; +} + +void FileStream::SetPosition(uint64 pos) +{ + cemu_assert(m_isValid); + if (m_prevOperationWasWrite) + m_fileStream.seekp((std::streampos)pos); + else + m_fileStream.seekg((std::streampos)pos); +} + +uint64 FileStream::GetSize() +{ + cemu_assert(m_isValid); + auto currentPos = m_fileStream.tellg(); + m_fileStream.seekg(0, std::ios::end); + auto fileSize = m_fileStream.tellg(); + m_fileStream.seekg(currentPos, std::ios::beg); + uint64 fs = (uint64)fileSize; + return fs; +} + +bool FileStream::SetEndOfFile() +{ + assert_dbg(); + return true; + //return ::SetEndOfFile(m_hFile) != 0; +} + +void FileStream::extract(std::vector& data) +{ + uint64 fileSize = GetSize(); + SetPosition(0); + data.resize(fileSize); + readData(data.data(), fileSize); +} + +uint32 FileStream::readData(void* data, uint32 length) +{ + SyncReadWriteSeek(false); + m_fileStream.read((char*)data, length); + size_t bytesRead = m_fileStream.gcount(); + return (uint32)bytesRead; +} + +bool FileStream::readU64(uint64& v) +{ + return readData(&v, sizeof(uint64)) == sizeof(uint64); +} + +bool FileStream::readU32(uint32& v) +{ + return readData(&v, sizeof(uint32)) == sizeof(uint32); +} + +bool FileStream::readU8(uint8& v) +{ + return readData(&v, sizeof(uint8)) == sizeof(uint8); +} + +bool FileStream::readLine(std::string& line) +{ + line.clear(); + uint8 c; + bool isEOF = true; + while (readU8(c)) + { + isEOF = false; + if (c == '\r') + continue; + if (c == '\n') + break; + line.push_back((char)c); + } + return !isEOF; +} + +sint32 FileStream::writeData(const void* data, sint32 length) +{ + SyncReadWriteSeek(true); + m_fileStream.write((const char*)data, length); + return length; +} + +void FileStream::writeU64(uint64 v) +{ + writeData(&v, sizeof(uint64)); +} + +void FileStream::writeU32(uint32 v) +{ + writeData(&v, sizeof(uint32)); +} + +void FileStream::writeU8(uint8 v) +{ + writeData(&v, sizeof(uint8)); +} + +void FileStream::writeStringFmt(const char* format, ...) +{ + char buffer[2048]; + va_list args; + va_start(args, format); + vsnprintf(buffer, sizeof(buffer), format, args); + writeData(buffer, (sint32)strlen(buffer)); +} + +void FileStream::writeString(const char* str) +{ + writeData(str, (sint32)strlen(str)); +} + +void FileStream::writeLine(const char* str) +{ + writeData(str, (sint32)strlen(str)); + writeData("\r\n", 2); +} + +FileStream::~FileStream() +{ + if (m_isValid) + { + m_fileStream.close(); + } + // CloseHandle(m_hFile); +} + +FileStream::FileStream(const fs::path& path, bool isOpen, bool isWriteable) +{ + if (isOpen) + { + m_fileStream.open(path, isWriteable ? (std::ios_base::in | std::ios_base::out | std::ios_base::binary) : (std::ios_base::in | std::ios_base::binary)); + m_isValid = m_fileStream.is_open(); + } + else + { + m_fileStream.open(path, std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); + m_isValid = m_fileStream.is_open(); + } +} + +void FileStream::SyncReadWriteSeek(bool nextOpIsWrite) +{ + // nextOpIsWrite == false -> read. Otherwise write + if (nextOpIsWrite == m_prevOperationWasWrite) + return; + if (nextOpIsWrite) + m_fileStream.seekp(m_fileStream.tellg(), std::ios::beg); + else + m_fileStream.seekg(m_fileStream.tellp(), std::ios::beg); + + m_prevOperationWasWrite = nextOpIsWrite; +} diff --git a/src/Common/unix/FileStream_unix.h b/src/Common/unix/FileStream_unix.h new file mode 100644 index 00000000..12c971d1 --- /dev/null +++ b/src/Common/unix/FileStream_unix.h @@ -0,0 +1,56 @@ +#pragma once +#include "Common/precompiled.h" + +class FileStream +{ + public: + static FileStream* openFile(std::string_view path); + static FileStream* openFile(const wchar_t* path, bool allowWrite = false); + static FileStream* openFile2(const fs::path& path, bool allowWrite = false); + + static FileStream* createFile(const wchar_t* path); + static FileStream* createFile(std::string_view path); + static FileStream* createFile2(const fs::path& path); + + // helper function to load a file into memory + static std::optional> LoadIntoMemory(const fs::path& path); + + // size and seek + void SetPosition(uint64 pos); + + uint64 GetSize(); + bool SetEndOfFile(); + void extract(std::vector& data); + + // reading + uint32 readData(void* data, uint32 length); + bool readU64(uint64& v); + bool readU32(uint32& v); + bool readU16(uint16& v); + bool readU8(uint8& v); + bool readLine(std::string& line); + + // writing (binary) + sint32 writeData(const void* data, sint32 length); + void writeU64(uint64 v); + void writeU32(uint32 v); + void writeU16(uint16 v); + void writeU8(uint8 v); + + // writing (strings) + void writeStringFmt(const char* format, ...); + void writeString(const char* str); + void writeLine(const char* str); + + ~FileStream(); + FileStream() {}; + + private: + void SyncReadWriteSeek(bool nextOpIsWrite); + FileStream(const fs::path& path, bool isOpen, bool isWriteable); + + bool m_isValid{}; + std::fstream m_fileStream; + bool m_prevOperationWasWrite{false}; + +}; diff --git a/src/Common/windows/FileStream_win32.cpp b/src/Common/windows/FileStream_win32.cpp new file mode 100644 index 00000000..2ae1247f --- /dev/null +++ b/src/Common/windows/FileStream_win32.cpp @@ -0,0 +1,187 @@ +#include "Common/windows/FileStream_win32.h" + +FileStream* FileStream::openFile(std::string_view path) +{ + HANDLE hFile = CreateFileW(boost::nowide::widen(path.data(), path.size()).c_str(), FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); + if (hFile == INVALID_HANDLE_VALUE) + return nullptr; + return new FileStream(hFile); +} + +FileStream* FileStream::openFile(const wchar_t* path, bool allowWrite) +{ + HANDLE hFile = CreateFileW(path, allowWrite ? (FILE_GENERIC_READ | FILE_GENERIC_WRITE) : FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); + if (hFile == INVALID_HANDLE_VALUE) + return nullptr; + return new FileStream(hFile); +} + +FileStream* FileStream::openFile2(const fs::path& path, bool allowWrite) +{ + return openFile(path.generic_wstring().c_str(), allowWrite); +} + +FileStream* FileStream::createFile(const wchar_t* path) +{ + HANDLE hFile = CreateFileW(path, FILE_GENERIC_READ | FILE_GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0); + if (hFile == INVALID_HANDLE_VALUE) + return nullptr; + return new FileStream(hFile); +} + +FileStream* FileStream::createFile(std::string_view path) +{ + auto w = boost::nowide::widen(path.data(), path.size()); + HANDLE hFile = CreateFileW(w.c_str(), FILE_GENERIC_READ | FILE_GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0); + if (hFile == INVALID_HANDLE_VALUE) + return nullptr; + return new FileStream(hFile); +} + +FileStream* FileStream::createFile2(const fs::path& path) +{ + return createFile(path.generic_wstring().c_str()); +} + +std::optional> FileStream::LoadIntoMemory(const fs::path& path) +{ + FileStream* fs = openFile2(path); + if (!fs) + return std::nullopt; + uint64 fileSize = fs->GetSize(); + if(fileSize > 0xFFFFFFFFull) + { + delete fs; + return std::nullopt; + } + std::optional> v(fileSize); + if (fs->readData(v->data(), (uint32)fileSize) != (uint32)fileSize) + { + delete fs; + return std::nullopt; + } + delete fs; + return v; +} + +void FileStream::SetPosition(uint64 pos) +{ + LONG posHigh = (LONG)(pos >> 32); + LONG posLow = (LONG)(pos); + SetFilePointer(m_hFile, posLow, &posHigh, FILE_BEGIN); +} + +uint64 FileStream::GetSize() +{ + DWORD fileSizeHigh = 0; + DWORD fileSizeLow = 0; + fileSizeLow = GetFileSize(m_hFile, &fileSizeHigh); + return ((uint64)fileSizeHigh << 32) | (uint64)fileSizeLow; +} + +bool FileStream::SetEndOfFile() +{ + return ::SetEndOfFile(m_hFile) != 0; +} + +void FileStream::extract(std::vector& data) +{ + DWORD fileSize = GetFileSize(m_hFile, nullptr); + data.resize(fileSize); + SetFilePointer(m_hFile, 0, 0, FILE_BEGIN); + DWORD bt; + ReadFile(m_hFile, data.data(), fileSize, &bt, nullptr); +} + +uint32 FileStream::readData(void* data, uint32 length) +{ + DWORD bytesRead = 0; + ReadFile(m_hFile, data, length, &bytesRead, NULL); + return bytesRead; +} + +bool FileStream::readU64(uint64& v) +{ + return readData(&v, sizeof(uint64)) == sizeof(uint64); +} + +bool FileStream::readU32(uint32& v) +{ + return readData(&v, sizeof(uint32)) == sizeof(uint32); +} + +bool FileStream::readU8(uint8& v) +{ + return readData(&v, sizeof(uint8)) == sizeof(uint8); +} + +bool FileStream::readLine(std::string& line) +{ + line.clear(); + uint8 c; + bool isEOF = true; + while (readU8(c)) + { + isEOF = false; + if(c == '\r') + continue; + if (c == '\n') + break; + line.push_back((char)c); + } + return !isEOF; +} + +sint32 FileStream::writeData(const void* data, sint32 length) +{ + DWORD bytesWritten = 0; + WriteFile(m_hFile, data, length, &bytesWritten, NULL); + return bytesWritten; +} + +void FileStream::writeU64(uint64 v) +{ + writeData(&v, sizeof(uint64)); +} + +void FileStream::writeU32(uint32 v) +{ + writeData(&v, sizeof(uint32)); +} + +void FileStream::writeU8(uint8 v) +{ + writeData(&v, sizeof(uint8)); +} + +void FileStream::writeStringFmt(const char* format, ...) +{ + char buffer[2048]; + va_list args; + va_start(args, format); + vsnprintf(buffer, sizeof(buffer), format, args); + writeData(buffer, (sint32)strlen(buffer)); +} + +void FileStream::writeString(const char* str) +{ + writeData(str, (sint32)strlen(str)); +} + +void FileStream::writeLine(const char* str) +{ + writeData(str, (sint32)strlen(str)); + writeData("\r\n", 2); +} + +FileStream::~FileStream() +{ + if(m_isValid) + CloseHandle(m_hFile); +} + +FileStream::FileStream(HANDLE hFile) +{ + m_hFile = hFile; + m_isValid = true; +} diff --git a/src/Common/windows/FileStream_win32.h b/src/Common/windows/FileStream_win32.h new file mode 100644 index 00000000..c7ad802a --- /dev/null +++ b/src/Common/windows/FileStream_win32.h @@ -0,0 +1,53 @@ +#pragma once +#include "Common/precompiled.h" + +class FileStream +{ + public: + static FileStream* openFile(std::string_view path); + static FileStream* openFile(const wchar_t* path, bool allowWrite = false); + static FileStream* openFile2(const fs::path& path, bool allowWrite = false); + + static FileStream* createFile(const wchar_t* path); + static FileStream* createFile(std::string_view path); + static FileStream* createFile2(const fs::path& path); + + // helper function to load a file into memory + static std::optional> LoadIntoMemory(const fs::path& path); + + // size and seek + void SetPosition(uint64 pos); + + uint64 GetSize(); + bool SetEndOfFile(); + void extract(std::vector& data); + + // reading + uint32 readData(void* data, uint32 length); + bool readU64(uint64& v); + bool readU32(uint32& v); + bool readU16(uint16& v); + bool readU8(uint8& v); + bool readLine(std::string& line); + + // writing (binary) + sint32 writeData(const void* data, sint32 length); + void writeU64(uint64 v); + void writeU32(uint32 v); + void writeU16(uint16 v); + void writeU8(uint8 v); + + // writing (strings) + void writeStringFmt(const char* format, ...); + void writeString(const char* str); + void writeLine(const char* str); + + ~FileStream(); + FileStream() {}; + + private: + FileStream(HANDLE hFile); + + bool m_isValid{}; + HANDLE m_hFile; +}; diff --git a/src/config/XMLConfig.h b/src/config/XMLConfig.h index 04e3cfbf..c2cba527 100644 --- a/src/config/XMLConfig.h +++ b/src/config/XMLConfig.h @@ -1,7 +1,7 @@ #pragma once #include "util/tinyxml2/tinyxml2.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "config/ConfigValue.h" #include diff --git a/src/gui/CemuUpdateWindow.cpp b/src/gui/CemuUpdateWindow.cpp index ef3aaab1..ef537a58 100644 --- a/src/gui/CemuUpdateWindow.cpp +++ b/src/gui/CemuUpdateWindow.cpp @@ -4,7 +4,7 @@ #include "util/helpers/helpers.h" #include "util/helpers/SystemException.h" #include "config/ActiveSettings.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include #include diff --git a/src/gui/DownloadGraphicPacksWindow.cpp b/src/gui/DownloadGraphicPacksWindow.cpp index 28e5bf6b..973b7c57 100644 --- a/src/gui/DownloadGraphicPacksWindow.cpp +++ b/src/gui/DownloadGraphicPacksWindow.cpp @@ -8,7 +8,7 @@ #include #include "config/ActiveSettings.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "Cafe/CafeSystem.h" diff --git a/src/gui/MemorySearcherTool.cpp b/src/gui/MemorySearcherTool.cpp index c7c6c734..904be1a0 100644 --- a/src/gui/MemorySearcherTool.cpp +++ b/src/gui/MemorySearcherTool.cpp @@ -8,7 +8,7 @@ #include "config/ActiveSettings.h" #include "gui/helpers/wxHelpers.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" #include "util/IniParser/IniParser.h" #include "util/helpers/StringHelpers.h" #include "Cafe/CafeSystem.h" diff --git a/src/gui/components/wxTitleManagerList.cpp b/src/gui/components/wxTitleManagerList.cpp index 99e6ed94..57e9ee22 100644 --- a/src/gui/components/wxTitleManagerList.cpp +++ b/src/gui/components/wxTitleManagerList.cpp @@ -31,7 +31,7 @@ #include #include -#include "Common/filestream.h" +#include "Common/FileStream.h" wxDEFINE_EVENT(wxEVT_TITLE_FOUND, wxCommandEvent); wxDEFINE_EVENT(wxEVT_TITLE_REMOVED, wxCommandEvent); diff --git a/src/mainLLE.cpp b/src/mainLLE.cpp index 2757c22c..2b7c18a0 100644 --- a/src/mainLLE.cpp +++ b/src/mainLLE.cpp @@ -2,7 +2,7 @@ #include "util/crypto/aes128.h" #include "gui/MainWindow.h" #include "gui/guiWrapper.h" -#include "Common/filestream.h" +#include "Common/FileStream.h" void mainEmulatorCommonInit(); diff --git a/src/util/ImageWriter/bmp.h b/src/util/ImageWriter/bmp.h index 46a9fb48..4cc2216f 100644 --- a/src/util/ImageWriter/bmp.h +++ b/src/util/ImageWriter/bmp.h @@ -1,4 +1,4 @@ -#include "Common/filestream.h" +#include "Common/FileStream.h" static void _bmp_write(FileStream* fs, sint32 width, sint32 height, uint32 bits, void* pixelData) { diff --git a/src/util/ImageWriter/tga.h b/src/util/ImageWriter/tga.h index e8d51741..822bdaae 100644 --- a/src/util/ImageWriter/tga.h +++ b/src/util/ImageWriter/tga.h @@ -1,4 +1,4 @@ -#include "Common/filestream.h" +#include "Common/FileStream.h" #include static bool tga_write_rgba(wchar_t* path, sint32 width, sint32 height, uint8* pixelData)