Skip to content

Commit 0edc766

Browse files
committed
Detect Steam game and save paths on Linux
1 parent 5549218 commit 0edc766

File tree

1 file changed

+83
-25
lines changed

1 file changed

+83
-25
lines changed

src/main/java/br/com/pinter/tqrespec/tqdata/GameInfo.java

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
import br.com.pinter.tqrespec.save.SaveLocation;
2828
import br.com.pinter.tqrespec.util.Constants;
2929
import com.google.inject.Singleton;
30-
import com.sun.jna.platform.win32.*;
30+
import com.sun.jna.platform.win32.Advapi32Util;
31+
import com.sun.jna.platform.win32.Shell32Util;
32+
import com.sun.jna.platform.win32.ShlObj;
33+
import com.sun.jna.platform.win32.Win32Exception;
34+
import com.sun.jna.platform.win32.WinNT;
35+
import com.sun.jna.platform.win32.WinReg;
3136
import org.apache.commons.lang3.StringUtils;
3237
import org.apache.commons.lang3.SystemUtils;
3338

@@ -41,7 +46,12 @@
4146
import java.nio.file.Files;
4247
import java.nio.file.Path;
4348
import java.nio.file.Paths;
44-
import java.util.*;
49+
import java.util.ArrayList;
50+
import java.util.Collections;
51+
import java.util.HashMap;
52+
import java.util.List;
53+
import java.util.Locale;
54+
import java.util.Objects;
4555
import java.util.regex.Matcher;
4656
import java.util.regex.Pattern;
4757

