Skip to content

Commit

Permalink
Generate Events
Browse files Browse the repository at this point in the history
* Also clean up code surrounding strings
  • Loading branch information
ds5678 committed Sep 14, 2024
1 parent 23b74e1 commit e131037
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 48 deletions.
14 changes: 8 additions & 6 deletions Il2CppInterop.Generator/Contexts/FieldRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ private string UnmangleFieldNameBase(FieldDefinition field, GeneratorOptions opt

if (!field.Name.IsObfuscated(options))
{
if (!field.Name.IsInvalidInSource())
return field.Name!;
return field.Name.FilterInvalidInSourceChars();
return field.Name.MakeValidInSource();
}

Debug.Assert(field.Signature is not null);
Expand All @@ -64,9 +62,13 @@ private string UnmangleFieldName(FieldDefinition field, GeneratorOptions options

if (!field.Name.IsObfuscated(options))
{
if (!field.Name.IsInvalidInSource())
return field.Name!;
return field.Name.FilterInvalidInSourceChars();
var name = field.Name.MakeValidInSource();
while (field.DeclaringType!.Events.Any(e => e.Name == name)
|| field.DeclaringType!.Fields.Any(f => f.Name == name && f != field))
{
name += "_"; // Backing fields for events have the same name as the event.
}
return name;
}

if (renamedFieldCounts == null) throw new ArgumentNullException(nameof(renamedFieldCounts));
Expand Down
28 changes: 10 additions & 18 deletions Il2CppInterop.Generator/Contexts/MethodRewriteContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using AsmResolver;
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using AsmResolver.PE.DotNet.Metadata.Tables;
Expand Down Expand Up @@ -73,12 +74,9 @@ public MethodRewriteContext(TypeRewriteContext declaringType, MethodDefinition o

foreach (var oldParameter in genericParams)
{
var genericParameter = new GenericParameter(oldParameter.Name);
genericParameter.Attributes = oldParameter.Attributes.StripValueTypeConstraint();
newMethod.GenericParameters.Add(genericParameter);

if (genericParameter.Name.IsInvalidInSource())
genericParameter.Name = genericParameter.Name.FilterInvalidInSourceChars();
newMethod.GenericParameters.Add(new GenericParameter(
oldParameter.Name.MakeValidInSource(),
oldParameter.Attributes.StripValueTypeConstraint()));
}
}

Expand All @@ -94,7 +92,7 @@ public MethodRewriteContext(TypeRewriteContext declaringType, MethodDefinition o
declaringType.AssemblyContext.GlobalContext.MethodStartAddresses.Add(FileOffset);
}

public string? UnmangledName { get; private set; }
public Utf8String? UnmangledName { get; private set; }
public string? UnmangledNameWithSignature { get; private set; }

public TypeDefinition? GenericInstantiationsStore { get; private set; }
Expand Down Expand Up @@ -137,9 +135,7 @@ public void CtorPhase2()
for (var index = 0; index < genericParams.Count; index++)
{
var oldParameter = genericParams[index];
var genericParameter = new GenericParameter(oldParameter.Name);
if (genericParameter.Name.IsInvalidInSource())
genericParameter.Name = genericParameter.Name.FilterInvalidInSourceChars();
var genericParameter = new GenericParameter(oldParameter.Name.MakeValidInSource());
genericMethodInfoStoreType.GenericParameters.Add(genericParameter);
selfSubstRef.TypeArguments.Add(genericParameter.ToTypeSignature());
var newParameter = NewMethod.GenericParameters[index];
Expand Down Expand Up @@ -204,22 +200,18 @@ private string UnmangleMethodName()
if (method.Name.IsObfuscated(DeclaringType.AssemblyContext.GlobalContext.Options))
return UnmangleMethodNameWithSignature();

if (method.Name.IsInvalidInSource())
return method.Name.FilterInvalidInSourceChars();

return method.Name!;
return method.Name.MakeValidInSource();
}

private string ProduceMethodSignatureBase()
{
var method = OriginalMethod;

var name = method.Name;
string name;
if (method.Name.IsObfuscated(DeclaringType.AssemblyContext.GlobalContext.Options))
name = "Method";

if (name.IsInvalidInSource())
name = name.FilterInvalidInSourceChars();
else
name = method.Name.MakeValidInSource();

if (method.Name == "GetType" && method.Parameters.Count == 0)
name = "GetIl2CppType";
Expand Down
5 changes: 1 addition & 4 deletions Il2CppInterop.Generator/Contexts/RewriteGlobalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ public AssemblyRewriteContext GetAssemblyByName(string name)
var paramsMethod = new MethodDefinition(newMethod.Name, newMethod.Attributes, MethodSignatureCreator.CreateMethodSignature(newMethod.Attributes, newMethod.Signature!.ReturnType, newMethod.Signature.GenericParameterCount));
foreach (var genericParameter in originalMethod.GenericParameters)
{
var newGenericParameter = new GenericParameter(genericParameter.Name, genericParameter.Attributes);

if (newGenericParameter.Name.IsInvalidInSource())
newGenericParameter.Name = newGenericParameter.Name.FilterInvalidInSourceChars();
var newGenericParameter = new GenericParameter(genericParameter.Name.MakeValidInSource(), genericParameter.Attributes);

foreach (var constraint in genericParameter.Constraints)
{
Expand Down
37 changes: 25 additions & 12 deletions Il2CppInterop.Generator/Extensions/StringEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,39 @@ public static string UnSystemify(this Utf8String? str, GeneratorOptions options)
return UnSystemify(str?.Value ?? "", options);
}

public static string FilterInvalidInSourceChars(this string str)
public static string MakeValidInSource(this string str)
{
var chars = str.ToCharArray();
for (var i = 0; i < chars.Length; i++)
if (string.IsNullOrEmpty(str))
return "";

char[]? chars = null;
for (var i = 0; i < str.Length; i++)
{
var it = chars[i];
if (!char.IsDigit(it) && !((it >= 'a' && it <= 'z') || (it >= 'A' && it <= 'Z')) && it != '_' &&
it != '`') chars[i] = '_';
var it = str[i];
if (char.IsDigit(it) || (it is >= 'a' and <= 'z') || (it is >= 'A' and <= 'Z') || it == '_' || it == '`')
continue;

chars ??= str.ToCharArray();
chars[i] = '_';
}

return new string(chars);
var result = chars is null ? str : new string(chars);
return char.IsDigit(result[0]) ? "_" + result : result;
}

public static string FilterInvalidInSourceChars(this Utf8String? str)
public static Utf8String MakeValidInSource(this Utf8String? str)
{
return str?.Value.FilterInvalidInSourceChars() ?? "";
if (Utf8String.IsNullOrEmpty(str))
return Utf8String.Empty;
var result = str.Value.MakeValidInSource();
return result == str ? str : result;
}

public static bool IsInvalidInSource(this string str)
public static bool IsInvalidInSource([NotNullWhen(true)] this string? str)
{
if (str is null)
return false;

for (var i = 0; i < str.Length; i++)
{
var it = str[i];
Expand All @@ -73,9 +86,9 @@ public static bool IsInvalidInSource(this string str)
return false;
}

public static bool IsInvalidInSource(this Utf8String? str)
public static bool IsInvalidInSource([NotNullWhen(true)] this Utf8String? str)
{
return IsInvalidInSource(str?.Value ?? "");
return IsInvalidInSource(str?.Value);
}

public static bool IsObfuscated([NotNullWhen(true)] this string? str, GeneratorOptions options)
Expand Down
5 changes: 1 addition & 4 deletions Il2CppInterop.Generator/Passes/Pass10CreateTypedefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ internal static (string? Namespace, string Name) GetConvertedTypeName(
return (null, convertedTypeName);
}

if (type.Name.IsInvalidInSource())
return (null, type.Name.FilterInvalidInSourceChars());

return (null, type.Name!);
return (null, type.Name.MakeValidInSource());
}

private static TypeAttributes AdjustAttributes(TypeAttributes typeAttributes)
Expand Down
6 changes: 2 additions & 4 deletions Il2CppInterop.Generator/Passes/Pass12FillTypedefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ public static void DoPass(RewriteGlobalContext context)
{
foreach (var originalParameter in typeContext.OriginalType.GenericParameters)
{
var newParameter = new GenericParameter(originalParameter.Name);
if (newParameter.Name.IsInvalidInSource())
newParameter.Name = newParameter.Name.FilterInvalidInSourceChars();
var newParameter = new GenericParameter(originalParameter.Name.MakeValidInSource(),
originalParameter.Attributes.StripValueTypeConstraint());
typeContext.NewType.GenericParameters.Add(newParameter);
newParameter.Attributes = originalParameter.Attributes.StripValueTypeConstraint();
}

if (typeContext.OriginalType.IsEnum)
Expand Down
60 changes: 60 additions & 0 deletions Il2CppInterop.Generator/Passes/Pass71GenerateEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using AsmResolver.DotNet;
using Il2CppInterop.Generator.Contexts;
using Il2CppInterop.Generator.Extensions;

namespace Il2CppInterop.Generator.Passes;

public static class Pass71GenerateEvents
{
public static void DoPass(RewriteGlobalContext context)
{
foreach (var assemblyContext in context.Assemblies)
foreach (var typeContext in assemblyContext.Types)
{
var type = typeContext.OriginalType;
var eventCountsByName = new Dictionary<string, int>();

foreach (var oldEvent in type.Events)
{
var unmangledEventName = UnmangleEventName(assemblyContext, oldEvent, typeContext.NewType, eventCountsByName);

var eventType = assemblyContext.RewriteTypeRef(oldEvent.EventType?.ToTypeSignature());
var @event = new EventDefinition(unmangledEventName, oldEvent.Attributes, eventType.ToTypeDefOrRef());

typeContext.NewType.Events.Add(@event);

@event.SetSemanticMethods(
oldEvent.AddMethod is null ? null : typeContext.GetMethodByOldMethod(oldEvent.AddMethod).NewMethod,
oldEvent.RemoveMethod is null ? null : typeContext.GetMethodByOldMethod(oldEvent.RemoveMethod).NewMethod,
oldEvent.FireMethod is null ? null : typeContext.GetMethodByOldMethod(oldEvent.FireMethod).NewMethod);
}
}
}

private static string UnmangleEventName(AssemblyRewriteContext assemblyContext, EventDefinition @event,
ITypeDefOrRef declaringType, Dictionary<string, int> countsByBaseName)
{
if (assemblyContext.GlobalContext.Options.PassthroughNames ||
!@event.Name.IsObfuscated(assemblyContext.GlobalContext.Options)) return @event.Name!;

var baseName = "event_" + assemblyContext.RewriteTypeRef(@event.EventType?.ToTypeSignature()).GetUnmangledName(@event.DeclaringType);

countsByBaseName.TryGetValue(baseName, out var index);
countsByBaseName[baseName] = index + 1;

var unmangleEventName = baseName + "_" + index;

if (assemblyContext.GlobalContext.Options.RenameMap.TryGetValue(
declaringType.GetNamespacePrefix() + "." + declaringType.Name + "::" + unmangleEventName, out var newNameByType))
{
unmangleEventName = newNameByType;
}
else if (assemblyContext.GlobalContext.Options.RenameMap.TryGetValue(
declaringType.GetNamespacePrefix() + "::" + unmangleEventName, out var newName))
{
unmangleEventName = newName;
}

return unmangleEventName;
}
}
25 changes: 25 additions & 0 deletions Il2CppInterop.Generator/Passes/Pass80UnstripMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ public static void DoPass(RewriteGlobalContext context)
var property = GetOrCreateProperty(unityMethod, newMethod);
property.SetMethod = newMethod;
}
else if (unityMethod.IsAddMethod)
{
var @event = GetOrCreateEvent(unityMethod, newMethod);
@event.AddMethod = newMethod;
}
else if (unityMethod.IsRemoveMethod)
{
var @event = GetOrCreateEvent(unityMethod, newMethod);
@event.RemoveMethod = newMethod;
}

var paramsMethod = context.CreateParamsMethod(unityMethod, newMethod, imports,
type => ResolveTypeInNewAssemblies(context, type, imports));
Expand Down Expand Up @@ -171,6 +181,21 @@ private static PropertyDefinition GetOrCreateProperty(MethodDefinition unityMeth
return newProperty;
}

private static EventDefinition GetOrCreateEvent(MethodDefinition unityMethod, MethodDefinition newMethod)
{
var unityEvent =
unityMethod.DeclaringType!.Events.Single(
it => it.AddMethod == unityMethod || it.RemoveMethod == unityMethod);
var newEvent = newMethod.DeclaringType!.Events.SingleOrDefault(it => it.Name == unityEvent.Name);
if (newEvent == null)
{
newEvent = new EventDefinition(unityEvent.Name, unityEvent.Attributes, newMethod.Signature!.ParameterTypes.Single().ToTypeDefOrRef());
newMethod.DeclaringType.Events.Add(newEvent);
}

return newEvent;
}

internal static TypeSignature? ResolveTypeInNewAssemblies(RewriteGlobalContext context, TypeSignature? unityType,
RuntimeAssemblyReferences imports)
{
Expand Down
5 changes: 5 additions & 0 deletions Il2CppInterop.Generator/Runners/InteropAssemblyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public void Run(GeneratorOptions options)
Pass70GenerateProperties.DoPass(rewriteContext);
}

using (new TimingCookie("Creating events"))
{
Pass71GenerateEvents.DoPass(rewriteContext);
}

if (options.UnityBaseLibsDir != null)
{
using (new TimingCookie("Unstripping types"))
Expand Down

0 comments on commit e131037

Please sign in to comment.