Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop/issue 863 #864

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Refactor to be customisable
  • Loading branch information
SteveGilham committed Jun 27, 2022
commit d01a34fa5bfcee7401d09b3ddbb908f754242481
57 changes: 33 additions & 24 deletions Mono.Cecil/BaseAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public void RemoveSearchDirectory (string directory)
directories.Remove (directory);
}

protected bool NetCore { get; set; }

protected bool AsMono { get; set; }

protected Module CoreModule { get; set; }

public string [] GetSearchDirectories ()
{
var directories = new string [this.directories.size];
Expand All @@ -98,6 +104,14 @@ public string [] GetSearchDirectories ()
protected BaseAssemblyResolver ()
{
directories = new Collection<string> (2) { ".", "bin" };
#if NET_CORE
NetCore = true;
#else
NetCore = false;
#endif

AsMono = on_mono;
CoreModule = typeof (object).Module;
}

private AssemblyDefinition GetAssembly (string file, ReaderParameters parameters)
Expand Down Expand Up @@ -129,17 +143,11 @@ public virtual AssemblyDefinition Resolve (AssemblyNameReference name, ReaderPar
};
}

#if NET_CORE
assembly = SearchTrustedPlatformAssemblies (name, parameters);
if (assembly != null)
return assembly;
#else
assembly = SearchFrameworkAssemblies (name, parameters);
assembly = NetCore ? SearchTrustedPlatformAssemblies (name, parameters) :
SearchFrameworkAssemblies (name, parameters);
if (assembly != null)
return assembly;

#endif

assembly = LastChanceResolution (assembly, name, parameters);
if (assembly != null)
return assembly;
Expand All @@ -159,8 +167,9 @@ protected virtual AssemblyDefinition LastChanceResolution (AssemblyDefinition as
protected AssemblyDefinition SearchFrameworkAssemblies (AssemblyNameReference name, ReaderParameters parameters)
{
AssemblyDefinition assembly = null;
var framework_dir = Path.GetDirectoryName (typeof (object).Module.FullyQualifiedName);
var framework_dirs = on_mono

var framework_dir = Path.GetDirectoryName (CoreModule.FullyQualifiedName);
var framework_dirs = AsMono
? new [] { framework_dir, Path.Combine (framework_dir, "Facades") }
: new [] { framework_dir };

Expand Down Expand Up @@ -246,16 +255,16 @@ private static bool IsZero (Version version)
private AssemblyDefinition GetCorlib (AssemblyNameReference reference, ReaderParameters parameters)
{
var version = reference.Version;
var corlib = typeof (object).Assembly.GetName ();
var corlib = CoreModule.Assembly.GetName (); // GetFramework
if (corlib.Version == version || IsZero (version))
return GetAssembly (typeof (object).Module.FullyQualifiedName, parameters);
return GetAssembly (CoreModule.FullyQualifiedName, parameters);

var path = Directory.GetParent (
Directory.GetParent (
typeof (object).Module.FullyQualifiedName).FullName
CoreModule.FullyQualifiedName).FullName
).FullName;

if (on_mono) {
if (AsMono) {
if (version.Major == 1)
path = Path.Combine (path, "1.0");
else if (version.Major == 2) {
Expand Down Expand Up @@ -293,7 +302,7 @@ private AssemblyDefinition GetCorlib (AssemblyNameReference reference, ReaderPar
if (File.Exists (file))
return GetAssembly (file, parameters);

if (on_mono && Directory.Exists (path + "-api")) {
if (AsMono && Directory.Exists (path + "-api")) {
file = Path.Combine (path + "-api", "mscorlib.dll");
if (File.Exists (file))
return GetAssembly (file, parameters);
Expand All @@ -302,10 +311,10 @@ private AssemblyDefinition GetCorlib (AssemblyNameReference reference, ReaderPar
return null;
}

private static Collection<string> GetGacPaths ()
private static Collection<string> GetGacPaths (bool mono, Module core)
{
if (on_mono)
return GetDefaultMonoGacPaths ();
if (mono)
return GetDefaultMonoGacPaths (core);

var paths = new Collection<string> (2);
var windir = Environment.GetEnvironmentVariable ("WINDIR");
Expand All @@ -317,10 +326,10 @@ private static Collection<string> GetGacPaths ()
return paths;
}

private static Collection<string> GetDefaultMonoGacPaths ()
private static Collection<string> GetDefaultMonoGacPaths (Module core)
{
var paths = new Collection<string> (1);
var gac = GetCurrentMonoGac ();
var gac = GetCurrentMonoGac (core);
if (gac != null)
paths.Add (gac);

Expand All @@ -341,11 +350,11 @@ private static Collection<string> GetDefaultMonoGacPaths ()
return paths;
}

private static string GetCurrentMonoGac ()
private static string GetCurrentMonoGac (Module core)
{
return Path.Combine (
Directory.GetParent (
Path.GetDirectoryName (typeof (object).Module.FullyQualifiedName)).FullName,
Path.GetDirectoryName (core.FullyQualifiedName)).FullName, // GetFrameworkDirectory
"gac");
}

Expand All @@ -355,9 +364,9 @@ private AssemblyDefinition GetAssemblyInGac (AssemblyNameReference reference, Re
return null;

if (gac_paths == null)
gac_paths = GetGacPaths ();
gac_paths = GetGacPaths (AsMono, CoreModule);

if (on_mono)
if (AsMono)
return GetAssemblyInMonoGac (reference, parameters);

return GetAssemblyInNetGac (reference, parameters);
Expand Down