Exemplo n.º 1
0
        public override ReferenceCollection GetReferences(ITreeNode element, ReferenceCollection oldReferences)
        {
            if (ResolveUtil.CheckThatAllReferencesBelongToElement <UnityEventFunctionReference>(oldReferences, element))
            {
                return(oldReferences);
            }

            var literal = element as ILiteralExpression;

            if (literal == null || !literal.ConstantValue.IsString())
            {
                return(ReferenceCollection.Empty);
            }

            if (!IsStringLiteralFirstArgument(literal))
            {
                return(ReferenceCollection.Empty);
            }

            var invocationExpression = literal.GetContainingNode <IInvocationExpression>();
            var invocationReference  = invocationExpression?.Reference;
            var invokedMethod        = invocationReference?.Resolve().DeclaredElement as IMethod;

            if (invokedMethod == null)
            {
                return(ReferenceCollection.Empty);
            }

            var isInvokedFunction = InvokeMethodNames.Contains(invokedMethod.ShortName);
            var isCoroutine       = IsCoroutine(invokedMethod);

            if (isInvokedFunction || isCoroutine)
            {
                var containingType = invokedMethod.GetContainingType();
                if (containingType != null && Equals(containingType.GetClrName(), KnownTypes.MonoBehaviour))
                {
                    var targetType = invocationExpression.ExtensionQualifier?.GetExpressionType()
                                     .ToIType()?.GetTypeElement()
                                     ??
                                     literal.GetContainingNode <IMethodDeclaration>()?
                                     .DeclaredElement?.GetContainingType();

                    if (targetType != null)
                    {
                        var methodSignature = GetMethodSignature(invocationExpression, invokedMethod, isCoroutine);
                        var reference       = new UnityEventFunctionReference(targetType, literal, methodSignature);
                        return(new ReferenceCollection(reference));
                    }
                }
            }

            return(ReferenceCollection.Empty);
        }
        public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            var literal = element as ILiteralExpression;

            if (literal != null && literal.ConstantValue.IsString())
            {
                var argument       = CSharpArgumentNavigator.GetByValue(literal as ICSharpExpression);
                var argumentsOwner = CSharpArgumentsOwnerNavigator.GetByArgument(argument);
                if (argumentsOwner != null && argumentsOwner.ArgumentsEnumerable.FirstOrDefault() != argument)
                {
                    return(EmptyArray <IReference> .Instance);
                }

                var invocationExpression = literal.GetContainingNode <IInvocationExpression>();
                var invocationReference  = invocationExpression?.Reference;
                var invokedMethod        = invocationReference?.Resolve().DeclaredElement as IMethod;
                if (invokedMethod != null && DoesMethodReferenceFunction(invokedMethod))
                {
                    var containingType = invokedMethod.GetContainingType();
                    if (containingType != null && Equals(containingType.GetClrName(), KnownTypes.MonoBehaviour))
                    {
                        var targetType = invocationExpression.ExtensionQualifier?.GetExpressionType().ToIType()?.GetTypeElement()
                                         ?? literal.GetContainingNode <IMethodDeclaration>()?.DeclaredElement?.GetContainingType();

                        // TODO: Check if currentType is derived from MonoBehaviour?
                        if (targetType != null)
                        {
                            IReference reference = new UnityEventFunctionReference(targetType, literal);

                            return(oldReferences != null && oldReferences.Length == 1 &&
                                   Equals(oldReferences[0], reference)
                                ? oldReferences
                                : new[] { reference });
                        }
                    }
                }
            }

            return(EmptyArray <IReference> .Instance);
        }