Move duplicated sanitizeIdentifier to Extensions

Also ensure that the identifier always starts with an underscore or
letter, which fixes issues with obfuscated identifiers.
This commit is contained in:
Robert Xiao
2020-06-23 00:12:00 -07:00
committed by Katy
parent 95ee085374
commit de6d9e2230
3 changed files with 18 additions and 15 deletions

View File

@@ -142,7 +142,7 @@ namespace Il2CppInspector.Outputs
// Generate structure fields for each field of a given type // Generate structure fields for each field of a given type
private void GenerateFieldList(StringBuilder csrc, Namespace ns, TypeInfo ti) { private void GenerateFieldList(StringBuilder csrc, Namespace ns, TypeInfo ti) {
var namer = ns.MakeNamer<FieldInfo>((field) => sanitizeIdentifier(field.Name)); var namer = ns.MakeNamer<FieldInfo>((field) => field.Name.ToCIdentifier());
foreach (var field in ti.DeclaredFields) { foreach (var field in ti.DeclaredFields) {
if (field.IsLiteral || field.IsStatic) if (field.IsLiteral || field.IsStatic)
continue; continue;
@@ -156,7 +156,7 @@ namespace Il2CppInspector.Outputs
if (ti.IsEnum) { if (ti.IsEnum) {
// Enums should be represented using enum syntax // Enums should be represented using enum syntax
// They otherwise behave like value types // They otherwise behave like value types
var namer = GlobalsNamespace.MakeNamer<FieldInfo>((field) => sanitizeIdentifier($"{name}_{field.Name}")); var namer = GlobalsNamespace.MakeNamer<FieldInfo>((field) => $"{name}_{field.Name}".ToCIdentifier());
csrc.Append($"enum {name} : {AsCType(ti.GetEnumUnderlyingType())} {{\n"); csrc.Append($"enum {name} : {AsCType(ti.GetEnumUnderlyingType())} {{\n");
foreach (var field in ti.DeclaredFields) { foreach (var field in ti.DeclaredFields) {
if (field.Name != "value__") if (field.Name != "value__")
@@ -386,7 +386,7 @@ namespace Il2CppInspector.Outputs
vtable = ti.GetVTable(); vtable = ti.GetVTable();
} }
var name = TypeNamer.GetName(ti); var name = TypeNamer.GetName(ti);
var namer = CreateNamespace().MakeNamer<int>((i) => sanitizeIdentifier(vtable[i]?.Name ?? "__unknown")); var namer = CreateNamespace().MakeNamer<int>((i) => vtable[i]?.Name?.ToCIdentifier() ?? "__unknown");
// Il2Cpp switched to `VirtualInvokeData *vtable` in Unity 5.3.6. // Il2Cpp switched to `VirtualInvokeData *vtable` in Unity 5.3.6.
// Previous versions used `MethodInfo **vtable`. // Previous versions used `MethodInfo **vtable`.
@@ -411,7 +411,7 @@ namespace Il2CppInspector.Outputs
GenerateVTableStruct(csrc, ti); GenerateVTableStruct(csrc, ti);
csrc.Append($"struct {name}__StaticFields {{\n"); csrc.Append($"struct {name}__StaticFields {{\n");
var namer = CreateNamespace().MakeNamer<FieldInfo>((field) => sanitizeIdentifier(field.Name)); var namer = CreateNamespace().MakeNamer<FieldInfo>((field) => field.Name.ToCIdentifier());
foreach (var field in ti.DeclaredFields) { foreach (var field in ti.DeclaredFields) {
if (field.IsLiteral || !field.IsStatic) if (field.IsLiteral || !field.IsStatic)
continue; continue;
@@ -489,7 +489,7 @@ namespace Il2CppInspector.Outputs
var paramNs = CreateNamespace(); var paramNs = CreateNamespace();
paramNs.ReserveName("method"); paramNs.ReserveName("method");
var paramNamer = paramNs.MakeNamer<ParameterInfo>((pi) => pi.Name == "" ? "arg" : sanitizeIdentifier(pi.Name)); var paramNamer = paramNs.MakeNamer<ParameterInfo>((pi) => pi.Name == "" ? "arg" : pi.Name.ToCIdentifier());
var paramList = new List<string>(); var paramList = new List<string>();
// Figure out the "this" param // Figure out the "this" param
@@ -551,7 +551,7 @@ namespace Il2CppInspector.Outputs
TypeNamer = TypeNamespace.MakeNamer<TypeInfo>((ti) => { TypeNamer = TypeNamespace.MakeNamer<TypeInfo>((ti) => {
if (ti.IsArray) if (ti.IsArray)
return TypeNamer.GetName(ti.ElementType) + "__Array"; return TypeNamer.GetName(ti.ElementType) + "__Array";
var name = sanitizeIdentifier(ti.Name); var name = ti.Name.ToCIdentifier();
if (name.StartsWith("Il2Cpp")) if (name.StartsWith("Il2Cpp"))
name = "_" + name; name = "_" + name;
name = Regex.Replace(name, "__+", "_"); name = Regex.Replace(name, "__+", "_");
@@ -563,7 +563,7 @@ namespace Il2CppInspector.Outputs
}); });
GlobalsNamespace = CreateNamespace(); GlobalsNamespace = CreateNamespace();
MethodNamer = TypeNamespace.MakeNamer<MethodBase>((method) => $"{TypeNamer.GetName(method.DeclaringType)}_{sanitizeIdentifier(method.Name)}"); MethodNamer = TypeNamespace.MakeNamer<MethodBase>((method) => $"{TypeNamer.GetName(method.DeclaringType)}_{method.Name.ToCIdentifier()}");
} }
// Reserve C/C++ keywords and built-in names // Reserve C/C++ keywords and built-in names
@@ -580,8 +580,6 @@ namespace Il2CppInspector.Outputs
return ns; return ns;
} }
private static string sanitizeIdentifier(string id) => Regex.Replace(id, "[^a-zA-Z0-9_]", "_");
/// <summary> /// <summary>
/// Namespace for all types and typedefs /// Namespace for all types and typedefs
/// </summary> /// </summary>

View File

@@ -10,7 +10,6 @@ using System.IO;
using System.Text; using System.Text;
using Il2CppInspector.Reflection; using Il2CppInspector.Reflection;
using Il2CppInspector.Outputs.UnityHeaders; using Il2CppInspector.Outputs.UnityHeaders;
using System.Text.RegularExpressions;
namespace Il2CppInspector.Outputs namespace Il2CppInspector.Outputs
{ {
@@ -118,13 +117,9 @@ typedef __int64 int64_t;
} }
} }
private static string sanitizeIdentifier(string str) {
return Regex.Replace(str, "[^a-zA-Z0-9_]", "_");
}
private static string stringToIdentifier(string str) { private static string stringToIdentifier(string str) {
str = str.Substring(0, Math.Min(32, str.Length)); str = str.Substring(0, Math.Min(32, str.Length));
return sanitizeIdentifier(str); return str.ToCIdentifier();
} }
private void writeUsages() { private void writeUsages() {

View File

@@ -8,6 +8,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace Il2CppInspector.Reflection namespace Il2CppInspector.Reflection
{ {
@@ -83,6 +84,15 @@ namespace Il2CppInspector.Reflection
return s.ToString(); return s.ToString();
} }
public static string ToCIdentifier(this string str) {
// replace illegal characters
str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_");
// ensure identifier starts with a letter or _ (and is non-empty)
if (!Regex.IsMatch(str, "^[a-zA-Z_]"))
str = "_" + str;
return str;
}
// 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)