Skip to content

Commit

Permalink
Lib: Improve perf of MetadataUsage#IsValid
Browse files Browse the repository at this point in the history
  • Loading branch information
SamboyCoding committed Dec 12, 2024
1 parent 4def87e commit d297c56
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
2 changes: 1 addition & 1 deletion LibCpp2IL/Metadata/Il2CppMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Il2CppMetadata : ClassReadingBinaryReader
private Il2CppParameterDefaultValue[] parameterDefaultValues;
public Il2CppPropertyDefinition[] propertyDefs;
public List<Il2CppCustomAttributeTypeRange> attributeTypeRanges;
private Il2CppStringLiteral[] stringLiterals;
public Il2CppStringLiteral[] stringLiterals;
public Il2CppMetadataUsageList[] metadataUsageLists;
private Il2CppMetadataUsagePair[] metadataUsagePairs;
public Il2CppRGCTXDefinition[] RgctxDefinitions; //Moved to binary in v24.2
Expand Down
57 changes: 20 additions & 37 deletions LibCpp2IL/MetadataUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,27 @@ public class MetadataUsage(MetadataUsageType type, ulong offset, uint value)

public uint RawValue => value;

public object Value
{
get
public object Value =>
Type switch
{
switch (Type)
{
case MetadataUsageType.Type:
case MetadataUsageType.TypeInfo:
return AsType();
case MetadataUsageType.MethodDef:
return AsMethod();
case MetadataUsageType.FieldInfo:
return AsField();
case MetadataUsageType.StringLiteral:
return AsLiteral();
case MetadataUsageType.MethodRef:
return AsGenericMethodRef();
default:
throw new ArgumentOutOfRangeException();
}
}
}
MetadataUsageType.Type or MetadataUsageType.TypeInfo => AsType(),
MetadataUsageType.MethodDef => AsMethod(),
MetadataUsageType.FieldInfo => AsField(),
MetadataUsageType.StringLiteral => AsLiteral(),
MetadataUsageType.MethodRef => AsGenericMethodRef(),
_ => throw new ArgumentOutOfRangeException()
};

public bool IsValid =>
Type switch
{
MetadataUsageType.Type or MetadataUsageType.TypeInfo => value < LibCpp2IlMain.Binary!.NumTypes,
MetadataUsageType.MethodDef => value < LibCpp2IlMain.TheMetadata!.methodDefs.Length,
MetadataUsageType.FieldInfo => value < LibCpp2IlMain.TheMetadata!.fieldRefs.Length,
MetadataUsageType.StringLiteral => value < LibCpp2IlMain.TheMetadata!.stringLiterals.Length,
MetadataUsageType.MethodRef => value < LibCpp2IlMain.Binary!.AllGenericMethodSpecs.Length,
_ => false
};

public Il2CppTypeReflectionData AsType()
{
Expand Down Expand Up @@ -155,22 +154,6 @@ public override string ToString()
return $"Metadata Usage {{type={Type}, Value={Value}}}";
}

public bool IsValid
{
get
{
try
{
var _ = Value;
return true;
}
catch (Exception)
{
return false;
}
}
}


public static MetadataUsage? DecodeMetadataUsage(ulong encoded, ulong address)
{
Expand Down

0 comments on commit d297c56

Please sign in to comment.