diff --git a/dnlib.sln b/dnlib.sln index cc58351c..f9980913 100644 --- a/dnlib.sln +++ b/dnlib.sln @@ -1,31 +1,70 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29318.209 -MinimumVisualStudioVersion = 16.0.29318.209 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dnlib", "src\dnlib.csproj", "{FDFC1237-143F-4919-8318-4926901F4639}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples", "Examples\Examples.csproj", "{F27E72B5-C4BD-40BF-AD19-4C8A99B55872}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {FDFC1237-143F-4919-8318-4926901F4639}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FDFC1237-143F-4919-8318-4926901F4639}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FDFC1237-143F-4919-8318-4926901F4639}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FDFC1237-143F-4919-8318-4926901F4639}.Release|Any CPU.Build.0 = Release|Any CPU - {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {49902A67-E85B-44E8-9C4F-26F8854AA894} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29318.209 +MinimumVisualStudioVersion = 16.0.29318.209 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dnlib", "src\dnlib.csproj", "{FDFC1237-143F-4919-8318-4926901F4639}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Examples", "Examples\Examples.csproj", "{F27E72B5-C4BD-40BF-AD19-4C8A99B55872}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dnlib.Tests", "tests\dnlib.Tests\dnlib.Tests.csproj", "{B57399F8-DA15-4876-A3C2-E2B66153D77C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FDFC1237-143F-4919-8318-4926901F4639}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Debug|x64.ActiveCfg = Debug|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Debug|x64.Build.0 = Debug|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Debug|x86.ActiveCfg = Debug|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Debug|x86.Build.0 = Debug|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Release|Any CPU.Build.0 = Release|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Release|x64.ActiveCfg = Release|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Release|x64.Build.0 = Release|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Release|x86.ActiveCfg = Release|Any CPU + {FDFC1237-143F-4919-8318-4926901F4639}.Release|x86.Build.0 = Release|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|x64.ActiveCfg = Debug|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|x64.Build.0 = Debug|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|x86.ActiveCfg = Debug|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Debug|x86.Build.0 = Debug|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|Any CPU.Build.0 = Release|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|x64.ActiveCfg = Release|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|x64.Build.0 = Release|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|x86.ActiveCfg = Release|Any CPU + {F27E72B5-C4BD-40BF-AD19-4C8A99B55872}.Release|x86.Build.0 = Release|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Debug|x64.ActiveCfg = Debug|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Debug|x64.Build.0 = Debug|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Debug|x86.ActiveCfg = Debug|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Debug|x86.Build.0 = Debug|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Release|Any CPU.Build.0 = Release|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Release|x64.ActiveCfg = Release|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Release|x64.Build.0 = Release|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Release|x86.ActiveCfg = Release|Any CPU + {B57399F8-DA15-4876-A3C2-E2B66153D77C}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {B57399F8-DA15-4876-A3C2-E2B66153D77C} = {0AB3BF05-4346-4AA6-1389-037BE0695223} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {49902A67-E85B-44E8-9C4F-26F8854AA894} + EndGlobalSection +EndGlobal diff --git a/src/DotNet/AssemblyResolver.cs b/src/DotNet/AssemblyResolver.cs index 791399d1..ae3b054e 100644 --- a/src/DotNet/AssemblyResolver.cs +++ b/src/DotNet/AssemblyResolver.cs @@ -701,6 +701,9 @@ IEnumerable GetDirs(string baseDir) { IEnumerable FindAssembliesModuleSearchPaths(IAssembly assembly, ModuleDef sourceModule, bool matchExactly) { string asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); var searchPaths = GetSearchPaths(sourceModule); +#if NETCOREAPP + searchPaths = searchPaths.Union(new string[] { System.IO.Path.GetDirectoryName(typeof(System.Math).Assembly.Location) }); +#endif var exts = assembly.IsContentTypeWindowsRuntime ? winMDAssemblyExtensions : assemblyExtensions; foreach (var ext in exts) { foreach (var path in searchPaths) { diff --git a/src/DotNet/FrameworkRedirect.cs b/src/DotNet/FrameworkRedirect.cs index 7208342f..43b2d22a 100644 --- a/src/DotNet/FrameworkRedirect.cs +++ b/src/DotNet/FrameworkRedirect.cs @@ -380,6 +380,9 @@ static bool TryApplyFrameworkRedirect(IAssembly assembly, Dictionary 0) + return false; redirectedAssembly = new AssemblyNameInfo(assembly); redirectedAssembly.Version = redirect.redirectVersion; } diff --git a/tests/dnlib.Tests/AssemblyResolutionTests.cs b/tests/dnlib.Tests/AssemblyResolutionTests.cs new file mode 100644 index 00000000..fdf74a77 --- /dev/null +++ b/tests/dnlib.Tests/AssemblyResolutionTests.cs @@ -0,0 +1,41 @@ +using dnlib.DotNet; +using dnlib.DotNet.Emit; + +namespace dnlib.Tests; + +[TestClass] +public sealed class AssemblyResolutionTests +{ + private static Stream? GetManifestStreamSource() => + typeof(AssemblyResolutionTests).Assembly.GetManifestResourceStream(typeof(AssemblyResolutionTests), "Test_Resource.txt"); + + [TestMethod] + public void ResolveSystemRuntimeMethods() + { + var moduleDef = LoadTestModuleDef(); + var thisTypeDef = moduleDef.Find(typeof(AssemblyResolutionTests).FullName, false); + var refMethod = thisTypeDef.FindMethod(nameof(GetManifestStreamSource)); + var instructions = refMethod.Body.Instructions; + for (var i = 0; i < instructions.Count; i++) { + var instruction = instructions[i]; + if (instruction.OpCode != OpCodes.Callvirt || + !(instruction.Operand is IMethodDefOrRef targetMethodDefOrRef) || + !UTF8String.Equals(targetMethodDefOrRef.Name, "GetManifestResourceStream") || + !UTF8String.Equals(targetMethodDefOrRef.DeclaringType.FullName, "System.Reflection.Assembly")) continue; + + var targetMethodDef = targetMethodDefOrRef.ResolveMethodDefThrow(); + Assert.AreEqual("System.IO.Stream", targetMethodDef.ReturnType.FullName); + Assert.AreEqual(3, targetMethodDef.Parameters.Count); + } + } + internal static ModuleDefMD LoadTestModuleDef() { + var asmResolver = new AssemblyResolver(); + asmResolver.DefaultModuleContext = new ModuleContext(asmResolver); + var options = new ModuleCreationOptions(asmResolver.DefaultModuleContext); + + var thisModule = ModuleDefMD.Load(typeof(AssemblyResolutionTests).Module, options); + asmResolver.AddToCache(thisModule); + + return thisModule; + } +} diff --git a/tests/dnlib.Tests/MSTestSettings.cs b/tests/dnlib.Tests/MSTestSettings.cs new file mode 100644 index 00000000..0f29e5d9 --- /dev/null +++ b/tests/dnlib.Tests/MSTestSettings.cs @@ -0,0 +1 @@ +[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] diff --git a/tests/dnlib.Tests/dnlib.Tests.csproj b/tests/dnlib.Tests/dnlib.Tests.csproj new file mode 100644 index 00000000..1686dc12 --- /dev/null +++ b/tests/dnlib.Tests/dnlib.Tests.csproj @@ -0,0 +1,22 @@ + + + + net10.0 + latest + enable + enable + + + + + + + + + + + + + + +