From 79adc83ad5860848124be857c44d3fb6038ef1b8 Mon Sep 17 00:00:00 2001 From: Jonno Downes Date: Fri, 27 Dec 2019 16:21:20 +1100 Subject: [PATCH 1/2] Allow damaged files to be displayed --- Data/MidiData.cs | 6 ++++++ Data/Sets/MidiFile.cs | 14 +++++++++++++- MIDIopsyWindow.cs | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Data/MidiData.cs b/Data/MidiData.cs index 32abdd1..52046b5 100644 --- a/Data/MidiData.cs +++ b/Data/MidiData.cs @@ -100,8 +100,14 @@ protected static int ReadVLQ(byte[] bytes, int index) /* A variable-length quantity is in big-endian order, and only the lowest seven bits of each byte are part of the * quantity. The Most Significant Bit (MSB) is set in all bytes except the last. It can be up to four bytes long. */ + for (int i = 0, n = 0; i < 4; ++i) { + if ((index + i) >= bytes.Length) + { + throw new ApplicationException(Properties.Resources.InvalidVLQ +String.Format(" offset {0}",index+i)); + } + /* As appropriate, initialize the result (or shift it seven bits * to the left) and add to it the lowest seven bits of this byte. */ diff --git a/Data/Sets/MidiFile.cs b/Data/Sets/MidiFile.cs index c0f470a..a2a54ca 100644 --- a/Data/Sets/MidiFile.cs +++ b/Data/Sets/MidiFile.cs @@ -25,6 +25,8 @@ namespace JeffBourdier /// Represents a standard MIDI file (essentially, a collection of MidiChunk objects). public class MidiFile : MidiSet { + + /**************** * Constructors * ****************/ @@ -36,7 +38,14 @@ public class MidiFile : MidiSet public MidiFile(string filePath) : base(new List()) { byte[] bytes = File.ReadAllBytes(filePath); - this.Replace(bytes); + try + { + this.Replace(bytes); + } + catch (Exception ex) + { + this.CreationException = ex; + } } /// Initializes a new instance of the MidiFile class with a header (MThd) chunk. @@ -55,6 +64,9 @@ public MidiFile(MidiHeaderChunk header) : base(new List(1)) #region Public Methods + public Exception CreationException { get; set; } + + /// Gets the chunk at the specified index. /// The zero-based index of the chunk to get. /// The chunk at the specified index. diff --git a/MIDIopsyWindow.cs b/MIDIopsyWindow.cs index c69e733..6d65971 100644 --- a/MIDIopsyWindow.cs +++ b/MIDIopsyWindow.cs @@ -276,6 +276,11 @@ protected override bool OpenFile(string filePath) return false; } + if (!(this.MidiFile.CreationException==null)){ + MessageBox.Show(String.Format("warning: {0}", this.MidiFile.CreationException.Message), Meta.Name, MessageBoxButton.OK, MessageBoxImage.Error); + } + + /* Load the file object into the UI. */ this.LoadFile(); return true; From 5c0eee2e81e5e1e777c2c72dd359bdba608f730c Mon Sep 17 00:00:00 2001 From: Jonno Downes Date: Fri, 27 Dec 2019 16:26:43 +1100 Subject: [PATCH 2/2] codestyle cleanup --- Data/MidiData.cs | 2 +- Data/Sets/MidiFile.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Data/MidiData.cs b/Data/MidiData.cs index 52046b5..bafdeb2 100644 --- a/Data/MidiData.cs +++ b/Data/MidiData.cs @@ -105,7 +105,7 @@ protected static int ReadVLQ(byte[] bytes, int index) { if ((index + i) >= bytes.Length) { - throw new ApplicationException(Properties.Resources.InvalidVLQ +String.Format(" offset {0}",index+i)); + throw new ApplicationException(Properties.Resources.InvalidVLQ + string.Format(" offset {0}", index + i)); } /* As appropriate, initialize the result (or shift it seven bits diff --git a/Data/Sets/MidiFile.cs b/Data/Sets/MidiFile.cs index a2a54ca..ed2891f 100644 --- a/Data/Sets/MidiFile.cs +++ b/Data/Sets/MidiFile.cs @@ -44,7 +44,7 @@ public MidiFile(string filePath) : base(new List()) } catch (Exception ex) { - this.CreationException = ex; + CreationException = ex; } }