// dnlib: See LICENSE.txt for more info
using System;
using System.IO;
using System.Runtime.Serialization;
using dnlib.IO;
namespace dnlib.DotNet.Resources {
///
/// Base class of all user data
///
public abstract class UserResourceData : IResourceData {
readonly UserResourceType type;
///
/// Full name including assembly of type
///
public string TypeName => type.Name;
///
/// User type code
///
public ResourceTypeCode Code => type.Code;
///
public FileOffset StartOffset { get; set; }
///
public FileOffset EndOffset { get; set; }
///
/// Constructor
///
/// User resource type
public UserResourceData(UserResourceType type) => this.type = type;
///
public abstract void WriteData(ResourceBinaryWriter writer, IFormatter formatter);
}
///
/// Binary data
///
public sealed class BinaryResourceData : UserResourceData {
byte[] data;
SerializationFormat format;
///
/// Gets the raw data
///
public byte[] Data => data;
///
/// Gets the serialization format of
///
public SerializationFormat Format => format;
///
/// Constructor
///
/// User resource type
/// Raw serialized data
///
public BinaryResourceData(UserResourceType type, byte[] data, SerializationFormat format)
: base(type) {
this.data = data;
this.format = format;
}
///
public override void WriteData(ResourceBinaryWriter writer, IFormatter formatter) {
if (writer.ReaderType == ResourceReaderType.ResourceReader && format != SerializationFormat.BinaryFormatter)
throw new NotSupportedException($"Unsupported serialization format: {format} for {writer.ReaderType}");
if (writer.ReaderType == ResourceReaderType.DeserializingResourceReader) {
writer.Write7BitEncodedInt((int)format);
writer.Write7BitEncodedInt(data.Length);
}
writer.Write(data);
}
///
public override string ToString() => $"Binary: Length: {data.Length} Format: {format}";
}
///
/// Specifies how the data in should be deserialized.
///
public enum SerializationFormat {
///
/// The data can be deserialized using .
///
BinaryFormatter = 1,
///
/// The data can be deserialized by passing in the raw data into
/// the method.
///
TypeConverterByteArray = 2,
///
/// The data can be deserialized by passing the UTF-8 string obtained from the raw data into
/// the method.
///
TypeConverterString = 3,
///
/// The data can be deserialized by creating a new instance of the type using a
/// constructor with a single parameter and passing in
/// a of the raw data into it.
///
ActivatorStream = 4,
}
}