From f104570907ec9ecfff648268957a21629b399b0d Mon Sep 17 00:00:00 2001 From: Jeddunk Date: Fri, 15 Nov 2019 18:37:54 +0100 Subject: [PATCH] Added handler for CreamAPI's DLLs (contains path and md5 hash) Fixed crash on startup if config file could not be found --- src/main/java/Controller.java | 56 +++++++++++++--------- src/main/java/util/CreamApiConfig.java | 12 +++-- src/main/java/util/CreamApiDllHandler.java | 49 +++++++++++++++++++ 3 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 src/main/java/util/CreamApiDllHandler.java diff --git a/src/main/java/Controller.java b/src/main/java/Controller.java index 45a66d3..dd60a49 100644 --- a/src/main/java/Controller.java +++ b/src/main/java/Controller.java @@ -6,12 +6,14 @@ import javafx.scene.control.Tooltip; import javafx.stage.FileChooser; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.configuration2.ex.ConfigurationException; +import org.jsoup.HttpStatusException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import pojo.App; import util.CreamApiConfig; +import util.CreamApiDllHandler; import util.SteamAppsListCache; import java.io.File; @@ -27,8 +29,9 @@ import java.util.Objects; import java.util.stream.Collectors; public class Controller { - private static final String X86_MD5_HASH = "c6f267a2d10b891ed352ed849a28d69b"; - private static final String X64_MD5_HASH = "645c728a6117946294130d07cf6c0cae"; + + private static final String REGEX = "(?steam_api(?:64)?.dll)$"; + private CreamApiDllHandler handler = CreamApiDllHandler.getInstance(); private CreamApiConfig config = CreamApiConfig.getInstance(); private SteamAppsListCache cache = new SteamAppsListCache(); @FXML @@ -68,18 +71,22 @@ public class Controller { //retrieveDlcList_button.setDisable(true); // WIP generate_tooltips(); fix_dlc_textarea_prompt_text(); - read(); + try { + read(); + } catch (NullPointerException e) { + System.err.println("Can't fill out fields, no configuration file set!"); + } } private void read() { + language_combobox.setItems(FXCollections.observableArrayList(config.getLanguages())); + language_combobox.getSelectionModel().select(config.getLanguage()); appId_textfield.setText(config.getAppId().toString()); dlc_textarea.setText(config.getDlcListAsString()); game_name_textfield.setText(cache.getGame(config.getAppId()).getName()); extra_protection_checkbox.setSelected(config.getExtraProtection()); offline_checkbox.setSelected(config.getForceOffline()); unlock_all_checkbox.setSelected(config.getUnlockAll()); - language_combobox.setItems(FXCollections.observableArrayList(config.getLanguages())); - language_combobox.getSelectionModel().select(config.getLanguage()); unlockAll_disableDlcTextArea(); } @@ -111,26 +118,28 @@ public class Controller { } public void reset() { - config.read(); - read(); + try { + config.read(); + read(); + } catch (NullPointerException e) { + System.err.println("Can't fill out fields, no configuration file set!"); + } } public void save() { try { setUpCreamApi(); - } catch (IOException e) { - e.printStackTrace(); - } - config.setDlcListFromString(dlc_textarea.getText()); - config.setAppId(Integer.parseInt(appId_textfield.getText())); - config.setExtraProtection(extra_protection_checkbox.isSelected()); - config.setForceOffline(offline_checkbox.isSelected()); - config.setUnlockAll(unlock_all_checkbox.isSelected()); - config.setLanguage(language_combobox.getValue()); - try { + config.setDlcListFromString(dlc_textarea.getText()); + config.setAppId(Integer.parseInt(appId_textfield.getText())); + config.setExtraProtection(extra_protection_checkbox.isSelected()); + config.setForceOffline(offline_checkbox.isSelected()); + config.setUnlockAll(unlock_all_checkbox.isSelected()); + config.setLanguage(language_combobox.getValue()); config.sync(); - } catch (ConfigurationException e) { + } catch (IOException | ConfigurationException e) { e.printStackTrace(); + } catch (NullPointerException e) { + System.err.println("No configuration file set!"); } } @@ -184,6 +193,10 @@ public class Controller { } steamDbDLCs.put(Integer.parseInt(dlc_id), dlc_name); } + } catch (HttpStatusException e) { + if (e.getStatusCode() == 404) { + System.err.println("App ID empty or not found! (HTTP Status Code: 404)"); + } } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { @@ -233,19 +246,18 @@ public class Controller { } InputStream is = Files.newInputStream(path); String md5 = DigestUtils.md5Hex(is); - boolean isSameFile = Objects.equals(md5, is64Bit ? X64_MD5_HASH : X86_MD5_HASH); + boolean isSameFile = Objects.equals(md5, handler.getDllMd5(is64Bit)); if (!isSameFile) { String pathOrigString = steamApiPathString; pathOrigString = pathOrigString - .replaceFirst("(?steam_api(?:64)?.dll)$", - is64Bit ? "steam_api64_o.dll" : "steam_api_o.dll"); + .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(Paths.get(is64Bit ? "steam_api64.dll" : "steam_api.dll"), path); + Files.copy(handler.getDllPath(is64Bit), path); } } } diff --git a/src/main/java/util/CreamApiConfig.java b/src/main/java/util/CreamApiConfig.java index 06acf13..57f255e 100644 --- a/src/main/java/util/CreamApiConfig.java +++ b/src/main/java/util/CreamApiConfig.java @@ -28,7 +28,8 @@ public class CreamApiConfig { config = CONFIGS.ini(path); config.setCommentLeadingCharsUsedInInput(";"); } catch (ConfigurationException e) { - e.printStackTrace(); + System.err.println("No config file found in default location!"); + //e.printStackTrace(); } File langFile = new File("languages.txt"); @@ -38,7 +39,12 @@ public class CreamApiConfig { } catch (IOException e) { e.printStackTrace(); } - read(); + + try { + read(); + } catch (NullPointerException e) { + System.err.println("Can't fill out fields, no configuration file set!"); + } } public static synchronized CreamApiConfig getInstance() { @@ -48,7 +54,7 @@ public class CreamApiConfig { return configInstance; } - public void read() { + public void read() throws NullPointerException { appId = config.getInt("steam.appid"); language = config.getString("steam.language"); if (language == null) { diff --git a/src/main/java/util/CreamApiDllHandler.java b/src/main/java/util/CreamApiDllHandler.java new file mode 100644 index 0000000..8027047 --- /dev/null +++ b/src/main/java/util/CreamApiDllHandler.java @@ -0,0 +1,49 @@ +package util; + +import org.apache.commons.codec.digest.DigestUtils; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class CreamApiDllHandler { + + private static CreamApiDllHandler creamApiDllHandlerInstance; + private final Path steamApiDllPath = Paths.get("steam_api.dll"); + private final Path steamApi64DllPath = Paths.get("steam_api64.dll"); + private final String steamApiDllMd5; + private final String steamApi64DllMd5; + + private CreamApiDllHandler() { + String steamApiDllMd5 = ""; + String steamApi64DllMd5 = ""; + try { + steamApiDllMd5 = DigestUtils.md5Hex(Files.newInputStream(steamApiDllPath)); + } catch (IOException e) { + System.err.println("File missing: " + steamApiDllPath.toAbsolutePath().toString()); + } + try { + steamApi64DllMd5 = DigestUtils.md5Hex(Files.newInputStream(steamApi64DllPath)); + } catch (IOException e) { + System.err.println("File missing: " + steamApi64DllPath.toAbsolutePath().toString()); + } + this.steamApiDllMd5 = steamApiDllMd5; + this.steamApi64DllMd5 = steamApi64DllMd5; + } + + public static synchronized CreamApiDllHandler getInstance() { + if (creamApiDllHandlerInstance == null) { + creamApiDllHandlerInstance = new CreamApiDllHandler(); + } + return creamApiDllHandlerInstance; + } + + public Path getDllPath(boolean is64Bit) { + return is64Bit ? steamApi64DllPath : steamApiDllPath; + } + + public String getDllMd5(boolean is64Bit) { + return is64Bit ? steamApi64DllMd5 : steamApiDllMd5; + } +}