From 8a27b45775293ba336305cb77cf31c35595ee909 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Mon, 27 Jan 2020 06:22:15 +0100 Subject: [PATCH] IDA: Output string literals correctly --- Il2CppDumper/Il2CppIDAScriptDumper.cs | 20 ++++++++++---------- Il2CppInspector/Reflection/Extensions.cs | 23 ++++++++++++++--------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Il2CppDumper/Il2CppIDAScriptDumper.cs b/Il2CppDumper/Il2CppIDAScriptDumper.cs index 4867bb1..2377a48 100644 --- a/Il2CppDumper/Il2CppIDAScriptDumper.cs +++ b/Il2CppDumper/Il2CppIDAScriptDumper.cs @@ -37,8 +37,10 @@ namespace Il2CppInspector writeSectionHeader("Methods"); writeMethods(); - writeSectionHeader( "Usages"); + writeSectionHeader("Usages"); writeUsages(); + + writer.Close(); } private void writePreamble() { @@ -47,8 +49,7 @@ namespace Il2CppInspector import idaapi def SetString(addr, comm): - global index - name = 'StringLiteral_' + str(index) + name = 'StringLiteral_' + str(addr) ret = idc.set_name(addr, name, SN_NOWARN) idc.set_cmt(addr, comm, 1) @@ -57,8 +58,6 @@ def SetName(addr, name): if ret == 0: new_name = name + '_' + str(addr) ret = idc.set_name(addr, new_name, SN_NOWARN | SN_NOCHECK) - -index = 1 " ); } @@ -78,12 +77,13 @@ index = 1 private void writeUsages() { foreach (var usage in model.Package.MetadataUsages) { - // TODO: String literals - if (usage.Type == MetadataUsageType.StringLiteral) - continue; - + var escapedName = model.GetMetadataUsageName(usage).ToEscapedString(); var address = model.Package.BinaryMetadataUsages[usage.DestinationIndex]; - writeLines($"SetName({address.ToAddressString()}, '{usagePrefixes[usage.Type]}${model.GetMetadataUsageName(usage)}'"); + + if (usage.Type != MetadataUsageType.StringLiteral) + writeLines($"SetName({address.ToAddressString()}, '{usagePrefixes[usage.Type]}${escapedName}')"); + else + writeLines($"SetString({address.ToAddressString()}, r'{escapedName}')"); } } diff --git a/Il2CppInspector/Reflection/Extensions.cs b/Il2CppInspector/Reflection/Extensions.cs index 0c77e9b..54dbab1 100644 --- a/Il2CppInspector/Reflection/Extensions.cs +++ b/Il2CppInspector/Reflection/Extensions.cs @@ -70,6 +70,19 @@ namespace Il2CppInspector.Reflection ['\v'] = @"\v" }; + // Output a string in Python-friendly syntax + public static string ToEscapedString(this string str) { + // Replace standard escape characters + var s = new StringBuilder(); + for (var i = 0; i < str.Length; i++) + // Standard escape characters + s.Append(escapeChars.ContainsKey(str[i]) ? escapeChars[str[i]] + // Replace everything else with UTF-16 Unicode + : str[i] < 32 || str[i] > 126 ? @"\u" + $"{(int) str[i]:X4}" + : str[i].ToString()); + return s.ToString(); + } + // Output a value in C#-friendly syntax public static string ToCSharpValue(this object value, TypeInfo type, Scope usingScope = null) { if (value is bool) @@ -77,15 +90,7 @@ namespace Il2CppInspector.Reflection if (value is float) return value + "f"; if (value is string str) { - // Replace standard escape characters - var s = new StringBuilder(); - for (var i = 0; i < str.Length; i++) - // Standard escape characters - s.Append(escapeChars.ContainsKey(str[i]) ? escapeChars[str[i]] - // Replace everything else with UTF-16 Unicode - : str[i] < 32 || str[i] > 126 ? @"\u" + $"{(int) str[i]:X4}" - : str[i].ToString()); - return $"\"{s}\""; + return $"\"{str.ToEscapedString()}\""; } if (value is char) { var cValue = (int) (char) value;