Skip to content

Commit dcf153d

Browse files
authored
Merge 40db1a3 into 17ac888
2 parents 17ac888 + 40db1a3 commit dcf153d

File tree

7 files changed

+104
-17
lines changed

7 files changed

+104
-17
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using dnlib.DotNet;
5+
6+
namespace Confuser.Core {
7+
internal sealed class ConfuserAssemblyResolver : IAssemblyResolver {
8+
AssemblyResolver InternalResolver { get; }
9+
10+
public bool EnableTypeDefCache {
11+
get => InternalResolver.EnableTypeDefCache;
12+
set => InternalResolver.EnableTypeDefCache = value;
13+
}
14+
15+
public ModuleContext DefaultModuleContext {
16+
get => InternalResolver.DefaultModuleContext;
17+
set => InternalResolver.DefaultModuleContext = value;
18+
}
19+
20+
public IList<string> PostSearchPaths => InternalResolver.PostSearchPaths;
21+
22+
internal ConfuserAssemblyResolver() =>
23+
InternalResolver = new AssemblyResolver();
24+
25+
/// <inheritdoc />
26+
public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule) {
27+
if (assembly is AssemblyDef assemblyDef)
28+
return assemblyDef;
29+
30+
var cachedAssemblyDef = InternalResolver
31+
.GetCachedAssemblies()
32+
.FirstOrDefault(a => AssemblyNameComparer.NameAndPublicKeyTokenOnly.Equals(a, assembly));
33+
if (!(cachedAssemblyDef is null))
34+
return cachedAssemblyDef;
35+
36+
AssemblyDef resolvedAssemblyDef = null;
37+
try {
38+
InternalResolver.FindExactMatch = true;
39+
resolvedAssemblyDef = InternalResolver.Resolve(assembly, sourceModule);
40+
}
41+
finally {
42+
InternalResolver.FindExactMatch = false;
43+
}
44+
45+
return resolvedAssemblyDef ?? InternalResolver.Resolve(assembly, sourceModule);
46+
}
47+
48+
public void Clear() => InternalResolver.Clear();
49+
50+
public IEnumerable<AssemblyDef> GetCachedAssemblies() => InternalResolver.GetCachedAssemblies();
51+
52+
public void AddToCache(ModuleDefMD modDef) => InternalResolver.AddToCache(modDef);
53+
}
54+
}

Confuser.Core/ConfuserContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public ServiceRegistry Registry {
4848
/// Gets the assembly resolver.
4949
/// </summary>
5050
/// <value>The assembly resolver.</value>
51-
public AssemblyResolver Resolver { get; internal set; }
51+
public IAssemblyResolver Resolver => InternalResolver;
52+
53+
/// <summary>
54+
/// Gets the assembly resolver.
55+
/// </summary>
56+
/// <value>The assembly resolver.</value>
57+
internal ConfuserAssemblyResolver InternalResolver { get; set; }
5258

5359
/// <summary>
5460
/// Gets the modules being protected.

Confuser.Core/ConfuserEngine.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ static void RunInternal(ConfuserParameters parameters, CancellationToken token)
9191
new SettingItem<Protection>(WatermarkingProtection._Id)
9292
});
9393

