Revert "Don't open/close file for every file operation." as it was crashing PokePark in Windows builds.

This reverts commit efcb2abe9b.

Fixes issue 6098.
This commit is contained in:
skidau 2013-03-27 13:06:15 +11:00
parent ae62af8a93
commit 5cea0d9def
2 changed files with 35 additions and 46 deletions

View File

@ -103,12 +103,7 @@ bool CWII_IPC_HLE_Device_FileIO::Open(u32 _CommandAddress, u32 _Mode)
return true; return true;
} }
// Opens file if needed. File::IOFile CWII_IPC_HLE_Device_FileIO::OpenFile()
// Clears any error state.
// Seeks to proper position position.
void CWII_IPC_HLE_Device_FileIO::PrepareFile()
{
if (!m_file.IsOpen())
{ {
const char* open_mode = ""; const char* open_mode = "";
@ -128,11 +123,7 @@ void CWII_IPC_HLE_Device_FileIO::PrepareFile()
break; break;
} }
m_file.Open(m_filepath, open_mode); return File::IOFile(m_filepath, open_mode);
}
m_file.Clear();
m_file.Seek(m_SeekPos, SEEK_SET);
} }
bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress) bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress)
@ -141,12 +132,11 @@ bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress)
const u32 SeekOffset = Memory::Read_U32(_CommandAddress + 0xC); const u32 SeekOffset = Memory::Read_U32(_CommandAddress + 0xC);
const u32 Mode = Memory::Read_U32(_CommandAddress + 0x10); const u32 Mode = Memory::Read_U32(_CommandAddress + 0x10);
PrepareFile(); if (auto file = OpenFile())
if (m_file)
{ {
ReturnValue = FS_RESULT_FATAL; ReturnValue = FS_RESULT_FATAL;
const u64 fileSize = m_file.GetSize(); const u64 fileSize = file.GetSize();
INFO_LOG(WII_IPC_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08llx)", SeekOffset, Mode, m_Name.c_str(), fileSize); INFO_LOG(WII_IPC_FILEIO, "FileIO: Seek Pos: 0x%08x, Mode: %i (%s, Length=0x%08llx)", SeekOffset, Mode, m_Name.c_str(), fileSize);
u64 wantedPos = 0; u64 wantedPos = 0;
switch (Mode) switch (Mode)
@ -156,7 +146,7 @@ bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress)
break; break;
case 1: case 1:
wantedPos = m_SeekPos + (s32)SeekOffset; wantedPos = m_SeekPos + SeekOffset;
break; break;
case 2: case 2:
@ -191,8 +181,8 @@ bool CWII_IPC_HLE_Device_FileIO::Read(u32 _CommandAddress)
const u32 Address = Memory::Read_U32(_CommandAddress + 0xC); // Read to this memory address const u32 Address = Memory::Read_U32(_CommandAddress + 0xC); // Read to this memory address
const u32 Size = Memory::Read_U32(_CommandAddress + 0x10); const u32 Size = Memory::Read_U32(_CommandAddress + 0x10);
PrepareFile();
if (m_file) if (auto file = OpenFile())
{ {
if (m_Mode == ISFS_OPEN_WRITE) if (m_Mode == ISFS_OPEN_WRITE)
{ {
@ -201,8 +191,9 @@ bool CWII_IPC_HLE_Device_FileIO::Read(u32 _CommandAddress)
else else
{ {
INFO_LOG(WII_IPC_FILEIO, "FileIO: Read 0x%x bytes to 0x%08x from %s", Size, Address, m_Name.c_str()); INFO_LOG(WII_IPC_FILEIO, "FileIO: Read 0x%x bytes to 0x%08x from %s", Size, Address, m_Name.c_str());
ReturnValue = (u32)fread(Memory::GetPointer(Address), 1, Size, m_file.GetHandle()); file.Seek(m_SeekPos, SEEK_SET);
if (ReturnValue != Size && ferror(m_file.GetHandle())) ReturnValue = (u32)fread(Memory::GetPointer(Address), 1, Size, file.GetHandle());
if (ReturnValue != Size && ferror(file.GetHandle()))
{ {
ReturnValue = FS_EACCESS; ReturnValue = FS_EACCESS;
} }
@ -229,8 +220,8 @@ bool CWII_IPC_HLE_Device_FileIO::Write(u32 _CommandAddress)
const u32 Address = Memory::Read_U32(_CommandAddress + 0xC); // Write data from this memory address const u32 Address = Memory::Read_U32(_CommandAddress + 0xC); // Write data from this memory address
const u32 Size = Memory::Read_U32(_CommandAddress + 0x10); const u32 Size = Memory::Read_U32(_CommandAddress + 0x10);
PrepareFile();
if (m_file) if (auto file = OpenFile())
{ {
if (m_Mode == ISFS_OPEN_READ) if (m_Mode == ISFS_OPEN_READ)
{ {
@ -239,7 +230,8 @@ bool CWII_IPC_HLE_Device_FileIO::Write(u32 _CommandAddress)
else else
{ {
INFO_LOG(WII_IPC_FILEIO, "FileIO: Write 0x%04x bytes from 0x%08x to %s", Size, Address, m_Name.c_str()); INFO_LOG(WII_IPC_FILEIO, "FileIO: Write 0x%04x bytes from 0x%08x to %s", Size, Address, m_Name.c_str());
if (m_file.WriteBytes(Memory::GetPointer(Address), Size)) file.Seek(m_SeekPos, SEEK_SET);
if (file.WriteBytes(Memory::GetPointer(Address), Size))
{ {
ReturnValue = Size; ReturnValue = Size;
m_SeekPos += Size; m_SeekPos += Size;
@ -269,10 +261,9 @@ bool CWII_IPC_HLE_Device_FileIO::IOCtl(u32 _CommandAddress)
{ {
case ISFS_IOCTL_GETFILESTATS: case ISFS_IOCTL_GETFILESTATS:
{ {
PrepareFile(); if (auto file = OpenFile())
if (m_file)
{ {
u32 m_FileLength = (u32)m_file.GetSize(); u32 m_FileLength = (u32)file.GetSize();
const u32 BufferOut = Memory::Read_U32(_CommandAddress + 0x18); const u32 BufferOut = Memory::Read_U32(_CommandAddress + 0x18);
INFO_LOG(WII_IPC_FILEIO, "FileIO: ISFS_IOCTL_GETFILESTATS"); INFO_LOG(WII_IPC_FILEIO, "FileIO: ISFS_IOCTL_GETFILESTATS");
@ -308,6 +299,5 @@ void CWII_IPC_HLE_Device_FileIO::DoState(PointerWrap &p)
p.Do(m_Mode); p.Do(m_Mode);
p.Do(m_SeekPos); p.Do(m_SeekPos);
m_file.Close();
m_filepath = HLE_IPC_BuildFilename(m_Name, 64); m_filepath = HLE_IPC_BuildFilename(m_Name, 64);
} }

View File

@ -38,9 +38,9 @@ public:
bool IOCtl(u32 _CommandAddress); bool IOCtl(u32 _CommandAddress);
void DoState(PointerWrap &p); void DoState(PointerWrap &p);
private: File::IOFile OpenFile();
void PrepareFile();
private:
enum enum
{ {
ISFS_OPEN_READ = 1, ISFS_OPEN_READ = 1,
@ -76,7 +76,6 @@ private:
u32 m_Mode; u32 m_Mode;
u32 m_SeekPos; u32 m_SeekPos;
File::IOFile m_file;
std::string m_filepath; std::string m_filepath;
}; };