コード例 #1
0
        private void Pulse_Main(object sender, EventArgs e)
        {
            try
            {
                if (!ZetaDia.Service.IsValid || !ZetaDia.Service.Platform.IsConnected)
                {
                    return;
                }

                if (!IsEnabled)
                {
                    ResetBotBehavior();
                }

                // Handle errors and other strange situations
                ErrorHandling();

                // YAR Health Check
                _pulseCheck   = true;
                _bs.LastPulse = DateTime.Now.Ticks;

                _bs.PluginPulse = DateTime.Now.Ticks;
                _bs.IsRunning   = BotMain.IsRunning;

                if (BotMain.IsPaused || BotMain.IsPausedForStateExecution)
                {
                    _bs.IsPaused = true;
                }
                else if (BotMain.IsRunning)
                {
                    _bs.IsPaused = false;
                    _bs.LastRun  = DateTime.Now.Ticks;
                }
                else
                {
                    _bs.IsPaused = false;
                }


                Queue <ReadOnlyCollection <Logging.LogMessage> > localQueue = new Queue <ReadOnlyCollection <Logging.LogMessage> >();
                lock (MessageQueue)
                    while (MessageQueue.Any())
                    {
                        localQueue.Enqueue(MessageQueue.Dequeue());
                    }

                foreach (var messages in localQueue)
                {
                    try
                    {
                        // Create new log buffer
                        if (_logBuffer == null)
                        {
                            _logBuffer = messages.ToArray();
                        }
                        else
                        {
                            // Append to existing log buffer
                            lock (_logBuffer)
                            {
                                var newbuffer = _logBuffer.Concat(messages.ToArray()).ToArray();
                                _logBuffer = newbuffer;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogException(ex);
                    }
                }

                // Keep Thread alive while log buffer is not empty
                while (_logBuffer != null)
                {
                    try
                    {
                        var duration = DateTime.Now;
                        Logging.LogMessage[] buffer;
                        // Lock buffer and copy to local variable for scanning
                        lock (_logBuffer)
                        {
                            buffer = new Logging.LogMessage[_logBuffer.Length + 1]; // set log new local log buffer size
                            _logBuffer.CopyTo(buffer, 0);                           // copy to local
                            _logBuffer = null;                                      // clear buffer
                        }
                        var count     = 0;                                          // Scan counter
                        var breakloop = false;
                        // Scan log items
                        foreach (Logging.LogMessage lm in buffer.Where(x => x != null))
                        {
                            string msg = lm.Message;
                            if (yarRegex.IsMatch(msg))
                            {
                                continue;
                            }

                            count++; // add to counter
                            // Log level specific scanning to prevent uneeded cpu usage
                            switch (lm.Level)
                            {
                            case LogLevel.Diagnostic:
                                var m = pluginsCompiled.Match(msg);
                                if (m.Success)
                                {
                                    Log("Plugins Compiled matched");
                                    _allPluginsCompiled = true;
                                    Send("AllCompiled");     // tell relogger about all plugin compile so the relogger can tell what to do next
                                    continue;
                                }
                                // Find all plugins compiled line
                                //if (!_allPluginsCompiled && FindPluginsCompiled(msg))
                                //    continue;

                                // Find Start stop button click
                                if (msg.Equals("Start/Stop Button Clicked!") && !BotMain.IsRunning)
                                {
                                    Send("UserStop");
                                }
                                break;     // case end

                            default:
                                //if (msg.Contains(d3Exit))
                                //{
                                //    Send("D3Exit");
                                //    Log("Attempting to safely close Demonbuddy");
                                //    SafeCloseProcess();
                                //    breakloop = true;
                                //    break;
                                //}

                                try
                                {
                                    if (!ZetaDia.IsInGame && FindStartDelay(msg))
                                    {
                                        continue;                                               // Find new start delay
                                    }
                                }
                                catch (AccessViolationException)
                                {
                                    if (ZetaDia.Memory.Process.HasExited)
                                    {
                                        Send("D3Exit");     // Proces has exited
                                        breakloop = true;   // break out of loop
                                        break;
                                    }
                                }
                                // Crash Tender check
                                if (ReCrashTender.Any(re => re.IsMatch(msg)))
                                {
                                    Send("CrashTender " + ProfileManager.CurrentProfile.Path); // tell relogger to "crash tender" :)
                                    breakloop = true;                                          // break out of loop
                                    break;
                                }
                                // YAR compatibility with other plugins
                                if (ReCompatibility.Any(re => re.IsMatch(msg)))
                                {
                                    Send("ThirdpartyStop");
                                }
                                break;     // case end
                            }
                            if (breakloop)
                            {
                                break;            // Check if we need to break out of loop
                            }
                        }

                        if (count > 1)
                        {
                            Logging.WriteDiagnostic("[YetAnotherRelogger] Scanned {0} log items in {1}ms", count, DateTime.Now.Subtract(duration).TotalMilliseconds);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogException(ex);
                    }
                }

                if (!pulseTimer.IsRunning)
                {
                    pulseTimer.Start();
                }

                if (pulseTimer.ElapsedMilliseconds <= 1000)
                {
                    return;
                }

                if (pulseTimer.ElapsedMilliseconds > 1000)
                {
                    pulseTimer.Restart();
                }
            }
            catch (Exception ex)
            {
                Log("Exception in Pulse_Main: {0}", ex);
            }
        }
コード例 #2
0
        private void ScanLogWorker()
        {
            // Keep Thread alive while log buffer is not empty
            while (_logBuffer != null)
            {
                try
                {
                    var duration = DateTime.Now;
                    Logging.LogMessage[] buffer;
                    // Lock buffer and copy to local variable for scanning
                    lock (_logBuffer)
                    {
                        buffer = new Logging.LogMessage[_logBuffer.Length + 1]; // set log new local log buffer size
                        _logBuffer.CopyTo(buffer, 0);                           // copy to local
                        _logBuffer = null;                                      // clear buffer
                    }
                    var count     = 0;                                          // Scan counter
                    var breakloop = false;
                    // Scan log items
                    foreach (Logging.LogMessage lm in buffer.Where(x => x != null))
                    {
                        count++; // add to counter
                        string msg = lm.Message;
                        // Log level specific scanning to prevent uneeded cpu usage
                        switch (lm.Level)
                        {
                        case LogLevel.Diagnostic:
                            if (!_allPluginsCompiled && FindPluginsCompiled(msg))
                            {
                                continue;                                                       // Find all plugins compiled line
                            }
                            // Find Start stop button click
                            if (msg.Equals("Start/Stop Button Clicked!") && !BotMain.IsRunning)
                            {
                                Send("UserStop");
                            }
                            break;     // case end

                        default:
                            //if (msg.Contains(d3Exit))
                            //{
                            //    Send("D3Exit");
                            //    Log("Attempting to safely close Demonbuddy");
                            //    SafeCloseProcess();
                            //    breakloop = true;
                            //    break;
                            //}

                            try
                            {
                                if (!ZetaDia.IsInGame && FindStartDelay(msg))
                                {
                                    continue;                                               // Find new start delay
                                }
                            }
                            catch (AccessViolationException)
                            {
                                if (ZetaDia.Memory.Process.HasExited)
                                {
                                    Send("D3Exit");     // Proces has exited
                                    breakloop = true;   // break out of loop
                                    break;
                                }
                            }
                            // Crash Tender check
                            if (ReCrashTender.Any(re => re.IsMatch(msg)))
                            {
                                Send("CrashTender " + ProfileManager.CurrentProfile.Path); // tell relogger to "crash tender" :)
                                breakloop = true;                                          // break out of loop
                                break;
                            }
                            // YAR compatibility with other plugins
                            if (ReCompatibility.Any(re => re.IsMatch(msg)))
                            {
                                Send("ThirdpartyStop");
                            }
                            break;     // case end
                        }
                        if (breakloop)
                        {
                            break;            // Check if we need to break out of loop
                        }
                    }

                    if (count > 1)
                    {
                        Logging.WriteDiagnostic("[YetAnotherRelogger] Scanned {0} log items in {1}ms", count, DateTime.Now.Subtract(duration).TotalMilliseconds);
                    }
                }
                catch (Exception ex)
                {
                    LogException(ex);
                }
            }
        }