Skip to content

Commit

Permalink
Extend support for new serialization formats
Browse files Browse the repository at this point in the history
  • Loading branch information
ElektroKill committed Sep 5, 2023
1 parent 4be7b3e commit efe9817
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Extensions/dnSpy.AsmEditor/Resources/ResourceCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,8 @@ static void Execute(Lazy<IUndoCommandService> undoCommandService, IAppService ap
var opts = data.CreateResourceElementOptions();
string? error;
try {
opts = new ResourceElementOptions(SerializedImageUtilities.Serialize(opts.Create()));
var format = ((BinaryResourceData)imgRsrcElNode.ResourceElement.ResourceData).Format;
opts = new ResourceElementOptions(SerializedImageUtilities.Serialize(opts.Create(), format));
error = imgRsrcElNode.CheckCanUpdateData(opts.Create());
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
*/

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -150,7 +151,15 @@ public static bool CheckType(ModuleDef? module, string name, TypeRef expectedTyp
/// </summary>
/// <param name="resElem">Resource element</param>
/// <returns></returns>
public static ResourceElement Serialize(ResourceElement resElem) {
public static ResourceElement Serialize(ResourceElement resElem) => Serialize(resElem, SerializationFormat.BinaryFormatter);

/// <summary>
/// Serializes the image
/// </summary>
/// <param name="resElem">Resource element</param>
/// <param name="format">Serialization format to use</param>
/// <returns></returns>
public static ResourceElement Serialize(ResourceElement resElem, SerializationFormat format) {
var data = (byte[])((BuiltInResourceData)resElem.ResourceData).Data;
bool isIcon = BitConverter.ToUInt32(data, 0) == 0x00010000;

Expand All @@ -165,9 +174,29 @@ public static ResourceElement Serialize(ResourceElement resElem) {
typeName = SystemDrawingBitmap.AssemblyQualifiedName;
}

byte[] serializedData;
if (format == SerializationFormat.BinaryFormatter) {
serializedData = SerializationUtilities.Serialize(obj);
}
else if (format == SerializationFormat.TypeConverterByteArray) {
var converter = TypeDescriptor.GetConverter(obj.GetType());
serializedData = (byte[])converter.ConvertTo(obj, typeof(byte[]));
}
else if (format == SerializationFormat.ActivatorStream) {
using (var stream = new MemoryStream()) {
if (obj is System.Drawing.Bitmap bitmap)
bitmap.Save(stream, bitmap.RawFormat);
else
((System.Drawing.Icon)obj).Save(stream);
serializedData = stream.ToArray();
}
}
else
throw new ArgumentOutOfRangeException();

return new ResourceElement {
Name = resElem.Name,
ResourceData = new BinaryResourceData(new UserResourceType(typeName, ResourceTypeCode.UserTypes), SerializationUtilities.Serialize(obj), SerializationFormat.BinaryFormatter),
ResourceData = new BinaryResourceData(new UserResourceType(typeName, ResourceTypeCode.UserTypes), serializedData, format),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ You should have received a copy of the GNU General Public License
along with dnSpy. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using dnlib.DotNet.Resources;
using dnSpy.Contracts.Images;
Expand Down Expand Up @@ -91,16 +94,59 @@ public void Deserialize() {
if (!CanDeserialize)
return;

var serializedData = ((BinaryResourceData)ResourceElement.ResourceData).Data;
var formatter = new BinaryFormatter();
try {
var binaryResourceData = ((BinaryResourceData)ResourceElement.ResourceData);
var serializedData = binaryResourceData.Data;
if (binaryResourceData.Format == SerializationFormat.BinaryFormatter) {
var formatter = new BinaryFormatter();
try {
#pragma warning disable SYSLIB0011
deserializedData = formatter.Deserialize(new MemoryStream(serializedData));
deserializedData = formatter.Deserialize(new MemoryStream(serializedData));
#pragma warning restore SYSLIB0011
}
catch {
return;
}
}
catch {
return;
else if (binaryResourceData.Format == SerializationFormat.TypeConverterByteArray) {
try {
var type = Type.GetType(binaryResourceData.TypeName);
if (type is null)
return;
var converter = TypeDescriptor.GetConverter(type);
if (converter is null)
return;
deserializedData = converter.ConvertFrom(serializedData);
}
catch {
return;
}
}
else if (binaryResourceData.Format == SerializationFormat.TypeConverterString) {
try {
var type = Type.GetType(binaryResourceData.TypeName);
if (type is null)
return;
var converter = TypeDescriptor.GetConverter(type);
if (converter is null)
return;
deserializedData = converter.ConvertFromInvariantString(Encoding.UTF8.GetString(serializedData));
}
catch {
return;
}
}
else if (binaryResourceData.Format == SerializationFormat.ActivatorStream) {
try {
var type = Type.GetType(binaryResourceData.TypeName);
if (type is null)
return;
deserializedData = Activator.CreateInstance(type, new MemoryStream(serializedData));
}
catch {
return;
}
}

if (deserializedData is null)
return;

Expand Down

0 comments on commit efe9817

Please sign in to comment.