Skip to content

Commit

Permalink
Merge pull request #561 from Washi1337/feature/reduce-redundant-abstr…
Browse files Browse the repository at this point in the history
…actions

Reduce redundant abstractions
  • Loading branch information
Washi1337 authored Jun 4, 2024
2 parents 36eeac9 + 79b7a01 commit 9ecc97a
Show file tree
Hide file tree
Showing 99 changed files with 792 additions and 1,467 deletions.
2 changes: 1 addition & 1 deletion docs/guides/dotnet/advanced-module-reading.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public class CustomFieldRvaDataReader : FieldRvaDataReader
public override ISegment ResolveFieldData(
IErrorListener listener,
Platform platform,
IDotNetDirectory directory,
DotNetDirectory directory,
in FieldRvaRow fieldRvaRow)
{
// ...
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/dotnet/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var module = ModuleDefinition.FromReader(reader);
```

``` csharp
IPEImage peImage = ...
PEImage peImage = ...
var module = ModuleDefinition.FromImage(peImage);
```

Expand Down Expand Up @@ -131,7 +131,7 @@ var assembly = AssemblyDefinition.FromFile(@"C:\myfile.exe");
```

``` csharp
IPEFile peFile = ...
PEFile peFile = ...
var assembly = AssemblyDefinition.FromFile(peFile);
```

Expand All @@ -141,7 +141,7 @@ var assembly = AssemblyDefinition.FromReader(reader);
```

``` csharp
IPEImage peImage = ...
PEImage peImage = ...
var assembly = AssemblyDefinition.FromImage(peImage);
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/dotnet/bundles.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ manifest.WriteUsingTemplate(
> [!NOTE]
> `BundleManifest` and `BundlerParameters` also define overloads of the
> `WriteUsingTemplate` and `FromTemplate` / `FromExistingBundle`
> respectively, taking `byte[]`, `IDataSource` or `IPEImage` instances
> respectively, taking `byte[]`, `IDataSource` or `PEImage` instances
> instead of file paths.

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/dotnet/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ In short, this means the following:

The third layer of abstraction is the highest level of abstraction for a
.NET assembly that AsmResolver provides. All objects exposed by this
layer are completely mutable and can be serialized back to a `IPEImage`
layer are completely mutable and can be serialized back to a `PEImage`
from the second layer, to a `PEFile` from the first layer, or to the
disk.
2 changes: 1 addition & 1 deletion docs/guides/peimage/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var peImage = PEImage.FromFile(@"C:\myfile.exe");
```

``` csharp
IPEFile peFile = ...
PEFile peFile = ...
var peImage = PEImage.FromFile(peFile);
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/peimage/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using AsmResolver.PE.Debug;

## The Debug Data Entries

The `IPEImage` exposes all debug information through the `DebugData`
The `PEImage` class exposes all debug information through the `DebugData`
property. This is a list of `DebugDataEntry`, providing access to the
type of debug data, as well as the version and raw contents of the data
that is stored.
Expand Down
18 changes: 9 additions & 9 deletions docs/guides/peimage/dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ applications.
## .NET directory / CLR 2.0 header

The .NET data directory can be accessed by the
`IPEImage.DotNetDirectory` property.
`PEImage.DotNetDirectory` property.

``` csharp
IPEImage peImage = ...
PEImage peImage = ...

Console.WriteLine($"Managed entry point: {peImage.DotNetDirectory.EntryPoint}");
```
Expand All @@ -26,9 +26,9 @@ that is referenced by the .NET directory. It contains the metadata
streams, such as the table and the blob stream, which play a key role in
the execution of a .NET binary.

To access the metadata directory, access the `IDotNetDirectory.Metadata`
property, which will provide you an instance of the `IMetadata`
interface:
To access the metadata directory, access the `DotNetDirectory.Metadata`
property, which will provide you an instance of the `MetadataDirectory`
class:

``` csharp
var metadata = peImage.DotNetDirectory.Metadata;
Expand All @@ -39,7 +39,7 @@ Console.WriteLine($"Target .NET runtime version: {metadata.VersionString}");

## Metadata streams

The `IMetadata` interface also exposes the `Streams` property, a list of
The `MetadataDirectory` class also exposes the `Streams` property, a list of
`IMetadataStream` instances.

``` csharp
Expand Down Expand Up @@ -330,14 +330,14 @@ only uses one native symbol (either `mscoree.dll!_CorExeMain` or
`mscoree.dll!_CorDllMain` ). AsmResolver includes a built-in implementation
for this that is based on [the reference implementation provided by GData](https://www.gdatasoftware.com/blog/2020/06/36164-introducing-the-typerefhash-trh).
The hash can be obtained using the `GetTypeReferenceHash` extension method on
`IPEImage`or on `IMetadata`:
`PEImage`or on `MetadataDirectory`:

``` csharp
IPEImage image = ...
PEImage image = ...
byte[] hash = image.GetTypeReferenceHash();
```

``` csharp
IMetadata metadata = ...
MetadataDirectory metadata = ...
byte[] hash = metadata.GetTypeReferenceHash();
```
2 changes: 1 addition & 1 deletion docs/guides/peimage/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using AsmResolver.PE.Exceptions;

## Runtime Functions

The `IPEImage` interface exposes these tables through the `Exceptions`
The `PEImage` class exposes these tables through the `Exceptions`
property. This is of type `IExceptionsDirectory`, which allows for read
access to instances of `IRuntimeFunction` through the `GetEntries`
method. This interface models all the runtime functions through the
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/peimage/exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Dynamically linked libraries (DLLs) often expose symbols through
defining exports in the exports directory.

The `IPEImage` interface exposes the `Exports` property, exposing a
The `PEImage` class exposes the `Exports` property, exposing a
mutable instance of `ExportDirectory`, which defines the following properties:

- `Name`: The name of the dynamically linked library.
Expand Down Expand Up @@ -32,7 +32,7 @@ defines:
## Example

Below is an example of a program that lists all symbols exported by a
given `IPEImage` instance:
given `PEImage` instance:

``` csharp
foreach (var symbol in peImage.Exports.Entries)
Expand Down
10 changes: 5 additions & 5 deletions docs/guides/peimage/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ using AsmResolver.PE.Imports;

## Imported Modules and Symbols

The `IPEImage` interface exposes the `Imports` property, which contains
The `PEImage` class exposes the `Imports` property, which contains
all members that are resolved at runtime, grouped by the defining
module. Below an example of a program that lists all members imported by
a given `IPEImage` instance:
a given `PEImage` instance:

``` csharp
foreach (var module in peImage.Imports)
Expand Down Expand Up @@ -48,10 +48,10 @@ set of dependencies.

AsmResolver provides a built-in implementation for calculating the
Import Hash. The hash can be obtained by using the `GetImportHash`
extension method on `IPEImage`:
extension method on `PEImage`:

``` csharp
IPEImage image = ...
PEImage image = ...
byte[] hash = image.GetImportHash();
```

Expand All @@ -70,7 +70,7 @@ public class MySymbolResolver : ISymbolResolver
}
}

IPEImage image = ...
PEImage image = ...
byte[] hash = image.GetImportHash(new MySymbolResolver());
```

Expand Down
8 changes: 4 additions & 4 deletions docs/guides/peimage/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Overview

The PE image layer is the second layer of abstraction of the portable
executable (PE) file format. It is mostly represented by `IPEImage` and
its derivatives (such as `PEImage`), and works on top of the `PEFile`
layer. Its main purpose is to provide access to mutable models that are
easier to use than the raw data structures the PE file layer exposes.
executable (PE) file format. It is mostly represented by `PEImage`, and
works on top of the `PEFile` layer. Its main purpose is to provide access
to mutable models that are easier to use than the raw data structures the
PE file layer exposes.
2 changes: 1 addition & 1 deletion docs/guides/peimage/ready-to-run.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To test whether a PE image has ReadyToRun metadata, query the
```csharp
using AsmResolver.PE.DotNet.ReadyToRun;

IPEImage image = ...
PEImage image = ...

var header = image.DotNetDirectory.ManagedNativeHeader;
if (header is ReadyToRunDirectory directory)
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/peimage/tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ base relocation directory of the PE image:
``` csharp
using AsmResolver.PE.Relocations;

IPEImage image = ...;
PEImage image = ...;

foreach (var relocation in tlsDirectory.GetRequiredBaseRelocations())
image.Relocations.Add(relocation);
Expand Down
16 changes: 8 additions & 8 deletions docs/guides/peimage/win32resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using AsmResolver.PE.Win32Resources;

## Directories

Resources are exposed by the `IPEImage.Resources` property, which
Resources are exposed by the `PEImage::Resources` property, which
represents the root directory of all resources stored in the image.
Every directory (including the root directory) is represented by
instances of `IResourceDirectory`. This type contains the `Entries`
Expand All @@ -19,7 +19,7 @@ containing more entries, or a data entry (an instance of
`IResourceData`) with the raw contents of the resource.

``` csharp
IPEImage image = ...
PEImage image = ...
IResourceDirectory root = image.Resources;

foreach (var entry in root.Entries)
Expand All @@ -35,7 +35,7 @@ Alternatively, you can access specific resources very easily by using
the `GetDirectory` and `GetData`:

``` csharp
IPEImage image = ...
PEImage image = ...
IResourceData stringDataEntry = image.Resources
.GetDirectory(ResourceType.String) // Get string tables directory.
.GetDirectory(251) // Get string block with ID 251
Expand All @@ -49,13 +49,13 @@ same ID is replaced with the new one, and that the sorting requirements
according to the PE file specification are presrved.

``` csharp
IPEImage image = ...
PEImage image = ...
var newDirectory = new ResourceDirectory(ResourceType.String);
image.Resources.Entries.Add(newDirectory);
```

``` csharp
IPEImage image = ...
PEImage image = ...
var newDirectory = new ResourceDirectory(ResourceType.String);
image.Resources.InsertOrReplaceEntry(newDirectory);
```
Expand All @@ -64,7 +64,7 @@ Similarly, removing can be done by modifying the `Entries` directory, or
by using the `RemoveEntry` method:

``` csharp
IPEImage image = ...
PEImage image = ...
image.Resources.RemoveEntry(ResourceType.String);
```

Expand All @@ -77,7 +77,7 @@ can check if this is a `IReadableSegment`, or use the shortcuts
the entry.

``` csharp
IPEImage image = ...
PEImage image = ...
IResourceData dataEntry = image.Resources
.GetDirectory(ResourceType.String) // Get string tables directory.
.GetDirectory(251) // Get string block with ID 251
Expand All @@ -94,7 +94,7 @@ Adding new data entries can be done by using the `ResourceData`
constructor:

``` csharp
IPEImage image = ...
PEImage image = ...

var data = new ResourceData(id: 1033, contents: new DataSegment(new byte[] { ... }));
image.Resources
Expand Down
4 changes: 2 additions & 2 deletions src/AsmResolver.DotNet/AssemblyDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static AssemblyDefinition FromReader(in BinaryStreamReader reader, PEMapp
/// <param name="peImage">The image containing the .NET metadata.</param>
/// <returns>The module.</returns>
/// <exception cref="BadImageFormatException">Occurs when the image does not contain a valid .NET metadata directory.</exception>
public static AssemblyDefinition FromImage(IPEImage peImage) =>
public static AssemblyDefinition FromImage(PEImage peImage) =>
FromImage(peImage, new ModuleReaderParameters(Path.GetDirectoryName(peImage.FilePath)));

/// <summary>
Expand All @@ -123,7 +123,7 @@ public static AssemblyDefinition FromImage(IPEImage peImage) =>
/// <param name="readerParameters">The parameters to use while reading the assembly.</param>
/// <returns>The module.</returns>
/// <exception cref="BadImageFormatException">Occurs when the image does not contain a valid .NET metadata directory.</exception>
public static AssemblyDefinition FromImage(IPEImage peImage, ModuleReaderParameters readerParameters) =>
public static AssemblyDefinition FromImage(PEImage peImage, ModuleReaderParameters readerParameters) =>
ModuleDefinition.FromImage(peImage, readerParameters).Assembly
?? throw new BadImageFormatException("The provided PE image does not contain an assembly manifest.");

Expand Down
6 changes: 3 additions & 3 deletions src/AsmResolver.DotNet/Builder/DotNetDirectoryBuildResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace AsmResolver.DotNet.Builder
{
/// <summary>
/// Describes the result of a construction of a <see cref="IDotNetDirectory"/>. from a <see cref="ModuleDefinition"/>.
/// Describes the result of a construction of a <see cref="DotNetDirectory"/>. from a <see cref="ModuleDefinition"/>.
/// </summary>
public class DotNetDirectoryBuildResult
{
Expand All @@ -13,7 +13,7 @@ public class DotNetDirectoryBuildResult
/// </summary>
/// <param name="directory">The constructed directory.</param>
/// <param name="mapping">An object defining a mapping between members and their new metadata tokens.</param>
public DotNetDirectoryBuildResult(IDotNetDirectory directory, ITokenMapping mapping)
public DotNetDirectoryBuildResult(DotNetDirectory directory, ITokenMapping mapping)
{
Directory = directory ?? throw new ArgumentNullException(nameof(directory));
TokenMapping = mapping ?? throw new ArgumentNullException(nameof(mapping));
Expand All @@ -22,7 +22,7 @@ public DotNetDirectoryBuildResult(IDotNetDirectory directory, ITokenMapping mapp
/// <summary>
/// Gets the constructed .NET data directory.
/// </summary>
public IDotNetDirectory Directory
public DotNetDirectory Directory
{
get;
}
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Builder/DotNetDirectoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private static void ImportTables<TMember>(ModuleDefinition module, TableIndex ta
importAction((TMember) member);
}

private void ReorderMetadataStreams(SerializedModuleDefinition serializedModule, IMetadata newMetadata)
private void ReorderMetadataStreams(SerializedModuleDefinition serializedModule, MetadataDirectory newMetadata)
{
IMetadataStream? GetStreamOrNull<TStream>()
where TStream : class, IMetadataStream
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Builder/IDotNetDirectoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace AsmResolver.DotNet.Builder
{
/// <summary>
/// Provides members for constructing a .NET data directory that can be inserted into a <see cref="IPEImage"/>.
/// Provides members for constructing a .NET data directory that can be inserted into a <see cref="PEImage"/>.
/// </summary>
public interface IDotNetDirectoryFactory
{
Expand Down
2 changes: 1 addition & 1 deletion src/AsmResolver.DotNet/Builder/Metadata/IMetadataBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ TablesStreamBuffer TablesStream
/// Flushes all metadata stream buffers and builds up a new metadata directory.
/// </summary>
/// <returns>The constructed metadata directory.</returns>
IMetadata CreateMetadata();
MetadataDirectory CreateMetadata();
}
}
8 changes: 4 additions & 4 deletions src/AsmResolver.DotNet/Builder/Metadata/MetadataBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public bool OptimizeStringIndices
} = true;

/// <inheritdoc />
public IMetadata CreateMetadata()
public MetadataDirectory CreateMetadata()
{
// Create metadata directory.
var result = new PE.DotNet.Metadata.Metadata
var result = new MetadataDirectory
{
VersionString = _versionString,
IsEncMetadata = TablesStream.IsEncMetadata
Expand All @@ -101,15 +101,15 @@ public IMetadata CreateMetadata()
return result;
}

private static TStream? AddIfNotEmpty<TStream>(IMetadata metadata, IMetadataStreamBuffer streamBuffer)
private static TStream? AddIfNotEmpty<TStream>(MetadataDirectory metadata, IMetadataStreamBuffer streamBuffer)
where TStream : class, IMetadataStream
{
return !streamBuffer.IsEmpty
? Add<TStream>(metadata, streamBuffer)
: null;
}

private static TStream Add<TStream>(IMetadata metadata, IMetadataStreamBuffer streamBuffer)
private static TStream Add<TStream>(MetadataDirectory metadata, IMetadataStreamBuffer streamBuffer)
where TStream : class, IMetadataStream
{
var stream = streamBuffer.CreateStream();
Expand Down
Loading

0 comments on commit 9ecc97a

Please sign in to comment.