mirror of
https://github.com/aelurum/AssetStudio.git
synced 2025-07-14 02:54:16 -04:00
clean up TypeDefinitionConverter code
This commit is contained in:
@ -0,0 +1,50 @@
|
||||
// Unity C# reference source
|
||||
// Copyright (c) Unity Technologies. For terms of use, see
|
||||
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
|
||||
|
||||
using Mono.Cecil;
|
||||
|
||||
namespace Unity.CecilTools.Extensions
|
||||
{
|
||||
static class MethodDefinitionExtensions
|
||||
{
|
||||
public static bool SameAs(this MethodDefinition self, MethodDefinition other)
|
||||
{
|
||||
// FIXME: should be able to compare MethodDefinition references directly
|
||||
return self.FullName == other.FullName;
|
||||
}
|
||||
|
||||
public static string PropertyName(this MethodDefinition self)
|
||||
{
|
||||
return self.Name.Substring(4);
|
||||
}
|
||||
|
||||
public static bool IsConversionOperator(this MethodDefinition method)
|
||||
{
|
||||
if (!method.IsSpecialName)
|
||||
return false;
|
||||
|
||||
return method.Name == "op_Implicit" || method.Name == "op_Explicit";
|
||||
}
|
||||
|
||||
public static bool IsSimpleSetter(this MethodDefinition original)
|
||||
{
|
||||
return original.IsSetter && original.Parameters.Count == 1;
|
||||
}
|
||||
|
||||
public static bool IsSimpleGetter(this MethodDefinition original)
|
||||
{
|
||||
return original.IsGetter && original.Parameters.Count == 0;
|
||||
}
|
||||
|
||||
public static bool IsSimplePropertyAccessor(this MethodDefinition method)
|
||||
{
|
||||
return method.IsSimpleGetter() || method.IsSimpleSetter();
|
||||
}
|
||||
|
||||
public static bool IsDefaultConstructor(MethodDefinition m)
|
||||
{
|
||||
return m.IsConstructor && !m.IsStatic && m.Parameters.Count == 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
// Unity C# reference source
|
||||
// Copyright (c) Unity Technologies. For terms of use, see
|
||||
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
|
||||
|
||||
using System;
|
||||
using Mono.Cecil;
|
||||
|
||||
namespace Unity.CecilTools.Extensions
|
||||
{
|
||||
public static class ResolutionExtensions
|
||||
{
|
||||
public static TypeDefinition CheckedResolve(this TypeReference type)
|
||||
{
|
||||
return Resolve(type, reference => reference.Resolve());
|
||||
}
|
||||
|
||||
public static MethodDefinition CheckedResolve(this MethodReference method)
|
||||
{
|
||||
return Resolve(method, reference => reference.Resolve());
|
||||
}
|
||||
|
||||
private static TDefinition Resolve<TReference, TDefinition>(TReference reference, Func<TReference, TDefinition> resolve)
|
||||
where TReference : MemberReference
|
||||
where TDefinition : class, IMemberDefinition
|
||||
{
|
||||
if (reference.Module == null)
|
||||
throw new ResolutionException(reference);
|
||||
|
||||
var definition = resolve(reference);
|
||||
if (definition == null)
|
||||
throw new ResolutionException(reference);
|
||||
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
// Unity C# reference source
|
||||
// Copyright (c) Unity Technologies. For terms of use, see
|
||||
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Mono.Cecil;
|
||||
|
||||
namespace Unity.CecilTools.Extensions
|
||||
{
|
||||
public static class TypeDefinitionExtensions
|
||||
{
|
||||
public static bool IsSubclassOf(this TypeDefinition type, string baseTypeName)
|
||||
{
|
||||
var baseType = type.BaseType;
|
||||
if (baseType == null)
|
||||
return false;
|
||||
if (baseType.FullName == baseTypeName)
|
||||
return true;
|
||||
|
||||
var baseTypeDef = baseType.Resolve();
|
||||
if (baseTypeDef == null)
|
||||
return false;
|
||||
|
||||
return IsSubclassOf(baseTypeDef, baseTypeName);
|
||||
}
|
||||
|
||||
public static bool IsSubclassOf(this TypeDefinition type, params string[] baseTypeNames)
|
||||
{
|
||||
var baseType = type.BaseType;
|
||||
if (baseType == null)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < baseTypeNames.Length; i++)
|
||||
if (baseType.FullName == baseTypeNames[i])
|
||||
return true;
|
||||
|
||||
var baseTypeDef = baseType.Resolve();
|
||||
if (baseTypeDef == null)
|
||||
return false;
|
||||
|
||||
return IsSubclassOf(baseTypeDef, baseTypeNames);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
// Unity C# reference source
|
||||
// Copyright (c) Unity Technologies. For terms of use, see
|
||||
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
|
||||
|
||||
using Mono.Cecil;
|
||||
|
||||
namespace Unity.CecilTools.Extensions
|
||||
{
|
||||
public static class TypeReferenceExtensions
|
||||
{
|
||||
public static string SafeNamespace(this TypeReference type)
|
||||
{
|
||||
if (type.IsGenericInstance)
|
||||
return ((GenericInstanceType)type).ElementType.SafeNamespace();
|
||||
if (type.IsNested)
|
||||
return type.DeclaringType.SafeNamespace();
|
||||
return type.Namespace;
|
||||
}
|
||||
|
||||
public static bool IsAssignableTo(this TypeReference typeRef, string typeName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (typeRef.IsGenericInstance)
|
||||
return ElementType.For(typeRef).IsAssignableTo(typeName);
|
||||
|
||||
if (typeRef.FullName == typeName)
|
||||
return true;
|
||||
|
||||
return typeRef.CheckedResolve().IsSubclassOf(typeName);
|
||||
}
|
||||
catch (AssemblyResolutionException) // If we can't resolve our typeref or one of its base types,
|
||||
{ // let's assume it is not assignable to our target type
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsEnum(this TypeReference type)
|
||||
{
|
||||
return type.IsValueType && !type.IsPrimitive && type.CheckedResolve().IsEnum;
|
||||
}
|
||||
|
||||
public static bool IsStruct(this TypeReference type)
|
||||
{
|
||||
return type.IsValueType && !type.IsPrimitive && !type.IsEnum() && !IsSystemDecimal(type);
|
||||
}
|
||||
|
||||
private static bool IsSystemDecimal(TypeReference type)
|
||||
{
|
||||
return type.FullName == "System.Decimal";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user