diff --git a/UnleashedRecomp/kernel/io/file_system.cpp b/UnleashedRecomp/kernel/io/file_system.cpp index 3ff0d41c..53bf8272 100644 --- a/UnleashedRecomp/kernel/io/file_system.cpp +++ b/UnleashedRecomp/kernel/io/file_system.cpp @@ -6,9 +6,46 @@ #include #include +static constexpr uint32_t GUEST_INVALID_HANDLE_VALUE = 0xFFFFFFFF; + +struct FileHandle +{ + std::fstream stream; + std::filesystem::path path; +}; + +struct FindHandle +{ + std::filesystem::path searchPath; + std::filesystem::directory_iterator iterator; + + void fillFindData(LPWIN32_FIND_DATAA lpFindFileData) + { + if (iterator->is_directory()) + lpFindFileData->dwFileAttributes = ByteSwap(FILE_ATTRIBUTE_DIRECTORY); + else if (iterator->is_regular_file()) + lpFindFileData->dwFileAttributes = ByteSwap(FILE_ATTRIBUTE_NORMAL); + + std::u8string pathU8Str = std::filesystem::relative(iterator->path(), searchPath).u8string(); + uint64_t fileSize = iterator->file_size(); + strncpy(lpFindFileData->cFileName, (const char *)(pathU8Str.c_str()), sizeof(lpFindFileData->cFileName)); + lpFindFileData->nFileSizeLow = ByteSwap(uint32_t(fileSize >> 32U)); + lpFindFileData->nFileSizeHigh = ByteSwap(uint32_t(fileSize)); + lpFindFileData->ftCreationTime = {}; + lpFindFileData->ftLastAccessTime = {}; + lpFindFileData->ftLastWriteTime = {}; + } +}; + +bool FileHandleCloser(void* handle) +{ + delete (FileHandle *)(handle); + return false; +} + bool FindHandleCloser(void* handle) { - FindClose(handle); + delete (FindHandle *)(handle); return false; } @@ -22,41 +59,78 @@ SWA_API uint32_t XCreateFileA DWORD dwFlagsAndAttributes ) { - const auto handle = (uint32_t)CreateFileA( - FileSystem::TransformPath(lpFileName), - dwDesiredAccess, - dwShareMode, - nullptr, - dwCreationDisposition, - dwFlagsAndAttributes & ~(FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED), - nullptr); + assert(((dwDesiredAccess & ~(GENERIC_READ | GENERIC_WRITE | FILE_READ_DATA)) == 0) && "Unknown desired access bits."); + assert(((dwShareMode & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) == 0) && "Unknown share mode bits."); + assert(((dwCreationDisposition & ~(CREATE_NEW | CREATE_ALWAYS)) == 0) && "Unknown creation disposition bits."); - GuestThread::SetLastError(GetLastError()); + std::filesystem::path filePath = FileSystem::TransformPath(lpFileName); + std::fstream fileStream; + std::ios::openmode fileOpenMode = std::ios::binary; + if (dwDesiredAccess & (GENERIC_READ | FILE_READ_DATA)) + { + fileOpenMode |= std::ios::in; + } - LOGF_UTILITY("\"{}\", 0x{:X}, 0x{:X}, 0x{:X}, 0x{:X}, 0x{:X} -> 0x{:X}", - lpFileName, dwDesiredAccess, dwShareMode, (intptr_t)lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, handle); + if (dwDesiredAccess & GENERIC_WRITE) + { + fileOpenMode |= std::ios::out; + } - return handle; + fileStream.open(filePath, fileOpenMode); + if (!fileStream.is_open()) + { +#ifdef _WIN32 + GuestThread::SetLastError(GetLastError()); +#endif + return GUEST_INVALID_HANDLE_VALUE; + } + + FileHandle *fileHandle = new FileHandle(); + fileHandle->stream = std::move(fileStream); + fileHandle->path = std::move(filePath); + return GUEST_HANDLE(ObInsertObject(fileHandle, FileHandleCloser)); } static DWORD XGetFileSizeA(uint32_t hFile, LPDWORD lpFileSizeHigh) { - DWORD fileSize = GetFileSize((HANDLE)hFile, lpFileSizeHigh); + FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile))); + if (handle != nullptr) + { + std::error_code ec; + auto fileSize = std::filesystem::file_size(handle->path, ec); + if (!ec) + { + if (lpFileSizeHigh != nullptr) + { + *lpFileSizeHigh = ByteSwap(DWORD(fileSize >> 32U)); + } - if (lpFileSizeHigh != nullptr) - *lpFileSizeHigh = ByteSwap(*lpFileSizeHigh); + return (DWORD)(fileSize); + } + } - return fileSize; + return INVALID_FILE_SIZE; } BOOL XGetFileSizeExA(uint32_t hFile, PLARGE_INTEGER lpFileSize) { - BOOL result = GetFileSizeEx((HANDLE)hFile, lpFileSize); + FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile))); + if (handle != nullptr) + { + std::error_code ec; + auto fileSize = std::filesystem::file_size(handle->path, ec); + if (!ec) + { + if (lpFileSize != nullptr) + { + lpFileSize->QuadPart = ByteSwap(fileSize); + } - if (result) - lpFileSize->QuadPart = ByteSwap(lpFileSize->QuadPart); + return 1; + } + } - return result; + return 0; } BOOL XReadFile @@ -68,16 +142,31 @@ BOOL XReadFile XOVERLAPPED* lpOverlapped ) { + FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile))); + if (handle == nullptr) + { + return FALSE; + } + + BOOL result = FALSE; if (lpOverlapped != nullptr) { - LONG distanceToMoveHigh = lpOverlapped->OffsetHigh; - - if (SetFilePointer((HANDLE)hFile, lpOverlapped->Offset, &distanceToMoveHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER) + std::streamoff streamOffset = lpOverlapped->Offset + (std::streamoff(lpOverlapped->OffsetHigh.get()) << 32U); + handle->stream.clear(); + handle->stream.seekg(streamOffset, std::ios::beg); + if (handle->stream.bad()) + { return FALSE; + } } DWORD numberOfBytesRead; - BOOL result = ReadFile((HANDLE)hFile, lpBuffer, nNumberOfBytesToRead, &numberOfBytesRead, nullptr); + handle->stream.read((char *)(lpBuffer), nNumberOfBytesToRead); + if (!handle->stream.bad()) + { + numberOfBytesRead = DWORD(handle->stream.gcount()); + result = TRUE; + } if (result) { @@ -95,78 +184,165 @@ BOOL XReadFile } } - // printf("ReadFile(): %x %x %x %x %x %x\n", hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, result); - return result; } DWORD XSetFilePointer(uint32_t hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod) { + FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile))); + if (handle == nullptr) + { + return INVALID_SET_FILE_POINTER; + } + LONG distanceToMoveHigh = lpDistanceToMoveHigh ? ByteSwap(*lpDistanceToMoveHigh) : 0; - DWORD result = SetFilePointer((HANDLE)hFile, lDistanceToMove, lpDistanceToMoveHigh ? &distanceToMoveHigh : nullptr, dwMoveMethod); + std::streamoff streamOffset = lDistanceToMove + (std::streamoff(distanceToMoveHigh) << 32U); + std::fstream::seekdir streamSeekDir = {}; + switch (dwMoveMethod) + { + case FILE_BEGIN: + streamSeekDir = std::ios::beg; + break; + case FILE_CURRENT: + streamSeekDir = std::ios::cur; + break; + case FILE_END: + streamSeekDir = std::ios::end; + break; + default: + assert(false && "Unknown move method."); + break; + } + handle->stream.clear(); + handle->stream.seekg(streamOffset, streamSeekDir); + if (handle->stream.bad()) + { + return INVALID_SET_FILE_POINTER; + } + + std::streampos streamPos = handle->stream.tellg(); if (lpDistanceToMoveHigh != nullptr) - *lpDistanceToMoveHigh = ByteSwap(distanceToMoveHigh); + *lpDistanceToMoveHigh = ByteSwap(LONG(streamPos >> 32U)); - return result; + return DWORD(streamPos); } BOOL XSetFilePointerEx(uint32_t hFile, LONG lDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod) { - LARGE_INTEGER distanceToMove; - distanceToMove.QuadPart = lDistanceToMove; + FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile))); + if (handle == nullptr) + { + return FALSE; + } - DWORD result = SetFilePointerEx((HANDLE)hFile, distanceToMove, lpNewFilePointer, dwMoveMethod); + std::fstream::seekdir streamSeekDir = {}; + switch (dwMoveMethod) + { + case FILE_BEGIN: + streamSeekDir = std::ios::beg; + break; + case FILE_CURRENT: + streamSeekDir = std::ios::cur; + break; + case FILE_END: + streamSeekDir = std::ios::end; + break; + default: + assert(false && "Unknown move method."); + break; + } + + handle->stream.clear(); + handle->stream.seekg(lDistanceToMove, streamSeekDir); + if (handle->stream.bad()) + { + return FALSE; + } if (lpNewFilePointer != nullptr) - lpNewFilePointer->QuadPart = ByteSwap(lpNewFilePointer->QuadPart); + { + lpNewFilePointer->QuadPart = ByteSwap(LONGLONG(handle->stream.tellg())); + } - return result; + return TRUE; } uint32_t XFindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData) { - auto& data = *lpFindFileData; - const auto handle = FindFirstFileA(FileSystem::TransformPath(lpFileName), &data); + const char *transformedPath = FileSystem::TransformPath(lpFileName); + size_t transformedPathLength = strlen(transformedPath); + if (transformedPathLength == 0) + return GUEST_INVALID_HANDLE_VALUE; - GuestThread::SetLastError(GetLastError()); + std::filesystem::path dirPath; + if (strstr(transformedPath, "\\*") == (&transformedPath[transformedPathLength - 2])) + { + dirPath = std::string(transformedPath, transformedPathLength - 2); + } + else if (strstr(transformedPath, "\\*.*") == (&transformedPath[transformedPathLength - 4])) + { + dirPath = std::string(transformedPath, transformedPathLength - 4); + } + else + { + dirPath = std::string(transformedPath, transformedPathLength); + assert(!dirPath.has_extension() && "Unknown search pattern."); + } - if (handle == INVALID_HANDLE_VALUE) - return 0xFFFFFFFF; + if (!std::filesystem::is_directory(dirPath)) + return GUEST_INVALID_HANDLE_VALUE; - ByteSwapInplace(data.dwFileAttributes); - ByteSwapInplace(*(uint64_t*)&data.ftCreationTime); - ByteSwapInplace(*(uint64_t*)&data.ftLastAccessTime); - ByteSwapInplace(*(uint64_t*)&data.ftLastWriteTime); - ByteSwapInplace(*(uint64_t*)&data.nFileSizeHigh); + std::filesystem::directory_iterator dirIterator(dirPath); + if (dirIterator == std::filesystem::directory_iterator()) + return GUEST_INVALID_HANDLE_VALUE; - return GUEST_HANDLE(ObInsertObject(handle, FindHandleCloser)); + FindHandle *findHandle = new FindHandle(); + findHandle->searchPath = std::move(dirPath); + findHandle->iterator = std::move(dirIterator); + findHandle->fillFindData(lpFindFileData); + return GUEST_HANDLE(ObInsertObject(findHandle, FindHandleCloser)); } uint32_t XFindNextFileA(uint32_t Handle, LPWIN32_FIND_DATAA lpFindFileData) { - auto* handle = ObQueryObject(HOST_HANDLE(Handle)); - auto& data = *lpFindFileData; - const auto result = FindNextFileA(handle, &data); + FindHandle *findHandle = (FindHandle *)(ObQueryObject(HOST_HANDLE(Handle))); + if (findHandle == nullptr) + return FALSE; - ByteSwapInplace(data.dwFileAttributes); - ByteSwapInplace(*(uint64_t*)&data.ftCreationTime); - ByteSwapInplace(*(uint64_t*)&data.ftLastAccessTime); - ByteSwapInplace(*(uint64_t*)&data.ftLastWriteTime); - ByteSwapInplace(*(uint64_t*)&data.nFileSizeHigh); + findHandle->iterator++; - return result; + if (findHandle->iterator == std::filesystem::directory_iterator()) + { + return FALSE; + } + else + { + findHandle->fillFindData(lpFindFileData); + return TRUE; + } } BOOL XReadFileEx(uint32_t hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, XOVERLAPPED* lpOverlapped, uint32_t lpCompletionRoutine) { - LONG distanceToMoveHigh = lpOverlapped->OffsetHigh; - - if (SetFilePointer((HANDLE)hFile, lpOverlapped->Offset, &distanceToMoveHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER) + FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile))); + if (handle == nullptr) return FALSE; + BOOL result = FALSE; DWORD numberOfBytesRead; - BOOL result = ReadFile((HANDLE)hFile, lpBuffer, nNumberOfBytesToRead, &numberOfBytesRead, nullptr); + std::streamoff streamOffset = lpOverlapped->Offset + (std::streamoff(lpOverlapped->OffsetHigh.get()) << 32U); + handle->stream.clear(); + handle->stream.seekg(streamOffset, std::ios::beg); + if (handle->stream.bad()) + return FALSE; + + handle->stream.read((char *)(lpBuffer), nNumberOfBytesToRead); + if (!handle->stream.bad()) + { + numberOfBytesRead = DWORD(handle->stream.gcount()); + result = TRUE; + } if (result) { @@ -177,26 +353,36 @@ BOOL XReadFileEx(uint32_t hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, XO SetEvent((HANDLE)lpOverlapped->hEvent.get()); } - // printf("ReadFileEx(): %x %x %x %x %x %x\n", hFile, lpBuffer, nNumberOfBytesToRead, lpOverlapped, lpCompletionRoutine, result); - return result; } DWORD XGetFileAttributesA(LPCSTR lpFileName) { - return GetFileAttributesA(FileSystem::TransformPath(lpFileName)); + std::filesystem::path filePath = FileSystem::TransformPath(lpFileName); + if (std::filesystem::is_directory(filePath)) + return FILE_ATTRIBUTE_DIRECTORY; + else if (std::filesystem::is_regular_file(filePath)) + return FILE_ATTRIBUTE_NORMAL; + else + return INVALID_FILE_ATTRIBUTES; } BOOL XWriteFile(uint32_t hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) { - assert(lpOverlapped == nullptr); + assert(lpOverlapped == nullptr && "Overlapped not implemented."); - BOOL result = WriteFile((HANDLE)hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, nullptr); + FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile))); + if (handle == nullptr) + return FALSE; - if (result && lpNumberOfBytesWritten != nullptr) - ByteSwapInplace(*lpNumberOfBytesWritten); + handle->stream.write((const char *)(lpBuffer), nNumberOfBytesToWrite); + if (handle->stream.bad()) + return FALSE; - return result; + if (lpNumberOfBytesWritten != nullptr) + *lpNumberOfBytesWritten = DWORD(handle->stream.gcount()); + + return TRUE; } const char* FileSystem::TransformPath(const char* path)