Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Revert "Don't open/close file for every file operation." as it was cr…
…ashing PokePark in Windows builds.

This reverts commit efcb2ab.

Fixes issue 6098.
  • Loading branch information
skidau committed Mar 27, 2013
1 parent ae62af8 commit 5cea0d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 46 deletions.
76 changes: 33 additions & 43 deletions Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.cpp
Expand Up @@ -103,36 +103,27 @@ bool CWII_IPC_HLE_Device_FileIO::Open(u32 _CommandAddress, u32 _Mode)
return true;
}

// Opens file if needed.
// Clears any error state.
// Seeks to proper position position.
void CWII_IPC_HLE_Device_FileIO::PrepareFile()
File::IOFile CWII_IPC_HLE_Device_FileIO::OpenFile()
{
if (!m_file.IsOpen())
const char* open_mode = "";

switch (m_Mode)
{
const char* open_mode = "";

switch (m_Mode)
{
case ISFS_OPEN_READ:
open_mode = "rb";
break;

case ISFS_OPEN_WRITE:
case ISFS_OPEN_RW:
open_mode = "r+b";
break;

default:
PanicAlertT("FileIO: Unknown open mode : 0x%02x", m_Mode);
break;
}

m_file.Open(m_filepath, open_mode);
case ISFS_OPEN_READ:
open_mode = "rb";
break;

case ISFS_OPEN_WRITE:
case ISFS_OPEN_RW:
open_mode = "r+b";
break;

default:
PanicAlertT("FileIO: Unknown open mode : 0x%02x", m_Mode);
break;
}

m_file.Clear();
m_file.Seek(m_SeekPos, SEEK_SET);
return File::IOFile(m_filepath, open_mode);
}

bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress)
Expand All @@ -141,12 +132,11 @@ bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress)
const u32 SeekOffset = Memory::Read_U32(_CommandAddress + 0xC);
const u32 Mode = Memory::Read_U32(_CommandAddress + 0x10);

PrepareFile();
if (m_file)
if (auto file = OpenFile())
{
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);
u64 wantedPos = 0;
switch (Mode)
Expand All @@ -156,7 +146,7 @@ bool CWII_IPC_HLE_Device_FileIO::Seek(u32 _CommandAddress)
break;

case 1:
wantedPos = m_SeekPos + (s32)SeekOffset;
wantedPos = m_SeekPos + SeekOffset;
break;

case 2:
Expand Down Expand Up @@ -190,9 +180,9 @@ bool CWII_IPC_HLE_Device_FileIO::Read(u32 _CommandAddress)
u32 ReturnValue = FS_EACCESS;
const u32 Address = Memory::Read_U32(_CommandAddress + 0xC); // Read to this memory address
const u32 Size = Memory::Read_U32(_CommandAddress + 0x10);
PrepareFile();
if (m_file)


if (auto file = OpenFile())
{
if (m_Mode == ISFS_OPEN_WRITE)
{
Expand All @@ -201,8 +191,9 @@ bool CWII_IPC_HLE_Device_FileIO::Read(u32 _CommandAddress)
else
{
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());
if (ReturnValue != Size && ferror(m_file.GetHandle()))
file.Seek(m_SeekPos, SEEK_SET);
ReturnValue = (u32)fread(Memory::GetPointer(Address), 1, Size, file.GetHandle());
if (ReturnValue != Size && ferror(file.GetHandle()))
{
ReturnValue = FS_EACCESS;
}
Expand All @@ -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 Size = Memory::Read_U32(_CommandAddress + 0x10);

PrepareFile();
if (m_file)

if (auto file = OpenFile())
{
if (m_Mode == ISFS_OPEN_READ)
{
Expand All @@ -239,7 +230,8 @@ bool CWII_IPC_HLE_Device_FileIO::Write(u32 _CommandAddress)
else
{
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;
m_SeekPos += Size;
Expand Down Expand Up @@ -269,10 +261,9 @@ bool CWII_IPC_HLE_Device_FileIO::IOCtl(u32 _CommandAddress)
{
case ISFS_IOCTL_GETFILESTATS:
{
PrepareFile();
if (m_file)
if (auto file = OpenFile())
{
u32 m_FileLength = (u32)m_file.GetSize();
u32 m_FileLength = (u32)file.GetSize();

const u32 BufferOut = Memory::Read_U32(_CommandAddress + 0x18);
INFO_LOG(WII_IPC_FILEIO, "FileIO: ISFS_IOCTL_GETFILESTATS");
Expand Down Expand Up @@ -307,7 +298,6 @@ void CWII_IPC_HLE_Device_FileIO::DoState(PointerWrap &p)

p.Do(m_Mode);
p.Do(m_SeekPos);

m_file.Close();

m_filepath = HLE_IPC_BuildFilename(m_Name, 64);
}
5 changes: 2 additions & 3 deletions Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_FileIO.h
Expand Up @@ -38,9 +38,9 @@ class CWII_IPC_HLE_Device_FileIO : public IWII_IPC_HLE_Device
bool IOCtl(u32 _CommandAddress);
void DoState(PointerWrap &p);

File::IOFile OpenFile();

private:
void PrepareFile();

enum
{
ISFS_OPEN_READ = 1,
Expand Down Expand Up @@ -76,7 +76,6 @@ class CWII_IPC_HLE_Device_FileIO : public IWII_IPC_HLE_Device

u32 m_Mode;
u32 m_SeekPos;
File::IOFile m_file;

std::string m_filepath;
};
Expand Down

0 comments on commit 5cea0d9

Please sign in to comment.