Skip to content

Commit

Permalink
Log when a ChaFile fails to load.
Browse files Browse the repository at this point in the history
  • Loading branch information
im-mi committed Sep 19, 2018
1 parent 8da9b8e commit 55f62b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
28 changes: 18 additions & 10 deletions Plugins/Koikatu/ChaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,30 @@ public ChaFile(Stream stream)
{
using (var br = new BinaryReader(stream))
{
PngFile.SkipPng(stream);

try
{
br.ReadByte();
stream.Position -= sizeof(byte);
PngFile.SkipPng(stream);

try
{
br.ReadByte();
stream.Position -= sizeof(byte);
}
catch (EndOfStreamException)
{
throw new IOException("File is just a plain image with no card data.");
}

loadProductNo = br.ReadInt32();
var title = br.ReadString();
if (title != ChaFileDefine.CharaFileMark)
throw new IOException("Not a character card.");
}
catch (EndOfStreamException)
catch (Exception ex)
{
throw new IOException("File is just a plain image with no card data.");
throw new FileTypeException("File is not a Koikatu character card or the file type could not be determined.", ex);
}

loadProductNo = br.ReadInt32();
var title = br.ReadString();
if (title != ChaFileDefine.CharaFileMark)
throw new IOException("Not a character card.");
var version = new Version(br.ReadString());
var facePngDataLength = br.ReadInt32();
stream.Position += facePngDataLength;
Expand Down
12 changes: 12 additions & 0 deletions Plugins/Koikatu/FileTypeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace KoiCatalog.Plugins.Koikatu
{
public sealed class FileTypeException : Exception
{
public FileTypeException(
string message, Exception innerException = null) : base(message, innerException)
{
}
}
}
1 change: 1 addition & 0 deletions Plugins/Koikatu/Koikatu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="ChaFileStatus.cs" />
<Compile Include="ClubActivity.cs" />
<Compile Include="ColorFilters.cs" />
<Compile Include="FileTypeException.cs" />
<Compile Include="HairStyle.cs" />
<Compile Include="HeightType.cs" />
<Compile Include="KoikatuCharacterCard.cs" />
Expand Down
5 changes: 4 additions & 1 deletion Plugins/Koikatu/KoikatuFileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public override void HandleFile(FileLoader loader)

var fileInfo = loader.Entity.GetComponent(FileInfo.TypeCode);

if (fileInfo == null)
throw new InvalidOperationException("Missing required component.");

ChaFile chaFile;
try
{
Expand All @@ -26,7 +29,7 @@ public override void HandleFile(FileLoader loader)
chaFile = new ChaFile(stream);
}
}
catch (Exception)
catch (FileTypeException)
{
return;
}
Expand Down

0 comments on commit 55f62b2

Please sign in to comment.