Merge pull request #1518 from PCSX2/pcsx2-high-level-fopen

Pcsx2 high level fopen
This commit is contained in:
Gregory Hainaut 2016-08-15 15:41:27 +02:00 committed by GitHub
commit 252c043409
1 changed files with 656 additions and 645 deletions

View File

@ -86,37 +86,35 @@ static int mg_BIToffset(u8 *buffer)
return ofs + 0x20;
}
FILE *_cdvdOpenMechaVer()
static void cdvdGetMechaVer(u8* ver)
{
// get the name of the bios file
wxFileName mecfile(EmuConfig.BiosFilename);
mecfile.SetExt( L"mec" );
const wxString fname( mecfile.GetFullPath() );
// if file doesnt exist, create empty one
FILE* fd = wxFopen(fname, L"r+b");
if (fd == NULL)
{
Console.Warning("MEC File Not Found, creating substitute...");
fd = wxFopen(fname, L"wb");
if (fd == NULL)
// Likely a bad idea to go further
if (mecfile.IsDir())
throw Exception::CannotCreateStream(fname);
fputc(0x03, fd);
fputc(0x06, fd);
fputc(0x02, fd);
fputc(0x00, fd);
}
return fd;
if (Path::GetFileSize(fname) < 4) {
Console.Warning("MEC File Not Found, creating substitute...");
wxFFile fp(fname, L"wb");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname);
u8 version[4] = {0x3, 0x6, 0x2, 0x0};
fp.Write(version, sizeof(version));
}
static void cdvdGetMechaVer(u8* ver)
{
FILE* fd = _cdvdOpenMechaVer();
fseek(fd, 0, SEEK_SET);
fread(ver, 1, 4, fd);
fclose(fd);
wxFFile fp(fname, L"rb");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname);
size_t ret = fp.Read(ver, 4);
if (ret != 4)
Console.Error(L"Failed to read from %s. Did only %zu/4 bytes", WX_STR(fname), ret);
}
NVMLayout* getNvmLayout()
@ -133,52 +131,58 @@ NVMLayout* getNvmLayout()
// Throws Exception::CannotCreateStream if the file cannot be opened for reading, or cannot
// be created for some reason.
FILE* _cdvdOpenNVM()
static void cdvdNVM(u8 *buffer, int offset, size_t bytes, bool read)
{
wxFileName nvmfile(EmuConfig.BiosFilename);
nvmfile.SetExt(L"nvm");
const wxString fname(nvmfile.GetFullPath());
// if file doesn't exist, create empty one
FILE* fd = wxFopen(fname, L"r+b");
if (fd == NULL)
{
Console.Warning("NVM File Not Found, Creating Blank File");
fd = wxFopen(fname, L"wb");
if (fd == NULL)
// Likely a bad idea to go further
if (nvmfile.IsDir())
throw Exception::CannotCreateStream(fname);
for (int i=0; i<1024; i++) fputc(0, fd);
if (Path::GetFileSize(fname) < 1024) {
Console.Warning("NVM File Not Found, creating substitute...");
wxFFile fp(fname, L"wb");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname);
u8 zero[1024] = {0};
fp.Write(zero, sizeof(zero));
//Write NVM ILink area with dummy data (Age of Empires 2)
NVMLayout* nvmLayout = getNvmLayout();
u8 ILinkID_Data[8] = { 0x00, 0xAC, 0xFF, 0xFF, 0xFF, 0xFF, 0xB9, 0x86 };
fseek(fd, *(s32*)(((u8*)nvmLayout)+offsetof(NVMLayout, ilinkId)), SEEK_SET);
fwrite(ILinkID_Data, 1, 8, fd);
}
return fd;
fp.Seek(*(s32*)(((u8*)nvmLayout) + offsetof(NVMLayout, ilinkId)));
fp.Write(ILinkID_Data, sizeof(ILinkID_Data));
}
//
// the following 'cdvd' functions all return 0 if successful
//
wxFFile fp(fname, L"r+b");
if (!fp.IsOpened())
throw Exception::CannotCreateStream(fname);
fp.Seek(offset);
size_t ret;
if (read)
ret = fp.Read(buffer, bytes);
else
ret = fp.Write(buffer, bytes);
if (ret != bytes)
Console.Error(L"Failed to %s %s. Did only %zu/%zu bytes",
read ? L"read from" : L"write to", WX_STR(fname), ret, bytes);
}
static void cdvdReadNVM(u8 *dst, int offset, int bytes) {
FILE* fd = _cdvdOpenNVM();
fseek(fd, offset, SEEK_SET);
fread(dst, 1, bytes, fd);
fclose(fd);
cdvdNVM(dst, offset, bytes, true);
}
static void cdvdWriteNVM(const u8 *src, int offset, int bytes) {
FILE* fd = _cdvdOpenNVM();
fseek(fd, offset, SEEK_SET);
fwrite(src, 1, bytes, fd);
fclose(fd);
cdvdNVM(const_cast<u8*>(src), offset, bytes, false);
}
void getNvmData(u8* buffer, s32 offset, s32 size, s32 fmtOffset)
@ -1417,6 +1421,7 @@ static __fi void fail_pol_cal()
static void cdvdWrite16(u8 rt) // SCOMMAND
{
try {
// cdvdTN diskInfo;
// cdvdTD trackInfo;
// int i, lbn, type, min, sec, frm, address;
@ -2031,6 +2036,12 @@ static void cdvdWrite16(u8 rt) // SCOMMAND
//Console.WriteLn("SCMD - 0x%x\n", rt);
cdvd.ParamP = 0;
cdvd.ParamC = 0;
} catch (Exception::CannotCreateStream& ex) {
Cpu->ThrowException(Exception::RuntimeError()
.SetDiagMsg(L"Failed to read/write NMV/MEC file.")
.SetUserMsg(pxE( L"Failed to read/write NMV/MEC file. Check your bios setup/permission settings"))
);
}
}
static __fi void cdvdWrite17(u8 rt) { // SDATAIN