/* Copyright (C) 2014-2019 de4dot@gmail.com This file is part of dnSpy dnSpy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. dnSpy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ using System; using System.ComponentModel; using System.Globalization; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using dnlib.DotNet.Resources; using dnSpy.Contracts.DnSpy.Properties; namespace dnSpy.Contracts.Documents.TreeView.Resources { /// /// Serialization utilities /// public static class SerializationUtilities { /// /// Creates a serialized image /// /// Filename of image /// public static ResourceElement CreateSerializedImage(string filename) { using (var stream = File.OpenRead(filename)) return CreateSerializedImage(stream, filename); } static ResourceElement CreateSerializedImage(Stream stream, string filename) { object obj; string typeName; if (filename.EndsWith(".ico", StringComparison.OrdinalIgnoreCase)) { obj = new System.Drawing.Icon(stream); typeName = SerializedImageUtilities.SystemDrawingIcon.AssemblyQualifiedName; } else { obj = new System.Drawing.Bitmap(stream); typeName = SerializedImageUtilities.SystemDrawingBitmap.AssemblyQualifiedName; } var serializedData = Serialize(obj); var userType = new UserResourceType(typeName, ResourceTypeCode.UserTypes); var rsrcElem = new ResourceElement { Name = Path.GetFileName(filename), ResourceData = new BinaryResourceData(userType, serializedData), }; return rsrcElem; } /// /// Serializes the object /// /// Data /// public static byte[] Serialize(object obj) { //TODO: The asm names of the saved types are saved in the serialized data. If the current // module is eg. a .NET 2.0 asm, you should replace the versions from 4.0.0.0 to 2.0.0.0. var formatter = new BinaryFormatter(); var outStream = new MemoryStream(); #pragma warning disable SYSLIB0011 formatter.Serialize(outStream, obj); #pragma warning restore SYSLIB0011 return outStream.ToArray(); } /// /// Deserializes the data /// /// Serialized data /// Deserialized data /// public static string Deserialize(byte[] data, out object? obj) { try { #pragma warning disable SYSLIB0011 obj = new BinaryFormatter().Deserialize(new MemoryStream(data)); #pragma warning restore SYSLIB0011 return string.Empty; } catch (Exception ex) { obj = null; return string.Format(dnSpy_Contracts_DnSpy_Resources.CouldNotDeserializeData, ex.Message); } } /// /// Creates an object from a string /// /// Target type /// Data as a string /// Updated with the deserialized data /// public static string CreateObjectFromString(Type targetType, string typeAsString, out object? obj) { obj = null; try { var typeConverter = TypeDescriptor.GetConverter(targetType); if (typeConverter.CanConvertFrom(null, typeof(string))) { obj = typeConverter.ConvertFrom(null, CultureInfo.InvariantCulture, typeAsString); return string.Empty; } } catch (Exception ex) { return string.Format(dnSpy_Contracts_DnSpy_Resources.CouldNotConvertFromString, ex.Message); } return string.Format(dnSpy_Contracts_DnSpy_Resources.NoTypeConverter, targetType); } /// /// Converts data to a string /// /// Data /// public static string? ConvertObjectToString(object obj) { var objType = obj.GetType(); try { var typeConverter = TypeDescriptor.GetConverter(objType); if (typeConverter.CanConvertTo(null, typeof(string))) { if (typeConverter.ConvertTo(null, CultureInfo.InvariantCulture, obj, typeof(string)) is string s) return s; } } catch { } return obj.ToString(); } } }