94-
var asmResolver = new AssemblyResolver();
95-
asmResolver.EnableTypeDefCache = true;
94+
var asmResolver = new ConfuserAssemblyResolver {EnableTypeDefCache = true};
9695
asmResolver.DefaultModuleContext = new ModuleContext(asmResolver);
97-
context.Resolver = asmResolver;
96+
context.InternalResolver = asmResolver;
9897
context.BaseDirectory = Path.Combine(Environment.CurrentDirectory, context.Project.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar);
9998
context.OutputDirectory = Path.Combine(context.Project.BaseDirectory, context.Project.OutputDirectory.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar);
10099
foreach (string probePath in context.Project.ProbePaths)
@@ -206,7 +205,7 @@ static void RunInternal(ConfuserParameters parameters, CancellationToken token)
206205
}
207206
finally {
208207
if (context.Resolver != null)
209-
context.Resolver.Clear();
208+
context.InternalResolver.Clear();
210209
context.Logger.Finish(ok);
211210
}
212211
}
@@ -265,7 +264,7 @@ static void Inspection(ConfuserContext context) {
265264
foreach (var dependency in context.Modules
266265
.SelectMany(module => module.GetAssemblyRefs().Select(asmRef => Tuple.Create(asmRef, module)))) {
267266
try {
268-
AssemblyDef assembly = context.Resolver.ResolveThrow(dependency.Item1, dependency.Item2);
267+
context.Resolver.ResolveThrow(dependency.Item1, dependency.Item2);
269268
}
270269
catch (AssemblyResolveException ex) {
271270
context.Logger.ErrorException("Failed to resolve dependency of '" + dependency.Item2.Name + "'.", ex);
@@ -434,7 +433,7 @@ static void Pack(ConfuserContext context) {
434433
}
435434

436435
static void SaveModules(ConfuserContext context) {
437-
context.Resolver.Clear();
436+
context.InternalResolver.Clear();
438437
for (int i = 0; i < context.OutputModules.Count; i++) {
439438
string path = Path.GetFullPath(Path.Combine(context.OutputDirectory, context.OutputPaths[i]));
440439
string dir = Path.GetDirectoryName(path);
@@ -529,7 +528,7 @@ static void PrintEnvironmentInfo(ConfuserContext context) {
529528

530529
if (context.Resolver != null) {
531530
context.Logger.Error("Cached assemblies:");
532-
foreach (AssemblyDef asm in context.Resolver.GetCachedAssemblies()) {
531+
foreach (AssemblyDef asm in context.InternalResolver.GetCachedAssemblies()) {
533532
if (string.IsNullOrEmpty(asm.ManifestModule.Location))
534533
context.Logger.ErrorFormat(" {0}", asm.FullName);
535534
else

Confuser.Core/Marker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ protected internal virtual MarkerResult MarkProject(ConfuserProject proj, Confus
152152
continue;
153153
}
154154

155-
ModuleDefMD modDef = module.Resolve(proj.BaseDirectory, context.Resolver.DefaultModuleContext);
155+
ModuleDefMD modDef = module.Resolve(proj.BaseDirectory, context.InternalResolver.DefaultModuleContext);
156156
context.CheckCancellation();
157157

158158
if (proj.Debug)
159159
modDef.LoadPdb();
160160

161-
context.Resolver.AddToCache(modDef);
161+
context.InternalResolver.AddToCache(modDef);
162162
modules.Add(Tuple.Create(module, modDef));
163163
}
164164

Confuser.Core/ObfAttrMarker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ protected internal override MarkerResult MarkProject(ConfuserProject proj, Confu
313313
continue;
314314
}
315315

316-
ModuleDefMD modDef = module.Resolve(proj.BaseDirectory, context.Resolver.DefaultModuleContext);
316+
ModuleDefMD modDef = module.Resolve(proj.BaseDirectory, context.InternalResolver.DefaultModuleContext);
317317
context.CheckCancellation();
318318

319-
context.Resolver.AddToCache(modDef);
319+
context.InternalResolver.AddToCache(modDef);
320320
modules.Add(Tuple.Create(module, modDef));
321321
}
322322
foreach (var module in modules) {

Tests/Confuser.UnitTest/TestBase.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace Confuser.UnitTest {
1111
public abstract class TestBase {
12+
private const string _externalPrefix = "external:";
13+
1214
readonly ITestOutputHelper outputHelper;
1315

1416
protected TestBase(ITestOutputHelper outputHelper) =>
@@ -30,16 +32,21 @@ protected async Task Run(string[] inputFileNames, string[] expectedOutput, Setti
3032
if (Directory.Exists(outputDir)) {
3133
Directory.Delete(outputDir, true);
3234
}
33-
string entryInputFileName = Path.Combine(baseDir, inputFileNames[0]);
34-
var entryOutputFileName = Path.Combine(outputDir, inputFileNames[0]);
35+
36+
string firstFileName = GetFileName(inputFileNames[0]);
37+
string entryInputFileName = Path.Combine(baseDir, firstFileName);
38+
var entryOutputFileName = Path.Combine(outputDir, firstFileName);
3539
var proj = new ConfuserProject {
3640
BaseDirectory = baseDir,
3741
OutputDirectory = outputDir,
3842
Packer = packer
3943
};
4044

4145
foreach (string name in inputFileNames) {
42-
var projectModule = new ProjectModule { Path = Path.Combine(baseDir, name) };
46+
var projectModule = new ProjectModule {
47+
Path = Path.Combine(baseDir, GetFileName(name)),
48+
IsExternal = IsExternal(name)
49+
};
4350
projectModuleAction?.Invoke(projectModule);
4451
proj.Add(projectModule);
4552
}
@@ -56,7 +63,7 @@ protected async Task Run(string[] inputFileNames, string[] expectedOutput, Setti
5663
await ConfuserEngine.Run(parameters);
5764

5865
for (var index = 0; index < inputFileNames.Length; index++) {
59-
string name = inputFileNames[index];
66+
string name = GetFileName(inputFileNames[index]);
6067
string outputName = Path.Combine(outputDir, name);
6168

6269
bool exists;
@@ -72,9 +79,14 @@ protected async Task Run(string[] inputFileNames, string[] expectedOutput, Setti
7279
// Check if output assemblies is obfuscated
7380
Assert.NotEqual(FileUtilities.ComputeFileChecksum(Path.Combine(baseDir, name)),
7481
FileUtilities.ComputeFileChecksum(outputName));
82+
} else if (IsExternal(inputFileNames[index])) {
83+
File.Copy(
84+
Path.Combine(baseDir, GetFileName(inputFileNames[index])),
85+
Path.Combine(outputDir, GetFileName(inputFileNames[index])));
7586
}
7687
}
7788

89+
7890
if (Path.GetExtension(entryInputFileName) == ".exe") {
7991
var info = new ProcessStartInfo(entryOutputFileName) { RedirectStandardOutput = true, UseShellExecute = false };
8092
using (var process = Process.Start(info)) {
@@ -92,5 +104,13 @@ protected async Task Run(string[] inputFileNames, string[] expectedOutput, Setti
92104
}
93105
}
94106
}
107+
108+
private static string GetFileName(string name) {
109+
if (IsExternal(name))
110+
return name.Substring(_externalPrefix.Length);
111+
return name;
112+
}
113+
114+
private static bool IsExternal(string name) => name.StartsWith(_externalPrefix, StringComparison.OrdinalIgnoreCase);
95115
}
96116
}

Tests/IncorrectRedirectToGac.Test/IncorrectRedirectToGacTest.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ namespace IncorrectRedirectToGac.Test {
77
public class IncorrectRedirectToGacTest : TestBase {
88
public IncorrectRedirectToGacTest(ITestOutputHelper outputHelper) : base(outputHelper) { }
99

10-
[Fact (Skip = "TODO: https://github.com/mkaring/ConfuserEx/issues/144")]
10+
[Fact]
1111
[Trait("Category", "core")]
1212
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/144")]
1313
public async Task IncorrectRedirectToGac() =>
1414
await Run(
1515
new [] { "IncorrectRedirectToGac.exe", "Microsoft.Build.Framework.dll" }, new string[0], null
1616
);
17+
18+
[Fact]
19+
[Trait("Category", "core")]
20+
[Trait("Issue", "https://github.com/mkaring/ConfuserEx/issues/144")]
21+
public async Task IncorrectExternalRedirectToGac() =>
22+
await Run(
23+
new [] { "IncorrectRedirectToGac.exe", "external:Microsoft.Build.Framework.dll" }, new string[0], null, outputDirSuffix: "_external"
24+
);
1725
}
1826
}

0 commit comments

Comments
 (0)