From fa69ae0e893c45009878ab0a8b1905201189241f Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Tue, 13 Sep 2022 23:32:46 -0700 Subject: [PATCH] Fix loading animated documents from the command line - Only dispose of the loading stream after the document is loaded as it can be used during animation - Ensure CanSeek/CanRead are propagated for BaudStream and SauceStream - Don't try to create the out directory if the out file does not contain a path when converting Fixes #57 --- Source/Pablo.Interface/Main.cs | 12 ++++++------ Source/Pablo/Document.cs | 2 +- .../Pablo/Formats/Animated/AnimatedDocument.cs | 4 ++-- Source/Pablo/Formats/Animated/BaudStream.cs | 17 ++++------------- Source/Pablo/Formats/Rip/FormatRip.cs | 12 ++++++------ Source/Pablo/Sauce/SauceStream.cs | 15 +++------------ .../CommandHandlers/ConvertCommandLine.cs | 5 +++-- 7 files changed, 25 insertions(+), 42 deletions(-) diff --git a/Source/Pablo.Interface/Main.cs b/Source/Pablo.Interface/Main.cs index be8c68e..3f443aa 100644 --- a/Source/Pablo.Interface/Main.cs +++ b/Source/Pablo.Interface/Main.cs @@ -805,7 +805,8 @@ void InternalLoadFile(string fileName, Stream stream, Format format, bool editMo /** var bufferedStream = new MemoryStream(); stream.WriteTo(bufferedStream); - stream.Close (); + stream.Close(); + bufferedStream.Position = 0; /**/ var bufferedStream = new BufferedStream(stream, 20 * 1024); @@ -840,7 +841,7 @@ void Document_Loaded(object sender, EventArgs e) { if (loadingStream != null) { - // loadingStream.Dispose(); + loadingStream.Dispose(); loadingStream = null; PabloApplication.Instance.Invoke(delegate { @@ -869,10 +870,9 @@ public bool LoadFile(string fileName, bool hasSavePermissions, bool setFileList if (File.Exists(fileName)) { - using (var stream = File.OpenRead(fileName)) - { - return LoadFile(fileName, stream, null, editMode ?? EditMode, setFileList, hasSavePermissions); - } + // do not dispose, we may need to load in the background for animated documents. + var stream = File.OpenRead(fileName); + return LoadFile(fileName, stream, null, editMode ?? EditMode, setFileList, hasSavePermissions); } return false; } diff --git a/Source/Pablo/Document.cs b/Source/Pablo/Document.cs index 13b2b8d..f4544aa 100644 --- a/Source/Pablo/Document.cs +++ b/Source/Pablo/Document.cs @@ -102,7 +102,7 @@ public virtual void PreLoad(Stream stream, Format format, Handler handler) public virtual void Load(string fileName, Format format, Handler handler) { this.FileName = fileName; - var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); // can't dispose here if we load in background! + var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); // can't dispose here if we load in background! Load(stream, format, handler); } diff --git a/Source/Pablo/Formats/Animated/AnimatedDocument.cs b/Source/Pablo/Formats/Animated/AnimatedDocument.cs index 44b3afa..8e3c644 100644 --- a/Source/Pablo/Formats/Animated/AnimatedDocument.cs +++ b/Source/Pablo/Formats/Animated/AnimatedDocument.cs @@ -151,8 +151,8 @@ private void Load() { if (bs != null) bs.Close(); - if (stream != null) - stream.Close(); + // if (stream != null) + // stream.Close(); bs = null; stream = null; } diff --git a/Source/Pablo/Formats/Animated/BaudStream.cs b/Source/Pablo/Formats/Animated/BaudStream.cs index bd8e72c..1751763 100644 --- a/Source/Pablo/Formats/Animated/BaudStream.cs +++ b/Source/Pablo/Formats/Animated/BaudStream.cs @@ -26,21 +26,12 @@ public long Baud TickWait = (value > 0) ? (long)(((TimeSpan.TicksPerSecond * 1.2) / (value / 8))) : 0; } } - - public override bool CanRead - { - get { return true; } - } - public override bool CanSeek - { - get { return false; } - } + public override bool CanRead => stream.CanRead; - public override bool CanWrite - { - get { return false; } - } + public override bool CanSeek => stream.CanSeek; + + public override bool CanWrite => false; public long TickWait { diff --git a/Source/Pablo/Formats/Rip/FormatRip.cs b/Source/Pablo/Formats/Rip/FormatRip.cs index 9e9d48d..6632733 100644 --- a/Source/Pablo/Formats/Rip/FormatRip.cs +++ b/Source/Pablo/Formats/Rip/FormatRip.cs @@ -47,12 +47,12 @@ public void Load (Stream stream, RipDocument document, RipHandler handler) */ if (document.AnimateView && Application.Instance != null) { document.BGI.DelayDraw = true; // for faster animation - Application.Instance.Invoke (delegate { - //lastEnableZoom = handler.EnableZoom; -#if DESKTOP - //handler.EnableZoom = false; -#endif - }); +// Application.Instance.Invoke (delegate { +// //lastEnableZoom = handler.EnableZoom; +// #if DESKTOP +// //handler.EnableZoom = false; +// #endif +// }); } try { diff --git a/Source/Pablo/Sauce/SauceStream.cs b/Source/Pablo/Sauce/SauceStream.cs index 49e32d7..1def018 100644 --- a/Source/Pablo/Sauce/SauceStream.cs +++ b/Source/Pablo/Sauce/SauceStream.cs @@ -22,20 +22,11 @@ public SauceStream(Stream stream) } else Sauce = null; } - public override bool CanRead - { - get { return true; } - } + public override bool CanRead => stream.CanRead; - public override bool CanSeek - { - get { return true; } - } + public override bool CanSeek => stream.CanSeek; - public override bool CanWrite - { - get { return false; } - } + public override bool CanWrite => false; public override void Flush() { stream.Flush(); } diff --git a/Source/PabloDraw/CommandHandlers/ConvertCommandLine.cs b/Source/PabloDraw/CommandHandlers/ConvertCommandLine.cs index e8905a1..0e459c2 100644 --- a/Source/PabloDraw/CommandHandlers/ConvertCommandLine.cs +++ b/Source/PabloDraw/CommandHandlers/ConvertCommandLine.cs @@ -111,8 +111,9 @@ public override bool Process(ProcessCommandLineArgs args) sw2.Stop (); args.Writer.WriteLine ("Convertion rate: {0}, total seconds: {1}", iters / sw2.Elapsed.TotalSeconds, sw2.Elapsed.TotalSeconds); /**/ - if (!Directory.Exists(Path.GetDirectoryName(outFile))) - Directory.CreateDirectory(Path.GetDirectoryName(outFile)); + var outDir = Path.GetDirectoryName(outFile); + if (!string.IsNullOrEmpty(outDir) && !Directory.Exists(outDir)) + Directory.CreateDirectory(outDir); using (var destinationStream = new FileStream(outFile, FileMode.Create, FileAccess.Write)) {