Browse Source

Merge pull request #118 from PixiEditor/pixiparser

Added PixiParser
Krzysztof Krysiński 4 years ago
parent
commit
3f0d2ce141

+ 77 - 0
PixiEditor/Helpers/Extensions/ParserHelpers.cs

@@ -0,0 +1,77 @@
+using System;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Windows;
+using System.Windows.Media;
+using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.ImageManipulation;
+using PixiEditor.Models.Layers;
+
+namespace PixiEditor.Helpers.Extensions
+{
+    public static class ParserHelpers
+    {
+        public static Document ToDocument(this Parser.SerializableDocument serializableDocument)
+        {
+            Document document = new Document(serializableDocument.Width, serializableDocument.Height)
+            {
+                Layers = serializableDocument.ToLayers(),
+                Swatches = new ObservableCollection<Color>(serializableDocument.Swatches.Select(x =>
+                    Color.FromArgb(x.Item1, x.Item2, x.Item3, x.Item4)))
+            };
+
+            return document;
+        }
+
+        public static ObservableCollection<Layer> ToLayers(this Parser.SerializableDocument serializableDocument)
+        {
+            ObservableCollection<Layer> layers = new ObservableCollection<Layer>();
+            for (int i = 0; i < serializableDocument.Layers.Length; i++)
+            {
+                Parser.SerializableLayer serLayer = serializableDocument.Layers[i];
+                Layer layer =
+                    new Layer(serLayer.Name, BitmapUtils.BytesToWriteableBitmap(serLayer.Width, serLayer.Height, serLayer.BitmapBytes))
+                    {
+                        IsVisible = serLayer.IsVisible,
+                        Offset = new Thickness(serLayer.OffsetX, serLayer.OffsetY, 0, 0),
+                        Opacity = serLayer.Opacity
+                    };
+                layers.Add(layer);
+            }
+
+            return layers;
+        }
+
+        public static Parser.SerializableDocument ToSerializable(this Document document)
+        {
+            Parser.SerializableDocument serializable = new Parser.SerializableDocument
+            {
+                Width = document.Width,
+                Height = document.Height,
+                Layers = document.Layers.Select(x => x.ToSerializable()).ToArray(),
+                Swatches = document.Swatches.Select(x => new Tuple<byte, byte, byte, byte>(x.A, x.R, x.G, x.B)).ToArray()
+            };
+
+            return serializable;
+        }
+
+        public static Parser.SerializableLayer ToSerializable(this Layer layer)
+        {
+            Parser.SerializableLayer serializable = new Parser.SerializableLayer
+            {
+                Name = layer.Name,
+                Width = layer.Width,
+                Height = layer.Height,
+                BitmapBytes = layer.ConvertBitmapToBytes(),
+                IsVisible = layer.IsVisible,
+                OffsetX = (int)layer.Offset.Left,
+                OffsetY = (int)layer.Offset.Top,
+                Opacity = layer.Opacity,
+                MaxWidth = layer.MaxWidth,
+                MaxHeight = layer.MaxHeight
+            };
+
+            return serializable;
+        }
+    }
+}

+ 3 - 1
PixiEditor/Models/IO/Exporter.cs

@@ -3,8 +3,10 @@ using System.IO;
 using System.Windows;
 using System.Windows;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
 using Microsoft.Win32;
 using Microsoft.Win32;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Dialogs;
