From c76ad989f92168bc8fc89343cb0995704cd7646d Mon Sep 17 00:00:00 2001 From: Jeddunk Date: Sun, 23 Aug 2020 14:00:46 +0200 Subject: [PATCH] If a second SteamAPI DLL has been found, it will now automatically be patched too. Now handles error if file chooser was canceled. Now handles error if cream_api.ini file was not found, which would be the case if CreamAPI is going to be set up for the first time. --- README.md | 4 ++ src/main/java/Controller.java | 56 +++++++++++++++++++------- src/main/java/util/CreamApiConfig.java | 23 ++++++----- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 3d6ae7b..384b2d2 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,10 @@ Download the latest release and extract it into any folder (e.g. `%USERPROFILE%\ * Select a language and tick the options if needed. * Click on *"Save"*. +### Java 11 + + + ## License Auto-CreamAPI itself is licensed under the GNU General Public License v3.0 diff --git a/src/main/java/Controller.java b/src/main/java/Controller.java index 6599657..6b7ea6d 100644 --- a/src/main/java/Controller.java +++ b/src/main/java/Controller.java @@ -37,6 +37,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.Map; import java.util.Objects; @@ -283,12 +284,15 @@ public class Controller { state_label.setText("Ready."); } catch (ConfigurationException | IOException e) { e.printStackTrace(); + } catch (NullPointerException e) { + System.err.println("Could not set config file location! Did you cancel the file chooser?"); } } /** * check if creamapi version of dll is there, if not, rename original to steam_api(64)_o.dll and copy - * creamapi one to the path. + * creamapi dll to the path. also looks for alternative steam dll (which means it patches both 64 and 32 bit + * dlls at once if found) * * @throws IOException If file is missing or not accessible/modifiable. */ @@ -299,21 +303,43 @@ public class Controller { if (path.endsWith("steam_api64.dll")) { is64Bit = true; } - InputStream is = Files.newInputStream(path); - String md5 = DigestUtils.md5Hex(is); - boolean isSameFile = Objects.equals(md5, handler.getDllMd5(is64Bit)); - if (!isSameFile) { - String pathOrigString = steamApiPathString; - pathOrigString = - pathOrigString - .replaceFirst(REGEX, is64Bit ? "steam_api64_o.dll" : "steam_api_o.dll"); - Path pathOrig = Paths.get(pathOrigString); - if (!Files.exists(pathOrig)) { - Files.move(path, pathOrig); - } - Files.deleteIfExists(path); - Files.copy(handler.getDllPath(is64Bit), path); + copyDllFile(is64Bit, path); + findAdditionalDll(is64Bit, path); + } + } + + private void findAdditionalDll(boolean is64Bit, Path path) throws IOException { + String secondDllString = is64Bit ? "\\steam_api.dll" : "\\steam_api64.dll"; + Path dir = path.getParent(); + Path secondDll = Paths.get(dir.toString() + secondDllString); + if (Files.exists(secondDll)) { + System.out.println("Alternative DLL found!"); + copyDllFile(!is64Bit, secondDll); + } + } + + private void copyDllFile(boolean is64Bit, Path path) throws IOException { + InputStream is = Files.newInputStream(path); + String md5 = DigestUtils.md5Hex(is); + boolean isSameFile = Objects.equals(md5, handler.getDllMd5(is64Bit)); + if (!isSameFile) { + String pathOrigString = steamApiPathString; + pathOrigString = + pathOrigString + .replaceFirst(REGEX, is64Bit ? "steam_api64_o.dll" : "steam_api_o.dll"); + Path pathOrig = Paths.get(pathOrigString); + if (!Files.exists(pathOrig)) { + Files.move(path, pathOrig); + } else { + String pathBakString = steamApiPathString; + pathBakString = + pathBakString + .replaceFirst(REGEX, is64Bit ? "steam_api64.dll.backup" : "steam_api.dll.backup"); + Path pathBak = Paths.get(pathBakString); + Files.move(path, pathBak, StandardCopyOption.REPLACE_EXISTING); } + //Files.deleteIfExists(path); + Files.copy(handler.getDllPath(is64Bit), path); } } diff --git a/src/main/java/util/CreamApiConfig.java b/src/main/java/util/CreamApiConfig.java index 5e9efb9..6355e54 100644 --- a/src/main/java/util/CreamApiConfig.java +++ b/src/main/java/util/CreamApiConfig.java @@ -70,16 +70,21 @@ public class CreamApiConfig { } public void read() throws NullPointerException { - appId = config.getInt("steam.appid"); - language = config.getString("steam.language"); - if (language == null) { - language = "english"; + try { + appId = config.getInt("steam.appid"); + language = config.getString("steam.language"); + if (language == null) { + language = "english"; + } + unlockAll = config.getBoolean("steam.unlockall"); + extraProtection = config.getBoolean("steam.extraprotection"); + forceOffline = config.getBoolean("steam.forceoffline"); + final SubnodeConfiguration dlc_section = config.getSection("dlc"); + dlc_section.getKeys().forEachRemaining(k -> dlc.put(Integer.parseInt(k), dlc_section.getString(k))); + } catch (NoSuchElementException e) { + System.err.println("Error reading cream_api.ini! " + + "This can be ignored if setting up CreamAPI for the first time."); } - unlockAll = config.getBoolean("steam.unlockall"); - extraProtection = config.getBoolean("steam.extraprotection"); - forceOffline = config.getBoolean("steam.forceoffline"); - final SubnodeConfiguration dlc_section = config.getSection("dlc"); - dlc_section.getKeys().forEachRemaining(k -> dlc.put(Integer.parseInt(k), dlc_section.getString(k))); } public void sync() throws ConfigurationException {