<commit_before>﻿using Cauldron.Interception.Cecilator;
using System;
using System.Linq;

namespace Cauldron.Interception.Fody
{
    public sealed partial class ModuleWeaver
    {
        public static void ImplementAssignMethodAttribute(Builder builder, AssignMethodAttributeInfo[] assignMethodAttributeInfos, Field interceptorInstance, BuilderType contentType, ICode coder) =>
            ImplementAssignMethodAttribute(builder, assignMethodAttributeInfos,
                x => coder.Assign(coder.NewCode().Load(interceptorInstance).As(contentType), x.assignMethodAttributeInfo.AttributeField.Import()).NewObj(x.delegateCtor, x.method));

        public static void ImplementAssignMethodAttribute(Builder builder, AssignMethodAttributeInfo[] assignMethodAttributeInfos, LocalVariable interceptorInstance, BuilderType contentType, ICode coder) =>
            ImplementAssignMethodAttribute(builder, assignMethodAttributeInfos,
                x => coder.Assign(coder.NewCode().Load(interceptorInstance).As(contentType), x.assignMethodAttributeInfo.AttributeField.Import()).NewObj(x.delegateCtor, x.method));

        private static void ImplementAssignMethodAttribute(Builder builder, AssignMethodAttributeInfo[] assignMethodAttributeInfos,
                    Action<(Method delegateCtor, Method method, AssignMethodAttributeInfo assignMethodAttributeInfo)> @delegate)
        {
            if (@delegate == null)
                throw new ArgumentNullException(nameof(@delegate));

            foreach (var item in assignMethodAttributeInfos)
            {
                var method = item.TargetMethod;

                if (method == null)
                {
                    builder.Log(item.ThrowError ? LogTypes.Error : LogTypes.Info,
                        item.AttributedMethod,
                        $"Unable to find matching method for the interceptor '{item.AttributeField.OriginType.Fullname}'. The interceptor requires a method with the name '{item.TargetMethodName}' and a return type of '{item.TargetMethodReturnType}'.");
                    continue;
                }

                var delegateCtor = item.AttributeField.FieldType.IsGenericInstance ?
                    item.AttributeField.FieldType
                        .GetMethod(".ctor", true, new Type[] { typeof(object), typeof(IntPtr) })
                        .MakeGeneric(item.AttributeField.FieldType.GenericArguments().ToArray())
                        .Import() :
                    item.AttributeField.FieldType
                        .GetMethod(".ctor", true, new Type[] { typeof(object), typeof(IntPtr) })
                        .Import();

                builder.Log(LogTypes.Info, $"- Implementing AssignMethodAttribute for '{method.Name}'.");
                @delegate((delegateCtor, method, item));<commit_msg>Method for AssignMethodAttribute was not imported<commit_after>﻿using Cauldron.Interception.Cecilator;
using System;
using System.Linq;

namespace Cauldron.Interception.Fody
{
    public sealed partial class ModuleWeaver
    {
        public static void ImplementAssignMethodAttribute(Builder builder, AssignMethodAttributeInfo[] assignMethodAttributeInfos, Field interceptorInstance, BuilderType contentType, ICode coder) =>
            ImplementAssignMethodAttribute(builder, assignMethodAttributeInfos,
                x => coder.Assign(coder.NewCode().Load(interceptorInstance).As(contentType), x.assignMethodAttributeInfo.AttributeField.Import()).NewObj(x.delegateCtor, x.method));

        public static void ImplementAssignMethodAttribute(Builder builder, AssignMethodAttributeInfo[] assignMethodAttributeInfos, LocalVariable interceptorInstance, BuilderType contentType, ICode coder) =>
            ImplementAssignMethodAttribute(builder, assignMethodAttributeInfos,
                x => coder.Assign(coder.NewCode().Load(interceptorInstance).As(contentType), x.assignMethodAttributeInfo.AttributeField.Import()).NewObj(x.delegateCtor, x.method));

        private static void ImplementAssignMethodAttribute(Builder builder, AssignMethodAttributeInfo[] assignMethodAttributeInfos,
                    Action<(Method delegateCtor, Method method, AssignMethodAttributeInfo assignMethodAttributeInfo)> @delegate)
        {
            if (@delegate == null)
                throw new ArgumentNullException(nameof(@delegate));

            foreach (var item in assignMethodAttributeInfos)
            {
                var method = item.TargetMethod.Import();

                if (method == null)
                {
                    builder.Log(item.ThrowError ? LogTypes.Error : LogTypes.Info,
                        item.AttributedMethod,
                        $"Unable to find matching method for the interceptor '{item.AttributeField.OriginType.Fullname}'. The interceptor requires a method with the name '{item.TargetMethodName}' and a return type of '{item.TargetMethodReturnType}'.");
                    continue;
                }

                var delegateCtor = item.AttributeField.FieldType.IsGenericInstance ?
                    item.AttributeField.FieldType
                        .GetMethod(".ctor", true, new Type[] { typeof(object), typeof(IntPtr) })
                        .MakeGeneric(item.AttributeField.FieldType.GenericArguments().ToArray())
                        .Import() :
                    item.AttributeField.FieldType
                        .GetMethod(".ctor", true, new Type[] { typeof(object), typeof(IntPtr) })
                        .Import();

                builder.Log(LogTypes.Info, $"- Implementing AssignMethodAttribute for '{method.Name}'.");
                @delegate((delegateCtor, method, item));
{'fraction_non_alphanumeric': 0.09004405286343613, 'fraction_numerical': 0.0, 'mean_word_length': 2.7317554240631163, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5564291', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1288, 'n_words': 282}