From cd0e5c0169543d91ef1a94d3131940deb4ac55b2 Mon Sep 17 00:00:00 2001 From: de4dot Date: Tue, 27 Sep 2011 23:29:38 +0200 Subject: [PATCH] Updated resource renaming of code strings --- de4dot.code/renamer/Module.cs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/de4dot.code/renamer/Module.cs b/de4dot.code/renamer/Module.cs index 40bbda4a..c75616da 100644 --- a/de4dot.code/renamer/Module.cs +++ b/de4dot.code/renamer/Module.cs @@ -92,6 +92,15 @@ namespace de4dot.renamer { public void renameResources() { var renamedTypes = new List(getRenamedTypeNames()); + + // Rename the longest names first. Otherwise eg. b.g.resources could be renamed + // Class0.g.resources instead of Class1.resources when b.g was renamed Class1. + renamedTypes.Sort((a, b) => { + if (a.OldName.Length > b.OldName.Length) return -1; + if (a.OldName.Length < b.OldName.Length) return 1; + return 0; + }); + renameResourceNamesInCode(renamedTypes); renameResources(renamedTypes); } @@ -102,6 +111,15 @@ namespace de4dot.renamer { foreach (var renamed in renamedTypes) oldToNewTypeName[renamed.OldName] = renamed.NewName; + List validResourceNames = new List(); + if (ModuleDefinition.Resources != null) { + foreach (var resource in ModuleDefinition.Resources) { + var name = resource.Name; + if (name.EndsWith(".resources", StringComparison.Ordinal)) + validResourceNames.Add(name); + } + } + foreach (var method in allMethods) { if (!method.HasBody) continue; @@ -111,14 +129,18 @@ namespace de4dot.renamer { var s = (string)instr.Operand; string newName = null; - if (oldToNewTypeName.ContainsKey(s)) + string oldName = null; + if (oldToNewTypeName.ContainsKey(s)) { + oldName = s; newName = oldToNewTypeName[s]; + } else if (s.EndsWith(".resources", StringComparison.Ordinal)) { // This should rarely, if ever, execute... foreach (var renamed in renamedTypes) { // Slow loop var newName2 = renameResourceString(s, renamed.OldName, renamed.NewName); if (newName2 != s) { newName = newName2; + oldName = renamed.OldName; break; } } @@ -126,6 +148,16 @@ namespace de4dot.renamer { if (newName == null) continue; + bool isValid = false; + foreach (var validName in validResourceNames) { + if (validName.StartsWith(oldName, StringComparison.Ordinal)) { + isValid = true; + break; + } + } + if (!isValid) + continue; + if (s == "" || !obfuscatedFile.RenameResourcesInCode) Log.v("Possible resource name in code: '{0}' => '{1}' in method {2}", s, newName, method); else {