From 638fa77954eac52a778cc2938c2a72164646cfdf Mon Sep 17 00:00:00 2001 From: "j4ck.fr0st" Date: Sun, 2 Jan 2011 12:27:33 +0000 Subject: [PATCH] ISFS_Seek: Turns out POSIX allows seek past EOF, the Wii does not. Should fix Issue 3761 (really!), please test this. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6723 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.cpp | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.cpp b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.cpp index f4ffcb478a..49a1ba1f78 100644 --- a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.cpp +++ b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.cpp @@ -148,34 +148,42 @@ bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress) u32 ReturnValue = FS_INVALID_ARGUMENT; s32 SeekPosition = Memory::Read_U32(_CommandAddress + 0xC); s32 Mode = Memory::Read_U32(_CommandAddress + 0x10); + u64 fileSize = File::GetSize(m_pFileHandle); - INFO_LOG(WII_IPC_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08llx)", SeekPosition, Mode, m_Name.c_str(), File::GetSize(m_pFileHandle)); - - /* TODO: Check if the new changes and the removed hack - "magically" fixes Zelda - Twilight Princess as well */ + INFO_LOG(WII_IPC_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08llx)", SeekPosition, Mode, m_Name.c_str(), fileSize); // Set seek mode int seek_mode[3] = {SEEK_SET, SEEK_CUR, SEEK_END}; if (Mode >= 0 && Mode <= 2) { - if (fseeko(m_pFileHandle, SeekPosition, seek_mode[Mode]) == 0) + // POSIX allows seek past EOF, the Wii does not. + // TODO: Can we check this without tell'ing/seek'ing twice? + u64 curPos = ftello(m_pFileHandle); + if (fseeko(m_pFileHandle, SeekPosition, seek_mode[Mode]) == 0) { - ReturnValue = (u32)ftello(m_pFileHandle); - } + u64 newPos = ftello(m_pFileHandle); + if (newPos > fileSize) + { + ERROR_LOG(WII_IPC_FILEIO, "FILEIO: Seek past EOF - %s", m_Name.c_str()); + fseeko(m_pFileHandle, curPos, SEEK_SET); + } + else + ReturnValue = (u32)newPos; + } else { - ERROR_LOG(WII_IPC_FILEIO, "FILEIO: Seek failed - %s", m_Name.c_str()); - } + ERROR_LOG(WII_IPC_FILEIO, "FILEIO: Seek failed - %s", m_Name.c_str()); + } } else { PanicAlert("CWII_IPC_HLE_Device_FileIO Unsupported seek mode %i", Mode); } - Memory::Write_U32(ReturnValue, _CommandAddress + 0x4); + Memory::Write_U32(ReturnValue, _CommandAddress + 0x4); - return true; + return true; } bool CWII_IPC_HLE_Device_FileIO::Read(u32 _CommandAddress)