+using PixiEditor.Parser;
 
 
 namespace PixiEditor.Models.IO
 namespace PixiEditor.Models.IO
 {
 {
@@ -40,7 +42,7 @@ namespace PixiEditor.Models.IO
         /// <returns>Path.</returns>
         /// <returns>Path.</returns>
         public static string SaveAsEditableFile(Document document, string path)
         public static string SaveAsEditableFile(Document document, string path)
         {
         {
-            BinarySerialization.WriteToBinaryFile(path, new SerializableDocument(document));
+            PixiParser.Serialize(document.ToSerializable(), path);
             return path;
             return path;
         }
         }
 
 

+ 19 - 3
PixiEditor/Models/IO/Importer.cs

@@ -4,8 +4,10 @@ using System.Runtime.Serialization;
 using System.Windows.Media.Imaging;
 using System.Windows.Media.Imaging;
 using PixiEditor.Exceptions;
 using PixiEditor.Exceptions;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
+using PixiEditor.Helpers.Extensions;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
-
+using PixiEditor.Parser;
+
 namespace PixiEditor.Models.IO
 namespace PixiEditor.Models.IO
 {
 {
     public class Importer : NotifyableObject
     public class Importer : NotifyableObject
@@ -57,11 +59,25 @@ namespace PixiEditor.Models.IO
         public static Document ImportDocument(string path)
         public static Document ImportDocument(string path)
         {
         {
             try
             try
-            {
-                Document doc = BinarySerialization.ReadFromBinaryFile<SerializableDocument>(path).ToDocument();
+            {
+                Document doc = PixiParser.Deserialize(path).ToDocument();
                 doc.DocumentFilePath = path;
                 doc.DocumentFilePath = path;
                 return doc;
                 return doc;
             }
             }
+            catch (InvalidFileException)
+            {
+                throw new CorruptedFileException();
+            }
+        }
+
+        public static Document ImportOldDocument(string path)
+        {
+            try
+            {
+                using FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
+
+                return PixiParser.DeserializeOld(stream).ToDocument();
+            }
             catch (SerializationException)
             catch (SerializationException)
             {
             {
                 throw new CorruptedFileException();
                 throw new CorruptedFileException();

+ 1 - 1
PixiEditor/PixiEditor.csproj

@@ -53,7 +53,7 @@
     <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
     <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
     <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
     <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
     <PackageReference Include="PixiEditor.ColorPicker" Version="1.0.1" />
     <PackageReference Include="PixiEditor.ColorPicker" Version="1.0.1" />
-    <PackageReference Include="System.Drawing.Common" Version="5.0.0" />
+    <PackageReference Include="PixiEditor.Parser" Version="1.0.1.1" />
     <PackageReference Include="WriteableBitmapEx">
     <PackageReference Include="WriteableBitmapEx">
       <Version>1.6.7</Version>
       <Version>1.6.7</Version>
     </PackageReference>
     </PackageReference>

+ 31 - 2
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -12,6 +12,7 @@ using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.UserPreferences;
 using PixiEditor.Models.UserPreferences;
+using PixiEditor.Parser;
 
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
 {
@@ -123,6 +124,22 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             {
             {
                 MessageBox.Show(ex.Message, "Failed to open file.", MessageBoxButton.OK, MessageBoxImage.Error);
                 MessageBox.Show(ex.Message, "Failed to open file.", MessageBoxButton.OK, MessageBoxImage.Error);
             }
             }
+            catch (OldFileFormatException)
+            {
+                MessageBoxResult result = MessageBox.Show("This pixi file uses the old file format and is insecure.\nOnly continue if you trust the source of the file", "Old file format", MessageBoxButton.OKCancel);
+
+                if (result == MessageBoxResult.OK)
+                {
+                    try
+                    {
+                        OpenDocument(path, true);
+                    }
+                    catch (CorruptedFileException ex)
+                    {
+                        MessageBox.Show(ex.Message, "Failed to open file.", MessageBoxButton.OK, MessageBoxImage.Error);
+                    }
+                }
+            }
         }
         }
 
 
         private void Open(object property)
         private void Open(object property)
@@ -142,11 +159,23 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             }
         }
         }
 
 
-        private void OpenDocument(string path)
+        private void OpenDocument(string path, bool openOld = false)
         {
         {
+            Document document;
+
+            if (openOld)
+            {
+                document = Importer.ImportOldDocument(path);
+            }
+            else
+            {
+                document = Importer.ImportDocument(path);
+            }
+
             if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
             if (Owner.BitmapManager.Documents.Select(x => x.DocumentFilePath).All(y => y != path))
             {
             {
-                Owner.BitmapManager.Documents.Add(Importer.ImportDocument(path));
+                Owner.BitmapManager.Documents.Add(document);
+                Owner.BitmapManager.ActiveDocument = Owner.BitmapManager.Documents.Last();
             }
             }
             else
             else
             {
             {

+ 1 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -148,7 +148,7 @@ namespace PixiEditor.ViewModels
                 document.PreviewLayer = null;
                 document.PreviewLayer = null;
             }
             }
 
 
-            BitmapManager.ActiveDocument.CenterViewport();
+            BitmapManager.ActiveDocument?.CenterViewport();
         }
         }
 
 
         public bool DocumentIsNotNull(object property)
         public bool DocumentIsNotNull(object property)