Skip to content

Commit

Permalink
Added option --dont-rename-public to only rename everything non-publi…
Browse files Browse the repository at this point in the history
…c and leave public types, methods, properties, fields, and events as they are
  • Loading branch information
djkrose committed Aug 23, 2017
1 parent 875c116 commit 8929dbb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
38 changes: 34 additions & 4 deletions de4dot.code/renamer/Renamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum RenamerFlags {
RestoreEventsFromNames = 0x800,
DontCreateNewParamDefs = 0x1000,
DontRenameDelegateFields = 0x2000,
DontRenamePublic = 0x4000,
}

public class Renamer {
Expand Down Expand Up @@ -173,6 +174,17 @@ public bool DontRenameDelegateFields {
RenamerFlags &= ~RenamerFlags.DontRenameDelegateFields;
}
}
public bool DontRenamePublic
{
get { return (RenamerFlags & RenamerFlags.DontRenamePublic) != 0; }
set
{
if (value)
RenamerFlags |= RenamerFlags.DontRenamePublic;
else
RenamerFlags &= ~RenamerFlags.DontRenamePublic;
}
}

Modules modules;
MemberInfos memberInfos = new MemberInfos();
Expand Down Expand Up @@ -351,7 +363,7 @@ void Rename(MTypeDef type) {

renameGenericParams2(type.GenericParams);

if (RenameTypes && info.GotNewName()) {
if (RenameTypes && info.GotNewName() && !(DontRenamePublic && type.IsGlobalType())) {
var old = typeDef.Name;
typeDef.Name = info.newName;
if (isVerbose)
Expand Down Expand Up @@ -419,6 +431,8 @@ void RenameFields2(TypeInfo info) {
continue;
if (isDelegateType && DontRenameDelegateFields)
continue;
if (DontRenamePublic && fieldDef.FieldDef.IsPublic)
continue;
fieldDef.FieldDef.Name = fieldInfo.newName;
if (isVerbose)
Logger.v("Field: {0} ({1:X8}) => {2}",
Expand All @@ -435,6 +449,8 @@ void RenameProperties2(TypeInfo info) {
var propInfo = memberInfos.Property(propDef);
if (!propInfo.GotNewName())
continue;
if (DontRenamePublic && ContainsPublicMethod(propDef.MethodDefs()))
continue;
propDef.PropertyDef.Name = propInfo.newName;
if (isVerbose)
Logger.v("Property: {0} ({1:X8}) => {2}",
Expand All @@ -451,6 +467,8 @@ void RenameEvents2(TypeInfo info) {
var eventInfo = memberInfos.Event(eventDef);
if (!eventInfo.GotNewName())
continue;
if (DontRenamePublic && ContainsPublicMethod(eventDef.MethodDefs()))
continue;
eventDef.EventDef.Name = eventInfo.newName;
if (isVerbose)
Logger.v("Event: {0} ({1:X8}) => {2}",
Expand All @@ -471,7 +489,7 @@ void RenameMethods2(TypeInfo info) {

renameGenericParams2(methodDef.GenericParams);

if (RenameMethods && methodInfo.GotNewName()) {
if (RenameMethods && methodInfo.GotNewName() && !(DontRenamePublic && methodDef.MethodDef.IsPublic)) {
methodDef.MethodDef.Name = methodInfo.newName;
if (isVerbose)
Logger.v("Name: {0} => {1}", Utils.RemoveNewlines(methodInfo.oldFullName), Utils.RemoveNewlines(methodDef.MethodDef.FullName));
Expand Down Expand Up @@ -609,7 +627,7 @@ void ResetVirtualPropertyNames(IEnumerable<MethodNameGroup> allGroups) {
prop = method.Property;
break;
}
if (prop == null)
if (prop == null || (DontRenamePublic && ContainsPublicMethod(prop.MethodDefs())))
continue;
foreach (var method in group.Methods) {
if (!method.Owner.HasModule)
Expand All @@ -636,6 +654,8 @@ void ResetVirtualEventNames(IEnumerable<MethodNameGroup> allGroups) {
}
if (evt == null)
continue;
if (DontRenamePublic && ContainsPublicMethod(evt.MethodDefs()))
continue;
foreach (var method in group.Methods) {
if (!method.Owner.HasModule)
continue;
Expand Down Expand Up @@ -1788,7 +1808,17 @@ bool HasDelegateOwner(MethodNameGroup group) {
return false;
}

void PrepareRenameEntryPoints() {
bool ContainsPublicMethod(IEnumerable<MethodDef> methods)
{
foreach (var methodDef in methods)
{
if (methodDef.IsPublic)
return true;
}
return false;
}

void PrepareRenameEntryPoints() {
foreach (var module in modules.TheModules) {
var entryPoint = module.ModuleDefMD.EntryPoint;
if (entryPoint == null)
Expand Down
4 changes: 4 additions & 0 deletions de4dot.cui/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ void AddAllOptions() {
filesOptions.RenameSymbols = false;
filesOptions.RenamerFlags = 0;
}));
miscOptions.Add(new NoArgOption(null, "dont-rename-public", "Don't rename public classes, methods, etc.", () =>
{
filesOptions.RenamerFlags |= RenamerFlags.DontRenamePublic;
}));
miscOptions.Add(new OneArgOption(null, "keep-names", "Don't rename n(amespaces), t(ypes), p(rops), e(vents), f(ields), m(ethods), a(rgs), g(enericparams), d(elegate fields). Can be combined, eg. efm", "flags", (val) => {
foreach (var c in val) {
switch (c) {
Expand Down

0 comments on commit 8929dbb

Please sign in to comment.