/
ResourceDescription.cs
158 lines (138 loc) · 5.85 KB
/
ResourceDescription.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.IO;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Emit;
using System.Reflection;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Security.Cryptography;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Representation of a resource whose contents are to be embedded in the output assembly.
/// </summary>
public sealed class ResourceDescription : Cci.IFileReference
{
internal readonly string ResourceName;
internal readonly string? FileName; // null if embedded
internal readonly bool IsPublic;
internal readonly Func<Stream> DataProvider;
private readonly CryptographicHashProvider _hashes;
/// <summary>
/// Creates a representation of a resource whose contents are to be embedded in the output assembly.
/// </summary>
/// <param name="resourceName">Resource name.</param>
/// <param name="dataProvider">The callers will dispose the result after use.
/// This allows the resources to be opened and read one at a time.
/// </param>
/// <param name="isPublic">True if the resource is public.</param>
/// <remarks>
/// Returns a stream of the data to embed.
/// </remarks>
public ResourceDescription(string resourceName, Func<Stream> dataProvider, bool isPublic)
: this(resourceName, fileName: null, dataProvider, isPublic, isEmbedded: true, checkArgs: true)
{
}
/// <summary>
/// Creates a representation of a resource whose file name will be recorded in the assembly.
/// </summary>
/// <param name="resourceName">Resource name.</param>
/// <param name="fileName">File name with an extension to be stored in metadata.</param>
/// <param name="dataProvider">The callers will dispose the result after use.
/// This allows the resources to be opened and read one at a time.
/// </param>
/// <param name="isPublic">True if the resource is public.</param>
/// <remarks>
/// Function returning a stream of the resource content (used to calculate hash).
/// </remarks>
public ResourceDescription(string resourceName, string? fileName, Func<Stream> dataProvider, bool isPublic)
: this(resourceName, fileName, dataProvider, isPublic, isEmbedded: false, checkArgs: true)
{
}
internal ResourceDescription(string resourceName, string? fileName, Func<Stream> dataProvider, bool isPublic, bool isEmbedded, bool checkArgs)
{
if (checkArgs)
{
if (dataProvider == null)
{
throw new ArgumentNullException(nameof(dataProvider));
}
if (resourceName == null)
{
throw new ArgumentNullException(nameof(resourceName));
}
if (!MetadataHelpers.IsValidMetadataIdentifier(resourceName))
{
throw new ArgumentException(CodeAnalysisResources.EmptyOrInvalidResourceName, nameof(resourceName));
}
if (!isEmbedded)
{
if (fileName == null)
{
throw new ArgumentNullException(nameof(fileName));
}
if (!MetadataHelpers.IsValidMetadataFileName(fileName))
{
throw new ArgumentException(CodeAnalysisResources.EmptyOrInvalidFileName, nameof(fileName));
}
}
}
this.ResourceName = resourceName;
this.DataProvider = dataProvider;
this.FileName = isEmbedded ? null : fileName;
this.IsPublic = isPublic;
_hashes = new ResourceHashProvider(this);
}
private sealed class ResourceHashProvider : CryptographicHashProvider
{
private readonly ResourceDescription _resource;
public ResourceHashProvider(ResourceDescription resource)
{
RoslynDebug.Assert(resource != null);
_resource = resource;
}
internal override ImmutableArray<byte> ComputeHash(HashAlgorithm algorithm)
{
try
{
using (var stream = _resource.DataProvider())
{
if (stream == null)
{
throw new InvalidOperationException(CodeAnalysisResources.ResourceDataProviderShouldReturnNonNullStream);
}
return ImmutableArray.CreateRange(algorithm.ComputeHash(stream));
}
}
catch (Exception ex)
{
throw new ResourceException(_resource.FileName, ex);
}
}
}
internal bool IsEmbedded
{
get { return FileName == null; }
}
internal Cci.ManagedResource ToManagedResource(CommonPEModuleBuilder moduleBeingBuilt)
{
return new Cci.ManagedResource(ResourceName, IsPublic, IsEmbedded ? DataProvider : null, IsEmbedded ? null : this, offset: 0);
}
ImmutableArray<byte> Cci.IFileReference.GetHashValue(AssemblyHashAlgorithm algorithmId)
{
return _hashes.GetHash(algorithmId);
}
string? Cci.IFileReference.FileName
{
get { return FileName; }
}
bool Cci.IFileReference.HasMetadata
{
get { return false; }
}
}
}