Exemplo n.º 1
0
    void HandleDrawing()
    {
        TimeTillReload -= Time.deltaTime;
        if (TimeTillReload <= 0)
        {
            loopState = LoopState.TERMINATED;
            //Play The "empty gun" sound
            loopManager.PlayEmpty();
            return;
        }

        loopTimer         = Time.time + loopDuration;
        currMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (Vector2.Distance(currMousePosition, prevMousePosition) > minDistanceMoved)
        {
            segments = CalculateSegments();
            winding  = LoopUtils.CalculateWinding(segments);

            if (Time.time > mouseDetectionTimer)
            {
                mouseDetectionTimer = Time.time + mouseDetectionInterval;
                if (points.Count >= maxSegments && points.Count > 0)
                {
                    points.RemoveAt(0);
                }
                points.Add(currMousePosition);
            }
            prevMousePosition = currMousePosition;
        }
    }
Exemplo n.º 2
0
    public static void ProcessOutRefInvocation(ISymbol symbol, BlockSyntax block, SemanticModel semanticModel, CancellationToken cancellationToken, RefCounterStatus status)
    {
        using (var pooledInvocations = InvocationWalker.Borrow(block))
        {
            foreach (var invocation in pooledInvocations.Invocations)
            {
                foreach (ArgumentSyntax arg in invocation.ArgumentList.Arguments)
                {
                    if (arg.RefOrOutKeyword.IsKind(SyntaxKind.OutKeyword) || arg.RefOrOutKeyword.IsKind(SyntaxKind.RefKeyword))
                    {
                        var     sym       = ModelExtensions.GetSymbolInfo(semanticModel, arg.Expression, cancellationToken);
                        ISymbol argSymbol = sym.Symbol;
                        if (symbol.Equals(argSymbol))
                        {
                            status.RemainRef("init from out/ref call", arg.GetLocation());
                            status.IncAssignCounter("out/ref call parameter", arg.GetLocation());

                            if (LoopUtils.HasLoopBetween(invocation, block))
                            {
                                status.Skip("loop between ref/out call and var block", arg.GetLocation());
                            }
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 3
0
        public static int DelRefInvocationCount(ISymbol symbol, BlockSyntax block, SemanticModel semanticModel, CancellationToken cancellationToken, RefCounterStatus status)
        {
            int      count = 0;
            Location loc   = Location.None;;

            using (var pooledInvocations = InvocationWalker.Borrow(block))
            {
                foreach (var invocation in pooledInvocations.Invocations)
                {
                    var isFromSymbol = IsInvokeFrom(symbol, semanticModel, cancellationToken, invocation);
                    var method       = semanticModel.GetSymbolSafe(invocation, cancellationToken) as IMethodSymbol;
                    if (isFromSymbol && method?.ReceiverType != null)
                    {
                        if (method.ReceiverType.Is(KnownSymbol.RefCounter) &&
                            method?.Name == KnownSymbol.ReleaseReference)
                        {
                            count++;
                            loc = invocation.GetLocation();
                            if (LoopUtils.HasLoopBetween(invocation, block))
                            {
                                status.Skip("loop between ReleaseReference call and var block", invocation.GetLocation());
                            }
                        }
                    }
                    else
                    {
                        if (method?.Name == KnownSymbol.ReleaseReference)
                        {
                            foreach (ArgumentSyntax arg in invocation.ArgumentList.Arguments)
                            {
                                var     sym       = ModelExtensions.GetSymbolInfo(semanticModel, arg.Expression, cancellationToken);
                                ISymbol argSymbol = sym.Symbol;
                                if (symbol.Equals(argSymbol))
                                {
                                    count++;
                                    loc = invocation.GetLocation();

                                    if (LoopUtils.HasLoopBetween(invocation, block))
                                    {
                                        status.Skip("loop between ReleaseReference call and var block", arg.GetLocation());
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (count > 1)
            {
                status.Skip("more than one ReleaseReference", loc);
            }
            else if (count == 1)
            {
                status.ReleaseReference("call ReleaseReference", loc);
            }

            return(count);
        }
    public static void ProcessRightOfAssignmentToField(
        ISymbol variable,
        BlockSyntax block,
        SemanticModel semanticModel,
        CancellationToken cancellationToken,
        RefCounterStatus status)
    {
        List <AssignmentExpressionSyntax> rc = new List <AssignmentExpressionSyntax>();

        block?.TryGetAssignment(variable, semanticModel, cancellationToken, rc);
        int      count = 0;
        ISymbol  field = null;
        Location loc   = Location.None;

        foreach (AssignmentExpressionSyntax assignment in rc)
        {
            var classDef    = block.FirstAncestor <ClassDeclarationSyntax>();
            var classSymbol = CSharpExtensions.GetDeclaredSymbol(semanticModel, classDef, cancellationToken);

            if (IsRightOfAssignmentToField(semanticModel, cancellationToken, assignment.Left, out field))
            {
                count++;
                loc = assignment.GetLocation();
                if (!field.ContainingType.Equals(classSymbol))
                {
                    status.Skip("assigned to field/property of other class", loc);
                }

                if (LoopUtils.HasLoopBetween(assignment, block))
                {
                    status.Skip("loop between assignment to field/property and var block", loc);
                }
            }
        }

        if (count == 1)
        {
            status.ReleaseReference("assign to class field/property", loc);

            var methodBlock = block?.FirstAncestorOrSelf <BlockSyntax>();
            if (methodBlock == null)
            {
                return;
            }

            if (RightSideOfAssignmentCount(field, methodBlock, semanticModel, cancellationToken) > 0)
            {
                status.Skip("var assigned to field/property, which is assigned to others", loc);
            }


            ChangeReferenceMethodUtils.ProcessIncDelRefInvocation(field, methodBlock, semanticModel, cancellationToken, status);
            ReturnUtils.ProcessReturnStatement(field, methodBlock, semanticModel, cancellationToken, status);
        }
        else if (count > 1)
        {
            status.Skip("multiple assign to field/property", loc);
        }
    }
Exemplo n.º 5
0
        private bool CheckLineOfSight(Coordinates to)
        {
            bool result = true;

            LoopUtils.Line(Agent.Owner.GetTilePosition().ToPoint(), to.ToPoint(), (p) =>
            {
                result &= !Agent.Owner.Level.GetTile(p.X, p.Y).BlockLineOfSight;
            });
            return(result);
        }
Exemplo n.º 6
0
    void HandleEffect()
    {
        segments = CalculateSegments();
        winding  = LoopUtils.CalculateWinding(segments);

        if (Time.time > loopTimer)
        {
            if (points.Count > 0)
            {
                points.RemoveAt(0);
            }
            if (points.Count == 0)
            {
                loopState = LoopState.TERMINATED;
            }
        }
    }
Exemplo n.º 7
0
    private void CheckLoopables()
    {
        loopManager.targets.Clear();

        foreach (Loopable loopable in containedLoopables.Keys)
        {
            loopable.isActive = false;
        }
        containedLoopables.Clear();

        foreach (LoopSegment segment in segments)
        {
            Vector2      midPoint = (segment.start + segment.end) / 2;
            Vector2      dir      = LoopUtils.CrossProduct(segment.start, segment.end, winding);
            RaycastHit2D hit      = Physics2D.Raycast(midPoint, dir, maxLoopRadius, loopables);

            if (hit.collider != null)
            {
                Debug.DrawLine(midPoint, hit.point, Color.green);
                Loopable hitLoopable = hit.collider.GetComponent <Loopable>();
                if (containedLoopables.ContainsKey(hitLoopable))
                {
                    containedLoopables[hitLoopable]++;
                }
                else
                {
                    containedLoopables.Add(hitLoopable, 1);
                }
            }
            else
            {
                Debug.DrawLine(midPoint, midPoint + dir.normalized * maxLoopRadius, Color.red);
            }
        }

        foreach (Loopable loopable in containedLoopables.Keys)
        {
            float currentRatio = containedLoopables[loopable] / segments.Count;
            if (currentRatio >= hitThreshold)
            {
                loopManager.targets.Add(loopable);
                loopable.onLoopActivate();
            }
        }
    }
Exemplo n.º 8
0
        private static int IncRefInvocationCount(ISymbol symbol, BlockSyntax block, SemanticModel semanticModel, CancellationToken cancellationToken, RefCounterStatus status)
        {
            int      count = 0;
            Location loc   = Location.None;;

            using (var pooledInvocations = InvocationWalker.Borrow(block))
            {
                foreach (var invocation in pooledInvocations.Invocations)
                {
                    var isFromSymbol = IsInvokeFrom(symbol, semanticModel, cancellationToken, invocation);

                    var method = semanticModel.GetSymbolSafe(invocation, cancellationToken) as IMethodSymbol;
                    if (isFromSymbol && method.ReceiverType != null)
                    {
                        if (method.ReceiverType.Is(KnownSymbol.RefCounter) &&
                            method?.Name == KnownSymbol.IncRef)
                        {
                            count++;
                            loc = invocation.GetLocation();
                            if (LoopUtils.HasLoopBetween(invocation, block))
                            {
                                status.Skip("loop between IncRef call and var block", loc);
                            }
                        }
                    }
                }
            }

            if (count > 1)
            {
                status.Skip("more than one IncRef", loc);
            }
            else if (count == 1)
            {
                status.IncRef("call IncRef", loc);
            }

            return(count);
        }
Exemplo n.º 9
0
    public static void ProcessAddedToCollection(
        ISymbol symbol, BlockSyntax block, SemanticModel semanticModel, CancellationToken cancellationToken, RefCounterStatus status)
    {
        int      count = 0;
        Location loc   = Location.None;

        using (var pooledInvocations = InvocationWalker.Borrow(block))
        {
            foreach (var invocation in pooledInvocations.Invocations)
            {
                bool isAddToCollection = false;
                var  method            = semanticModel.GetSymbolSafe(invocation, cancellationToken) as IMethodSymbol;
                if (method?.ReceiverType != null)
                {
                    if (!isAddToCollection)
                    {
                        isAddToCollection = (method.ReceiverType.Is(KnownSymbol.IDictionary) || method.ReceiverType.Is(KnownSymbol.ICollection)) &&
                                            method?.Name == "Add";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.QueueOfT) &&
                                            method?.Name == "Enqueue";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.Queue) &&
                                            method?.Name == "Enqueue";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.ListOfT) &&
                                            method?.Name == "Insert";
                    }
                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.IRefCounterContainerOfT) &&
                                            method?.Name == "Add";
                    }

                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.CollectionOfT) &&
                                            method?.Name == "Add";
                    }
                    if (!isAddToCollection)
                    {
                        isAddToCollection = method.ReceiverType.Is(KnownSymbol.LinkedListOfT) &&
                                            (method?.Name == "AddLast" || method?.Name == "AddFirst");
                    }
                }

                if (isAddToCollection)
                {
                    bool isInArg = false;
                    foreach (ArgumentSyntax arg in invocation.ArgumentList.Arguments)
                    {
                        var     sym       = ModelExtensions.GetSymbolInfo(semanticModel, arg.Expression, cancellationToken);
                        ISymbol argSymbol = sym.Symbol;
                        if (symbol.Equals(argSymbol))
                        {
                            isInArg = true;
                        }
                    }

                    if (isInArg)
                    {
                        count++;
                        loc = invocation.GetLocation();
                        if (LoopUtils.HasLoopBetween(invocation, block))
                        {
                            status.Skip("loop between add to collection and var block", invocation.GetLocation());
                        }
                    }
                }
            }
        }

        if (count == 1)
        {
            status.ReleaseReference("added to collection", loc);
        }
    }