Beispiel #1
0
        public void AsyncRun(CancellationToken cancellationToken)
        {
            //Check if WF is running
            var wfAlreadyRunning = System.Diagnostics.Process.GetProcessesByName("Warframe.x64").Length > 0;

            if (System.Diagnostics.Process.GetProcessesByName("Warframe.x64").Length == 0)
            {
                StartWarframe();
            }
            WaitForLoadingScreen(wfAlreadyRunning);

            //Check if on login screen
            LogIn();

            //Check if on daily reward screen
            ClaimDailyReward();

            var cropper = _rivenParserFactory.CreateRivenParser();

            //start an infinite loop
            while (System.Diagnostics.Process.GetProcessesByName("Warframe.x64").Length > 0)
            {
                //Get to Glyph screen if not already there
                SetForegroundWindow(Process.GetProcessesByName("Warframe.x64").First().MainWindowHandle);
                using (var screen = _gameCapture.GetFullImage())
                {
                    screen.Save("screen.png");
                    var state = _screenStateHandler.GetScreenState(screen);
                    if (state != Enums.ScreenState.GlyphWindow)
                    {
                        GoToGlyphScreenAndSetupFilters();
                    }
                    else if (state == ScreenState.GlyphWindow && _screenStateHandler.IsChatOpen(screen))
                    {
                        var chatLines = _chatParser.ParseChatImage(screen, true, true, 30);
                        var workItems = new List <RivenParseTaskWorkItem>();
                        foreach (var line in chatLines)
                        {
                            if (line.LineType == LineParseResult.LineType.RedText)
                            {
                                _dataSender.AsyncSendRedtext(line.RawMessage).Wait();
                            }
                            else if (line.LineType == LineParseResult.LineType.NewMessage && line is ChatMessageLineResult)
                            {
                                var clr       = line as ChatMessageLineResult;
                                var chatModel = MakeChatModel(line as LineParseResult.ChatMessageLineResult);
                                if (clr.ClickPoints.Count == 0)
                                {
                                    _dataSender.AsyncSendChatMessage(chatModel);
                                }
                                else
                                {
                                    var crops = new List <RivenParseTaskWorkItemDetails>();
                                    foreach (var clickpoint in clr.ClickPoints)
                                    {
                                        //Click riven
                                        _mouse.MoveTo(clickpoint.X, clickpoint.Y);
                                        Thread.Sleep(17);
                                        _mouse.Click(clickpoint.X, clickpoint.Y);
                                        Thread.Sleep(45);
                                        _mouse.MoveTo(0, 0);
                                        Thread.Sleep(45);

                                        //Wait for riven to open
                                        Bitmap crop       = null;
                                        var    foundRiven = false;
                                        for (int tries = 0; tries < 6; tries++)
                                        {
                                            var sw = new Stopwatch();
                                            sw.Start();
                                            using (var b = _gameCapture.GetFullImage())
                                            {
                                                Console.WriteLine("Got capture in: " + sw.Elapsed.TotalSeconds);
                                                sw.Stop();
                                                if (_screenStateHandler.GetScreenState(b) == ScreenState.RivenWindow)
                                                {
                                                    Console.WriteLine("found riven after: " + (tries + 1) + " tries");
                                                    foundRiven = true;
                                                    crop       = cropper.CropToRiven(b);

                                                    _mouse.Click(3816, 2013);
                                                    Thread.Sleep(17);
                                                    _mouse.MoveTo(0, 0);
                                                    Thread.Sleep(17);
                                                    break;
                                                }
                                            }
                                        }
                                        if (!foundRiven || crop == null)
                                        {
                                            _chatParser.InvalidCache(line.GetKey());
                                            if (crop != null)
                                            {
                                                crop.Dispose();
                                            }
                                            break;
                                        }
                                        for (int tries = 0; tries < 6; tries++)
                                        {
                                            using (var b = _gameCapture.GetFullImage())
                                            {
                                                var subState = _screenStateHandler.GetScreenState(b);
                                                if (_screenStateHandler.IsChatOpen(b))
                                                {
                                                    break;
                                                }
                                                else if (tries < 5 && subState == ScreenState.RivenWindow)
                                                {
                                                    Thread.Sleep(17);
                                                    //_mouse.Click(3816, 2013);
                                                    //Thread.Sleep(17);
                                                    //_mouse.MoveTo(0, 0);
                                                    //Thread.Sleep(80);
                                                }
                                                else if (tries >= 5 && _screenStateHandler.IsExitable(b))
                                                {
                                                    _mouse.Click(3816, 2013);
                                                    Thread.Sleep(17);
                                                    _mouse.MoveTo(0, 0);
                                                    Thread.Sleep(17);
                                                }
                                            }
                                        }
                                        crops.Add(new RivenParseTaskWorkItemDetails()
                                        {
                                            RivenIndex = clickpoint.Index, RivenName = clickpoint.RivenName, CroppedRivenBitmap = crop
                                        });
                                    }
                                    workItems.Add(new RivenParseTaskWorkItem()
                                    {
                                        Model = chatModel, RivenWorkDetails = crops
                                    });
                                }
                            }
                        }
                        workItems.ForEach(item =>
                        {
                            var modelTask = new Task(() =>
                            {
                                var tasks = item.RivenWorkDetails.Select(r =>
                                {
                                    var t = new Task <Riven>(() =>
                                    {
                                        using (var cleaned = _rivenCleaner.CleanRiven(r.CroppedRivenBitmap))
                                        {
                                            var parser               = _rivenParserFactory.CreateRivenParser();
                                            var riven                = parser.ParseRivenTextFromImage(cleaned, null);
                                            riven.Polarity           = parser.ParseRivenPolarityFromColorImage(r.CroppedRivenBitmap);
                                            riven.Rank               = parser.ParseRivenRankFromColorImage(r.CroppedRivenBitmap);
                                            riven.MessagePlacementId = r.RivenIndex;
                                            riven.Name               = r.RivenName;
                                            _dataSender.AsyncSendRivenImage(riven.ImageID, r.CroppedRivenBitmap);
                                            //r.CroppedRivenBitmap.Dispose();

                                            if (parser is IDisposable)
                                            {
                                                ((IDisposable)parser).Dispose();
                                            }
                                            return(riven);
                                        }
                                    });
                                    t.Start();
                                    return(t);
                                });
                                var rivens = Task.WhenAll(tasks).Result.ToList();
                                rivens.ForEach(r => item.Model.Rivens.Add(r));
                                _dataSender.AsyncSendChatMessage(item.Model);
                            });
                            modelTask.Start();
                        });
                    }
                }
                //Scroll down to get 27 more messages
                _mouse.MoveTo(3250, 768);
                //Scroll down for new page of messages
                for (int i = 0; i < 27; i++)
                {
                    _mouse.ScrollDown();
                    Thread.Sleep(17);
                }
                for (int i = 0; i < 1; i++)
                {
                    _mouse.ScrollUp();//Pause chat
                    Thread.Sleep(17);
                }
                _mouse.MoveTo(0, 0);
                Thread.Sleep(100);
            }
            if (cropper is IDisposable)
            {
                ((IDisposable)cropper).Dispose();
            }
        }