@@ -59,6 +69,7 @@ public class GameInfo {
5969
private String gamePath = null;
6070
private InstallType installType = InstallType.UNKNOWN;
6171
private Path tqBasePath = null;
72+
private Path steamLibraryPathFound = null;
6273
private HashMap<String, String> gameOptions;
6374
private GameVersion installedVersion = GameVersion.UNKNOWN;
6475
private boolean dlcRagnarok = false;
@@ -122,26 +133,28 @@ private Path getGameSteamPath() {
122133

123134
private Path getSteamLibraryPath() {
124135
String steamPath = null;
125-
try {
126-
steamPath = Advapi32Util.registryGetStringValue(
127-
WinReg.HKEY_CURRENT_USER, REG_KEY_VALVE_STEAM, "SteamPath");
128-
} catch (Win32Exception e) {
129-
logger.log(System.Logger.Level.ERROR, "", e);
130-
}
136+
Path steamLibraryFolderVdf;
131137

132-
try {
133-
if (steamPath == null) {
138+
if (SystemUtils.IS_OS_WINDOWS) {
139+
try {
134140
steamPath = Advapi32Util.registryGetStringValue(
135-
WinReg.HKEY_LOCAL_MACHINE, REG_KEY_VALVE_STEAM, "InstallPath",
136-
WinNT.KEY_WOW64_32KEY);
141+
WinReg.HKEY_CURRENT_USER, REG_KEY_VALVE_STEAM, "SteamPath");
142+
143+
if (steamPath == null) {
144+
steamPath = Advapi32Util.registryGetStringValue(
145+
WinReg.HKEY_LOCAL_MACHINE, REG_KEY_VALVE_STEAM, "InstallPath",
146+
WinNT.KEY_WOW64_32KEY);
147+
}
148+
} catch (Win32Exception e) {
149+
logger.log(System.Logger.Level.ERROR, "", e);
150+
return null;
137151
}
138-
} catch (Win32Exception e) {
139-
logger.log(System.Logger.Level.ERROR, "", e);
140-
return null;
152+
steamLibraryFolderVdf = Paths.get(steamPath, "SteamApps", "libraryfolders.vdf").toAbsolutePath();
153+
} else {
154+
steamLibraryFolderVdf = Paths.get(System.getProperty("user.home"), ".steam", "steam", "config", "libraryfolders.vdf").toAbsolutePath();
141155
}
142156

143157
try {
144-
Path steamLibraryFolderVdf = Paths.get(steamPath, "SteamApps", "libraryfolders.vdf").toAbsolutePath();
145158

146159
List<String> libraryPaths = getLibraryPathsFromSteam(steamLibraryFolderVdf.toString());
147160

@@ -150,10 +163,11 @@ private Path getSteamLibraryPath() {
150163
for (String directory : libraryPaths) {
151164
logger.log(System.Logger.Level.DEBUG, "Trying library -- ''{0}''", directory);
152165

153-
Path libraryPath = Paths.get(directory, "SteamApps").toAbsolutePath();
166+
Path libraryPath = Paths.get(directory, "steamapps").toAbsolutePath();
154167
Path libraryGamePath = Paths.get(libraryPath.toString(), Constants.GAME_DIRECTORY_STEAM).toAbsolutePath();
155168
if (isValidGamePath(libraryGamePath)) {
156169
logger.log(System.Logger.Level.DEBUG, "VALID PATH FOUND!! -- ''{0}''", libraryGamePath);
170+
steamLibraryPathFound = libraryPath;
157171
return libraryPath;
158172
}
159173
}
@@ -222,6 +236,10 @@ private List<String> getLibraryPathsFromSteam(String configPath) {
222236
}
223237

224238
private Path getGameGogPath() {
239+
if (!SystemUtils.IS_OS_WINDOWS) {
240+
return null;
241+
}
242+
225243
try {
226244
//TQAE GOG 1196955511
227245
String gog = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE,
@@ -247,6 +265,10 @@ private Path getGameDiscTqPath() {
247265
}
248266

249267
private Path getGameDiscPath(String reg) {
268+
if (!SystemUtils.IS_OS_WINDOWS) {
269+
return null;
270+
}
271+
250272
try {
251273
String disc = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, reg, "Install Location", WinNT.KEY_WOW64_32KEY);
252274
if (StringUtils.isNotBlank(disc)) {
@@ -263,6 +285,10 @@ private Path getGameDiscPath(String reg) {
263285
}
264286

265287
private Path getGameInstalledPath(String regexGameName) {
288+
if (!SystemUtils.IS_OS_WINDOWS) {
289+
return null;
290+
}
291+
266292
String[] installedApps = new String[0];
267293
try {
268294
installedApps = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE,
@@ -293,6 +319,10 @@ private Path getGameInstalledPath(String regexGameName) {
293319
}
294320

295321
private Path getGameMicrosoftStorePath() {
322+
if (!SystemUtils.IS_OS_WINDOWS) {
323+
return null;
324+
}
325+
296326
String regexGameName = Constants.REGEX_REGISTRY_PACKAGE;
297327
String[] pkgList;
298328
String pkgKeyPath = "Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\" +
@@ -329,6 +359,10 @@ private Path getGameMicrosoftStorePath() {
329359
}
330360

331361
private Path getGameSteamApiBasedPath() {
362+
if (!SystemUtils.IS_OS_WINDOWS) {
363+
return null;
364+
}
365+
332366
try {
333367
String steamPath = Advapi32Util.registryGetStringValue(
334368
WinReg.HKEY_CURRENT_USER, REG_KEY_VALVE_STEAM, "SteamPath");
@@ -532,6 +566,10 @@ private String setDevGamePath(String path) {
532566
}
533567

534568
private String getLastUsedGamePath() throws GameNotFoundException {
569+
if (!SystemUtils.IS_OS_WINDOWS) {
570+
return null;
571+
}
572+
535573
String lastUsedPath = Settings.getLastDetectedGamePath();
536574
String lastUsedTqBase = Settings.getLastDetectedTqBasePath();
537575
GameVersion lastUsedVersion = GameVersion.fromValue(Settings.getLastDetectedGameVersion());
@@ -560,11 +598,6 @@ private String getLastUsedGamePath() throws GameNotFoundException {
560598
}
561599

562600
public String getGamePath() throws GameNotFoundException {
563-
if (StringUtils.isEmpty(gamePath) && !SystemUtils.IS_OS_WINDOWS) {
564-
logger.log(System.Logger.Level.DEBUG, "OS is not windows, using dev game path");
565-
return setDevGamePath(Constants.DEV_GAMEDATA);
566-
}
567-
568601
if (isValidLocalPath(Paths.get(Constants.DEV_GAMEDATA))) {
569602
logger.log(System.Logger.Level.INFO, "Local gamedata path found");
570603
return setDevGamePath(Constants.DEV_GAMEDATA);
@@ -587,6 +620,11 @@ public String getGamePath() throws GameNotFoundException {
587620
}
588621
}
589622

623+
if (StringUtils.isEmpty(gamePath) && !SystemUtils.IS_OS_WINDOWS) {
624+
logger.log(System.Logger.Level.DEBUG, "OS is not windows, using dev game path");
625+
return setDevGamePath(Constants.DEV_GAMEDATA);
626+
}
627+
590628
logger.log(System.Logger.Level.DEBUG, "Game data found: ''{0}''", gamePath);
591629
if (StringUtils.isEmpty(gamePath)) {
592630
removeSavedDetectedGame();
@@ -698,6 +736,18 @@ public String getSavePath() {
698736
logger.log(System.Logger.Level.DEBUG, "SavePath: user.home is ''{0}''", userHome);
699737

700738
if (!SystemUtils.IS_OS_WINDOWS) {
739+
if (gamePath == null || steamLibraryPathFound == null) {
740+
try {
741+
getGamePath();
742+
} catch (GameNotFoundException ignored) {
743+
}
744+
}
745+
if (installType == InstallType.STEAM) {
746+
return Path.of(
747+
steamLibraryPathFound.toString(),
748+
"compatdata", "475150", "pfx", "drive_c", "users", "steamuser", "Documents",
749+
Constants.SAVEGAME_SUBDIR).toAbsolutePath().toString();
750+
}
701751
prepareDevGameSaveData();
702752
return Constants.DEV_GAMEDATA;
703753
}
@@ -734,7 +784,7 @@ public String getSaveDataUserArchivedPath() {
734784
}
735785

736786
public String getSaveDataMainPath() {
737-
if (!SystemUtils.IS_OS_WINDOWS) {
787+
if (!SystemUtils.IS_OS_WINDOWS && installType != InstallType.STEAM) {
738788
prepareDevGameSaveData();
739789
return Paths.get(Constants.DEV_GAMEDATA, Constants.SAVEDATA, "Main").toString();
740790
}
@@ -746,7 +796,7 @@ public String getSaveDataMainPath() {
746796
}
747797

748798
public String getSaveDataUserPath() {
749-
if (!SystemUtils.IS_OS_WINDOWS) {
799+
if (!SystemUtils.IS_OS_WINDOWS && installType != InstallType.STEAM) {
750800
prepareDevGameSaveData();
751801
return Paths.get(Constants.DEV_GAMEDATA, Constants.SAVEDATA, "User").toString();
752802
}
@@ -759,7 +809,15 @@ public String getSaveDataUserPath() {
759809

760810
public String getSaveSetingsPath() {
761811
if (!SystemUtils.IS_OS_WINDOWS) {
762-
return Paths.get(Constants.DEV_GAMEDATA, Constants.SETTINGS).toString();
812+
if (gamePath == null) {
813+
try {
814+
getGamePath();
815+
} catch (GameNotFoundException ignored) {
816+
}
817+
}
818+
if (installType != InstallType.STEAM) {
819+
return Paths.get(Constants.DEV_GAMEDATA, Constants.SETTINGS).toString();
820+
}
763821
}
764822

765823
String savePath = getSavePath();

0 commit comments

Comments
 (0)