Example #1
0
        void RunImpl()
        {
            IStep step;
            Delay delay;
            Macro macroStep;

            InputWrapper[] release;

            int  elapsedMs;
            int  releaseIndex;
            long timestamp;

            Macro macro = macroStack.Peek();

            while (macroStack.Count > 0)
            {
                step = macro.CurrentStep;
                if (step == null)
                {
                    macroStack.Pop();
                    macro = macroStack.Peek();
                }
                else
                {
                    if (!step.IsEnabled)
                    {
                        continue;
                    }

                    elapsedMs = (int)stopwatch.ElapsedMilliseconds;
                    timestamp = step.Timestamp;
                    if (timestamp > 0 && timestamp + step.Cooldown < elapsedMs)
                    {
                        continue;
                    }

                    step.Timestamp = elapsedMs;

                    System.Diagnostics.Debug.WriteLine(step.ToString());

                    delay = step as Delay;
                    if (delay != null)
                    {
                        timer.Interval = delay.Milliseconds;
                        timer.Start();
                        return;
                    }
                    else
                    {
                        macroStep = step as Macro;
                        if (macroStep != null)
                        {
                            macroStep.ResetSteps();
                            macroStack.Push(macroStep);
                            macro = macroStep;
                        }
                        else
                        {
                            release = step.Run();
                            if (release != null)
                            {
                                if (releaseLookup.TryGetValue(release[0], out releaseIndex))
                                {
                                    releaseList[releaseIndex] = null;
                                }

                                releaseLookup[release[0]] = releaseList.Count;
                                releaseList.Add(release);
                            }
                        }
                    }
                }
            }

            System.Diagnostics.Debug.WriteLine("");

            Release();
            return;
        }
