CDVD: Use wxFFile API to handle MEC file too

again nicer, exception safe, less compilation warning :)

v2:
* check file is properly opened in write mode
* only print an error when result is bad
This commit is contained in:
Gregory Hainaut 2016-08-15 13:02:30 +02:00
parent 534e01e2d6
commit deb7121fde
1 changed files with 19 additions and 21 deletions

View File

@ -86,37 +86,35 @@ static int mg_BIToffset(u8 *buffer)
return ofs + 0x20; return ofs + 0x20;
} }
FILE *_cdvdOpenMechaVer() static void cdvdGetMechaVer(u8* ver)
{ {
// get the name of the bios file
wxFileName mecfile(EmuConfig.BiosFilename); wxFileName mecfile(EmuConfig.BiosFilename);
mecfile.SetExt( L"mec" ); mecfile.SetExt( L"mec" );
const wxString fname( mecfile.GetFullPath() ); const wxString fname( mecfile.GetFullPath() );
// if file doesnt exist, create empty one // Likely a bad idea to go further
FILE* fd = wxFopen(fname, L"r+b"); if (mecfile.IsDir())
if (fd == NULL) throw Exception::CannotCreateStream(fname);
{
if (Path::GetFileSize(fname) < 4) {
Console.Warning("MEC File Not Found, creating substitute..."); Console.Warning("MEC File Not Found, creating substitute...");
fd = wxFopen(fname, L"wb");
if (fd == NULL) wxFFile fp(fname, L"wb");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname); throw Exception::CannotCreateStream(fname);
fputc(0x03, fd); u8 version[4] = {0x3, 0x6, 0x2, 0x0};
fputc(0x06, fd); fp.Write(version, sizeof(version));
fputc(0x02, fd);
fputc(0x00, fd);
} }
return fd;
}
static void cdvdGetMechaVer(u8* ver) wxFFile fp(fname, L"rb");
{ if (!fp.IsOpened())
FILE* fd = _cdvdOpenMechaVer(); throw Exception::CannotCreateStream(fname);
fseek(fd, 0, SEEK_SET);
fread(ver, 1, 4, fd); size_t ret = fp.Read(ver, 4);
fclose(fd); if (ret != 4)
Console.Error(L"Failed to read from %s. Did only %zu/4 bytes", WX_STR(fname), ret);
} }
NVMLayout* getNvmLayout() NVMLayout* getNvmLayout()