فهرست منبع

Added corrupted file handling

flabbet 4 سال پیش
والد
کامیت
982691fd01

+ 1 - 1
PixiEditor.UpdateInstaller/PixiEditor.UpdateInstaller.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <OutputType>WinExe</OutputType>
-    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <TargetFramework>net5.0-windows</TargetFramework>
     <UseWPF>true</UseWPF>
     <ApplicationManifest>app.manifest</ApplicationManifest>
   </PropertyGroup>

+ 1 - 1
PixiEditor.UpdateModule/PixiEditor.UpdateModule.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
-    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <TargetFramework>net5.0</TargetFramework>
   </PropertyGroup>
 
 </Project>

+ 35 - 0
PixiEditor/Exceptions/CorruptedFileException.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PixiEditor.Exceptions
+{
+
+    [Serializable]
+    public class CorruptedFileException : Exception
+    {
+        public CorruptedFileException()
+            : base("Selected file is invalid or corrupted.")
+        {
+        }
+
+        public CorruptedFileException(string message)
+            : base(message)
+        {
+        }
+
+        public CorruptedFileException(string message, Exception inner)
+            : base(message, inner)
+        {
+        }
+
+        protected CorruptedFileException(
+          System.Runtime.Serialization.SerializationInfo info,
+          System.Runtime.Serialization.StreamingContext context)
+            : base(info, context)
+        {
+        }
+    }
+}

+ 5 - 0
PixiEditor/Models/DataHolders/BitmapPixelChanges.cs

@@ -59,6 +59,11 @@ namespace PixiEditor.Models.DataHolders
             return output;
         }
 
+        public static BitmapPixelChanges CombineOverride(BitmapPixelChanges changes1, BitmapPixelChanges changes2)
+        {
+            return CombineOverride(new[] { changes1, changes2 });
+        }
+
         /// <summary>
         ///     Builds BitmapPixelChanges using 2 same-length enumerables of coordinates and colors.
         /// </summary>

+ 43 - 27
PixiEditor/Models/IO/Importer.cs

@@ -1,5 +1,7 @@
 using System;
+using System.Runtime.Serialization;
 using System.Windows.Media.Imaging;
+using PixiEditor.Exceptions;
 using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
 
@@ -13,42 +15,56 @@ namespace PixiEditor.Models.IO
         /// <param name="path">Path of image.</param>
         /// <param name="width">New width of image.</param>
         /// <param name="height">New height of image.</param>
-        /// <returns></returns>
+        /// <returns>WriteableBitmap of improted image.</returns>
         public static WriteableBitmap ImportImage(string path, int width, int height)
-        {
-            WriteableBitmap wbmp = ImportImage(path);
+        {
+            WriteableBitmap wbmp = ImportImage(path);
             if (wbmp.PixelWidth != width || wbmp.PixelHeight != height)
             {
                 return wbmp.Resize(width, height, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
             }
 
-            return wbmp;
-        }
-
+            return wbmp;
+        }
+
         /// <summary>
         ///     Imports image from path and resizes it to given dimensions.
         /// </summary>
         /// <param name="path">Path of image.</param>
-        public static WriteableBitmap ImportImage(string path)
-        {
-            Uri uri = new Uri(path);
-            BitmapImage bitmap = new BitmapImage();
-            bitmap.BeginInit();
-            bitmap.UriSource = uri;
-            bitmap.EndInit();
-
-            return BitmapFactory.ConvertToPbgra32Format(bitmap);
-        }
-
-        public static Document ImportDocument(string path)
-        {
-            return BinarySerialization.ReadFromBinaryFile<SerializableDocument>(path).ToDocument();
-        }
-
-        public static bool IsSupportedFile(string path)
-        {
-            path = path.ToLower();
-            return path.EndsWith(".pixi") || path.EndsWith(".png") || path.EndsWith(".jpg") || path.EndsWith(".jpeg");
-        }
+        public static WriteableBitmap ImportImage(string path)
+        {
+            try
+            {
+                Uri uri = new Uri(path);
+                BitmapImage bitmap = new BitmapImage();
+                bitmap.BeginInit();
+                bitmap.UriSource = uri;
+                bitmap.EndInit();
+
+                return BitmapFactory.ConvertToPbgra32Format(bitmap);
+            }
+            catch (NotSupportedException)
+            {
+                throw new CorruptedFileException();
+            }
+        }
+
+        public static Document ImportDocument(string path)
+        {
+            try
+            {
+                return BinarySerialization.ReadFromBinaryFile<SerializableDocument>(path).ToDocument();
+            }
+            catch (SerializationException)
+            {
+                throw new CorruptedFileException();
+            }
+        }
+
+        public static bool IsSupportedFile(string path)
+        {
+            path = path.ToLower();
+            return path.EndsWith(".pixi") || path.EndsWith(".png") || path.EndsWith(".jpg") || path.EndsWith(".jpeg");
+        }
     }
 }

+ 5 - 5
PixiEditor/Models/Tools/BitmapOperationTool.cs

@@ -11,20 +11,20 @@ namespace PixiEditor.Models.Tools
 
         public bool UseDefaultUndoMethod { get; set; } = true;
 
-        private LayerChange[] _onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, 0) };
+        private readonly LayerChange[] onlyLayerArr = new LayerChange[] { new LayerChange(BitmapPixelChanges.Empty, 0) };
 
         public abstract LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color);
 
         protected LayerChange[] Only(BitmapPixelChanges changes, Layer layer)
         {
-            _onlyLayerArr[0] = new LayerChange(changes, layer);
-            return _onlyLayerArr;
+            onlyLayerArr[0] = new LayerChange(changes, layer);
+            return onlyLayerArr;
         }
 
         protected LayerChange[] Only(BitmapPixelChanges changes, int layerIndex)
         {
-            _onlyLayerArr[0] = new LayerChange(changes, layerIndex);
-            return _onlyLayerArr;
+            onlyLayerArr[0] = new LayerChange(changes, layerIndex);
+            return onlyLayerArr;
         }
     }
 }

