Ejemplo n.º 1
0
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == KEY_MESSAGE)
            {
                Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);

                //Reset table key was pressed
                if (key == resetTableHotkey)
                {
                    tableController.ResetTable();
                }
                //Pause timer key was pressed
                if (key == pauseTimerHotkey)
                {
                    //Toggle the tracker time
                    settingsController.ToggleTimerPaused();
                    tableController.SetLastChangeTime(); //On unpause we want to reset the inactivity timer
                }
            }
        }
Ejemplo n.º 2
0
        public void Detect()
        {
            //Pointer to the Temtem Window
            IntPtr temtemWindow;

            //Get the currently focused window
            IntPtr focused = User32.GetForegroundWindow();

            //If we're not focusing a window
            if (focused == IntPtr.Zero)
            {
                return;
            }
            //Check if the focused window is Temtem
            StringBuilder windowName = new StringBuilder(100);

            User32.GetWindowText(focused, windowName, 100);
            User32.GetWindowThreadProcessId(focused, out uint focusedWindowProcessID); //Inline variable declaration
            string focusedWindowProcessName = Process.GetProcessById((int)focusedWindowProcessID).ProcessName;

            //Is the window in question a Temtem window? Assume not
            Boolean isTemtemWindow = false;

            //Check if The window matches any of the window and process names in the config
            foreach (WindowAndProcessNames windowAndProcess in windowAndProcessNames)
            {
                if (windowName.ToString().Equals(windowAndProcess.windowName) && focusedWindowProcessName.Equals(windowAndProcess.processName))
                {
                    isTemtemWindow = true;
                    break;
                }
            }
            //If the window is a Temtem window, run detection on it
            if (isTemtemWindow)
            {
                temtemWindow = focused;
                //Here we check if this is one of our already detected windows and if not we add it
                if (!temtemWindows.ContainsKey(focusedWindowProcessID))
                {
                    temtemWindows.Add(focusedWindowProcessID, new TemtemWindowData {
                        detectionSpots = new List <Point>(),
                        OCRViewports   = new List <Rectangle>(),
                        gameWindowSize = new Size(0, 0),
                        detectedBattle = false
                    });
                }
            }
            else
            {
                //We aren't in the right window
                return;
            }

            IntPtr hdcWindow = User32.GetDC(temtemWindow);

            User32.POINT lpPoint = new User32.POINT();

            User32.GetClientRect(temtemWindow, out User32.RECT bounds);
            User32.ClientToScreen(temtemWindow, ref lpPoint);

            ////Release the device context
            User32.ReleaseDC(temtemWindow, hdcWindow);

            Rectangle gameWindowRect = new Rectangle(lpPoint.X, lpPoint.Y, (bounds.right - bounds.left), (bounds.bottom - bounds.top));

            if (gameWindowRect.Height == 0 || gameWindowRect.Width == 0)
            {
                //If the rectangle is 0 high or wide, we've got a closing/closed window or an error
                return;
            }
            //If the game window dimensions have changed we need to recalculate all the spots.
            //This shouldn't happen often
            if (!gameWindowRect.Size.Equals(temtemWindows[focusedWindowProcessID].gameWindowSize))
            {
                temtemWindows[focusedWindowProcessID].gameWindowSize = gameWindowRect.Size;
                RecalculateDetectionElements(temtemWindows[focusedWindowProcessID], gameWindowRect);
            }

            if (temtemWindows[focusedWindowProcessID].detectedBattle == false)
            {
                //We'll only test for battle if we aren't in a battle
                List <Color> pixelColors = GetBattleDetectionColors(temtemWindows[focusedWindowProcessID], lpPoint);

                //We'll also get the OCR reading spots right here. It's a waste of resources if we aren't detecting battle, but is faster and more reliable when we do
                List <Bitmap> viewportImages = GetOCRViewportImages(lpPoint, temtemWindows[focusedWindowProcessID].OCRViewports);

                if (((ColorDistance(pixelColors[0], spot1RGB) < maxAllowedColorDistance &&
                      ColorDistance(pixelColors[1], spot2RGB) < maxAllowedColorDistance) ||
                     (ColorDistance(pixelColors[2], spot3RGB) < maxAllowedColorDistance &&
                      ColorDistance(pixelColors[3], spot4RGB) < maxAllowedColorDistance)))
                {
                    temtemWindows[focusedWindowProcessID].detectedBattle = true;
                    //TODO: Add criteria for not detecting due to inactivity
                    if (timerEnabled ||
                        (pauseWasAutopause && autoresumeEnabled) ||
                        !detectionDisabledWhileTimerPaused)
                    {
                        //Do OCR operation. The OCR controller will dispose of the images so we're ok
                        List <string> results = ocrController.DoOCR(viewportImages);
                        //Log the encounter in the database
                        DatabaseController.Instance.LogEncounter(results);
                        //Add the encounters to the UI
                        results.ForEach(result => {
                            //Here we add the detected Temtem to the UI
                            tableController.AddTemtem(result);
                        });
                        if (autoresumeEnabled && pauseWasAutopause)
                        {
                            settingsController.ToggleTimerPaused();
                        }
                    }
                }
            }
            else if (temtemWindows[focusedWindowProcessID].detectedBattle == true)
            {
                //If we're in battle we'll test for being out of battle
                List <Color> pixelColors = GetOutOfBattleDetectionColors(temtemWindows[focusedWindowProcessID], lpPoint);

                if (ColorDistance(pixelColors[0], spot5RGB) < maxAllowedColorDistance &&
                    ColorDistance(pixelColors[1], spot6RGB) < maxAllowedColorDistance &&
                    ColorDistance(pixelColors[2], spot7RGB) < maxAllowedColorDistance &&
                    ColorDistance(pixelColors[3], spot8RGB) < maxAllowedColorDistance)
                {
                    //Set battle to false
                    temtemWindows[focusedWindowProcessID].detectedBattle = false;
                }
            }
        }