From 55f62b26c20ce2c3568014c22d0dbb18190ae4c3 Mon Sep 17 00:00:00 2001 From: im-mi Date: Wed, 19 Sep 2018 18:56:17 -0400 Subject: [PATCH] Log when a ChaFile fails to load. --- Plugins/Koikatu/ChaFile.cs | 28 +++++++++++++++++---------- Plugins/Koikatu/FileTypeException.cs | 12 ++++++++++++ Plugins/Koikatu/Koikatu.csproj | 1 + Plugins/Koikatu/KoikatuFileHandler.cs | 5 ++++- 4 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 Plugins/Koikatu/FileTypeException.cs diff --git a/Plugins/Koikatu/ChaFile.cs b/Plugins/Koikatu/ChaFile.cs index 9093ce6..f0c2527 100644 --- a/Plugins/Koikatu/ChaFile.cs +++ b/Plugins/Koikatu/ChaFile.cs @@ -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; diff --git a/Plugins/Koikatu/FileTypeException.cs b/Plugins/Koikatu/FileTypeException.cs new file mode 100644 index 0000000..6369407 --- /dev/null +++ b/Plugins/Koikatu/FileTypeException.cs @@ -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) + { + } + } +} diff --git a/Plugins/Koikatu/Koikatu.csproj b/Plugins/Koikatu/Koikatu.csproj index c87b8e0..057913d 100644 --- a/Plugins/Koikatu/Koikatu.csproj +++ b/Plugins/Koikatu/Koikatu.csproj @@ -63,6 +63,7 @@ + diff --git a/Plugins/Koikatu/KoikatuFileHandler.cs b/Plugins/Koikatu/KoikatuFileHandler.cs index 42914b1..390212a 100644 --- a/Plugins/Koikatu/KoikatuFileHandler.cs +++ b/Plugins/Koikatu/KoikatuFileHandler.cs @@ -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 { @@ -26,7 +29,7 @@ public override void HandleFile(FileLoader loader) chaFile = new ChaFile(stream); } } - catch (Exception) + catch (FileTypeException) { return; }