4 Commits
Author SHA1 Message Date
ds5678 c8e1b8809c Bump version 2024-09-07 12:50:59 -07:00
ds5678 a0c48e0216 Backing field improvements
* More strict identification
* Add extensions for fields, events, and properties
2024-09-07 12:50:18 -07:00
ds5678 1df36de7df Field extensions 2024-09-01 08:21:39 -07:00
ds5678 89d8b4a7cd Event and property extensions 2024-09-01 08:15:23 -07:00
5 changed files with 165 additions and 5 deletions

No files matched your search

@@ -0,0 +1,53 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
namespace AssetRipper.CIL;
public static class EventDefinitionExtensions
{
public static bool IsInstance(this EventDefinition eventDefinition)
{
return (eventDefinition.AddMethod?.IsInstance() ?? false)
|| (eventDefinition.RemoveMethod?.IsInstance() ?? false);
}
public static bool IsStatic(this EventDefinition eventDefinition)
{
return (eventDefinition.AddMethod?.IsStatic ?? false)
|| (eventDefinition.RemoveMethod?.IsStatic ?? false);
}
public static bool HasAddMethod(this EventDefinition eventDefinition)
{
return eventDefinition.AddMethod is not null;
}
public static bool HasRemoveMethod(this EventDefinition eventDefinition)
{
return eventDefinition.RemoveMethod is not null;
}
public static bool HasFireMethod(this EventDefinition eventDefinition)
{
return eventDefinition.FireMethod is not null;
}
public static bool HasBackingField(this EventDefinition eventDefinition, out FieldDefinition? backingField)
{
TypeSignature? eventType = eventDefinition.EventType?.ToTypeSignature();
if (eventType is not null)
{
foreach (FieldDefinition field in eventDefinition.DeclaringType?.Fields ?? [])
{
if (field.IsPrivate && field.Name == eventDefinition.Name && SignatureComparer.Default.Equals(field.Signature?.FieldType, eventType))
{
backingField = field;
return true;
}
}
}
backingField = null;
return false;
}
}
@@ -0,0 +1,60 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using System.Diagnostics.CodeAnalysis;
namespace AssetRipper.CIL;
public static class FieldDefinitionExtensions
{
public static bool IsInstance(this FieldDefinition field)
{
return !field.IsStatic;
}
private static bool IsPropertyBackingField(this FieldDefinition field, [NotNullWhen(true)] out string? propertyName)
{
string? name = field.Name;
if (name is ['<', ..] && name.EndsWith(">k__BackingField", StringComparison.Ordinal))
{
propertyName = name[1..^16];
return true;
}
else
{
propertyName = null;
return false;
}
}
public static bool IsPropertyBackingField(this FieldDefinition field, [NotNullWhen(true)] out PropertyDefinition? property)
{
if (field.IsPropertyBackingField(out string? propertyName))
{
property = field.DeclaringType?.Properties.FirstOrDefault(p => p.Name == propertyName);
return property is not null
&& field.IsStatic == property.IsStatic()
&& SignatureComparer.Default.Equals(field.Signature?.FieldType, property.Signature?.ReturnType);
}
else
{
property = null;
return false;
}
}
public static bool IsEventBackingField(this FieldDefinition field, [NotNullWhen(true)] out EventDefinition? @event)
{
if (field.IsPrivate)
{
@event = field.DeclaringType?.Events.FirstOrDefault(e => e.Name == field.Name);
return @event is not null
&& field.IsStatic == @event.IsStatic()
&& SignatureComparer.Default.Equals(@event.EventType?.ToTypeSignature(), field.Signature?.FieldType);
}
else
{
@event = null;
return false;
}
}
}
@@ -87,8 +87,9 @@ public static class MethodDefinitionExtensions
string backingFieldName = $"<{method.Name.Value[4..]}>k__BackingField";
FieldDefinition? field = method.DeclaringType.Fields.FirstOrDefault(f => f.Name == backingFieldName);
if (field is null
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType)
|| field.IsStatic != method.IsStatic)
|| !field.IsPrivate
|| field.IsStatic != method.IsStatic
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Signature?.ReturnType))
{
backingField = null;
return false;
@@ -116,8 +117,9 @@ public static class MethodDefinitionExtensions
string backingFieldName = $"<{method.Name.Value[4..]}>k__BackingField";
FieldDefinition? field = method.DeclaringType.Fields.FirstOrDefault(f => f.Name == backingFieldName);
if (field is null
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType)
|| field.IsStatic != method.IsStatic)
|| !field.IsPrivate
|| field.IsStatic != method.IsStatic
|| !SignatureComparer.Default.Equals(field.Signature?.FieldType, method.Parameters[0].ParameterType))
{
backingField = null;
return false;
@@ -0,0 +1,45 @@
using AsmResolver.DotNet;
using AsmResolver.DotNet.Signatures;
using System.Diagnostics.CodeAnalysis;
namespace AssetRipper.CIL;
public static class PropertyDefinitionExtensions
{
public static bool HasBackingField(this PropertyDefinition property, [NotNullWhen(true)] out FieldDefinition? backingField)
{
string backingFieldName = $"<{property.Name}>k__BackingField";
backingField = property.DeclaringType?.Fields.FirstOrDefault(f => f.Name == backingFieldName);
if (backingField is null
|| !backingField.IsPrivate
|| backingField.IsStatic != property.IsStatic()
|| !SignatureComparer.Default.Equals(backingField.Signature?.FieldType, property.Signature?.ReturnType))
{
backingField = null;
return false;
}
else
{
return true;
}
}
public static bool HasGetMethod(this PropertyDefinition property)
{
return property.GetMethod is not null;
}
public static bool HasSetMethod(this PropertyDefinition property)
{
return property.SetMethod is not null;
}
public static bool HasBothAccessors(this PropertyDefinition property)
{
return property.HasGetMethod() && property.HasSetMethod();
}
public static bool IsInstance(this PropertyDefinition property) => property.Signature?.HasThis ?? false;
public static bool IsStatic(this PropertyDefinition property) => !property.Signature?.HasThis ?? false;
}
+1 -1
View File
@@ -6,7 +6,7 @@
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
<Version>1.1.1</Version>
<Version>1.1.2</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Copyright>Copyright (c) 2024 ds5678</Copyright>
<Authors>ds5678</Authors>