private static ValueSerializer GetUsingAttribute(ModuleDefinition module, TypeDefinition holder, ICustomAttributeProvider attributeProvider, TypeReference fieldType, string fieldName, ValueSerializer valueSerializer) { if (attributeProvider.HasCustomAttribute <BitCountAttribute>()) { valueSerializer = BitCountFinder.GetSerializer(attributeProvider, fieldType); } if (attributeProvider.HasCustomAttribute <VarIntAttribute>()) { if (HasIntAttribute(valueSerializer)) { throw new VarIntException($"[VarInt] can't be used with [BitCount], [VarIntBlocks] or [BitCountFromRange]"); } valueSerializer = new VarIntFinder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <VarIntBlocksAttribute>()) { if (HasIntAttribute(valueSerializer)) { throw new VarIntBlocksException($"[VarIntBlocks] can't be used with [BitCount], [VarInt] or [BitCountFromRange]"); } valueSerializer = VarIntBlocksFinder.GetSerializer(attributeProvider, fieldType); } if (attributeProvider.HasCustomAttribute <BitCountFromRangeAttribute>()) { if (HasIntAttribute(valueSerializer)) { throw new BitCountFromRangeException($"[BitCountFromRange] can't be used with [BitCount], [VarInt] or [VarIntBlocks]"); } valueSerializer = BitCountFromRangeFinder.GetSerializer(attributeProvider, fieldType); } ZigZagFinder.CheckZigZag(attributeProvider, fieldType, ref valueSerializer); if (attributeProvider.HasCustomAttribute <FloatPackAttribute>()) { valueSerializer = new FloatPackFinder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <Vector2PackAttribute>()) { valueSerializer = new Vector2Finder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <Vector3PackAttribute>()) { valueSerializer = new Vector3Finder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <QuaternionPackAttribute>()) { valueSerializer = new QuaternionFinder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } return(valueSerializer); }
// This method will look in any stored attributes in the link context as well as the provider itself. public static bool HasCustomAttribute(this ICustomAttributeProvider provider, DerivedLinkContext context, string @namespace, string name) { if (provider?.HasCustomAttribute(@namespace, name) == true) { return(true); } return(context?.GetCustomAttributes(provider, @namespace, name)?.Count > 0); }
public static void CheckZigZag(ICustomAttributeProvider attributeProvider, TypeReference fieldType, ref ValueSerializer valueSerializer) { bool hasAttribute = attributeProvider.HasCustomAttribute <ZigZagEncodeAttribute>(); if (!hasAttribute) { return; } if (valueSerializer is BitCountSerializer bitCountSerializer) { ThrowIfUnsignedType(fieldType); valueSerializer = bitCountSerializer.CopyWithZigZag(); } else { throw new ZigZagException($"[ZigZagEncode] can only be used with [BitCount]"); } }
/// <summary> /// Determines if the attribute provider has got a specific MonoMod attribute. /// </summary> /// <returns><c>true</c> if the attribute provider contains the given MonoMod attribute, <c>false</c> otherwise.</returns> /// <param name="cap">Attribute provider to check.</param> /// <param name="attribute">Attribute.</param> public static bool HasMMAttribute(this ICustomAttributeProvider cap, string attribute) => cap.HasCustomAttribute("MonoMod.MonoMod" + attribute);
// [GeneratedCode] is not enough - e.g. it's used for anonymous delegates even if the // code itself is not tool/compiler generated static bool IsExport(ICustomAttributeProvider provider) { return(provider.HasCustomAttribute(Namespaces.Foundation, "ExportAttribute")); }
static bool HasGeneratedCodeAttribute(ICustomAttributeProvider provider) { return(provider.HasCustomAttribute("System.Runtime.CompilerServices", "CompilerGeneratedAttribute")); }
static bool HasGeneratedCodeAttribute(ICustomAttributeProvider provider) { return provider.HasCustomAttribute ("System.Runtime.CompilerServices", "CompilerGeneratedAttribute"); }
// [GeneratedCode] is not enough - e.g. it's used for anonymous delegates even if the // code itself is not tool/compiler generated static bool IsExport(ICustomAttributeProvider provider) { return provider.HasCustomAttribute (Namespaces.Foundation, "ExportAttribute"); }
/// <summary> /// Checks whether or not the <see cref="ICustomAttributeProvider"/> has at least one attribute of the given type. /// </summary> /// <param name="customAttributeProvider">The <see cref="ICustomAttributeProvider"/> to check.</param> /// <param name="attributeType">The type of attribute to check for.</param> /// <returns>True if the <see cref="ICustomAttributeProvider"/> has the attribute, false if not.</returns> /// <exception cref="ArgumentNullException"><paramref name="customAttributeProvider"/> or /// <paramref name="attributeType"/> is null.</exception> public static bool HasCustomAttribute(this ICustomAttributeProvider customAttributeProvider, Type attributeType) { return(customAttributeProvider.HasCustomAttribute(attributeType, true)); }
/// <summary> /// Checks whether or not the <see cref="ICustomAttributeProvider"/> has at least one attribute of the given type. /// </summary> /// <typeparam name="T">The type of attribute to check for.</typeparam> /// <param name="customAttributeProvider">The <see cref="ICustomAttributeProvider"/> to check.</param> /// <returns>True if the <see cref="ICustomAttributeProvider"/> has the attribute, false if not.</returns> /// <exception cref="ArgumentNullException"><paramref name="customAttributeProvider"/> is null.</exception> public static bool HasCustomAttribute <T>(this ICustomAttributeProvider customAttributeProvider) { return(customAttributeProvider.HasCustomAttribute <T>(true)); }
/// <summary> /// /// </summary> /// <param name="module">dll being weaved</param> /// <param name="holder">class that will old any packer functions</param> /// <param name="attributeProvider">a field or param that might have attribute</param> /// <param name="fieldType">type of the field or param with the attribute</param> /// <param name="fieldName">name that will be used for packer field</param> /// <param name="writers"></param> /// <param name="readers"></param> /// <returns></returns> /// <exception cref="ValueSerializerException">Throws when attribute is used incorrectly</exception> /// <exception cref="SerializeFunctionException">Throws when can not generate read or write function</exception> /// <exception cref="InvalidOperationException">Throws when <paramref name="holder"/> is not in <paramref name="module"/></exception> public static ValueSerializer GetSerializer(ModuleDefinition module, TypeDefinition holder, ICustomAttributeProvider attributeProvider, TypeReference fieldType, string fieldName, Writers writers, Readers readers) { if (holder.Module != module) { throw new InvalidOperationException($"{holder.Name} was not in the weaving module, holderModule: {holder.Module}, weaver Module: {module}"); } // Store result in variable but DONT return early // We need to check if other attributes are also used // if user adds 2 attributes that dont work together weaver should then throw error ValueSerializer valueSerializer = null; bool HasIntAttribute() => valueSerializer != null && valueSerializer.IsIntType; if (attributeProvider.HasCustomAttribute <BitCountAttribute>()) { valueSerializer = BitCountFinder.GetSerializer(attributeProvider, fieldType); } if (attributeProvider.HasCustomAttribute <VarIntAttribute>()) { if (HasIntAttribute()) { throw new VarIntException($"[VarInt] can't be used with [BitCount], [VarIntBlocks] or [BitCountFromRange]"); } valueSerializer = new VarIntFinder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <VarIntBlocksAttribute>()) { if (HasIntAttribute()) { throw new VarIntBlocksException($"[VarIntBlocks] can't be used with [BitCount], [VarInt] or [BitCountFromRange]"); } valueSerializer = VarIntBlocksFinder.GetSerializer(attributeProvider, fieldType); } if (attributeProvider.HasCustomAttribute <BitCountFromRangeAttribute>()) { if (HasIntAttribute()) { throw new BitCountFromRangeException($"[BitCountFromRange] can't be used with [BitCount], [VarInt] or [VarIntBlocks]"); } valueSerializer = BitCountFromRangeFinder.GetSerializer(attributeProvider, fieldType); } ZigZagFinder.CheckZigZag(attributeProvider, fieldType, ref valueSerializer); if (attributeProvider.HasCustomAttribute <FloatPackAttribute>()) { valueSerializer = new FloatPackFinder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <Vector2PackAttribute>()) { valueSerializer = new Vector2Finder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <Vector3PackAttribute>()) { valueSerializer = new Vector3Finder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (attributeProvider.HasCustomAttribute <QuaternionPackAttribute>()) { valueSerializer = new QuaternionFinder().GetSerializer(module, holder, attributeProvider, fieldType, fieldName); } if (valueSerializer == null) { valueSerializer = FindSerializeFunctions(writers, readers, fieldType); } return(valueSerializer); }
static bool HasGeneratedCodeAttribute(ICustomAttributeProvider provider, DerivedLinkContext context) { return(provider.HasCustomAttribute(context, "System.Runtime.CompilerServices", "CompilerGeneratedAttribute")); }
public static bool HasCustomAttribute <T>(this ICustomAttributeProvider cap, bool inherit = false) where T : Attribute => cap.HasCustomAttribute(typeof(T), inherit);