Add Caliburn.Micro compatibility

Fix #132
This commit is contained in:
yck1509 2014-12-08 21:23:01 +08:00
Родитель 676b3a6f0f
Коммит c0f7141e04
4 изменённых файлов: 58 добавлений и 2 удалений

Просмотреть файл

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Confuser.Core;
using Confuser.Renamer.Analyzers;
using dnlib.DotNet;
namespace Confuser.Renamer {
@ -49,6 +50,7 @@ namespace Confuser.Renamer {
}
context.Logger.Debug("Analyzing...");
RegisterRenamers(context, service);
IList<IRenamer> renamers = service.Renamers;
foreach (IDnlibDef def in parameters.Targets.WithProgress(context.Logger)) {
Analyze(service, context, parameters, def, true);
@ -56,6 +58,30 @@ namespace Confuser.Renamer {
}
}
private void RegisterRenamers(ConfuserContext context, NameService service) {
bool wpf = false,
caliburn = false;
foreach (var module in context.Modules)
foreach (var asmRef in module.GetAssemblyRefs()) {
if (asmRef.Name == "WindowsBase" || asmRef.Name == "PresentationCore" ||
asmRef.Name == "PresentationFramework" || asmRef.Name == "System.Xaml") {
if (!wpf) {
context.Logger.Debug("WPF found, enabling compatibility.");
service.Renamers.Add(new WPFAnalyzer());
wpf = true;
}
}
else if (asmRef.Name == "Caliburn.Micro") {
if (!caliburn) {
context.Logger.Debug("Caliburn.Micro found, enabling compatibility.");
service.Renamers.Add(new CaliburnAnalyzer());
caliburn = true;
}
}
}
}
internal void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, IDnlibDef def, bool runAnalyzer) {
if (def is TypeDef)
Analyze(service, context, parameters, (TypeDef)def);

Просмотреть файл

@ -0,0 +1,30 @@
using System;
using Confuser.Core;
using dnlib.DotNet;
namespace Confuser.Renamer.Analyzers {
internal class CaliburnAnalyzer : IRenamer {
public void Analyze(ConfuserContext context, INameService service, IDnlibDef def) {
var type = def as TypeDef;
if (type == null || type.DeclaringType != null)
return;
if (type.Name.Contains("ViewModel")) {
string viewNs = type.Namespace.Replace("ViewModels", "Views");
string viewName = type.Name.Replace("PageViewModel", "Page").Replace("ViewModel", "View");
TypeDef view = type.Module.Find(viewNs + "." + viewName, true);
if (view != null) {
service.SetCanRename(type, false);
service.SetCanRename(view, false);
}
}
}
public void PreRename(ConfuserContext context, INameService service, IDnlibDef def) {
//
}
public void PostRename(ConfuserContext context, INameService service, IDnlibDef def) {
//
}
}
}

Просмотреть файл

@ -47,6 +47,7 @@
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="AnalyzePhase.cs" />
<Compile Include="Analyzers\CaliburnAnalyzer.cs" />
<Compile Include="Analyzers\InterReferenceAnalyzer.cs" />
<Compile Include="Analyzers\LdtokenEnumAnalyzer.cs" />
<Compile Include="Analyzers\ResourceAnalyzer.cs" />

Просмотреть файл

@ -60,8 +60,7 @@ namespace Confuser.Renamer {
new VTableAnalyzer(),
new TypeBlobAnalyzer(),
new ResourceAnalyzer(),
new WPFAnalyzer(),
new LdtokenEnumAnalyzer()
new LdtokenEnumAnalyzer(),
};
}