From ab405901ee84dfa17dc432ef9a2b5d745bb5acaf Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Sun, 19 Apr 2015 16:19:24 +0300 Subject: [PATCH] wxFile removed (rFile -> rfile_t) --- Utilities/AutoPause.cpp | 10 +- Utilities/BEType.h | 84 ------ Utilities/Log.cpp | 11 +- Utilities/rFile.cpp | 306 +++++++++++--------- Utilities/rFile.h | 184 ++++++++++-- rpcs3/Crypto/unedat.cpp | 82 +++--- rpcs3/Crypto/unedat.h | 2 +- rpcs3/Crypto/unpkg.cpp | 122 ++++---- rpcs3/Crypto/unpkg.h | 41 ++- rpcs3/Crypto/unself.cpp | 153 +++++----- rpcs3/Emu/Audio/AudioDumper.cpp | 12 +- rpcs3/Emu/Audio/AudioDumper.h | 2 +- rpcs3/Emu/FS/VFS.cpp | 19 +- rpcs3/Emu/FS/VFS.h | 4 +- rpcs3/Emu/FS/vfsFile.cpp | 15 +- rpcs3/Emu/FS/vfsFile.h | 11 +- rpcs3/Emu/FS/vfsFileBase.cpp | 11 +- rpcs3/Emu/FS/vfsFileBase.h | 20 +- rpcs3/Emu/FS/vfsLocalFile.cpp | 95 +----- rpcs3/Emu/FS/vfsLocalFile.h | 12 +- rpcs3/Emu/FS/vfsStream.cpp | 78 ----- rpcs3/Emu/FS/vfsStream.h | 62 ++-- rpcs3/Emu/FS/vfsStreamMemory.cpp | 48 +-- rpcs3/Emu/FS/vfsStreamMemory.h | 58 +++- rpcs3/Emu/HDD/HDD.cpp | 61 ++-- rpcs3/Emu/HDD/HDD.h | 23 +- rpcs3/Emu/RSX/CgBinaryProgram.h | 18 +- rpcs3/Emu/RSX/GL/GLGSRender.cpp | 12 +- rpcs3/Emu/SysCalls/Modules/cellFs.cpp | 6 +- rpcs3/Emu/SysCalls/Modules/cellSaveData.cpp | 12 +- rpcs3/Emu/SysCalls/lv2/sys_fs.cpp | 108 ++----- rpcs3/Emu/System.cpp | 5 +- rpcs3/Gui/AutoPauseManager.cpp | 24 +- rpcs3/Gui/MainFrame.cpp | 12 +- rpcs3/Gui/VHDDManager.cpp | 2 +- rpcs3/Loader/ELF32.cpp | 6 +- rpcs3/Loader/ELF32.h | 1 - rpcs3/Loader/ELF64.h | 1 - rpcs3/Loader/Loader.h | 1 - rpcs3/Loader/PKG.cpp | 32 +- rpcs3/Loader/PKG.h | 11 +- rpcs3/Loader/TROPUSR.cpp | 5 +- rpcs3/Loader/TRP.cpp | 5 +- 43 files changed, 814 insertions(+), 973 deletions(-) diff --git a/Utilities/AutoPause.cpp b/Utilities/AutoPause.cpp index b1e4c3ed0a..94e4f058bf 100644 --- a/Utilities/AutoPause.cpp +++ b/Utilities/AutoPause.cpp @@ -51,16 +51,15 @@ void AutoPause::Reload(void) m_pause_syscall.clear(); m_pause_syscall.reserve(16); - rFile list; - list.Open("pause.bin", rFile::read); + rfile_t list("pause.bin"); //System calls ID and Function calls ID are all u32 iirc. u32 num; - size_t fmax = list.Length(); + size_t fmax = list.size(); size_t fcur = 0; - list.Seek(0); + list.seek(0); while (fcur <= fmax - sizeof(u32)) { - list.Read(&num, sizeof(u32)); + list.read(&num, sizeof(u32)); fcur += sizeof(u32); if (num == 0xFFFFFFFF) break; @@ -77,7 +76,6 @@ void AutoPause::Reload(void) LOG_WARNING(HLE, "Auto Pause: Find Function Call ID 0x%x", num); } } - list.Close(); } else { diff --git a/Utilities/BEType.h b/Utilities/BEType.h index 96ef78f59d..ec2ba15605 100644 --- a/Utilities/BEType.h +++ b/Utilities/BEType.h @@ -879,90 +879,6 @@ template struct _se, T1, value> : pub #define se32(x) _se::value #define se64(x) _se::value -template __forceinline u8 Read8(T& f) -{ - u8 ret; - f.Read(&ret, sizeof(ret)); - return ret; -} - -template __forceinline u16 Read16(T& f) -{ - be_t ret; - f.Read(&ret, sizeof(ret)); - return ret; -} - -template __forceinline u32 Read32(T& f) -{ - be_t ret; - f.Read(&ret, sizeof(ret)); - return ret; -} - -template __forceinline u64 Read64(T& f) -{ - be_t ret; - f.Read(&ret, sizeof(ret)); - return ret; -} - -template __forceinline u16 Read16LE(T& f) -{ - u16 ret; - f.Read(&ret, sizeof(ret)); - return ret; -} - -template __forceinline u32 Read32LE(T& f) -{ - u32 ret; - f.Read(&ret, sizeof(ret)); - return ret; -} - -template __forceinline u64 Read64LE(T& f) -{ - u64 ret; - f.Read(&ret, sizeof(ret)); - return ret; -} - -template __forceinline void Write8(T& f, const u8 data) -{ - f.Write(&data, sizeof(data)); -} - -template __forceinline void Write16LE(T& f, const u16 data) -{ - f.Write(&data, sizeof(data)); -} - -template __forceinline void Write32LE(T& f, const u32 data) -{ - f.Write(&data, sizeof(data)); -} - -template __forceinline void Write64LE(T& f, const u64 data) -{ - f.Write(&data, sizeof(data)); -} - -template __forceinline void Write16(T& f, const u16 data) -{ - Write16LE(f, re16(data)); -} - -template __forceinline void Write32(T& f, const u32 data) -{ - Write32LE(f, re32(data)); -} - -template __forceinline void Write64(T& f, const u64 data) -{ - Write64LE(f, re64(data)); -} - template struct convert_le_be_t { diff --git a/Utilities/Log.cpp b/Utilities/Log.cpp index 5aaf98579d..9add021af4 100644 --- a/Utilities/Log.cpp +++ b/Utilities/Log.cpp @@ -90,14 +90,14 @@ struct CoutListener : LogListener struct FileListener : LogListener { - rFile mFile; + rfile_t mFile; bool mPrependChannelName; FileListener(const std::string& name = _PRGNAME_, bool prependChannel = true) - : mFile(std::string(rPlatform::getConfigDir() + name + ".log").c_str(), rFile::write), - mPrependChannelName(prependChannel) + : mFile(rPlatform::getConfigDir() + name + ".log", o_write | o_create | o_trunc) + , mPrependChannelName(prependChannel) { - if (!mFile.IsOpened()) + if (!mFile) { rMessageBox("Can't create log file! (" + name + ".log)", "Error", rICON_ERROR); } @@ -119,7 +119,8 @@ struct FileListener : LogListener } } } - mFile.Write(text); + + mFile.write(text.c_str(), text.size()); } }; diff --git a/Utilities/rFile.cpp b/Utilities/rFile.cpp index 5229474d97..5013734cda 100644 --- a/Utilities/rFile.cpp +++ b/Utilities/rFile.cpp @@ -2,7 +2,6 @@ #include "Log.h" #pragma warning(disable : 4996) #include -#include #include #include "rFile.h" @@ -38,6 +37,7 @@ time_t to_time_t(const FILETIME& ft) bool truncate_file(const std::string& file, uint64_t length) { + // open the file const auto handle = CreateFileW(ConvertUTF8ToWChar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) @@ -48,15 +48,12 @@ bool truncate_file(const std::string& file, uint64_t length) LARGE_INTEGER distance; distance.QuadPart = length; - if (!SetFilePointerEx(handle, distance, NULL, FILE_BEGIN)) - { - CloseHandle(handle); - return false; - } - - if (!SetEndOfFile(handle)) + // seek and truncate + if (!SetFilePointerEx(handle, distance, NULL, FILE_BEGIN) || !SetEndOfFile(handle)) { + const auto error = GetLastError(); CloseHandle(handle); + SetLastError(error); return false; } @@ -297,7 +294,7 @@ bool rTruncate(const std::string& file, uint64_t length) #ifdef _WIN32 if (!truncate_file(file, length)) #else - if (truncate64(file.c_str()), length) + if (truncate64(file.c_str(), length)) #endif { LOG_ERROR(GENERAL, "Error resizing file '%s' to 0x%llx: 0x%llx", file, length, GET_API_ERROR); @@ -307,164 +304,207 @@ bool rTruncate(const std::string& file, uint64_t length) return true; } -wxFile::OpenMode convertOpenMode(rFile::OpenMode open) +rfile_t::rfile_t() +#ifdef _WIN32 + : fd(INVALID_HANDLE_VALUE) +#else + : fd(-1) +#endif { - wxFile::OpenMode mode; - switch (open) +} + +rfile_t::~rfile_t() +{ +#ifdef _WIN32 + if (fd != INVALID_HANDLE_VALUE) { - case rFile::read: - mode = wxFile::read; - break; - case rFile::write: - mode = wxFile::write; - break; - case rFile::read_write: - mode = wxFile::read_write; - break; - case rFile::write_append: - mode = wxFile::write_append; - break; - case rFile::write_excl: - mode = wxFile::write_excl; - break; + CloseHandle(fd); } - return mode; -} - -rFile::OpenMode rConvertOpenMode(wxFile::OpenMode open) -{ - rFile::OpenMode mode; - switch (open) +#else + if (fd != -1) { - case wxFile::read: - mode = rFile::read; - break; - case wxFile::write: - mode = rFile::write; - break; - case wxFile::read_write: - mode = rFile::read_write; - break; - case wxFile::write_append: - mode = rFile::write_append; - break; - case wxFile::write_excl: - mode = rFile::write_excl; - break; + close(fd); } - return mode; +#endif } -wxSeekMode convertSeekMode(rSeekMode mode) +rfile_t::rfile_t(handle_type handle) + : fd(handle) { - wxSeekMode ret; - switch (mode) +} + +rfile_t::rfile_t(const std::string& filename, u32 mode) +#ifdef _WIN32 + : fd(INVALID_HANDLE_VALUE) +#else + : fd(-1) +#endif +{ + open(filename, mode); +} + +rfile_t::operator bool() const +{ +#ifdef _WIN32 + return fd != INVALID_HANDLE_VALUE; +#else + return fd != -1; +#endif +} + +bool rfile_t::open(const std::string& filename, u32 mode) +{ + this->~rfile_t(); + +#ifdef _WIN32 + DWORD access = 0; + if (mode & o_read) access |= GENERIC_READ; + if (mode & o_write) access |= GENERIC_WRITE; + + DWORD disp = 0; + switch (mode & (o_create | o_trunc | o_excl)) { - case rFromStart: - ret = wxFromStart; - break; - case rFromCurrent: - ret = wxFromCurrent; - break; - case rFromEnd: - ret = wxFromEnd; - break; - } - return ret; -} - -rSeekMode rConvertSeekMode(wxSeekMode mode) -{ - rSeekMode ret; - switch (mode) + case 0: disp = OPEN_EXISTING; break; + case o_create: disp = OPEN_ALWAYS; break; + case o_trunc: disp = TRUNCATE_EXISTING; break; + case o_create | o_trunc: disp = CREATE_ALWAYS; break; + case o_create | o_excl: disp = CREATE_NEW; break; + case o_excl: // ??? + case o_trunc | o_excl: // ??? + case o_create | o_trunc | o_excl: // ??? { - case wxFromStart: - ret = rFromStart; - break; - case wxFromCurrent: - ret = rFromCurrent; - break; - case wxFromEnd: - ret = rFromEnd; - break; + LOG_ERROR(GENERAL, "rfile_t::open(): unknown mode specified (0x%x)", mode); + return false; } - return ret; + } + + fd = CreateFileW(ConvertUTF8ToWChar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL); +#else + int flags = 0; + if (mode & o_read) flags |= O_READ; + if (mode & o_write) flags |= O_WRITE; + if (mode & o_create) flags |= O_CREAT; + if (mode & o_trunc) flags |= O_TRUNC; + if (mode & o_excl) flags |= O_EXCL; + + fd = open(filename.c_str(), flags, 0666); +#endif + + return is_opened(); } - -rFile::rFile() +bool rfile_t::is_opened() const { - handle = reinterpret_cast(new wxFile()); + return *this; } -rFile::rFile(const std::string& filename, rFile::OpenMode open) +bool rfile_t::trunc(u64 size) const { - handle = reinterpret_cast(new wxFile(fmt::FromUTF8(filename), convertOpenMode(open))); +#ifdef _WIN32 + LARGE_INTEGER old, pos; + + pos.QuadPart = 0; + SetFilePointerEx(fd, pos, &old, FILE_CURRENT); // get old position + + pos.QuadPart = size; + SetFilePointerEx(fd, pos, NULL, FILE_BEGIN); // set new position + + SetEndOfFile(fd); // change file size + SetFilePointerEx(fd, old, NULL, FILE_BEGIN); // restore position + + return true; // TODO +#else + return !ftruncate64(fd, size); +#endif } -rFile::rFile(int fd) +bool rfile_t::close() { - handle = reinterpret_cast(new wxFile(fd)); +#ifdef _WIN32 + if (CloseHandle(fd)) + { + fd = INVALID_HANDLE_VALUE; + return true; + } +#else + if (!close(fd)) + { + fd = -1; + return true; + } +#endif + + return false; } -rFile::~rFile() +u64 rfile_t::read(void* buffer, u64 count) const { - delete reinterpret_cast(handle); +#ifdef _WIN32 + DWORD nread; + if (!ReadFile(fd, buffer, count, &nread, NULL)) + { + return -1; + } + + return nread; +#else + return read64(fd, buffer, count); +#endif } -bool rFile::Access(const std::string &filename, rFile::OpenMode mode) +u64 rfile_t::write(const void* buffer, u64 count) const { - return wxFile::Access(fmt::FromUTF8(filename), convertOpenMode(mode)); +#ifdef _WIN32 + DWORD nwritten; + if (!WriteFile(fd, buffer, count, &nwritten, NULL)) + { + return -1; + } + + return nwritten; +#else + return write64(fd, buffer, count); +#endif } -size_t rFile::Write(const void *buffer, size_t count) +u64 rfile_t::seek(u64 offset, u32 mode) const { - return reinterpret_cast(handle)->Write(buffer,count); + assert(mode < 3); + +#ifdef _WIN32 + LARGE_INTEGER pos; + pos.QuadPart = offset; + + if (!SetFilePointerEx(fd, pos, &pos, mode)) + { + return -1; + } + + return pos.QuadPart; +#else + return lseek64(fd, offset, mode); +#endif } -bool rFile::Write(const std::string &text) +u64 rfile_t::size() const { - return reinterpret_cast(handle)->Write(reinterpret_cast(text.c_str()),text.size()) != 0; -} +#ifdef _WIN32 + LARGE_INTEGER size; + if (!GetFileSizeEx(fd, &size)) + { + return -1; + } -bool rFile::Close() -{ - return reinterpret_cast(handle)->Close(); -} + return size.QuadPart; +#else + struct stat64 file_info; + if (fstat64(fd, &file_info) < 0) + { + return -1; + } -bool rFile::Create(const std::string &filename, bool overwrite, int access) -{ - return reinterpret_cast(handle)->Create(fmt::FromUTF8(filename), overwrite, access); -} - -bool rFile::Open(const std::string &filename, rFile::OpenMode mode, int access) -{ - return reinterpret_cast(handle)->Open(fmt::FromUTF8(filename), convertOpenMode(mode), access); -} - -bool rFile::IsOpened() const -{ - return reinterpret_cast(handle)->IsOpened(); -} - -size_t rFile::Length() const -{ - return reinterpret_cast(handle)->Length(); -} - -size_t rFile::Read(void *buffer, size_t count) -{ - return reinterpret_cast(handle)->Read(buffer,count); -} - -size_t rFile::Seek(size_t ofs, rSeekMode mode) -{ - return reinterpret_cast(handle)->Seek(ofs, convertSeekMode(mode)); -} - -size_t rFile::Tell() const -{ - return reinterpret_cast(handle)->Tell(); + return file_info.st_size; +#endif } rDir::rDir() diff --git a/Utilities/rFile.h b/Utilities/rFile.h index d7e9f6fa12..144eac09e2 100644 --- a/Utilities/rFile.h +++ b/Utilities/rFile.h @@ -24,43 +24,57 @@ bool rExists(const std::string& file); bool rRemoveFile(const std::string& file); bool rTruncate(const std::string& file, uint64_t length); -enum rSeekMode +enum rfile_seek_mode : u32 { - rFromStart, - rFromCurrent, - rFromEnd + from_begin, + from_cur, + from_end, }; -class rFile +enum rfile_open_mode : u32 { -public: - enum OpenMode - { - read, - write, - read_write, - write_append, - write_excl - }; - rFile(); - rFile(const rFile& other) = delete; - ~rFile(); - rFile(const std::string& filename, rFile::OpenMode open = rFile::read); - rFile(int fd); - static bool Access(const std::string &filename, rFile::OpenMode mode); - size_t Write(const void *buffer, size_t count); - bool Write(const std::string &text); - bool Close(); - bool Create(const std::string &filename, bool overwrite = false, int access = 0666); - bool Open(const std::string &filename, rFile::OpenMode mode = rFile::read, int access = 0666); - static bool Exists(const std::string&); - bool IsOpened() const; - size_t Length() const; - size_t Read(void *buffer, size_t count); - size_t Seek(size_t ofs, rSeekMode mode = rFromStart); - size_t Tell() const; + o_read = 1 << 0, + o_write = 1 << 1, + o_create = 1 << 2, + o_trunc = 1 << 3, + o_excl = 1 << 4, +}; - void *handle; +struct rfile_t final +{ +#ifdef _WIN32 + using handle_type = void*; +#else + using handle_type = int; +#endif + +private: + handle_type fd; + +public: + rfile_t(); + ~rfile_t(); + explicit rfile_t(handle_type fd); + explicit rfile_t(const std::string& filename, u32 mode = o_read); + + rfile_t(const rfile_t&) = delete; + rfile_t(rfile_t&&) = delete; + + rfile_t& operator =(const rfile_t&) = delete; + rfile_t& operator =(rfile_t&&) = delete; + + operator bool() const; + + bool open(const std::string& filename, u32 mode = o_read); + bool is_opened() const; + bool trunc(u64 size) const; + bool close(); + + u64 read(void* buffer, u64 count) const; + u64 write(const void* buffer, u64 count) const; + //u64 write(const std::string& str) const; + u64 seek(u64 offset, u32 mode = from_begin) const; + u64 size() const; }; struct rDir @@ -91,3 +105,109 @@ struct rFileName void *handle; }; + +// TODO: eliminate this: + +template __forceinline u8 Read8(T& f) +{ + u8 ret; + f.Read(&ret, sizeof(ret)); + return ret; +} + +template __forceinline u16 Read16(T& f) +{ + be_t ret; + f.Read(&ret, sizeof(ret)); + return ret; +} + +template __forceinline u32 Read32(T& f) +{ + be_t ret; + f.Read(&ret, sizeof(ret)); + return ret; +} + +template __forceinline u64 Read64(T& f) +{ + be_t ret; + f.Read(&ret, sizeof(ret)); + return ret; +} + +template __forceinline u16 Read16LE(T& f) +{ + u16 ret; + f.Read(&ret, sizeof(ret)); + return ret; +} + +template __forceinline u32 Read32LE(T& f) +{ + u32 ret; + f.Read(&ret, sizeof(ret)); + return ret; +} + +template __forceinline u64 Read64LE(T& f) +{ + u64 ret; + f.Read(&ret, sizeof(ret)); + return ret; +} + +template __forceinline void Write8(T& f, const u8 data) +{ + f.Write(&data, sizeof(data)); +} + +__forceinline void Write8(const rfile_t& f, const u8 data) +{ + f.write(&data, sizeof(data)); +} + +template __forceinline void Write16LE(T& f, const u16 data) +{ + f.Write(&data, sizeof(data)); +} + +__forceinline void Write16LE(const rfile_t& f, const u16 data) +{ + f.write(&data, sizeof(data)); +} + +template __forceinline void Write32LE(T& f, const u32 data) +{ + f.Write(&data, sizeof(data)); +} + +__forceinline void Write32LE(const rfile_t& f, const u32 data) +{ + f.write(&data, sizeof(data)); +} + +template __forceinline void Write64LE(T& f, const u64 data) +{ + f.Write(&data, sizeof(data)); +} + +__forceinline void Write64LE(const rfile_t& f, const u64 data) +{ + f.write(&data, sizeof(data)); +} + +template __forceinline void Write16(T& f, const u16 data) +{ + Write16LE(f, re16(data)); +} + +template __forceinline void Write32(T& f, const u32 data) +{ + Write32LE(f, re32(data)); +} + +template __forceinline void Write64(T& f, const u64 data) +{ + Write64LE(f, re64(data)); +} diff --git a/rpcs3/Crypto/unedat.cpp b/rpcs3/Crypto/unedat.cpp index f4d57c49d0..c7d89b4346 100644 --- a/rpcs3/Crypto/unedat.cpp +++ b/rpcs3/Crypto/unedat.cpp @@ -136,7 +136,7 @@ unsigned char* get_block_key(int block, NPD_HEADER *npd) } // EDAT/SDAT decryption. -int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsigned char* crypt_key, bool verbose) +int decrypt_data(const rfile_t *in, const rfile_t *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsigned char* crypt_key, bool verbose) { // Get metadata info and setup buffers. int block_num = (int)((edat->file_size + edat->block_size - 1) / edat->block_size); @@ -170,11 +170,11 @@ int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsi if ((edat->flags & EDAT_COMPRESSED_FLAG) != 0) { metadata_sec_offset = metadata_offset + (unsigned long long) i * metadata_section_size; - in->Seek(metadata_sec_offset); + in->seek(metadata_sec_offset); unsigned char metadata[0x20]; memset(metadata, 0, 0x20); - in->Read(metadata, 0x20); + in->read(metadata, 0x20); // If the data is compressed, decrypt the metadata. // NOTE: For NPD version 1 the metadata is not encrypted. @@ -199,11 +199,11 @@ int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsi { // If FLAG 0x20, the metadata precedes each data block. metadata_sec_offset = metadata_offset + (unsigned long long) i * (metadata_section_size + length); - in->Seek(metadata_sec_offset); + in->seek(metadata_sec_offset); unsigned char metadata[0x20]; memset(metadata, 0, 0x20); - in->Read(metadata, 0x20); + in->read(metadata, 0x20); memcpy(hash_result, metadata, 0x14); // If FLAG 0x20 is set, apply custom xor. @@ -220,9 +220,9 @@ int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsi else { metadata_sec_offset = metadata_offset + (unsigned long long) i * metadata_section_size; - in->Seek(metadata_sec_offset); + in->seek(metadata_sec_offset); - in->Read(hash_result, 0x10); + in->read(hash_result, 0x10); offset = metadata_offset + (unsigned long long) i * edat->block_size + (unsigned long long) block_num * metadata_section_size; length = edat->block_size; @@ -242,8 +242,8 @@ int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsi memset(hash, 0, 0x10); memset(key_result, 0, 0x10); - in->Seek(offset); - in->Read(enc_data, length); + in->seek(offset); + in->read(enc_data, length); // Generate a key for the current block. b_key = get_block_key(i, npd); @@ -305,7 +305,7 @@ int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsi LOG_NOTICE(LOADER, "EDAT: Decompressing data..."); int res = decompress(decomp_data, dec_data, decomp_size); - out->Write(decomp_data, res); + out->write(decomp_data, res); if (verbose) { @@ -330,7 +330,7 @@ int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsi } else { - out->Write(dec_data, pad_length); + out->write(dec_data, pad_length); } delete[] enc_data; @@ -340,9 +340,9 @@ int decrypt_data(rFile *in, rFile *out, EDAT_HEADER *edat, NPD_HEADER *npd, unsi return 0; } -int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, rFile *f, bool verbose) +int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, const rfile_t *f, bool verbose) { - f->Seek(0); + f->seek(0); unsigned char header[0xA0]; unsigned char empty_header[0xA0]; unsigned char header_hash[0x10]; @@ -384,12 +384,12 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, rFile *f, } // Read in the file header. - f->Read(header, 0xA0); + f->read(header, 0xA0); // Read in the header and metadata section hashes. - f->Seek(0x90); - f->Read(metadata_hash, 0x10); - f->Read(header_hash, 0x10); + f->seek(0x90); + f->read(metadata_hash, 0x10); + f->read(header_hash, 0x10); // Setup the hashing mode and the crypto mode used in the file. int crypto_mode = 0x1; @@ -443,10 +443,10 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, rFile *f, while (bytes_to_read > 0) { // Locate the metadata blocks. - f->Seek(metadata_section_offset); + f->seek(metadata_section_offset); // Read in the metadata. - f->Read(metadata + bytes_read, metadata_section_size); + f->read(metadata + bytes_read, metadata_section_size); // Adjust sizes. bytes_read += metadata_section_size; @@ -490,10 +490,10 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, rFile *f, // Read in the metadata and header signatures. - f->Seek(0xB0); - f->Read(metadata_signature, 0x28); - f->Seek(0xD8); - f->Read(header_signature, 0x28); + f->seek(0xB0); + f->read(metadata_signature, 0x28); + f->seek(0xD8); + f->read(header_signature, 0x28); // Checking metadata signature. // Setup signature r and s. @@ -508,8 +508,8 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, rFile *f, { int metadata_buf_size = block_num * 0x10; unsigned char *metadata_buf = new unsigned char[metadata_buf_size]; - f->Seek(metadata_offset); - f->Read(metadata_buf, metadata_buf_size); + f->seek(metadata_offset); + f->read(metadata_buf, metadata_buf_size); sha1(metadata_buf, metadata_buf_size, signature_hash); delete[] metadata_buf; } @@ -541,8 +541,8 @@ int check_data(unsigned char *key, EDAT_HEADER *edat, NPD_HEADER *npd, rFile *f, // Setup header signature hash. memset(signature_hash, 0, 20); unsigned char *header_buf = new unsigned char[0xD8]; - f->Seek(0x00); - f->Read(header_buf, 0xD8); + f->seek(0x00); + f->read(header_buf, 0xD8); sha1(header_buf, 0xD8, signature_hash ); delete[] header_buf; @@ -639,7 +639,7 @@ int validate_npd_hashes(const char* file_name, unsigned char *klicensee, NPD_HEA return (title_hash_result && dev_hash_result); } -bool extract_data(rFile *input, rFile *output, const char* input_file_name, unsigned char* devklic, unsigned char* rifkey, bool verbose) +bool extract_data(const rfile_t *input, const rfile_t *output, const char* input_file_name, unsigned char* devklic, unsigned char* rifkey, bool verbose) { // Setup NPD and EDAT/SDAT structs. NPD_HEADER *NPD = new NPD_HEADER(); @@ -648,8 +648,8 @@ bool extract_data(rFile *input, rFile *output, const char* input_file_name, unsi // Read in the NPD and EDAT/SDAT headers. char npd_header[0x80]; char edat_header[0x10]; - input->Read(npd_header, sizeof(npd_header)); - input->Read(edat_header, sizeof(edat_header)); + input->read(npd_header, sizeof(npd_header)); + input->read(edat_header, sizeof(edat_header)); memcpy(NPD->magic, npd_header, 4); NPD->version = swap32(*(int*)&npd_header[4]); @@ -812,9 +812,9 @@ bool extract_data(rFile *input, rFile *output, const char* input_file_name, unsi int DecryptEDAT(const std::string& input_file_name, const std::string& output_file_name, int mode, const std::string& rap_file_name, unsigned char *custom_klic, bool verbose) { // Prepare the files. - rFile input(input_file_name.c_str()); - rFile output(output_file_name.c_str(), rFile::write); - rFile rap(rap_file_name.c_str()); + rfile_t input(input_file_name); + rfile_t output(output_file_name, o_write | o_create | o_trunc); + rfile_t rap(rap_file_name); // Set keys (RIF and DEVKLIC). unsigned char rifkey[0x10]; @@ -866,36 +866,30 @@ int DecryptEDAT(const std::string& input_file_name, const std::string& output_fi } // Check the input/output files. - if (!input.IsOpened() || !output.IsOpened()) + if (!input || !output) { LOG_ERROR(LOADER, "EDAT: Failed to open files!"); return -1; } // Read the RAP file, if provided. - if (rap.IsOpened()) + if (rap) { unsigned char rapkey[0x10]; memset(rapkey, 0, 0x10); - rap.Read(rapkey, 0x10); + rap.read(rapkey, 0x10); rap_to_rif(rapkey, rifkey); - - rap.Close(); } // Delete the bad output file if any errors arise. if (extract_data(&input, &output, input_file_name.c_str(), devklic, rifkey, verbose)) { - input.Close(); - output.Close(); + output.close(); rRemoveFile(output_file_name); return -1; } - // Cleanup. - input.Close(); - output.Close(); return 0; -} \ No newline at end of file +} diff --git a/rpcs3/Crypto/unedat.h b/rpcs3/Crypto/unedat.h index f1a9f81ab5..4513902e83 100644 --- a/rpcs3/Crypto/unedat.h +++ b/rpcs3/Crypto/unedat.h @@ -33,4 +33,4 @@ typedef struct unsigned long long file_size; } EDAT_HEADER; -int DecryptEDAT(const std::string& input_file_name, const std::string& output_file_name, int mode, const std::string& rap_file_name, unsigned char *custom_klic, bool verbose); \ No newline at end of file +int DecryptEDAT(const std::string& input_file_name, const std::string& output_file_name, int mode, const std::string& rap_file_name, unsigned char *custom_klic, bool verbose); diff --git a/rpcs3/Crypto/unpkg.cpp b/rpcs3/Crypto/unpkg.cpp index b4d18eb92d..e96895055c 100644 --- a/rpcs3/Crypto/unpkg.cpp +++ b/rpcs3/Crypto/unpkg.cpp @@ -13,54 +13,63 @@ #include "Utilities/rFile.h" // Decryption. -bool CheckHeader(rFile& pkg_f, PKGHeader* m_header) +bool CheckHeader(const rfile_t& pkg_f, PKGHeader* m_header) { - if (m_header->pkg_magic != 0x7F504B47) { + if (m_header->pkg_magic != 0x7F504B47) + { LOG_ERROR(LOADER, "PKG: Not a package file!"); return false; } - switch ((u32)m_header->pkg_type) + switch (const u16 type = m_header->pkg_type) { case PKG_RELEASE_TYPE_DEBUG: break; case PKG_RELEASE_TYPE_RELEASE: break; default: - LOG_ERROR(LOADER, "PKG: Unknown PKG type!"); + { + LOG_ERROR(LOADER, "PKG: Unknown PKG type (0x%x)", type); return false; } + } - switch ((u32)m_header->pkg_platform) + switch (const u16 platform = m_header->pkg_platform) { case PKG_PLATFORM_TYPE_PS3: break; case PKG_PLATFORM_TYPE_PSP: break; default: - LOG_ERROR(LOADER, "PKG: Unknown PKG type!"); + { + LOG_ERROR(LOADER, "PKG: Unknown PKG platform (0x%x)", platform); + return false; + } + } + + if (m_header->header_size != PKG_HEADER_SIZE) + { + LOG_ERROR(LOADER, "PKG: Wrong header size (0x%x)", m_header->header_size); return false; } - if (m_header->header_size != PKG_HEADER_SIZE) { - LOG_ERROR(LOADER, "PKG: Wrong header size!"); - return false; - } - - if (m_header->pkg_size != pkg_f.Length()) { - LOG_WARNING(LOADER, "PKG: File size mismatch."); + if (m_header->pkg_size != pkg_f.size()) + { + LOG_ERROR(LOADER, "PKG: File size mismatch (pkg_size=0x%x)", m_header->pkg_size); //return false; } - if (m_header->data_size + m_header->data_offset + 0x60 != pkg_f.Length()) { - LOG_WARNING(LOADER, "PKG: Data size mismatch."); + if (m_header->data_size + m_header->data_offset + 0x60 != pkg_f.size()) + { + LOG_ERROR(LOADER, "PKG: Data size mismatch (data_size=0x%x, offset=0x%x)", m_header->data_size, m_header->data_offset); //return false; } return true; } -bool LoadHeader(rFile& pkg_f, PKGHeader* m_header) +bool LoadHeader(const rfile_t& pkg_f, PKGHeader* m_header) { - pkg_f.Seek(0); + pkg_f.seek(0); - if (pkg_f.Read(m_header, sizeof(PKGHeader)) != sizeof(PKGHeader)) { + if (pkg_f.read(m_header, sizeof(PKGHeader)) != sizeof(PKGHeader)) + { LOG_ERROR(LOADER, "PKG: Package file is too short!"); return false; } @@ -71,10 +80,12 @@ bool LoadHeader(rFile& pkg_f, PKGHeader* m_header) return true; } -int Decrypt(rFile& pkg_f, rFile& dec_pkg_f, PKGHeader* m_header) +int Decrypt(const rfile_t& pkg_f, const rfile_t& dec_pkg_f, PKGHeader* m_header) { if (!LoadHeader(pkg_f, m_header)) + { return -1; + } aes_context c; u8 iv[HASH_LEN]; @@ -89,7 +100,7 @@ int Decrypt(rFile& pkg_f, rFile& dec_pkg_f, PKGHeader* m_header) memcpy(key+0x10, &m_header->qa_digest[8], 8); // &data[0x68] memcpy(key+0x18, &m_header->qa_digest[8], 8); // &data[0x68] - pkg_f.Seek(m_header->data_offset); + pkg_f.seek(m_header->data_offset); u32 parts = (m_header->data_size + BUF_SIZE - 1) / BUF_SIZE; wxProgressDialog pdlg("PKG Decrypter / Installer", "Please wait, decrypting...", parts, 0, wxPD_AUTO_HIDE | wxPD_APP_MODAL); @@ -100,7 +111,7 @@ int Decrypt(rFile& pkg_f, rFile& dec_pkg_f, PKGHeader* m_header) for (u32 i=0; ipkg_type == PKG_RELEASE_TYPE_DEBUG) @@ -136,7 +147,7 @@ int Decrypt(rFile& pkg_f, rFile& dec_pkg_f, PKGHeader* m_header) buf[j] ^= ctr[j]; } } - dec_pkg_f.Write(buf, length); + dec_pkg_f.write(buf, length); pdlg.Update(i); } pdlg.Update(parts); @@ -144,12 +155,13 @@ int Decrypt(rFile& pkg_f, rFile& dec_pkg_f, PKGHeader* m_header) } // Unpacking. -bool LoadEntries(rFile& dec_pkg_f, PKGHeader* m_header, PKGEntry *m_entries) +bool LoadEntries(const rfile_t& dec_pkg_f, PKGHeader* m_header, PKGEntry* m_entries) { - dec_pkg_f.Seek(0); - dec_pkg_f.Read(m_entries, sizeof(PKGEntry) * m_header->file_count); + dec_pkg_f.seek(0); + dec_pkg_f.read(m_entries, sizeof(PKGEntry) * m_header->file_count); - if (m_entries->name_offset / sizeof(PKGEntry) != m_header->file_count) { + if (m_entries->name_offset / sizeof(PKGEntry) != m_header->file_count) + { LOG_ERROR(LOADER, "PKG: Entries are damaged!"); return false; } @@ -157,12 +169,12 @@ bool LoadEntries(rFile& dec_pkg_f, PKGHeader* m_header, PKGEntry *m_entries) return true; } -bool UnpackEntry(rFile& dec_pkg_f, const PKGEntry& entry, std::string dir) +bool UnpackEntry(const rfile_t& dec_pkg_f, const PKGEntry& entry, std::string dir) { char buf[BUF_SIZE]; - dec_pkg_f.Seek(entry.name_offset); - dec_pkg_f.Read(buf, entry.name_size); + dec_pkg_f.seek(entry.name_offset); + dec_pkg_f.read(buf, entry.name_size); buf[entry.name_size] = 0; switch (entry.type.data() >> 24) @@ -172,30 +184,33 @@ bool UnpackEntry(rFile& dec_pkg_f, const PKGEntry& entry, std::string dir) case PKG_FILE_ENTRY_SDAT: case PKG_FILE_ENTRY_REGULAR: { - rFile out; auto path = dir + std::string(buf, entry.name_size); + if (rExists(path)) { - LOG_WARNING(LOADER, "PKG Loader: File is overwritten: %s", path.c_str()); + LOG_WARNING(LOADER, "PKG Loader: '%s' is overwritten", path); } - if (out.Create(path, true /* overwriting */)) - { - dec_pkg_f.Seek(entry.file_offset); + rfile_t out; - for (u64 size = 0; size < entry.file_size;) { - size += dec_pkg_f.Read(buf, BUF_SIZE); + if (out.open(path, o_write | o_create | o_trunc)) + { + dec_pkg_f.seek(entry.file_offset); + + for (u64 size = 0; size < entry.file_size;) + { + size += dec_pkg_f.read(buf, BUF_SIZE); if (size > entry.file_size) - out.Write(buf, BUF_SIZE - (size - entry.file_size)); + out.write(buf, BUF_SIZE - (size - entry.file_size)); else - out.Write(buf, BUF_SIZE); + out.write(buf, BUF_SIZE); } - out.Close(); + return true; } else { - LOG_ERROR(LOADER, "PKG Loader: Could not create file: %s", path.c_str()); + LOG_ERROR(LOADER, "PKG Loader: Could not create file '%s'", path); return false; } } @@ -220,41 +235,44 @@ bool UnpackEntry(rFile& dec_pkg_f, const PKGEntry& entry, std::string dir) } } -int Unpack(rFile& pkg_f, std::string src, std::string dst) +int Unpack(const rfile_t& pkg_f, std::string src, std::string dst) { PKGHeader* m_header = (PKGHeader*) malloc (sizeof(PKGHeader)); - rFile dec_pkg_f; // TODO: This shouldn't use current dir std::string decryptedFile = "./dev_hdd1/" + src + ".dec"; - dec_pkg_f.Create(decryptedFile, true); + rfile_t dec_pkg_f(decryptedFile, o_read | o_write | o_create | o_trunc); if (Decrypt(pkg_f, dec_pkg_f, m_header) < 0) + { return -1; - - dec_pkg_f.Close(); + } - rFile n_dec_pkg_f(decryptedFile, rFile::read); + dec_pkg_f.seek(0); std::vector m_entries; m_entries.resize(m_header->file_count); - PKGEntry *m_entries_ptr = &m_entries[0]; - if (!LoadEntries(n_dec_pkg_f, m_header, m_entries_ptr)) + auto m_entries_ptr = m_entries.data(); + + if (!LoadEntries(dec_pkg_f, m_header, m_entries_ptr)) + { return -1; + } wxProgressDialog pdlg("PKG Decrypter / Installer", "Please wait, unpacking...", m_entries.size(), 0, wxPD_AUTO_HIDE | wxPD_APP_MODAL); - for (const PKGEntry& entry : m_entries) + for (const auto& entry : m_entries) { - UnpackEntry(n_dec_pkg_f, entry, dst + src + "/"); + UnpackEntry(dec_pkg_f, entry, dst + src + "/"); pdlg.Update(pdlg.GetValue() + 1); } + pdlg.Update(m_entries.size()); - n_dec_pkg_f.Close(); - wxRemoveFile(decryptedFile); + dec_pkg_f.close(); + rRemoveFile(decryptedFile); return 0; } diff --git a/rpcs3/Crypto/unpkg.h b/rpcs3/Crypto/unpkg.h index e47e061fbc..8e516fd56c 100644 --- a/rpcs3/Crypto/unpkg.h +++ b/rpcs3/Crypto/unpkg.h @@ -1,21 +1,32 @@ #pragma once // Constants -#define PKG_HEADER_SIZE 0xC0 //sizeof(pkg_header) + sizeof(pkg_unk_checksum) -#define PKG_RELEASE_TYPE_RELEASE 0x8000 -#define PKG_RELEASE_TYPE_DEBUG 0x0000 -#define PKG_PLATFORM_TYPE_PS3 0x0001 -#define PKG_PLATFORM_TYPE_PSP 0x0002 +enum +{ + HASH_LEN = 16, + BUF_SIZE = 4096, + PKG_HEADER_SIZE = 0xC0, //sizeof(pkg_header) + sizeof(pkg_unk_checksum) +}; -#define PKG_FILE_ENTRY_NPDRM 0x0001 -#define PKG_FILE_ENTRY_NPDRMEDAT 0x0002 -#define PKG_FILE_ENTRY_REGULAR 0x0003 -#define PKG_FILE_ENTRY_FOLDER 0x0004 -#define PKG_FILE_ENTRY_SDAT 0x0009 -#define PKG_FILE_ENTRY_OVERWRITE 0x80000000 +enum : u16 +{ + PKG_RELEASE_TYPE_RELEASE = 0x8000, + PKG_RELEASE_TYPE_DEBUG = 0x0000, -#define HASH_LEN 16 -#define BUF_SIZE 4096 + PKG_PLATFORM_TYPE_PS3 = 0x0001, + PKG_PLATFORM_TYPE_PSP = 0x0002, +}; + +enum : u32 +{ + PKG_FILE_ENTRY_NPDRM = 1, + PKG_FILE_ENTRY_NPDRMEDAT = 2, + PKG_FILE_ENTRY_REGULAR = 3, + PKG_FILE_ENTRY_FOLDER = 4, + PKG_FILE_ENTRY_SDAT = 9, + + PKG_FILE_ENTRY_OVERWRITE = 0x80000000, +}; // Structs struct PKGHeader @@ -45,6 +56,6 @@ struct PKGEntry be_t pad; // Padding (zeros) }; -class rFile; +struct rfile_t; -extern int Unpack(rFile& dec_pkg_f, std::string src, std::string dst); \ No newline at end of file +int Unpack(const rfile_t& dec_pkg_f, std::string src, std::string dst); diff --git a/rpcs3/Crypto/unself.cpp b/rpcs3/Crypto/unself.cpp index 954f29eb22..f9b33c3abc 100644 --- a/rpcs3/Crypto/unself.cpp +++ b/rpcs3/Crypto/unself.cpp @@ -11,53 +11,56 @@ #include -void WriteEhdr(rFile& f, Elf64_Ehdr& ehdr) +void WriteEhdr(const rfile_t& f, Elf64_Ehdr& ehdr) { -Write32(f, ehdr.e_magic); -Write8(f, ehdr.e_class); -Write8(f, ehdr.e_data); -Write8(f, ehdr.e_curver); -Write8(f, ehdr.e_os_abi); -Write64(f, ehdr.e_abi_ver); -Write16(f, ehdr.e_type); -Write16(f, ehdr.e_machine); -Write32(f, ehdr.e_version); -Write64(f, ehdr.e_entry); -Write64(f, ehdr.e_phoff); -Write64(f, ehdr.e_shoff); -Write32(f, ehdr.e_flags); -Write16(f, ehdr.e_ehsize); -Write16(f, ehdr.e_phentsize); -Write16(f, ehdr.e_phnum); -Write16(f, ehdr.e_shentsize); -Write16(f, ehdr.e_shnum); -Write16(f, ehdr.e_shstrndx); + Write32(f, ehdr.e_magic); + Write8(f, ehdr.e_class); + Write8(f, ehdr.e_data); + Write8(f, ehdr.e_curver); + Write8(f, ehdr.e_os_abi); + Write64(f, ehdr.e_abi_ver); + Write16(f, ehdr.e_type); + Write16(f, ehdr.e_machine); + Write32(f, ehdr.e_version); + Write64(f, ehdr.e_entry); + Write64(f, ehdr.e_phoff); + Write64(f, ehdr.e_shoff); + Write32(f, ehdr.e_flags); + Write16(f, ehdr.e_ehsize); + Write16(f, ehdr.e_phentsize); + Write16(f, ehdr.e_phnum); + Write16(f, ehdr.e_shentsize); + Write16(f, ehdr.e_shnum); + Write16(f, ehdr.e_shstrndx); } -void WritePhdr(rFile& f, Elf64_Phdr& phdr) + +void WritePhdr(const rfile_t& f, Elf64_Phdr& phdr) { -Write32(f, phdr.p_type); -Write32(f, phdr.p_flags); -Write64(f, phdr.p_offset); -Write64(f, phdr.p_vaddr); -Write64(f, phdr.p_paddr); -Write64(f, phdr.p_filesz); -Write64(f, phdr.p_memsz); -Write64(f, phdr.p_align); + Write32(f, phdr.p_type); + Write32(f, phdr.p_flags); + Write64(f, phdr.p_offset); + Write64(f, phdr.p_vaddr); + Write64(f, phdr.p_paddr); + Write64(f, phdr.p_filesz); + Write64(f, phdr.p_memsz); + Write64(f, phdr.p_align); } -void WriteShdr(rFile& f, Elf64_Shdr& shdr) + +void WriteShdr(const rfile_t& f, Elf64_Shdr& shdr) { -Write32(f, shdr.sh_name); -Write32(f, shdr.sh_type); -Write64(f, shdr.sh_flags); -Write64(f, shdr.sh_addr); -Write64(f, shdr.sh_offset); -Write64(f, shdr.sh_size); -Write32(f, shdr.sh_link); -Write32(f, shdr.sh_info); -Write64(f, shdr.sh_addralign); -Write64(f, shdr.sh_entsize); + Write32(f, shdr.sh_name); + Write32(f, shdr.sh_type); + Write64(f, shdr.sh_flags); + Write64(f, shdr.sh_addr); + Write64(f, shdr.sh_offset); + Write64(f, shdr.sh_size); + Write32(f, shdr.sh_link); + Write32(f, shdr.sh_info); + Write64(f, shdr.sh_addralign); + Write64(f, shdr.sh_entsize); } -void WriteEhdr(rFile& f, Elf32_Ehdr& ehdr) + +void WriteEhdr(const rfile_t& f, Elf32_Ehdr& ehdr) { Write32(f, ehdr.e_magic); Write8(f, ehdr.e_class); @@ -79,7 +82,8 @@ void WriteEhdr(rFile& f, Elf32_Ehdr& ehdr) Write16(f, ehdr.e_shnum); Write16(f, ehdr.e_shstrndx); } -void WritePhdr(rFile& f, Elf32_Phdr& phdr) + +void WritePhdr(const rfile_t& f, Elf32_Phdr& phdr) { Write32(f, phdr.p_type); Write32(f, phdr.p_offset); @@ -90,7 +94,8 @@ void WritePhdr(rFile& f, Elf32_Phdr& phdr) Write32(f, phdr.p_flags); Write32(f, phdr.p_align); } -void WriteShdr(rFile& f, Elf32_Shdr& shdr) + +void WriteShdr(const rfile_t& f, Elf32_Shdr& shdr) { Write32(f, shdr.sh_name); Write32(f, shdr.sh_type); @@ -794,8 +799,8 @@ bool SELFDecrypter::DecryptData() bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32) { // Create a new ELF file. - rFile e(elf.c_str(), rFile::write); - if(!e.IsOpened()) + rfile_t e(elf, o_write | o_create | o_trunc); + if(!e) { LOG_ERROR(LOADER, "Could not create ELF file! (%s)", elf.c_str()); return false; @@ -819,8 +824,8 @@ bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32) if (meta_shdr[i].type == 2) { // Seek to the program header data offset and write the data. - e.Seek(phdr32_arr[meta_shdr[i].program_idx].p_offset); - e.Write(data_buf + data_buf_offset, meta_shdr[i].data_size); + e.seek(phdr32_arr[meta_shdr[i].program_idx].p_offset); + e.write(data_buf + data_buf_offset, meta_shdr[i].data_size); // Advance the data buffer offset by data size. data_buf_offset += meta_shdr[i].data_size; @@ -830,7 +835,7 @@ bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32) // Write section headers. if(self_hdr.se_shdroff != 0) { - e.Seek(elf32_hdr.e_shoff); + e.seek(elf32_hdr.e_shoff); for(u32 i = 0; i < elf32_hdr.e_shnum; ++i) WriteShdr(e, shdr32_arr[i]); @@ -870,8 +875,8 @@ bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32) decomp_stream_out.CopyTo(decomp_buf, phdr64_arr[meta_shdr[i].program_idx].p_filesz); // Seek to the program header data offset and write the data. - e.Seek(phdr64_arr[meta_shdr[i].program_idx].p_offset); - e.Write(decomp_buf, phdr64_arr[meta_shdr[i].program_idx].p_filesz); + e.seek(phdr64_arr[meta_shdr[i].program_idx].p_offset); + e.write(decomp_buf, phdr64_arr[meta_shdr[i].program_idx].p_filesz); // Release the decompression buffer. free(decomp_buf); @@ -879,8 +884,8 @@ bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32) else { // Seek to the program header data offset and write the data. - e.Seek(phdr64_arr[meta_shdr[i].program_idx].p_offset); - e.Write(data_buf + data_buf_offset, meta_shdr[i].data_size); + e.seek(phdr64_arr[meta_shdr[i].program_idx].p_offset); + e.write(data_buf + data_buf_offset, meta_shdr[i].data_size); } // Advance the data buffer offset by data size. @@ -891,14 +896,13 @@ bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32) // Write section headers. if(self_hdr.se_shdroff != 0) { - e.Seek(elf64_hdr.e_shoff); + e.seek(elf64_hdr.e_shoff); for(u32 i = 0; i < elf64_hdr.e_shnum; ++i) WriteShdr(e, shdr64_arr[i]); } } - e.Close(); return true; } @@ -921,17 +925,16 @@ bool SELFDecrypter::GetKeyFromRap(u8 *content_id, u8 *npdrm_key) } // Open the RAP file and read the key. - rFile rap_file(rap_path, rFile::read); + rfile_t rap_file(rap_path); - if (!rap_file.IsOpened()) + if (!rap_file) { LOG_ERROR(LOADER, "Failed to load RAP file!"); return false; } - LOG_NOTICE(LOADER, "Loading RAP file %s", (ci_str + ".rap").c_str()); - rap_file.Read(rap_key, 0x10); - rap_file.Close(); + LOG_NOTICE(LOADER, "Loading RAP file %s.rap", ci_str); + rap_file.read(rap_key, 0x10); // Convert the RAP key. rap_to_rif(rap_key, npdrm_key); @@ -975,18 +978,18 @@ bool IsSelfElf32(const std::string& path) bool CheckDebugSelf(const std::string& self, const std::string& elf) { // Open the SELF file. - rFile s(self); + rfile_t s(self); - if(!s.IsOpened()) + if(!s) { LOG_ERROR(LOADER, "Could not open SELF file! (%s)", self.c_str()); return false; } // Get the key version. - s.Seek(0x08); + s.seek(0x08); u16 key_version; - s.Read(&key_version, sizeof(key_version)); + s.read(&key_version, sizeof(key_version)); // Check for DEBUG version. if(swap16(key_version) == 0x8000) @@ -994,17 +997,17 @@ bool CheckDebugSelf(const std::string& self, const std::string& elf) LOG_WARNING(LOADER, "Debug SELF detected! Removing fake header..."); // Get the real elf offset. - s.Seek(0x10); + s.seek(0x10); u64 elf_offset; - s.Read(&elf_offset, sizeof(elf_offset)); + s.read(&elf_offset, sizeof(elf_offset)); // Start at the real elf offset. elf_offset = swap64(elf_offset); - s.Seek(elf_offset); + s.seek(elf_offset); // Write the real ELF file back. - rFile e(elf, rFile::write); - if(!e.IsOpened()) + rfile_t e(elf, o_write | o_create | o_trunc); + if(!e) { LOG_ERROR(LOADER, "Could not create ELF file! (%s)", elf.c_str()); return false; @@ -1012,18 +1015,14 @@ bool CheckDebugSelf(const std::string& self, const std::string& elf) // Copy the data. char buf[2048]; - while (ssize_t size = s.Read(buf, 2048)) - e.Write(buf, size); + while (ssize_t size = s.read(buf, 2048)) + e.write(buf, size); - e.Close(); return true; } - else - { - // Leave the file untouched. - s.Seek(0); - return false; - } + + // Leave the file untouched. + return false; } bool DecryptSelf(const std::string& elf, const std::string& self) diff --git a/rpcs3/Emu/Audio/AudioDumper.cpp b/rpcs3/Emu/Audio/AudioDumper.cpp index 1ccd7d24cc..7353a499a3 100644 --- a/rpcs3/Emu/Audio/AudioDumper.cpp +++ b/rpcs3/Emu/Audio/AudioDumper.cpp @@ -12,7 +12,7 @@ AudioDumper::~AudioDumper() bool AudioDumper::Init(u8 ch) { - if ((m_init = m_output.Open("audio.wav", rFile::write))) + if ((m_init = m_output.open("audio.wav", o_write | o_create | o_trunc))) { m_header = WAVHeader(ch); WriteHeader(); @@ -25,7 +25,7 @@ void AudioDumper::WriteHeader() { if (m_init) { - m_output.Write(&m_header, sizeof(m_header)); // write file header + m_output.write(&m_header, sizeof(m_header)); // write file header } } @@ -47,7 +47,7 @@ size_t AudioDumper::WriteData(const void* buffer, size_t size) if (m_init) #endif { - size_t ret = m_output.Write(buffer, size); + size_t ret = m_output.write(buffer, size); m_header.Size += (u32)ret; m_header.RIFF.Size += (u32)ret; return ret; @@ -60,8 +60,8 @@ void AudioDumper::Finalize() { if (m_init) { - m_output.Seek(0); - m_output.Write(&m_header, sizeof(m_header)); // write fixed file header - m_output.Close(); + m_output.seek(0); + m_output.write(&m_header, sizeof(m_header)); // write fixed file header + m_output.close(); } } \ No newline at end of file diff --git a/rpcs3/Emu/Audio/AudioDumper.h b/rpcs3/Emu/Audio/AudioDumper.h index 849adb851d..54c8f00edf 100644 --- a/rpcs3/Emu/Audio/AudioDumper.h +++ b/rpcs3/Emu/Audio/AudioDumper.h @@ -56,7 +56,7 @@ struct WAVHeader class AudioDumper { WAVHeader m_header; - rFile m_output; + rfile_t m_output; bool m_init; public: diff --git a/rpcs3/Emu/FS/VFS.cpp b/rpcs3/Emu/FS/VFS.cpp index c3fbad4d33..c2e6594609 100644 --- a/rpcs3/Emu/FS/VFS.cpp +++ b/rpcs3/Emu/FS/VFS.cpp @@ -133,7 +133,7 @@ void VFS::UnMountAll() m_devices.clear(); } -vfsFileBase* VFS::OpenFile(const std::string& ps3_path, vfsOpenMode mode) const +vfsFileBase* VFS::OpenFile(const std::string& ps3_path, u32 mode) const { std::string path; @@ -165,23 +165,6 @@ vfsDirBase* VFS::OpenDir(const std::string& ps3_path) const return nullptr; } -bool VFS::CreateFile(const std::string& ps3_path, bool overwrite) const -{ - std::string path; - - if (vfsDevice* dev = GetDevice(ps3_path, path)) - { - std::unique_ptr res(dev->GetNewFileStream()); - - if (res) - { - return res->Create(path, overwrite); - } - } - - return false; -} - bool VFS::CreateDir(const std::string& ps3_path) const { std::string path; diff --git a/rpcs3/Emu/FS/VFS.h b/rpcs3/Emu/FS/VFS.h index 274b2a32c3..dfe14048b9 100644 --- a/rpcs3/Emu/FS/VFS.h +++ b/rpcs3/Emu/FS/VFS.h @@ -4,7 +4,6 @@ class vfsDevice; struct vfsFileBase; class vfsDirBase; -enum vfsOpenMode : u8; enum vfsDeviceType { @@ -79,9 +78,8 @@ struct VFS std::string GetLinked(const std::string& ps3_path) const; - vfsFileBase* OpenFile(const std::string& ps3_path, vfsOpenMode mode) const; + vfsFileBase* OpenFile(const std::string& ps3_path, u32 mode) const; vfsDirBase* OpenDir(const std::string& ps3_path) const; - bool CreateFile(const std::string& ps3_path, bool overwrite = false) const; bool CreateDir(const std::string& ps3_path) const; bool RemoveFile(const std::string& ps3_path) const; bool RemoveDir(const std::string& ps3_path) const; diff --git a/rpcs3/Emu/FS/vfsFile.cpp b/rpcs3/Emu/FS/vfsFile.cpp index 6f990c31e2..415167ebdc 100644 --- a/rpcs3/Emu/FS/vfsFile.cpp +++ b/rpcs3/Emu/FS/vfsFile.cpp @@ -10,14 +10,14 @@ vfsFile::vfsFile() { } -vfsFile::vfsFile(const std::string& path, vfsOpenMode mode) +vfsFile::vfsFile(const std::string& path, u32 mode) : vfsFileBase(nullptr) , m_stream(nullptr) { Open(path, mode); } -bool vfsFile::Open(const std::string& path, vfsOpenMode mode) +bool vfsFile::Open(const std::string& path, u32 mode) { Close(); @@ -26,11 +26,6 @@ bool vfsFile::Open(const std::string& path, vfsOpenMode mode) return m_stream && m_stream->IsOpened(); } -bool vfsFile::Create(const std::string& path, bool overwrite) -{ - return m_stream->Create(path, overwrite); -} - bool vfsFile::Exists(const std::string& path) { return m_stream->Exists(path); @@ -52,7 +47,7 @@ bool vfsFile::Close() return vfsFileBase::Close(); } -u64 vfsFile::GetSize() +u64 vfsFile::GetSize() const { return m_stream->GetSize(); } @@ -67,7 +62,7 @@ u64 vfsFile::Read(void* dst, u64 size) return m_stream->Read(dst, size); } -u64 vfsFile::Seek(s64 offset, vfsSeekMode mode) +u64 vfsFile::Seek(s64 offset, u32 mode) { return m_stream->Seek(offset, mode); } @@ -79,5 +74,5 @@ u64 vfsFile::Tell() const bool vfsFile::IsOpened() const { - return m_stream && m_stream->IsOpened() && vfsFileBase::IsOpened(); + return m_stream && m_stream->IsOpened() /*&& vfsFileBase::IsOpened()*/; } diff --git a/rpcs3/Emu/FS/vfsFile.h b/rpcs3/Emu/FS/vfsFile.h index a5ed8cf138..dfb211cbd4 100644 --- a/rpcs3/Emu/FS/vfsFile.h +++ b/rpcs3/Emu/FS/vfsFile.h @@ -8,22 +8,21 @@ private: public: vfsFile(); - vfsFile(const std::string& path, vfsOpenMode mode = vfsRead); + vfsFile(const std::string& path, u32 mode = vfsRead); - virtual bool Open(const std::string& path, vfsOpenMode mode = vfsRead) override; - virtual bool Create(const std::string& path, bool overwrite = false) override; + virtual bool Open(const std::string& path, u32 mode = vfsRead) override; virtual bool Exists(const std::string& path) override; virtual bool Rename(const std::string& from, const std::string& to) override; virtual bool Remove(const std::string& path) override; virtual bool Close() override; - virtual u64 GetSize() override; + virtual u64 GetSize() const override; virtual u64 Write(const void* src, u64 size) override; virtual u64 Read(void* dst, u64 size) override; - virtual u64 Seek(s64 offset, vfsSeekMode mode = vfsSeekSet) override; + virtual u64 Seek(s64 offset, u32 mode = from_begin) override; virtual u64 Tell() const override; virtual bool IsOpened() const override; -}; \ No newline at end of file +}; diff --git a/rpcs3/Emu/FS/vfsFileBase.cpp b/rpcs3/Emu/FS/vfsFileBase.cpp index 1cbed18a2c..dc7aa6dd56 100644 --- a/rpcs3/Emu/FS/vfsFileBase.cpp +++ b/rpcs3/Emu/FS/vfsFileBase.cpp @@ -12,18 +12,11 @@ vfsFileBase::~vfsFileBase() Close(); } -bool Access(const std::string& path, vfsOpenMode mode) -{ - return false; -} - -bool vfsFileBase::Open(const std::string& path, vfsOpenMode mode) +bool vfsFileBase::Open(const std::string& path, u32 mode) { m_path = path; m_mode = mode; - vfsStream::Reset(); - return true; } @@ -39,7 +32,7 @@ std::string vfsFileBase::GetPath() const return m_path; } -vfsOpenMode vfsFileBase::GetOpenMode() const +u32 vfsFileBase::GetOpenMode() const { return m_mode; } diff --git a/rpcs3/Emu/FS/vfsFileBase.h b/rpcs3/Emu/FS/vfsFileBase.h index 700f397a2a..1ce6f83011 100644 --- a/rpcs3/Emu/FS/vfsFileBase.h +++ b/rpcs3/Emu/FS/vfsFileBase.h @@ -1,15 +1,12 @@ #pragma once #include "vfsStream.h" -enum vfsOpenMode : u8 +enum vfsOpenMode : u32 { - vfsRead = 0x1, - vfsWrite = 0x2, - vfsExcl = 0x4, - vfsAppend = 0x8, - vfsReadWrite = vfsRead | vfsWrite, - vfsWriteExcl = vfsWrite | vfsExcl, - vfsWriteAppend = vfsWrite | vfsAppend, + vfsRead = o_read, + vfsReadWrite = o_read | o_write, + vfsWriteNew = o_write | o_create | o_trunc, + vfsWriteExcl = o_write | o_create | o_excl, }; class vfsDevice; @@ -18,20 +15,19 @@ struct vfsFileBase : public vfsStream { protected: std::string m_path; - vfsOpenMode m_mode; + u32 m_mode; vfsDevice* m_device; public: vfsFileBase(vfsDevice* device); virtual ~vfsFileBase(); - virtual bool Open(const std::string& path, vfsOpenMode mode); + virtual bool Open(const std::string& path, u32 mode); virtual bool Close() override; - virtual bool Create(const std::string& path, bool overwrite = false) { return false; } virtual bool Exists(const std::string& path) { return false; } virtual bool Rename(const std::string& from, const std::string& to) { return false; } virtual bool Remove(const std::string& path) { return false; } std::string GetPath() const; - vfsOpenMode GetOpenMode() const; + u32 GetOpenMode() const; }; diff --git a/rpcs3/Emu/FS/vfsLocalFile.cpp b/rpcs3/Emu/FS/vfsLocalFile.cpp index 1542a49be5..18be60ccb4 100644 --- a/rpcs3/Emu/FS/vfsLocalFile.cpp +++ b/rpcs3/Emu/FS/vfsLocalFile.cpp @@ -2,123 +2,50 @@ #include "Utilities/Log.h" #include "vfsLocalFile.h" -static const rFile::OpenMode vfs2wx_mode(vfsOpenMode mode) -{ - switch(mode) - { - case vfsRead: return rFile::read; - case vfsWrite: return rFile::write; - case vfsReadWrite: return rFile::read_write; - case vfsWriteExcl: return rFile::write_excl; - case vfsWriteAppend: return rFile::write_append; - } - - return rFile::read; -} - -static const rSeekMode vfs2wx_seek(vfsSeekMode mode) -{ - switch(mode) - { - case vfsSeekSet: return rFromStart; - case vfsSeekCur: return rFromCurrent; - case vfsSeekEnd: return rFromEnd; - } - - return rFromStart; -} - vfsLocalFile::vfsLocalFile(vfsDevice* device) : vfsFileBase(device) { } -bool vfsLocalFile::Open(const std::string& path, vfsOpenMode mode) +bool vfsLocalFile::Open(const std::string& path, u32 mode) { Close(); - // if(m_device) - // { - // if(!m_file.Access(fmt::FromUTF8(vfsDevice::GetWinPath(m_device->GetLocalPath(), path)), vfs2wx_mode(mode))) return false; - - // return m_file.Open(fmt::FromUTF8(vfsDevice::GetWinPath(m_device->GetLocalPath(), path)), vfs2wx_mode(mode)) && - // vfsFileBase::Open(fmt::FromUTF8(vfsDevice::GetPs3Path(m_device->GetPs3Path(), path)), mode); - // } - // else - // { - if(!m_file.Access(path, vfs2wx_mode(mode))) return false; - - return m_file.Open(path, vfs2wx_mode(mode)) && vfsFileBase::Open(path, mode); - // } -} - -bool vfsLocalFile::Create(const std::string& path, bool overwrite) -{ - LOG_WARNING(HLE, "vfsLocalFile::Create('%s', overwrite=%d)", path.c_str(), overwrite); - for(uint p=1; p < path.length() && path[p] != '\0' ; p++) - { - for(; p < path.length() && path[p] != '\0'; p++) - if(path[p] == '/' || path[p] == '\\') break; // ??? - - if(p == path.length() || path[p] == '\0') - break; - - const std::string& dir = path.substr(0, p); - if(!rExists(dir)) - { - LOG_NOTICE(HLE, "create dir: %s", dir.c_str()); - rMkdir(dir); - } - } - - //create file - const char m = path[path.length() - 1]; - if(m != '/' && m != '\\' && !rExists(path)) // ??? - { - rFile f; - if (!f.Create(path, overwrite)) { - if (overwrite) LOG_NOTICE(HLE, "vfsLocalFile::Create: couldn't create file"); - return false; - } - else - return true; - } - - return true; + return m_file.open(path, mode) && vfsFileBase::Open(path, mode); } bool vfsLocalFile::Close() { - return m_file.Close() && vfsFileBase::Close(); + return m_file.close() && vfsFileBase::Close(); } -u64 vfsLocalFile::GetSize() +u64 vfsLocalFile::GetSize() const { - return m_file.Length(); + return m_file.size(); } u64 vfsLocalFile::Write(const void* src, u64 size) { - return m_file.Write(src, size); + return m_file.write(src, size); } u64 vfsLocalFile::Read(void* dst, u64 size) { - return m_file.Read(dst, size); + return m_file.read(dst, size); } -u64 vfsLocalFile::Seek(s64 offset, vfsSeekMode mode) +u64 vfsLocalFile::Seek(s64 offset, u32 mode) { - return m_file.Seek(offset, vfs2wx_seek(mode)); + return m_file.seek(offset, mode); } u64 vfsLocalFile::Tell() const { - return m_file.Tell(); + return m_file.seek(0, from_cur); } bool vfsLocalFile::IsOpened() const { - return m_file.IsOpened() && vfsFileBase::IsOpened(); + return m_file /*&& vfsFileBase::IsOpened()*/; } bool vfsLocalFile::Exists(const std::string& path) diff --git a/rpcs3/Emu/FS/vfsLocalFile.h b/rpcs3/Emu/FS/vfsLocalFile.h index 42eb7a90bc..625bb90aa9 100644 --- a/rpcs3/Emu/FS/vfsLocalFile.h +++ b/rpcs3/Emu/FS/vfsLocalFile.h @@ -1,29 +1,27 @@ #pragma once #include "vfsFileBase.h" -#include "Utilities/rFile.h" class vfsLocalFile : public vfsFileBase { private: - rFile m_file; + rfile_t m_file; public: vfsLocalFile(vfsDevice* device); - virtual bool Open(const std::string& path, vfsOpenMode mode = vfsRead) override; - virtual bool Create(const std::string& path, bool overwrite = false) override; + virtual bool Open(const std::string& path, u32 mode = vfsRead) override; virtual bool Close() override; virtual bool Exists(const std::string& path) override; virtual bool Rename(const std::string& from, const std::string& to) override; virtual bool Remove(const std::string& path) override; - virtual u64 GetSize() override; + virtual u64 GetSize() const override; virtual u64 Write(const void* src, u64 size) override; virtual u64 Read(void* dst, u64 size) override; - virtual u64 Seek(s64 offset, vfsSeekMode mode = vfsSeekSet) override; + virtual u64 Seek(s64 offset, u32 mode = from_begin) override; virtual u64 Tell() const override; virtual bool IsOpened() const override; -}; \ No newline at end of file +}; diff --git a/rpcs3/Emu/FS/vfsStream.cpp b/rpcs3/Emu/FS/vfsStream.cpp index eb15ad0837..a1fe0ffc0e 100644 --- a/rpcs3/Emu/FS/vfsStream.cpp +++ b/rpcs3/Emu/FS/vfsStream.cpp @@ -1,80 +1,2 @@ #include "stdafx.h" #include "vfsStream.h" - -vfsStream::vfsStream() -{ -} - -vfsStream::~vfsStream() -{ - Close(); -} - -void vfsStream::Reset() -{ - m_pos = 0; -} - -bool vfsStream::Close() -{ - Reset(); - - return true; -} - -u64 vfsStream::GetSize() -{ - u64 last_pos = Tell(); - Seek(0, vfsSeekEnd); - u64 size = Tell(); - Seek(last_pos, vfsSeekSet); - - return size; -} - -u64 vfsStream::Write(const void* src, u64 size) -{ - m_pos += size; - - return size; -} - -u64 vfsStream::Read(void* dst, u64 size) -{ - m_pos += size; - - return size; -} - -u64 vfsStream::Seek(s64 offset, vfsSeekMode mode) -{ - switch(mode) - { - case vfsSeekSet: - m_pos = offset; - break; - case vfsSeekCur: - m_pos += offset; - break; - case vfsSeekEnd: - m_pos = GetSize() + offset; - break; - } - - return m_pos; -} - -u64 vfsStream::Tell() const -{ - return m_pos; -} - -bool vfsStream::Eof() -{ - return Tell() >= GetSize(); -} - -bool vfsStream::IsOpened() const -{ - return true; -} \ No newline at end of file diff --git a/rpcs3/Emu/FS/vfsStream.h b/rpcs3/Emu/FS/vfsStream.h index 8af37bd825..e49848c788 100644 --- a/rpcs3/Emu/FS/vfsStream.h +++ b/rpcs3/Emu/FS/vfsStream.h @@ -1,44 +1,44 @@ #pragma once - -enum vfsSeekMode -{ - vfsSeekSet, - vfsSeekCur, - vfsSeekEnd, -}; +#include "Utilities/rFile.h" struct vfsStream { -protected: - u64 m_pos; + vfsStream() = default; -public: - vfsStream(); - - virtual ~vfsStream(); - - virtual void Reset(); - virtual bool Close(); - - virtual u64 GetSize(); - - virtual u64 Write(const void* src, u64 size); - - template __forceinline bool SWrite(const T& data, u64 size = sizeof(T)) + virtual ~vfsStream() { - return Write(&data, size) == size; + Close(); } - virtual u64 Read(void* dst, u64 size); - - template __forceinline bool SRead(T& data, u64 size = sizeof(T)) + virtual bool Close() { - return Read(&data, size) == size; + return true; } - virtual u64 Seek(s64 offset, vfsSeekMode mode = vfsSeekSet); - virtual u64 Tell() const; - virtual bool Eof(); + virtual u64 GetSize() const = 0; - virtual bool IsOpened() const; + virtual u64 Write(const void* src, u64 count) = 0; + + template __forceinline bool SWrite(const T& data, u64 count = sizeof(T)) + { + return Write(&data, count) == count; + } + + virtual u64 Read(void* dst, u64 count) = 0; + + template __forceinline bool SRead(T& data, u64 count = sizeof(T)) + { + return Read(&data, count) == count; + } + + virtual u64 Seek(s64 offset, u32 mode = from_begin) = 0; + + virtual u64 Tell() const = 0; + + virtual bool Eof() const + { + return Tell() >= GetSize(); + } + + virtual bool IsOpened() const = 0; }; diff --git a/rpcs3/Emu/FS/vfsStreamMemory.cpp b/rpcs3/Emu/FS/vfsStreamMemory.cpp index 5ce30c3a7c..67134f8d3c 100644 --- a/rpcs3/Emu/FS/vfsStreamMemory.cpp +++ b/rpcs3/Emu/FS/vfsStreamMemory.cpp @@ -2,48 +2,28 @@ #include "Emu/Memory/Memory.h" #include "vfsStreamMemory.h" -vfsStreamMemory::vfsStreamMemory() : vfsStream() +u64 vfsStreamMemory::Write(const void* src, u64 count) { -} - -vfsStreamMemory::vfsStreamMemory(u32 addr, u32 size) : vfsStream() -{ - Open(addr, size); -} - -void vfsStreamMemory::Open(u32 addr, u32 size) -{ - m_addr = addr; - m_size = size ? size : 0x100000000ull - addr; // determine max possible size - - vfsStream::Reset(); -} - -u64 vfsStreamMemory::GetSize() -{ - return m_size; -} - -u64 vfsStreamMemory::Write(const void* src, u64 size) -{ - assert(Tell() < m_size); - if (Tell() + size > m_size) + assert(m_pos < m_size); + if (m_pos + count > m_size) { - size = m_size - Tell(); + count = m_size - m_pos; } - memcpy(vm::get_ptr(vm::cast(m_addr + Tell())), src, size); - return vfsStream::Write(src, size); + memcpy(vm::get_ptr(vm::cast(m_addr + m_pos)), src, count); + m_pos += count; + return count; } -u64 vfsStreamMemory::Read(void* dst, u64 size) +u64 vfsStreamMemory::Read(void* dst, u64 count) { - assert(Tell() < GetSize()); - if (Tell() + size > GetSize()) + assert(m_pos < m_size); + if (m_pos + count > m_size) { - size = GetSize() - Tell(); + count = m_size - m_pos; } - memcpy(dst, vm::get_ptr(vm::cast(m_addr + Tell())), size); - return vfsStream::Read(dst, size); + memcpy(dst, vm::get_ptr(vm::cast(m_addr + m_pos)), count); + m_pos += count; + return count; } diff --git a/rpcs3/Emu/FS/vfsStreamMemory.h b/rpcs3/Emu/FS/vfsStreamMemory.h index 1e3e06a73b..e6d7f48482 100644 --- a/rpcs3/Emu/FS/vfsStreamMemory.h +++ b/rpcs3/Emu/FS/vfsStreamMemory.h @@ -1,19 +1,57 @@ #pragma once #include "vfsStream.h" -struct vfsStreamMemory : public vfsStream +class vfsStreamMemory : public vfsStream { - u32 m_addr; - u64 m_size; + u64 m_pos = 0; + u32 m_addr = 0; + u64 m_size = 0; public: - vfsStreamMemory(); - vfsStreamMemory(u32 addr, u32 size = 0); + vfsStreamMemory() = default; - void Open(u32 addr, u32 size = 0); + vfsStreamMemory(u32 addr, u32 size = 0) + { + Open(addr, size); + } - virtual u64 GetSize() override; + void Open(u32 addr, u32 size = 0) + { + m_pos = 0; + m_addr = addr; + m_size = size ? size : 0x100000000ull - addr; // determine max possible size + } - virtual u64 Write(const void* src, u64 size) override; - virtual u64 Read(void* dst, u64 size) override; -}; \ No newline at end of file + virtual u64 GetSize() const override + { + return m_size; + } + + virtual u64 Write(const void* src, u64 count) override; + + virtual u64 Read(void* dst, u64 count) override; + + virtual u64 Seek(s64 offset, u32 mode = from_begin) override + { + assert(mode < 3); + + switch (mode) + { + case from_begin: return m_pos = offset; + case from_cur: return m_pos += offset; + case from_end: return m_pos = m_size + offset; + } + + return m_pos; + } + + virtual u64 Tell() const override + { + return m_pos; + } + + virtual bool IsOpened() const override + { + return true; + } +}; diff --git a/rpcs3/Emu/HDD/HDD.cpp b/rpcs3/Emu/HDD/HDD.cpp index 80eda63429..1a661ebd6a 100644 --- a/rpcs3/Emu/HDD/HDD.cpp +++ b/rpcs3/Emu/HDD/HDD.cpp @@ -21,7 +21,7 @@ void vfsHDDManager::CreateEntry(vfsHDD_Entry& entry) void vfsHDDManager::CreateHDD(const std::string& path, u64 size, u64 block_size) { - rFile f(path, rFile::write); + rfile_t f(path, o_write | o_create | o_trunc); static const u64 cur_dir_block = 1; @@ -32,7 +32,7 @@ void vfsHDDManager::CreateHDD(const std::string& path, u64 size, u64 block_size) hdr.version = g_hdd_version; hdr.block_count = (size + block_size) / block_size; hdr.block_size = block_size; - f.Write(&hdr, sizeof(vfsHDD_Hdr)); + f.write(&hdr, sizeof(vfsHDD_Hdr)); { vfsHDD_Entry entry; @@ -41,14 +41,14 @@ void vfsHDDManager::CreateHDD(const std::string& path, u64 size, u64 block_size) entry.data_block = hdr.next_block; entry.next_block = 0; - f.Seek(cur_dir_block * hdr.block_size); - f.Write(&entry, sizeof(vfsHDD_Entry)); - f.Write("."); + f.seek(cur_dir_block * hdr.block_size); + f.write(&entry, sizeof(vfsHDD_Entry)); + f.write(".", 1); } u8 null = 0; - f.Seek(hdr.block_count * hdr.block_size - sizeof(null)); - f.Write(&null, sizeof(null)); + f.seek(hdr.block_count * hdr.block_size - sizeof(null)); + f.write(&null, sizeof(null)); } void vfsHDDManager::Format() @@ -599,7 +599,7 @@ bool vfsHDD::GetNextEntry(u64& block, vfsHDD_Entry& entry, std::string& name) return true; } -bool vfsHDD::Open(const std::string& path, vfsOpenMode mode) +bool vfsHDD::Open(const std::string& path, u32 mode) { const char* s = path.c_str(); u64 from = 0; @@ -737,47 +737,44 @@ bool vfsHDD::RemoveEntry(const std::string& name) return true; } -bool vfsHDD::Create(const std::string& path) +u64 vfsHDD::Write(const void* src, u64 size) { - return false; + return m_file.Write(src, size); // ??? } -u32 vfsHDD::Write(const void* src, u32 size) +u64 vfsHDD::Read(void* dst, u64 size) { - return vfsFileBase::Write(src, m_file.Write(src, size)); + return m_file.Read(dst, size); // ??? } -u32 vfsHDD::Read(void* dst, u32 size) -{ - return vfsFileBase::Read(dst, m_file.Read(dst, size)); -} - -u64 vfsHDD::Seek(s64 offset, vfsSeekMode mode) +u64 vfsHDD::Seek(s64 offset, u32 mode) { switch (mode) { - case vfsSeekCur: - m_file.Seek(Tell() + offset); - break; - - case vfsSeekSet: - m_file.Seek(offset); - break; - - case vfsSeekEnd: - m_file.Seek(m_file.GetSize() + offset); - break; + case from_begin: return m_file.Seek(offset); + case from_cur: return m_file.Seek(Tell() + offset); + case from_end: return m_file.Seek(m_file.GetSize() + offset); } - return vfsFileBase::Seek(offset, mode); + return m_file.Tell(); // ??? } -bool vfsHDD::Eof() +u64 vfsHDD::Tell() const +{ + return m_file.Tell(); // ??? +} + +bool vfsHDD::Eof() const { return m_file.Eof(); } -u64 vfsHDD::GetSize() +bool vfsHDD::IsOpened() const +{ + return true; // ??? +} + +u64 vfsHDD::GetSize() const { return m_file.GetSize(); } \ No newline at end of file diff --git a/rpcs3/Emu/HDD/HDD.h b/rpcs3/Emu/HDD/HDD.h index f71fa77f35..22668d9474 100644 --- a/rpcs3/Emu/HDD/HDD.h +++ b/rpcs3/Emu/HDD/HDD.h @@ -32,7 +32,7 @@ enum vfsHDD_EntryType : u8 struct vfsHDD_Entry : public vfsHDD_Block { u64 data_block; - vfsOpenMode access; + u32 access; vfsHDD_EntryType type; u64 size; u64 ctime; @@ -109,6 +109,11 @@ public: bool Seek(u64 pos); + u64 Tell() const + { + return m_cur_block * m_hdd_info.block_size + m_position; // ??? + } + void SaveInfo(); u64 Read(void* dst, u64 size); @@ -177,7 +182,7 @@ public: bool GetNextEntry(u64& block, vfsHDD_Entry& entry, std::string& name); - virtual bool Open(const std::string& path, vfsOpenMode mode = vfsRead); + virtual bool Open(const std::string& path, u32 mode = vfsRead); bool HasEntry(const std::string& name); @@ -187,15 +192,17 @@ public: bool RemoveEntry(const std::string& name); - virtual bool Create(const std::string& path); + virtual u64 Write(const void* src, u64 count) override; - virtual u32 Write(const void* src, u32 size); + virtual u64 Read(void* dst, u64 count) override; - virtual u32 Read(void* dst, u32 size); + virtual u64 Seek(s64 offset, u32 mode = from_begin) override; - virtual u64 Seek(s64 offset, vfsSeekMode mode = vfsSeekSet); + virtual u64 Tell() const override; - virtual bool Eof(); + virtual bool Eof() const override; - virtual u64 GetSize(); + virtual bool IsOpened() const override; + + virtual u64 GetSize() const; }; diff --git a/rpcs3/Emu/RSX/CgBinaryProgram.h b/rpcs3/Emu/RSX/CgBinaryProgram.h index e8084a541d..15bdd5c50a 100644 --- a/rpcs3/Emu/RSX/CgBinaryProgram.h +++ b/rpcs3/Emu/RSX/CgBinaryProgram.h @@ -182,14 +182,13 @@ public: , m_arb_shader("") , m_dst_reg_name("") { - rFile f(path); - if (!f.IsOpened()) + rfile_t f(path); + if (!f) return; - m_buffer_size = f.Length(); + m_buffer_size = f.size(); m_buffer = new u8[m_buffer_size]; - f.Read(m_buffer, m_buffer_size); - f.Close(); + f.read(m_buffer, m_buffer_size); m_arb_shader += fmt::format("Loading... [%s]\n", path.c_str()); } @@ -315,16 +314,15 @@ public: { u32 ptr; { - rFile f(m_path); + rfile_t f(m_path); - if (!f.IsOpened()) + if (!f) return; - size_t size = f.Length(); + size_t size = f.size(); vm::ps3::init(); ptr = vm::alloc(size); - f.Read(vm::get_ptr(ptr), size); - f.Close(); + f.read(vm::get_ptr(ptr), size); } auto& vmprog = vm::get_ref(ptr); diff --git a/rpcs3/Emu/RSX/GL/GLGSRender.cpp b/rpcs3/Emu/RSX/GL/GLGSRender.cpp index 5a47ec8a62..f794e88044 100644 --- a/rpcs3/Emu/RSX/GL/GLGSRender.cpp +++ b/rpcs3/Emu/RSX/GL/GLGSRender.cpp @@ -580,10 +580,8 @@ void GLTexture::Save(RSXTexture& tex, const std::string& name) return; } - { - rFile f(name + ".raw", rFile::write); - f.Write(alldata, texPixelCount * 4); - } + rfile_t(name + ".raw", o_write | o_create | o_trunc).write(alldata, texPixelCount * 4); + u8* data = new u8[texPixelCount * 3]; u8* alpha = new u8[texPixelCount]; @@ -1136,8 +1134,7 @@ bool GLGSRender::LoadProgram() checkForGlError("m_fragment_prog.Compile"); // TODO: This shouldn't use current dir - rFile f("./FragmentProgram.txt", rFile::write); - f.Write(m_fragment_prog.shader); + rfile_t("./FragmentProgram.txt", o_write | o_create | o_trunc).write(m_fragment_prog.shader.c_str(), m_fragment_prog.shader.size()); } if (m_vp_buf_num == -1) @@ -1148,8 +1145,7 @@ bool GLGSRender::LoadProgram() checkForGlError("m_vertex_prog.Compile"); // TODO: This shouldn't use current dir - rFile f("./VertexProgram.txt", rFile::write); - f.Write(m_vertex_prog.shader); + rfile_t("./VertexProgram.txt", o_write | o_create | o_trunc).write(m_vertex_prog.shader.c_str(), m_vertex_prog.shader.size()); } if (m_fp_buf_num != -1 && m_vp_buf_num != -1) diff --git a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp index 6985f1ff70..10d8cf44fc 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellFs.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellFs.cpp @@ -756,17 +756,17 @@ bool sdata_check(u32 version, u32 flags, u64 filesizeInput, u64 filesizeTmp) int sdata_unpack(const std::string& packed_file, const std::string& unpacked_file) { std::shared_ptr packed_stream(Emu.GetVFS().OpenFile(packed_file, vfsRead)); - std::shared_ptr unpacked_stream(Emu.GetVFS().OpenFile(unpacked_file, vfsWrite)); + std::shared_ptr unpacked_stream(Emu.GetVFS().OpenFile(unpacked_file, vfsWriteNew)); if (!packed_stream || !packed_stream->IsOpened()) { - cellFs.Error("'%s' not found! flags: 0x%02x", packed_file.c_str(), vfsRead); + cellFs.Error("File '%s' not found!", packed_file.c_str()); return CELL_ENOENT; } if (!unpacked_stream || !unpacked_stream->IsOpened()) { - cellFs.Error("'%s' couldn't be created! flags: 0x%02x", unpacked_file.c_str(), vfsWrite); + cellFs.Error("File '%s' couldn't be created!", unpacked_file.c_str()); return CELL_ENOENT; } diff --git a/rpcs3/Emu/SysCalls/Modules/cellSaveData.cpp b/rpcs3/Emu/SysCalls/Modules/cellSaveData.cpp index 119564a2ad..af18b44ad2 100644 --- a/rpcs3/Emu/SysCalls/Modules/cellSaveData.cpp +++ b/rpcs3/Emu/SysCalls/Modules/cellSaveData.cpp @@ -570,7 +570,7 @@ __noinline s32 savedata_op( { case CELL_SAVEDATA_FILEOP_READ: { - file.reset(Emu.GetVFS().OpenFile(filepath, vfsRead)); + file.reset(Emu.GetVFS().OpenFile(filepath, o_read)); file->Seek(fileSet->fileOffset); fileGet->excSize = file->Read(fileSet->fileBuf.get_ptr(), std::min(fileSet->fileSize, fileSet->fileBufSize)); break; @@ -578,8 +578,7 @@ __noinline s32 savedata_op( case CELL_SAVEDATA_FILEOP_WRITE: { - Emu.GetVFS().CreateFile(filepath); - file.reset(Emu.GetVFS().OpenFile(filepath, vfsReadWrite)); + file.reset(Emu.GetVFS().OpenFile(filepath, o_write | o_create)); file->Seek(fileSet->fileOffset); fileGet->excSize = file->Write(fileSet->fileBuf.get_ptr(), std::min(fileSet->fileSize, fileSet->fileBufSize)); // TODO: truncate this fucked shit @@ -595,8 +594,7 @@ __noinline s32 savedata_op( case CELL_SAVEDATA_FILEOP_WRITE_NOTRUNC: { - Emu.GetVFS().CreateFile(filepath); - file.reset(Emu.GetVFS().OpenFile(filepath, vfsReadWrite)); + file.reset(Emu.GetVFS().OpenFile(filepath, o_write | o_create)); file->Seek(fileSet->fileOffset); fileGet->excSize = file->Write(fileSet->fileBuf.get_ptr(), std::min(fileSet->fileSize, fileSet->fileBufSize)); break; @@ -613,9 +611,7 @@ __noinline s32 savedata_op( // Write PARAM.SFO if (psf) { - Emu.GetVFS().CreateFile(sfo_path, true); - - vfsFile f(sfo_path, vfsWrite); + vfsFile f(sfo_path, vfsWriteNew); psf.Save(f); } diff --git a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp index 4279d9fbf5..b6d073b841 100644 --- a/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp +++ b/rpcs3/Emu/SysCalls/lv2/sys_fs.cpp @@ -32,8 +32,6 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, sys_fs.Warning("sys_fs_open(path=*0x%x, flags=%#o, fd=*0x%x, mode=%#o, arg=*0x%x, size=0x%llx)", path, flags, fd, mode, arg, size); sys_fs.Warning("*** path = '%s'", path.get_ptr()); - std::shared_ptr file; - // TODO: other checks for path if (Emu.GetVFS().ExistsDir(path.get_ptr())) @@ -42,81 +40,54 @@ s32 sys_fs_open(vm::ptr path, s32 flags, vm::ptr fd, s32 mode, return CELL_FS_EISDIR; } - switch (flags) + u32 open_mode = 0; + + switch (flags & CELL_FS_O_ACCMODE) { - case CELL_FS_O_RDONLY: - { - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsRead)); - break; + case CELL_FS_O_RDONLY: open_mode |= o_read; break; + case CELL_FS_O_WRONLY: open_mode |= o_write; break; + case CELL_FS_O_RDWR: open_mode |= o_read | o_write; break; } - case CELL_FS_O_WRONLY: - case CELL_FS_O_RDWR: + if (flags & CELL_FS_O_CREAT) { - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); - break; - } - - case CELL_FS_O_WRONLY | CELL_FS_O_CREAT: - case CELL_FS_O_RDWR | CELL_FS_O_CREAT: - { - Emu.GetVFS().CreateFile(path.get_ptr()); - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); - break; + open_mode |= o_create; } - case CELL_FS_O_WRONLY | CELL_FS_O_APPEND: + if (flags & CELL_FS_O_TRUNC) { - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); - break; + open_mode |= o_trunc; } - case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_EXCL: - case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_EXCL: // ??? + if (flags & CELL_FS_O_EXCL) { - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteExcl)); - - if ((!file || !file->IsOpened()) && Emu.GetVFS().ExistsFile(path.get_ptr())) + if ((flags & CELL_FS_O_CREAT) && !(flags & CELL_FS_O_TRUNC)) { - return CELL_FS_EEXIST; + open_mode |= o_excl; + } + else + { + open_mode = 0; // error } - - break; } - case CELL_FS_O_WRONLY | CELL_FS_O_TRUNC: + if (flags & ~(CELL_FS_O_ACCMODE | CELL_FS_O_CREAT | CELL_FS_O_TRUNC | CELL_FS_O_EXCL)) { - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWrite)); - break; + open_mode = 0; // error } - case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: + if ((flags & CELL_FS_O_ACCMODE) == CELL_FS_O_ACCMODE) { - Emu.GetVFS().CreateFile(path.get_ptr()); - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWrite)); - break; + open_mode = 0; // error } - case CELL_FS_O_WRONLY | CELL_FS_O_CREAT | CELL_FS_O_APPEND: - { - Emu.GetVFS().CreateFile(path.get_ptr()); - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsWriteAppend)); - break; - } - - case CELL_FS_O_RDWR | CELL_FS_O_CREAT | CELL_FS_O_TRUNC: - { - Emu.GetVFS().CreateFile(path.get_ptr(), true); - file.reset(Emu.GetVFS().OpenFile(path.get_ptr(), vfsReadWrite)); - break; - } - - default: + if (!open_mode) { sys_fs.Error("sys_fs_open(): invalid or unimplemented flags (%#o)", flags); return CELL_FS_EINVAL; } - } + + std::shared_ptr file(Emu.GetVFS().OpenFile(path.get_ptr(), open_mode)); if (!file || !file->IsOpened()) { @@ -468,15 +439,9 @@ s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr pos) { sys_fs.Log("sys_fs_lseek(fd=0x%x, offset=0x%llx, whence=0x%x, pos=*0x%x)", fd, offset, whence, pos); - vfsSeekMode seek_mode; - - switch (whence) + if (whence >= 3) { - case CELL_FS_SEEK_SET: seek_mode = vfsSeekSet; break; - case CELL_FS_SEEK_CUR: seek_mode = vfsSeekCur; break; - case CELL_FS_SEEK_END: seek_mode = vfsSeekEnd; break; - default: - sys_fs.Error("sys_fs_lseek(fd=0x%x): unknown seek whence (0x%x)", fd, whence); + sys_fs.Error("sys_fs_lseek(fd=0x%x): unknown seek whence (%d)", fd, whence); return CELL_FS_EINVAL; } @@ -489,7 +454,7 @@ s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr pos) std::lock_guard lock(file->mutex); - *pos = file->file->Seek(offset, seek_mode); + *pos = file->file->Seek(offset, whence); return CELL_OK; } @@ -544,7 +509,7 @@ s32 sys_fs_truncate(vm::ptr path, u64 size) s32 sys_fs_ftruncate(u32 fd, u64 size) { - sys_fs.Warning("sys_fs_ftruncate(fd=0x%x, size=0x%llx)", fd, size); + sys_fs.Todo("sys_fs_ftruncate(fd=0x%x, size=0x%llx)", fd, size); const auto file = Emu.GetIdManager().GetIDData(fd); @@ -555,22 +520,7 @@ s32 sys_fs_ftruncate(u32 fd, u64 size) std::lock_guard lock(file->mutex); - u64 initialSize = file->file->GetSize(); - - if (initialSize < size) - { - u64 last_pos = file->file->Tell(); - file->file->Seek(0, vfsSeekEnd); - static const char nullbyte = 0; - file->file->Seek(size - initialSize - 1, vfsSeekCur); - file->file->Write(&nullbyte, sizeof(char)); - file->file->Seek(last_pos, vfsSeekSet); - } - - if (initialSize > size) - { - // (TODO) - } + // it's near return CELL_OK; } diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index e156add969..5462af30d8 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -146,11 +146,12 @@ bool Emulator::BootGame(const std::string& path, bool direct) "/USRDIR/EBOOT.BIN", "/EBOOT.BIN" }; + auto curpath = path; if (direct) { - if (rFile::Access(curpath, rFile::read)) + if (rfile_t(curpath)) { SetPath(curpath); Load(); @@ -163,7 +164,7 @@ bool Emulator::BootGame(const std::string& path, bool direct) { curpath = path + elf_path[i]; - if (rFile::Access(curpath, rFile::read)) + if (rfile_t(curpath)) { SetPath(curpath); Load(); diff --git a/rpcs3/Gui/AutoPauseManager.cpp b/rpcs3/Gui/AutoPauseManager.cpp index 11dbc5e9d9..4954452f61 100644 --- a/rpcs3/Gui/AutoPauseManager.cpp +++ b/rpcs3/Gui/AutoPauseManager.cpp @@ -73,22 +73,20 @@ void AutoPauseManagerDialog::LoadEntries(void) if (rExists("pause.bin")) { - rFile list; - list.Open("pause.bin", rFile::read); + rfile_t list("pause.bin"); //System calls ID and Function calls ID are all u32 iirc. u32 num; - size_t fmax = list.Length(); + size_t fmax = list.size(); size_t fcur = 0; - list.Seek(0); + list.seek(0); while (fcur <= fmax - sizeof(u32)) { - list.Read(&num, sizeof(u32)); + list.read(&num, sizeof(u32)); fcur += sizeof(u32); if (num == 0xFFFFFFFF) break; m_entries.emplace_back(num); } - list.Close(); } } @@ -97,24 +95,18 @@ void AutoPauseManagerDialog::LoadEntries(void) //This would always use a 0xFFFFFFFF as end of the pause.bin void AutoPauseManagerDialog::SaveEntries(void) { - if (rExists("pause.bin")) - { - rRemoveFile("pause.bin"); - } - rFile list; - list.Open("pause.bin", rFile::write); + rfile_t list("pause.bin", o_write | o_create | o_trunc); //System calls ID and Function calls ID are all u32 iirc. u32 num = 0; - list.Seek(0); + list.seek(0); for (size_t i = 0; i < m_entries.size(); ++i) { if (num == 0xFFFFFFFF) continue; num = m_entries[i]; - list.Write(&num, sizeof(u32)); + list.write(&num, sizeof(u32)); } num = 0xFFFFFFFF; - list.Write(&num, sizeof(u32)); - list.Close(); + list.write(&num, sizeof(u32)); } void AutoPauseManagerDialog::UpdateList(void) diff --git a/rpcs3/Gui/MainFrame.cpp b/rpcs3/Gui/MainFrame.cpp index ae4ae0556a..665cc25d02 100644 --- a/rpcs3/Gui/MainFrame.cpp +++ b/rpcs3/Gui/MainFrame.cpp @@ -235,8 +235,7 @@ void MainFrame::InstallPkg(wxCommandEvent& WXUNUSED(event)) stopped = true; } - wxFileDialog ctrl (this, L"Select PKG", wxEmptyString, wxEmptyString, "PKG files (*.pkg)|*.pkg|All files (*.*)|*.*", - wxFD_OPEN | wxFD_FILE_MUST_EXIST); + wxFileDialog ctrl(this, L"Select PKG", wxEmptyString, wxEmptyString, "PKG files (*.pkg)|*.pkg|All files (*.*)|*.*", wxFD_OPEN | wxFD_FILE_MUST_EXIST); if(ctrl.ShowModal() == wxID_CANCEL) { @@ -247,14 +246,11 @@ void MainFrame::InstallPkg(wxCommandEvent& WXUNUSED(event)) Emu.Stop(); // Open and install PKG file - std::string filePath = ctrl.GetPath().ToStdString(); - rFile pkg_f(filePath, rFile::read); // TODO: Use VFS to install PKG files + rfile_t pkg_f(ctrl.GetPath().ToStdString(), o_read); - if (pkg_f.IsOpened()) + if(pkg_f) { - PKGLoader pkg(pkg_f); - pkg.Install("/dev_hdd0/game/"); - pkg.Close(); + PKGLoader::Install(pkg_f, "/dev_hdd0/game/"); // Refresh game list m_game_viewer->Refresh(); diff --git a/rpcs3/Gui/VHDDManager.cpp b/rpcs3/Gui/VHDDManager.cpp index aa818aa7a8..4792d44647 100644 --- a/rpcs3/Gui/VHDDManager.cpp +++ b/rpcs3/Gui/VHDDManager.cpp @@ -122,7 +122,7 @@ void VHDDExplorer::Import(const std::string& path, const std::string& to) return; } - if(!m_hdd->Open(to, vfsWrite)) + if(!m_hdd->Open(to, o_write)) { wxMessageBox("IMPORT ERROR: file open error."); return; diff --git a/rpcs3/Loader/ELF32.cpp b/rpcs3/Loader/ELF32.cpp index a6885f93ce..58d990c33e 100644 --- a/rpcs3/Loader/ELF32.cpp +++ b/rpcs3/Loader/ELF32.cpp @@ -153,11 +153,9 @@ namespace loader m_stream->Seek(handler::get_stream_offset() + m_shdrs[m_ehdr.data_le.e_shstrndx].data_le.sh_offset + shdr.data_le.sh_name); std::string name; - while (!m_stream->Eof()) + char c; + while (m_stream->SRead(c) && c) { - char c; - m_stream->Read(&c, 1); - if (c == 0) break; name.push_back(c); } diff --git a/rpcs3/Loader/ELF32.h b/rpcs3/Loader/ELF32.h index 6a13b6f7cb..76cc910ec2 100644 --- a/rpcs3/Loader/ELF32.h +++ b/rpcs3/Loader/ELF32.h @@ -2,7 +2,6 @@ #include "Loader.h" struct vfsStream; -class rFile; namespace loader { diff --git a/rpcs3/Loader/ELF64.h b/rpcs3/Loader/ELF64.h index 85416d5def..baa634eb95 100644 --- a/rpcs3/Loader/ELF64.h +++ b/rpcs3/Loader/ELF64.h @@ -2,7 +2,6 @@ #include "Loader.h" struct vfsStream; -class rFile; namespace loader { diff --git a/rpcs3/Loader/Loader.h b/rpcs3/Loader/Loader.h index 9ff96b0dc5..0cafa3554a 100644 --- a/rpcs3/Loader/Loader.h +++ b/rpcs3/Loader/Loader.h @@ -3,7 +3,6 @@ struct vfsFileBase; struct vfsStream; -class rFile; #ifdef _DEBUG //#define LOADER_DEBUG diff --git a/rpcs3/Loader/PKG.cpp b/rpcs3/Loader/PKG.cpp index 022a11085e..7a20598c83 100644 --- a/rpcs3/Loader/PKG.cpp +++ b/rpcs3/Loader/PKG.cpp @@ -5,35 +5,38 @@ #include "PKG.h" #include "../Crypto/unpkg.h" -PKGLoader::PKGLoader(rFile& f) : pkg_f(f) -{ -} - -bool PKGLoader::Install(std::string dest) +bool PKGLoader::Install(const rfile_t& pkg_f, std::string dest) { // Initial checks - if (!pkg_f.IsOpened()) + if (!pkg_f) + { return false; + } // TODO: This shouldn't use current dir dest.insert(0, 1, '.'); if (!dest.empty() && dest.back() != '/') + { dest += '/'; + } // Fetch title ID from the header. char title_id[48]; - pkg_f.Seek(48); - pkg_f.Read(title_id, 48); + pkg_f.seek(48); + pkg_f.read(title_id, 48); std::string titleID = std::string(title_id).substr(7, 9); - if (rExists(dest + titleID)) { - rMessageDialog d_overwrite(NULL, "Another installation found. Do you want to overwrite it?", "PKG Decrypter / Installer", rYES_NO|rCENTRE); - if (d_overwrite.ShowModal() != rID_YES) { + if (rExists(dest + titleID)) + { + if (rMessageDialog(NULL, "Another installation found. Do you want to overwrite it?", "PKG Decrypter / Installer", rYES_NO | rCENTRE).ShowModal() != rID_YES) + { LOG_ERROR(LOADER, "PKG Loader: Another installation found in: %s", titleID.c_str()); return false; } - } else if (!rMkdir(dest + titleID)) { + } + else if (!rMkdir(dest + titleID)) + { LOG_ERROR(LOADER, "PKG Loader: Could not create the installation directory: %s", titleID.c_str()); return false; } @@ -50,8 +53,3 @@ bool PKGLoader::Install(std::string dest) return true; } } - -bool PKGLoader::Close() -{ - return pkg_f.Close(); -} diff --git a/rpcs3/Loader/PKG.h b/rpcs3/Loader/PKG.h index 3c18619d11..d72fdd03db 100644 --- a/rpcs3/Loader/PKG.h +++ b/rpcs3/Loader/PKG.h @@ -1,13 +1,8 @@ #pragma once -class rFile; +struct rfile_t; -class PKGLoader +struct PKGLoader { - rFile& pkg_f; - -public: - PKGLoader(rFile& f); - virtual bool Install(std::string dest); - virtual bool Close(); + static bool Install(const rfile_t& pkg_f, std::string dest); }; diff --git a/rpcs3/Loader/TROPUSR.cpp b/rpcs3/Loader/TROPUSR.cpp index 7d6b7fb655..31f99d86f4 100644 --- a/rpcs3/Loader/TROPUSR.cpp +++ b/rpcs3/Loader/TROPUSR.cpp @@ -102,10 +102,7 @@ bool TROPUSRLoader::Save(const std::string& filepath) if (m_file) Close(); - if (!Emu.GetVFS().ExistsFile(filepath)) - Emu.GetVFS().CreateFile(filepath); - - m_file = Emu.GetVFS().OpenFile(filepath, vfsWrite); + m_file = Emu.GetVFS().OpenFile(filepath, vfsWriteNew); m_file->Write(&m_header, sizeof(TROPUSRHeader)); for (const TROPUSRTableHeader& tableHeader : m_tableHeaders) diff --git a/rpcs3/Loader/TRP.cpp b/rpcs3/Loader/TRP.cpp index 6c02e4221a..4ced4804d6 100644 --- a/rpcs3/Loader/TRP.cpp +++ b/rpcs3/Loader/TRP.cpp @@ -26,12 +26,9 @@ bool TRPLoader::Install(std::string dest, bool show) for (const TRPEntry& entry : m_entries) { char* buffer = new char [(u32)entry.size]; - Emu.GetVFS().CreateFile(dest+entry.name); - vfsFile file(dest+entry.name, vfsWrite); trp_f.Seek(entry.offset); trp_f.Read(buffer, entry.size); - file.Write(buffer, entry.size); - file.Close(); + vfsFile(dest + entry.name, vfsWriteNew).Write(buffer, entry.size); delete[] buffer; }