de4dot-cex/de4dot.code/ExternalAssemblies.cs

108 lines
3.0 KiB
C#
Raw Normal View History

2011-11-15 21:26:51 +08:00
/*
2012-01-10 06:02:47 +08:00
Copyright (C) 2011-2012 de4dot@gmail.com
2011-11-15 21:26:51 +08:00
This file is part of de4dot.
de4dot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
de4dot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
2012-04-05 03:06:10 +08:00
using System;
2011-11-15 21:26:51 +08:00
using System.Collections.Generic;
2012-11-01 23:42:02 +08:00
using dot10.DotNet;
2011-11-15 21:26:51 +08:00
using de4dot.blocks;
2012-01-07 07:05:43 +08:00
namespace de4dot.code {
2011-11-15 21:26:51 +08:00
class ExternalAssembly {
2012-11-01 23:42:02 +08:00
AssemblyDef asmDef;
2011-11-15 21:26:51 +08:00
2012-11-01 23:42:02 +08:00
public ExternalAssembly(AssemblyDef asmDef) {
2011-11-15 21:26:51 +08:00
this.asmDef = asmDef;
}
2012-11-01 23:42:02 +08:00
public TypeDef resolve(ITypeDefOrRef type) {
2011-11-15 21:26:51 +08:00
foreach (var module in asmDef.Modules) {
2012-11-01 23:42:02 +08:00
var typeDef = module.Find(type);
2011-11-15 21:26:51 +08:00
if (typeDef != null)
return typeDef;
}
return null;
}
public void unload(string asmFullName) {
foreach (var module in asmDef.Modules) {
2012-11-01 23:42:02 +08:00
//TODO: DotNetUtils.typeCaches.invalidate(module);
2012-11-02 15:23:20 +08:00
TheAssemblyResolver.Instance.removeModule(module);
}
2012-11-02 15:23:20 +08:00
TheAssemblyResolver.Instance.removeModule(asmFullName);
2011-11-15 21:26:51 +08:00
}
}
// Loads assemblies that aren't renamed
class ExternalAssemblies {
2012-04-05 03:06:10 +08:00
Dictionary<string, ExternalAssembly> assemblies = new Dictionary<string, ExternalAssembly>(StringComparer.Ordinal);
Dictionary<string, bool> failedLoads = new Dictionary<string, bool>(StringComparer.Ordinal);
2011-11-15 21:26:51 +08:00
2012-11-01 23:42:02 +08:00
ExternalAssembly load(TypeRef type) {
if (type == null || type.DefinitionAssembly == null)
2012-08-31 06:24:42 +08:00
return null;
2012-11-01 23:42:02 +08:00
var asmFullName = type.DefinitionAssembly.FullName;
2011-11-15 21:26:51 +08:00
ExternalAssembly asm;
if (assemblies.TryGetValue(asmFullName, out asm))
return asm;
2012-11-02 15:23:20 +08:00
var asmDef = TheAssemblyResolver.Instance.Resolve(type.DefinitionAssembly, type.OwnerModule);
2011-11-15 21:26:51 +08:00
if (asmDef == null) {
2012-04-05 03:06:10 +08:00
if (!failedLoads.ContainsKey(asmFullName))
Log.w("Could not load assembly {0}", asmFullName);
failedLoads[asmFullName] = true;
2011-11-15 21:26:51 +08:00
return null;
}
2012-11-01 23:42:02 +08:00
if (assemblies.ContainsKey(asmDef.FullName)) {
assemblies[asmFullName] = assemblies[asmDef.FullName];
return assemblies[asmDef.FullName];
2011-11-15 21:26:51 +08:00
}
2012-11-01 23:42:02 +08:00
if (asmFullName == asmDef.FullName)
2011-11-15 21:26:51 +08:00
Log.v("Loaded assembly {0}", asmFullName);
else
2012-11-01 23:42:02 +08:00
Log.v("Loaded assembly {0} (but wanted {1})", asmDef.FullName, asmFullName);
2011-11-15 21:26:51 +08:00
asm = new ExternalAssembly(asmDef);
assemblies[asmFullName] = asm;
2012-11-01 23:42:02 +08:00
assemblies[asmDef.FullName] = asm;
2011-11-15 21:26:51 +08:00
return asm;
}
2012-11-01 23:42:02 +08:00
public TypeDef resolve(TypeRef type) {
2012-04-05 03:06:10 +08:00
if (type == null)
return null;
2011-11-15 21:26:51 +08:00
var asm = load(type);
if (asm == null)
return null;
return asm.resolve(type);
}
public void unloadAll() {
foreach (var pair in assemblies) {
if (pair.Value == null)
2011-11-15 21:26:51 +08:00
continue;
pair.Value.unload(pair.Key);
2011-11-15 21:26:51 +08:00
}
assemblies.Clear();
2012-04-05 03:06:10 +08:00
failedLoads.Clear();
2011-11-15 21:26:51 +08:00
}
}
}