From 41ee80ae30133a7321d2dec53d823d463f613eed Mon Sep 17 00:00:00 2001 From: davue Date: Mon, 10 Aug 2020 19:29:45 +0200 Subject: [PATCH] Host does not try to change ingame speed if game is out of focus --- pom.xml | 12 ++++ .../davue/pss/server/SpeedNegotiator.java | 7 ++- .../davue/pss/server/WindowFocusListener.java | 59 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/github/davue/pss/server/WindowFocusListener.java diff --git a/pom.xml b/pom.xml index 1db8e5b..7634c05 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,18 @@ 13 + + net.java.dev.jna + jna + 5.6.0 + + + + net.java.dev.jna + jna-platform + 5.6.0 + + com.1stleg jnativehook diff --git a/src/main/java/com/github/davue/pss/server/SpeedNegotiator.java b/src/main/java/com/github/davue/pss/server/SpeedNegotiator.java index 3a702a2..4a58033 100644 --- a/src/main/java/com/github/davue/pss/server/SpeedNegotiator.java +++ b/src/main/java/com/github/davue/pss/server/SpeedNegotiator.java @@ -44,6 +44,9 @@ public void check() { if (clients.isEmpty()) return; + if (!WindowFocusListener.isGameFocused()) + return; + boolean doNothing = false; if (server.SPEED_UP_KEY == 0) { server.LOGGER.warn("No ingame speed up key set!"); @@ -83,9 +86,9 @@ public void check() { robot.keyRelease(server.SPEED_UP_KEY); server.hostSpeed++; } - - server.LOGGER.info("Host is now running at speed: {}", server.hostSpeed); } + + server.LOGGER.info("Host is now running at speed: {}", server.hostSpeed); } /** diff --git a/src/main/java/com/github/davue/pss/server/WindowFocusListener.java b/src/main/java/com/github/davue/pss/server/WindowFocusListener.java new file mode 100644 index 0000000..40c1610 --- /dev/null +++ b/src/main/java/com/github/davue/pss/server/WindowFocusListener.java @@ -0,0 +1,59 @@ +/* + * A cross-platform tool to overcome the limitations of the speed controls of Paradox Interactive games. + * Copyright (C) 2020 David Enderlin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.github.davue.pss.server; + +import com.sun.jna.Platform; +import com.sun.jna.platform.win32.*; +import com.sun.jna.ptr.IntByReference; + +public class WindowFocusListener { + private static final User32 user32 = User32.INSTANCE; + private static final Kernel32 kernel32 = Kernel32.INSTANCE; + private static final Psapi psapi = Psapi.INSTANCE; + + private static final String[] validGames = {"ck2game", "stellaris", "hoi4", "eu4"}; + + public static boolean isGameFocused() { + if (Platform.isWindows()) { + String processPath = getFocusedProcess(); + String[] split = processPath.split("\\\\"); + String executableName = split[split.length - 1].toLowerCase(); + for (String game : validGames) { + String validName = game + ".exe"; + if (executableName.substring(0, validName.length()).equals(validName)) + return true; + } + + return false; + } else + return true; + } + + private static String getFocusedProcess() { + int PROCESS_QUERY_INFORMATION = 0x0400; + WinDef.HWND windowHandle = user32.GetForegroundWindow(); + IntByReference pid = new IntByReference(); + user32.GetWindowThreadProcessId(windowHandle, pid); + WinNT.HANDLE processHandle = kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, true, pid.getValue()); + + byte[] filename = new byte[512]; + Psapi.INSTANCE.GetModuleFileNameExA(processHandle, null, filename, filename.length); + return new String(filename); + } +}