Skip to content

Commit

Permalink
Fix resource renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mobile46 committed Mar 5, 2020
1 parent fd71bc1 commit 5b47252
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions de4dot.code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ static void AppendEscape(StringBuilder sb, char c) {
sb.Append(c);
}

public static string RemoveBackslash(this string s) => s.Replace("\\", string.Empty);

public static string RemoveNewlines(object o) => RemoveNewlines(o.ToString());
public static string RemoveNewlines(string s) => s.Replace('\n', ' ').Replace('\r', ' ');

Expand Down
4 changes: 3 additions & 1 deletion de4dot.code/renamer/Renamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,10 @@ void RenameResources(Module module) {
var renamedTypes = new List<TypeInfo>();
foreach (var type in module.GetAllTypes()) {
var info = memberInfos.Type(type);
if (info.oldFullName != info.type.TypeDef.FullName)
if (info.oldFullName != info.type.TypeDef.FullName) {
info.oldFullName = info.oldFullName.RemoveBackslash();
renamedTypes.Add(info);
}
}
if (renamedTypes.Count == 0)
return;
Expand Down
11 changes: 6 additions & 5 deletions de4dot.code/renamer/ResourceRenamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Rename(List<TypeInfo> renamedTypes) {

nameToResource = new Dictionary<string, Resource>(module.ModuleDefMD.Resources.Count * 3, StringComparer.Ordinal);
foreach (var resource in module.ModuleDefMD.Resources) {
var name = resource.Name.String;
var name = resource.Name.String.RemoveBackslash();
nameToResource[name] = resource;
if (name.EndsWith(".g.resources"))
nameToResource[name.Substring(0, name.Length - 12)] = resource;
Expand All @@ -68,7 +68,7 @@ void RenameResourceNamesInCode(List<TypeInfo> renamedTypes) {
var instr = instrs[i];
if (instr.OpCode != OpCodes.Ldstr)
continue;
var codeString = (string)instr.Operand;
var codeString = ((string)instr.Operand).RemoveBackslash();
if (string.IsNullOrEmpty(codeString))
continue;

Expand All @@ -81,8 +81,10 @@ void RenameResourceNamesInCode(List<TypeInfo> renamedTypes) {

bool renameCodeString = module.ObfuscatedFile.RenameResourcesInCode ||
IsCallingResourceManagerCtor(instrs, i, typeInfo);
if (!renameCodeString)
if (!renameCodeString) {
nameToResource.Remove(typeInfo.oldFullName);
Logger.v("Possible resource name in code: '{0}' => '{1}' in method {2}", Utils.RemoveNewlines(codeString), newName, Utils.RemoveNewlines(method));
}
else {
instr.Operand = newName;
Logger.v("Renamed resource string in code: '{0}' => '{1}' ({2})", Utils.RemoveNewlines(codeString), newName, Utils.RemoveNewlines(method));
Expand Down Expand Up @@ -148,8 +150,7 @@ void RenameResources(List<TypeInfo> renamedTypes) {
continue;
if (newNames.ContainsKey(resource))
continue;
var newTypeName = info.type.TypeDef.FullName;
var newName = newTypeName + resource.Name.String.Substring(oldFullName.Length);
var newName = info.type.TypeDef.FullName + (resource.Name.EndsWith(".g.resources") ? ".g.resources" : ".resources");
newNames[resource] = new RenameInfo(resource, info, newName);

Logger.v("Renamed resource in resources: {0} => {1}", Utils.RemoveNewlines(resource.Name), newName);
Expand Down

0 comments on commit 5b47252

Please sign in to comment.