C#: Sanitize type names (#70)
This commit is contained in:
@@ -88,11 +88,11 @@ namespace Il2CppInspector.Reflection
|
|||||||
return s.ToString();
|
return s.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string ToCIdentifier(this string str) {
|
public static string ToCIdentifier(this string str, bool allowScopeQualifiers = false) {
|
||||||
// replace * with Ptr
|
// replace * with Ptr
|
||||||
str = str.Replace("*", "Ptr");
|
str = str.Replace("*", "Ptr");
|
||||||
// replace illegal characters
|
// replace illegal characters
|
||||||
str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_");
|
str = Regex.Replace(str, allowScopeQualifiers? @"[^a-zA-Z0-9_\.:]" : "[^a-zA-Z0-9_]", "_");
|
||||||
// ensure identifier starts with a letter or _ (and is non-empty)
|
// ensure identifier starts with a letter or _ (and is non-empty)
|
||||||
if (!Regex.IsMatch(str, "^[a-zA-Z_]"))
|
if (!Regex.IsMatch(str, "^[a-zA-Z_]"))
|
||||||
str = "_" + str;
|
str = "_" + str;
|
||||||
|
|||||||
@@ -297,8 +297,8 @@ namespace Il2CppInspector.Reflection
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get rid of generic backticks
|
// Get rid of generic backticks and invalid characters
|
||||||
public string CSharpBaseName => unmangleName(base.Name);
|
public string CSharpBaseName => unmangleName(base.Name).ToCIdentifier();
|
||||||
|
|
||||||
// C# colloquial name of the type (if available)
|
// C# colloquial name of the type (if available)
|
||||||
public override string CSharpName {
|
public override string CSharpName {
|
||||||
@@ -315,8 +315,7 @@ namespace Il2CppInspector.Reflection
|
|||||||
} else {
|
} else {
|
||||||
var s = Namespace + "." + base.Name;
|
var s = Namespace + "." + base.Name;
|
||||||
var i = Il2CppConstants.FullNameTypeString.IndexOf(s);
|
var i = Il2CppConstants.FullNameTypeString.IndexOf(s);
|
||||||
var n = (i != -1 ? Il2CppConstants.CSharpTypeString[i] : base.Name);
|
var n = (i != -1 ? Il2CppConstants.CSharpTypeString[i] : CSharpBaseName);
|
||||||
n = unmangleName(n);
|
|
||||||
var ga = GetGenericArguments();
|
var ga = GetGenericArguments();
|
||||||
if (ga.Any())
|
if (ga.Any())
|
||||||
n += "<" + string.Join(", ", ga.Select(x => x.CSharpName)) + ">";
|
n += "<" + string.Join(", ", ga.Select(x => x.CSharpName)) + ">";
|
||||||
@@ -339,7 +338,7 @@ namespace Il2CppInspector.Reflection
|
|||||||
n += "*";
|
n += "*";
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
var n = unmangleName(base.Name);
|
var n = CSharpBaseName;
|
||||||
var ga = IsNested ? GetGenericArguments().Where(p => DeclaringType.GetGenericArguments().All(dp => dp.Name != p.Name)) : GetGenericArguments();
|
var ga = IsNested ? GetGenericArguments().Where(p => DeclaringType.GetGenericArguments().All(dp => dp.Name != p.Name)) : GetGenericArguments();
|
||||||
if (ga.Any())
|
if (ga.Any())
|
||||||
n += "<" + string.Join(", ", ga.Select(x => (!x.IsGenericTypeParameter ? x.Namespace + "." : "") + x.GetCSharpTypeDeclarationName(includeVariance: true))) + ">";
|
n += "<" + string.Join(", ", ga.Select(x => (!x.IsGenericTypeParameter ? x.Namespace + "." : "") + x.GetCSharpTypeDeclarationName(includeVariance: true))) + ">";
|
||||||
@@ -580,7 +579,7 @@ namespace Il2CppInspector.Reflection
|
|||||||
// Built-in keyword type names do not require a scope
|
// Built-in keyword type names do not require a scope
|
||||||
var i = Il2CppConstants.FullNameTypeString.IndexOf(s);
|
var i = Il2CppConstants.FullNameTypeString.IndexOf(s);
|
||||||
var n = i != -1 ? Il2CppConstants.CSharpTypeString[i] : getScopedFullName(usingScope);
|
var n = i != -1 ? Il2CppConstants.CSharpTypeString[i] : getScopedFullName(usingScope);
|
||||||
n = unmangleName(n);
|
n = unmangleName(n).ToCIdentifier(allowScopeQualifiers: true);
|
||||||
|
|
||||||
// Generic type parameters and type arguments
|
// Generic type parameters and type arguments
|
||||||
// Inheriting from a base class or implementing an interface use the type's declaring scope, not the type's scope itself
|
// Inheriting from a base class or implementing an interface use the type's declaring scope, not the type's scope itself
|
||||||
|
|||||||
Reference in New Issue
Block a user