+ 37 - 30
PixiEditor/ViewModels/ImportFilePopupViewModel.cs

@@ -3,25 +3,22 @@ using System.IO;
 using System.Windows;
 using System.Windows.Media.Imaging;
 using Microsoft.Win32;
+using PixiEditor.Exceptions;
 using PixiEditor.Helpers;
 
 namespace PixiEditor.ViewModels
 {
     internal class ImportFilePopupViewModel : ViewModelBase
     {
-        private string _filePath;
+        private string filePath;
 
+        private int importHeight = 16;
 
-        private int _importHeight = 16;
+        private int importWidth = 16;
 
+        private string pathButtonBorder = "#f08080";
 
-        private int _importWidth = 16;
-
-
-        private string _pathButtonBorder = "#f08080";
-
-
-        private bool _pathIsCorrect;
+        private bool pathIsCorrect;
 
         public ImportFilePopupViewModel()
         {
@@ -32,18 +29,21 @@ namespace PixiEditor.ViewModels
         }
 
         public RelayCommand CloseButtonCommand { get; set; }
+
         public RelayCommand DragMoveCommand { get; set; }
+
         public RelayCommand ChoosePathCommand { get; set; }
+
         public RelayCommand OkCommand { get; set; }
 
         public string PathButtonBorder
         {
-            get => _pathButtonBorder;
+            get => pathButtonBorder;
             set
             {
-                if (_pathButtonBorder != value)
+                if (pathButtonBorder != value)
                 {
-                    _pathButtonBorder = value;
+                    pathButtonBorder = value;
                     RaisePropertyChanged("PathButtonBorder");
                 }
             }
@@ -51,12 +51,12 @@ namespace PixiEditor.ViewModels
 
         public bool PathIsCorrect
         {
-            get => _pathIsCorrect;
+            get => pathIsCorrect;
             set
             {
-                if (_pathIsCorrect != value)
+                if (pathIsCorrect != value)
                 {
-                    _pathIsCorrect = value;
+                    pathIsCorrect = value;
                     RaisePropertyChanged("PathIsCorrect");
                 }
             }
@@ -64,12 +64,12 @@ namespace PixiEditor.ViewModels
 
         public string FilePath
         {
-            get => _filePath;
+            get => filePath;
             set
             {
-                if (_filePath != value)
+                if (filePath != value)
                 {
-                    _filePath = value;
+                    filePath = value;
                     CheckForPath(value);
                     RaisePropertyChanged("FilePath");
                 }
@@ -78,12 +78,12 @@ namespace PixiEditor.ViewModels
 
         public int ImportWidth
         {
-            get => _importWidth;
+            get => importWidth;
             set
             {
-                if (_importWidth != value)
+                if (importWidth != value)
                 {
-                    _importWidth = value;
+                    importWidth = value;
                     RaisePropertyChanged("ImportWidth");
                 }
             }
@@ -91,12 +91,12 @@ namespace PixiEditor.ViewModels
 
         public int ImportHeight
         {
-            get => _importHeight;
+            get => importHeight;
             set
             {
-                if (_importHeight != value)
+                if (importHeight != value)
                 {
-                    _importHeight = value;
+                    importHeight = value;
                     RaisePropertyChanged("ImportHeight");
                 }
             }
@@ -132,12 +132,19 @@ namespace PixiEditor.ViewModels
         {
             if (File.Exists(path) && (path.EndsWith(".png") || path.EndsWith(".jpeg") || path.EndsWith(".jpg")))
             {
-                PathButtonBorder = "#b8f080";
-                PathIsCorrect = true;
-                _filePath = path;
-                BitmapImage bitmap = new BitmapImage(new Uri(path));
-                ImportHeight = bitmap.PixelHeight;
-                ImportWidth = bitmap.PixelWidth;
+                try
+                {
+                    PathButtonBorder = "#b8f080";
+                    PathIsCorrect = true;
+                    filePath = path;
+                    BitmapImage bitmap = new BitmapImage(new Uri(path));
+                    ImportHeight = bitmap.PixelHeight;
+                    ImportWidth = bitmap.PixelWidth;
+                }
+                catch (NotSupportedException)
+                {
+                    throw new CorruptedFileException();
+                }
             }
         }
 

+ 13 - 5
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Windows;
 using System.Windows.Media.Imaging;
 using Microsoft.Win32;
+using PixiEditor.Exceptions;
 using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
@@ -109,14 +110,21 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
                 }
             }
 
-            Owner.ResetProgramStateValues();
-            if (path.EndsWith(".pixi"))
+            try
             {
-                OpenDocument(path);
+                Owner.ResetProgramStateValues();
+                if (path.EndsWith(".pixi"))
+                {
+                    OpenDocument(path);
+                }
+                else
+                {
+                    OpenFile(path);
+                }
             }
-            else
+            catch (CorruptedFileException ex)
             {
-                OpenFile(path);
+                MessageBox.Show(ex.Message, "Failed to open file.", MessageBoxButton.OK, MessageBoxImage.Error);
             }
         }
 

+ 2 - 0
PixiEditorTests/PixiEditorTests.csproj

@@ -4,6 +4,8 @@
     <TargetFramework>netcoreapp3.1</TargetFramework>
 
     <IsPackable>false</IsPackable>
+
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
   </PropertyGroup>
 
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">