Better error handling when parsing TZX files -

This commit is contained in:
Asnivor 2018-03-26 07:42:55 +01:00
parent d4ee8f480b
commit 7b711cb890
3 changed files with 76 additions and 20 deletions
BizHawk.Emulation.Cores/Computers/SinclairSpectrum

View File

@ -329,30 +329,39 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
/// <param name="tapeData"></param>
public void LoadTape(byte[] tapeData)
{
// attempt TZX deserialization
// check TZX first
TzxSerializer tzxSer = new TzxSerializer(this);
try
if (tzxSer.CheckType(tapeData))
{
tzxSer.DeSerialize(tapeData);
return;
// this file has a tzx header - attempt serialization
try
{
tzxSer.DeSerialize(tapeData);
return;
}
catch (Exception ex)
{
// exception during operation
var e = ex;
throw new Exception(this.GetType().ToString() +
"\n\nTape image file has a valid TZX header, but threw an exception whilst data was being parsed.\n\n" + e.ToString());
}
}
catch (Exception ex)
else
{
// TAP format not detected
var e = ex;
}
// attempt TAP deserialization
TapSerializer tapSer = new TapSerializer(this);
try
{
tapSer.DeSerialize(tapeData);
return;
}
catch (Exception ex)
{
// TAP format not detected
var e = ex;
TapSerializer tapSer = new TapSerializer(this);
try
{
tapSer.DeSerialize(tapeData);
return;
}
catch (Exception ex)
{
// exception during operation
var e = ex;
throw new Exception(this.GetType().ToString() +
"\n\nAn exception was thrown whilst data from this tape image was being parsed as TAP.\n\n" + e.ToString());
}
}
}

View File

@ -58,6 +58,16 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
"DeSerialize operation is not implemented for this serializer");
}
/// <summary>
/// Serializer does a quick check, returns TRUE if file is detected as this type
/// </summary>
/// <param name="data"></param>
public virtual bool CheckType(byte[] data)
{
throw new NotImplementedException(this.GetType().ToString() +
"Check type operation is not implemented for this serializer");
}
#region Static Tools
/// <summary>

View File

@ -60,6 +60,43 @@ namespace BizHawk.Emulation.Cores.Computers.SinclairSpectrum
#endregion
/// <summary>
/// Returns TRUE if tzx header is detected
/// </summary>
/// <param name="data"></param>
public override bool CheckType(byte[] data)
{
/*
// TZX Header
length: 10 bytes
Offset Value Type Description
0x00 "ZXTape!" ASCII[7] TZX signature
0x07 0x1A BYTE End of text file marker
0x08 1 BYTE TZX major revision number
0x09 20 BYTE TZX minor revision number
*/
// check whether this is a valid tzx format file by looking at the identifier in the header
// (first 7 bytes of the file)
string ident = Encoding.ASCII.GetString(data, 0, 7);
// and 'end of text' marker
byte eotm = data[7];
// version info
int majorVer = data[8];
int minorVer = data[9];
if (ident != "ZXTape!" || eotm != 0x1A)
{
// this is not a valid TZX format file
return false;
}
else
{
return true;
}
}
/// <summary>
/// DeSerialization method
/// </summary>