/* 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; namespace dnSpy.Debugger.DotNet.Metadata { /// /// Base class of all classes that contain metadata /// public abstract class DmdLazyMetadataBytes { } /// /// Metadata in a array /// public sealed class DmdLazyMetadataBytesArray : DmdLazyMetadataBytes { /// /// Gets the raw PE file bytes /// public byte[] Bytes { get; } /// /// true if file layout, false if memory layout /// public bool IsFileLayout { get; } /// /// Constructor /// /// Raw PE file bytes /// true if file layout, false if memory layout public DmdLazyMetadataBytesArray(byte[] bytes, bool isFileLayout) { Bytes = bytes ?? throw new ArgumentNullException(nameof(bytes)); IsFileLayout = isFileLayout; } } /// /// Metadata in a file /// public sealed class DmdLazyMetadataBytesFile : DmdLazyMetadataBytes { /// /// Gets the filename /// public string Filename { get; } /// /// true if file layout, false if memory layout /// public bool IsFileLayout { get; } /// /// Constructor /// /// Filename /// true if file layout, false if memory layout public DmdLazyMetadataBytesFile(string filename, bool isFileLayout = true) { Filename = filename ?? throw new ArgumentNullException(nameof(filename)); IsFileLayout = isFileLayout; } } /// /// Metadata in memory /// public sealed class DmdLazyMetadataBytesPtr : DmdLazyMetadataBytes { /// /// Gets the address of the PE file /// public IntPtr Address { get; } /// /// Gets the size of the PE file /// public uint Size { get; } /// /// true if file layout, false if memory layout /// public bool IsFileLayout { get; } /// /// Constructor /// /// Address of the PE file /// Size of the PE file /// true if file layout, false if memory layout public DmdLazyMetadataBytesPtr(IntPtr address, uint size, bool isFileLayout) { if (address == IntPtr.Zero) throw new ArgumentNullException(nameof(address)); if (size == 0) throw new ArgumentOutOfRangeException(nameof(size)); Address = address; Size = size; IsFileLayout = isFileLayout; } } /// /// COM IMetaDataImport metadata /// public sealed class DmdLazyMetadataBytesCom : DmdLazyMetadataBytes { /// /// Gets the COM IMetaDataImport instance /// public object ComMetadata => MetaDataImport; internal Impl.COMD.IMetaDataImport2 MetaDataImport { get; } /// /// Gets the dispatcher to use when accessing /// public DmdDispatcher Dispatcher { get; } /// /// Gets the helper class /// public DmdDynamicModuleHelper DynamicModuleHelper { get; } /// /// Constructor /// /// COM IMetaDataImport instance /// Helper class /// Dispatcher to use when accessing public DmdLazyMetadataBytesCom(object comMetadata, DmdDynamicModuleHelper dynamicModuleHelper, DmdDispatcher dispatcher) { MetaDataImport = comMetadata as Impl.COMD.IMetaDataImport2 ?? throw new ArgumentException("Only " + nameof(Impl.COMD.IMetaDataImport2) + " is supported"); DynamicModuleHelper = dynamicModuleHelper ?? throw new ArgumentNullException(nameof(dynamicModuleHelper)); Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher)); } internal DmdLazyMetadataBytesCom(Impl.COMD.IMetaDataImport2 metaDataImport, DmdDynamicModuleHelper dynamicModuleHelper, DmdDispatcher dispatcher) { MetaDataImport = metaDataImport ?? throw new ArgumentNullException(nameof(metaDataImport)); DynamicModuleHelper = dynamicModuleHelper ?? throw new ArgumentNullException(nameof(dynamicModuleHelper)); Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher)); } } }