Example #2
0
        void Run()
        {
            IStep istep;
            Step step;
            Delay delay;
            bool stepEnabled;
            long elapsedMs;
            InputWrapper[] release;
            int releaseIndex;
            Macro macro;
            int loopCount;
            int currentLoop;

            while (running)
            {
                if (currentMacro == null)
                {
                    if (macroStack.Count > 0)
                        currentMacro = macroStack.Pop();
                    else if (macroQueue.Count > 0)
                    {
                        lock (macroQueue)
                        {
                            currentMacro = macroQueue.Dequeue();
                        }

                        currentMacro.Reset();
                    }
                }
                else
                {
                    if (macroQueue.Count > 0)
                    {
                        lock (macroQueue)
                        {
                            macro = macroQueue.Peek();
                        }

                        if (macro.Interrupt && macro.Priority >= currentMacro.Priority)
                        {
                            if (currentMacro.Release)
                                Release();

                            lock (macroQueue)
                            {
                                currentMacro = macroQueue.Dequeue();
                            }
                        }
                    }
                }

                if (currentMacro == null)
                    runEvent.WaitOne();
                else
                {
                    istep = currentMacro.CurrentStep;
                    currentMacro.IncStep();

                    if (istep == null)
                    {
                        if (currentMacro.Release)
                            Release();

                        loopCount = currentMacro.LoopCount;
                        currentLoop = currentMacro.CurrentLoop;
                        currentMacro.IncLoop();
                        if (loopCount < 0 || currentLoop < loopCount - 1)
                        {
                            System.Diagnostics.Debug.WriteLine("loopCount: " + loopCount + ", currentLoop = " + currentLoop);
                            currentMacro.ResetSteps();
                        }
                        else
                            currentMacro = null;
                    }
                    else
                    {
                        elapsedMs = stopwatch.ElapsedMilliseconds;

                        stepEnabled = istep.IsEnabled;
                        if (stepEnabled)
                        {
                            if (istep.Timestamp > 0 && istep.Timestamp + istep.Cooldown > elapsedMs)
                                stepEnabled = false;
                        }

                        if (stepEnabled)
                        {
                            istep.Timestamp = elapsedMs;

                            switch (istep.Type)
                            {
                                case StepType.Delay:
                                    delay = istep as Delay;
                                    timer.Interval = delay.Milliseconds;
                                    timer.Start();
                                    runEvent.WaitOne();
                                    break;

                                case StepType.Macro:
                                    macroStack.Push(currentMacro);
                                    currentMacro = istep as Macro;
                                    currentMacro.Reset();
                                    break;

                                default:
                                    step = istep as Step;
                                    release = istep.Run();
                                    if (release != null)
                                    {
                                        if (releaseLookup.TryGetValue(release[0], out releaseIndex))
                                            releaseList[releaseIndex] = null;
                                        releaseLookup[release[0]] = releaseList.Count;
                                        releaseList.Add(release);
                                    }
                                    else if (step != null && step.Inputs != null)
                                    {
                                        foreach (var input in step.Inputs)
                                            if (releaseLookup.TryGetValue(input, out releaseIndex))
                                                releaseList[releaseIndex] = null;
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        void Run()
        {
            IStep istep;
            Step  step;
            Delay delay;
            bool  stepEnabled;
            long  elapsedMs;

            InputWrapper[] release;
            int            releaseIndex;
            Macro          macro;
            int            loopCount;
            int            currentLoop;

            while (running)
            {
                if (currentMacro == null)
                {
                    if (macroStack.Count > 0)
                    {
                        currentMacro = macroStack.Pop();
                    }
                    else if (macroQueue.Count > 0)
                    {
                        lock (macroQueue)
                        {
                            currentMacro = macroQueue.Dequeue();
                        }

                        currentMacro.Reset();
                    }
                }
                else
                {
                    if (macroQueue.Count > 0)
                    {
                        lock (macroQueue)
                        {
                            macro = macroQueue.Peek();
                        }

                        if (macro.Interrupt && macro.Priority >= currentMacro.Priority)
                        {
                            if (currentMacro.Release)
                            {
                                Release();
                            }

                            lock (macroQueue)
                            {
                                currentMacro = macroQueue.Dequeue();
                            }
                        }
                    }
                }

                if (currentMacro == null)
                {
                    runEvent.WaitOne();
                }
                else
                {
                    istep = currentMacro.CurrentStep;
                    currentMacro.IncStep();

                    if (istep == null)
                    {
                        if (currentMacro.Release)
                        {
                            Release();
                        }

                        loopCount   = currentMacro.LoopCount;
                        currentLoop = currentMacro.CurrentLoop;
                        currentMacro.IncLoop();
                        if (loopCount < 0 || currentLoop < loopCount - 1)
                        {
                            System.Diagnostics.Debug.WriteLine("loopCount: " + loopCount + ", currentLoop = " + currentLoop);
                            currentMacro.ResetSteps();
                        }
                        else
                        {
                            currentMacro = null;
                        }
                    }
                    else
                    {
                        elapsedMs = stopwatch.ElapsedMilliseconds;

                        stepEnabled = istep.IsEnabled;
                        if (stepEnabled)
                        {
                            if (istep.Timestamp > 0 && istep.Timestamp + istep.Cooldown > elapsedMs)
                            {
                                stepEnabled = false;
                            }
                        }

                        if (stepEnabled)
                        {
                            istep.Timestamp = elapsedMs;

                            switch (istep.Type)
                            {
                            case StepType.Delay:
                                delay          = istep as Delay;
                                timer.Interval = delay.Milliseconds;
                                timer.Start();
                                runEvent.WaitOne();
                                break;

                            case StepType.Macro:
                                macroStack.Push(currentMacro);
                                currentMacro = istep as Macro;
                                currentMacro.Reset();
                                break;

                            default:
                                step    = istep as Step;
                                release = istep.Run();
                                if (release != null)
                                {
                                    if (releaseLookup.TryGetValue(release[0], out releaseIndex))
                                    {
                                        releaseList[releaseIndex] = null;
                                    }
                                    releaseLookup[release[0]] = releaseList.Count;
                                    releaseList.Add(release);
                                }
                                else if (step != null && step.Inputs != null)
                                {
                                    foreach (var input in step.Inputs)
                                    {
                                        if (releaseLookup.TryGetValue(input, out releaseIndex))
                                        {
                                            releaseList[releaseIndex] = null;
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }
Example #4
0
        public void Enqueue(Macro macro)
        {
            lock (macroQueue)
            {
                macroQueue.Enqueue(macro);
            }

            runEvent.Set();
        }