Skip to content

Commit

Permalink
Host does not try to change ingame speed if game is out of focus
Browse files Browse the repository at this point in the history
  • Loading branch information
davue committed Aug 10, 2020
1 parent 600be7e commit 41ee80a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@
<version>13</version>
</dependency>

<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.6.0</version>
</dependency>

<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.6.0</version>
</dependency>

<dependency>
<groupId>com.1stleg</groupId>
<artifactId>jnativehook</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down Expand Up @@ -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);
}

/**
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/github/davue/pss/server/WindowFocusListener.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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);
}
}

0 comments on commit 41ee80a

Please sign in to comment.