Beispiel #2
0
        public async Task MonitorLive(string debugImageDectory = null)
        {
            if (debugImageDectory != null && !Directory.Exists(debugImageDectory))
            {
                Directory.CreateDirectory(debugImageDectory);
            }
            if (!Directory.Exists(Path.Combine(Path.GetTempPath(), "wfchat")))
            {
                Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "wfchat"));
            }
            if (!Directory.Exists(Path.Combine(Path.GetTempPath(), "wfchat", "rivens")))
            {
                Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "wfchat", "rivens"));
            }
            foreach (var oldRiven in Directory.GetFiles(Path.Combine(Path.GetTempPath(), "wfchat", "rivens")))
            {
                File.Delete(oldRiven);
            }

            var    newSW          = new Stopwatch();
            var    sw             = new Stopwatch();
            var    scrollbarFound = false;
            Bitmap b                 = null;
            var    scrollDots        = 0;
            var    cachedRivens      = new Queue <string>();
            var    cachedRivenValues = new Dictionary <string, Riven>();

            while (true)
            {
                UpdateUISeperators();
                _UISecondLine = null;
                UpdateUISecondLine();
                _UIThirdLine = null;
                UpdateUIThirdLine();
                if (!scrollbarFound)
                {
                    _UIFirstLine = "Finding scrollbar";
                }
                else
                {
                    _UIFirstLine = "Getting image";
                }
                UpdateUIFirstLine();

                newSW.Restart();
                sw.Restart();
                //var image = string.Empty;
                //try
                //{
                //    newSW.Restart();
                //    b = _gameCapture.GetFullImage();
                //    b.Save(Path.Combine(Path.GetTempPath(), "wfchat", "capture_0.png"));
                //    b.Dispose();
                //    image = Path.Combine(Path.GetTempPath(), "wfchat", "capture_0.png");
                //    if (debugImageDectory != null)
                //    {
                //        File.Copy(image, Path.Combine(debugImageDectory, "capture_0.png"), true);
                //    }
                //}
                //catch { continue; }
                var imageTime = sw.Elapsed.TotalSeconds;
                sw.Restart();

                var image = _gameCapture.GetFullImage();
                if (image == null)
                {
                    _UIFirstLine = "Failed to get image";
                    UpdateUIFirstLine();
                    continue;
                }
                if (!_screenStateHandler.IsChatOpen(image))
                {
                    if (_screenStateHandler.IsExitable(image))
                    {
                        _UIFirstLine = "RECOVERING: clickign exit";
                        UpdateUIFirstLine();

                        //Click exit
                        _mouseMover.Click(3814, 2014);
                        await Task.Delay(30);

                        continue;
                    }
                    await _dataSender.AsyncSendDebugMessage("Help I'm stuck!");

                    await Task.Delay(5000);

                    image.Dispose();
                    continue;
                }

                //Wait for scrollbar to be ready
                if (!scrollbarFound)
                {
                    if (_chatParser.IsScrollbarPresent(image))
                    {
                        scrollbarFound = true;
                        _mouseMover.MoveTo(3259, 658);
                        await Task.Delay(33);

                        _mouseMover.Click(3259, 658);
                        await Task.Delay(100);

                        continue;
                    }
                    else
                    {
                        scrollDots++;
                        if (scrollDots > 4)
                        {
                            scrollDots = 0;
                        }
                        await Task.Delay(100);

                        continue;
                    }
                }

                sw.Restart();
                newSW.Restart();

                _UIFirstLine = "Parsing chat";
                UpdateUIFirstLine();
                newSW.Restart();
                var lines = _chatParser.ParseChatImage(image, true, true, 27);
                _UIMessages.AddRange(lines.Select(l => l.RawMessage));
                _UIFirstLine = "Parsing chat: " + lines.Length + " new messages. Riven cache count: " + cachedRivens.Count;
                UpdateUIFirstLine();
                UpdateUIMessages();
                var parseTime = sw.Elapsed.TotalSeconds;
                sw.Restart();

                string debugImageName = null;
                if (debugImageDectory != null)
                {
                    debugImageName = Path.Combine(debugImageDectory, "debug_image_" + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss-fff") + ".png");
                }

                var newMessags      = 0;
                var shouldCopyImage = false;
                var badNameRegex    = new Regex("[^-A-Za-z0-9._]");
                //[00:00] f: .
                ChatMessageModel lastMessage = null;
                var hasher = MD5.Create();
                sw.Restart();
                var linesBad       = false;
                var rivenParseTime = 0.0;
                var sendingTime    = 0.0;
                var rivenSW        = new Stopwatch();
                var sendingSW      = new Stopwatch();
                foreach (var line in lines)
                {
                    _UISecondLine = "Handling generic: " + line.RawMessage;
                    UpdateUISecondLine();

                    var lineSw = new Stopwatch();
                    lineSw.Start();
                    if (linesBad)
                    {
                        lines.ToList().ForEach(l => _chatParser.InvalidCache(l.GetKey()));
                        break;
                    }
                    if (line.LineType == LineParseResult.LineType.RedText)
                    {
                        _UISecondLine = "Handing redtext: " + line.RawMessage;
                        UpdateUISecondLine();
                        await _dataSender.AsyncSendRedtext(line.RawMessage);
                    }
                    else if (line.LineType == LineParseResult.LineType.NewMessage && line is ChatMessageLineResult)
                    {
                        _UISecondLine = "Current msg: " + line.RawMessage;
                        UpdateUISecondLine();

                        var chatMessageSw = new Stopwatch();
                        chatMessageSw.Start();
                        newSW.Restart();
                        var clr     = line as ChatMessageLineResult;
                        var message = MakeChatModel(line as LineParseResult.ChatMessageLineResult, badNameRegex);
                        newMessags++;

                        for (int i = 0; i < clr.ClickPoints.Count; i++)
                        {
                            var clickPointSw = new Stopwatch();
                            clickPointSw.Start();
                            rivenSW.Restart();
                            var clickpoint = clr.ClickPoints[i];

                            _UIThirdLine = "Parsing riven: " + clickpoint.RivenName + " " + clickpoint.X + "," + clickpoint.Y;
                            UpdateUIThirdLine();

                            if (cachedRivenValues.ContainsKey(clr.Username + clickpoint.RivenName))
                            {
                                var cachedRiven = cachedRivenValues[clr.Username + clickpoint.RivenName];
                                var copiedRiven = new Riven();
                                copiedRiven.Drain              = cachedRiven.Drain;
                                copiedRiven.ImageID            = cachedRiven.ImageID;
                                copiedRiven.MasteryRank        = cachedRiven.MasteryRank;
                                copiedRiven.MessagePlacementId = clickpoint.Index;
                                copiedRiven.Modifiers          = cachedRiven.Modifiers;
                                copiedRiven.Name     = cachedRiven.Name;
                                copiedRiven.Polarity = cachedRiven.Polarity;
                                copiedRiven.Rank     = cachedRiven.Rank;
                                copiedRiven.Rolls    = cachedRiven.Rolls;
                                message.Rivens.Add(copiedRiven);
                                rivenParseTime += rivenSW.Elapsed.TotalSeconds;

                                await _dataSender.AsyncSendDebugMessage("Found a riven from cache: " + clr.Username + " " + clickpoint.RivenName);

                                _UISecondLine = "Riven: " + clickpoint.RivenName + " found in cache";
                                _UILastRiven  = cachedRiven;
                                UpdateUISecondLine();
                                //UpdateUIRiven(cachedRiven);

                                continue;
                            }

                            var rivenImage    = string.Empty;
                            var originalBytes = Encoding.UTF8.GetBytes(clr.Username);
                            var hashedBytes   = hasher.ComputeHash(originalBytes);
                            var usernameHash  = new StringBuilder();
                            foreach (Byte hashed in hashedBytes)
                            {
                                usernameHash.AppendFormat("{0:x2}", hashed);
                            }
                            rivenImage = Path.Combine(Path.GetTempPath(), "wfchat", "rivens", usernameHash.ToString() + "_" + i + ".png");
                            b          = _gameCapture.GetChatIcon();
                            if (_chatParser.IsChatFocused(b))
                            {
                                _mouseMover.MoveTo(clickpoint.X, clickpoint.Y);
                                await Task.Delay(30);

                                _mouseMover.Click(clickpoint.X, clickpoint.Y);
                                await Task.Delay(17);
                            }
                            else
                            {
                                _chatParser.InvalidCache(line.GetKey());
                                linesBad        = true;
                                rivenParseTime += rivenSW.Elapsed.TotalSeconds;
                                break;
                            }
                            b.Dispose();

                            _mouseMover.MoveTo(0, 0);
                            Bitmap crop       = null;
                            var    foundRiven = false;
                            for (int tries = 0; tries < 15; tries++)
                            {
                                b = _gameCapture.GetFullImage();
                                if (_screenStateHandler.GetScreenState(b) == ScreenState.RivenWindow)
                                {
                                    foundRiven = true;
                                    crop       = _rivenParser.CropToRiven(b);
                                    b.Dispose();

                                    _mouseMover.Click(3816, 2013);
                                    await Task.Delay(17);

                                    _mouseMover.MoveTo(0, 0);
                                    await Task.Delay(17);

                                    break;
                                }
                                b.Dispose();
                            }
                            if (!foundRiven || crop == null)
                            {
                                linesBad = true;
                                _chatParser.InvalidCache(line.GetKey());
                                if (crop != null)
                                {
                                    crop.Dispose();
                                }
                                rivenParseTime += rivenSW.Elapsed.TotalSeconds;
                                break;
                            }

                            var newC  = _rivenCleaner.CleanRiven(crop);
                            var riven = _rivenParser.ParseRivenTextFromImage(newC, clickpoint.RivenName);
                            riven.Name     = clickpoint.RivenName;
                            riven.Polarity = _rivenParser.ParseRivenPolarityFromColorImage(crop);
                            riven.Rank     = _rivenParser.ParseRivenRankFromColorImage(crop);
                            newC.Dispose();
                            //crop.Dispose();
                            if (riven == null)
                            {
                                //crop.Dispose();
                                _chatParser.InvalidCache(line.GetKey());
                                linesBad        = true;
                                rivenParseTime += rivenSW.Elapsed.TotalSeconds;
                                break;
                            }

                            riven.MessagePlacementId = clickpoint.Index;

                            if (riven.Drain > 0 && riven.MasteryRank > 0)
                            {
                                cachedRivens.Enqueue(clr.Username + clickpoint.RivenName);
                                cachedRivenValues[clr.Username + clickpoint.RivenName] = riven;
                                while (cachedRivens.Count > 5000)
                                {
                                    var removed = cachedRivens.Dequeue();
                                    cachedRivenValues.Remove(removed);
                                }
                                _UIFirstLine = "Parsing chat: " + lines.Length + " new messages. Riven cache count: " + cachedRivens.Count;
                                UpdateUIFirstLine();
                            }
                            message.Rivens.Add(riven);

                            _UILastRiven = riven;
                            crop.Save(riven.ImageID + ".png");
                            UpdateUIRiven(riven);

                            File.Delete(rivenImage);

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            _dataSender.AsyncSendRivenImage(riven.ImageID, crop);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                               //await _dataSender.AsyncSendRivenImage(riven.ImageID, rivenBase64);

                            for (int tries = 0; tries < 15; tries++)
                            {
                                b = _gameCapture.GetFullImage();
                                var state = _screenStateHandler.GetScreenState(b);
                                if (_screenStateHandler.IsChatOpen(b))
                                {
                                    b.Dispose();
                                    break;
                                }
                                else if (state == ScreenState.RivenWindow)
                                {
                                    _mouseMover.Click(3816, 2013);
                                    await Task.Delay(17);

                                    _mouseMover.MoveTo(0, 0);
                                    await Task.Delay(17);
                                }
                                b.Dispose();
                            }
                        }
                        if (linesBad)
                        {
                            lines.ToList().ForEach(l => _chatParser.InvalidCache(l.GetKey()));
                            break;
                        }
                        if (message.DEBUGREASON != null)
                        {
                            message.DEBUGIMAGE = debugImageName;
                            shouldCopyImage    = true;
                        }

                        rivenParseTime += rivenSW.Elapsed.TotalSeconds;
                        sendingSW.Restart();
                        await _dataSender.AsyncSendChatMessage(message);

                        sendingTime += sendingSW.Elapsed.TotalSeconds;
                    }
                }

                if (shouldCopyImage && debugImageName != null && debugImageName.Length > 0)
                {
                    try
                    {
                        image.Save(debugImageName);
                    }
                    catch { }
                    //File.Copy(image, debugImageName);
                }
                image.Dispose();
                var transmitTime = sw.Elapsed.TotalSeconds;
                sw.Stop();
                var debugMessage = $"Image capture: {imageTime:00.00} Parse time: {parseTime:00.00} TransmitTime: {transmitTime:0.000} New messages {newMessags} {newMessags / parseTime}/s";
                await _dataSender.AsyncSendDebugMessage(debugMessage);

                _UIFirstLine  = "Scrolling";
                _UISecondLine = null;
                _UIThirdLine  = null;
                UpdateUIFirstLine();
                UpdateUISecondLine();
                UpdateUIThirdLine();

                //Scroll down to get 27 more messages
                _mouseMover.MoveTo(3250, 768);
                //Scroll down for new page of messages
                for (int i = 0; i < 27; i++)
                {
                    _mouseMover.ScrollDown();
                    await Task.Delay(17);
                }
                for (int i = 0; i < 1; i++)
                {
                    _mouseMover.ScrollUp();//Pause chat
                    await Task.Delay(17);
                }
                _mouseMover.MoveTo(0, 0);
                await Task.Delay(100);
            }
        }