IDA: Output string literals correctly
This commit is contained in:
@@ -37,8 +37,10 @@ namespace Il2CppInspector
|
|||||||
writeSectionHeader("Methods");
|
writeSectionHeader("Methods");
|
||||||
writeMethods();
|
writeMethods();
|
||||||
|
|
||||||
writeSectionHeader( "Usages");
|
writeSectionHeader("Usages");
|
||||||
writeUsages();
|
writeUsages();
|
||||||
|
|
||||||
|
writer.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writePreamble() {
|
private void writePreamble() {
|
||||||
@@ -47,8 +49,7 @@ namespace Il2CppInspector
|
|||||||
import idaapi
|
import idaapi
|
||||||
|
|
||||||
def SetString(addr, comm):
|
def SetString(addr, comm):
|
||||||
global index
|
name = 'StringLiteral_' + str(addr)
|
||||||
name = 'StringLiteral_' + str(index)
|
|
||||||
ret = idc.set_name(addr, name, SN_NOWARN)
|
ret = idc.set_name(addr, name, SN_NOWARN)
|
||||||
idc.set_cmt(addr, comm, 1)
|
idc.set_cmt(addr, comm, 1)
|
||||||
|
|
||||||
@@ -57,8 +58,6 @@ def SetName(addr, name):
|
|||||||
if ret == 0:
|
if ret == 0:
|
||||||
new_name = name + '_' + str(addr)
|
new_name = name + '_' + str(addr)
|
||||||
ret = idc.set_name(addr, new_name, SN_NOWARN | SN_NOCHECK)
|
ret = idc.set_name(addr, new_name, SN_NOWARN | SN_NOCHECK)
|
||||||
|
|
||||||
index = 1
|
|
||||||
"
|
"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -78,12 +77,13 @@ index = 1
|
|||||||
|
|
||||||
private void writeUsages() {
|
private void writeUsages() {
|
||||||
foreach (var usage in model.Package.MetadataUsages) {
|
foreach (var usage in model.Package.MetadataUsages) {
|
||||||
// TODO: String literals
|
var escapedName = model.GetMetadataUsageName(usage).ToEscapedString();
|
||||||
if (usage.Type == MetadataUsageType.StringLiteral)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var address = model.Package.BinaryMetadataUsages[usage.DestinationIndex];
|
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}')");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,6 +70,19 @@ namespace Il2CppInspector.Reflection
|
|||||||
['\v'] = @"\v"
|
['\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
|
// Output a value in C#-friendly syntax
|
||||||
public static string ToCSharpValue(this object value, TypeInfo type, Scope usingScope = null) {
|
public static string ToCSharpValue(this object value, TypeInfo type, Scope usingScope = null) {
|
||||||
if (value is bool)
|
if (value is bool)
|
||||||
@@ -77,15 +90,7 @@ namespace Il2CppInspector.Reflection
|
|||||||
if (value is float)
|
if (value is float)
|
||||||
return value + "f";
|
return value + "f";
|
||||||
if (value is string str) {
|
if (value is string str) {
|
||||||
// Replace standard escape characters
|
return $"\"{str.ToEscapedString()}\"";
|
||||||
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}\"";
|
|
||||||
}
|
}
|
||||||
if (value is char) {
|
if (value is char) {
|
||||||
var cValue = (int) (char) value;
|
var cValue = (int) (char) value;
|
||||||
|
|||||||
Reference in New Issue
Block a user