Added handler for CreamAPI's DLLs (contains path and md5 hash)

Fixed crash on startup if config file could not be found
This commit is contained in:
Jeddunk 2019-11-15 18:37:54 +01:00
parent 4cdb4037d1
commit f104570907
3 changed files with 92 additions and 25 deletions

View File

@ -6,12 +6,14 @@ import javafx.scene.control.Tooltip;
import javafx.stage.FileChooser; import javafx.stage.FileChooser;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.commons.configuration2.ex.ConfigurationException;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import pojo.App; import pojo.App;
import util.CreamApiConfig; import util.CreamApiConfig;
import util.CreamApiDllHandler;
import util.SteamAppsListCache; import util.SteamAppsListCache;
import java.io.File; import java.io.File;
@ -27,8 +29,9 @@ import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Controller { public class Controller {
private static final String X86_MD5_HASH = "c6f267a2d10b891ed352ed849a28d69b";
private static final String X64_MD5_HASH = "645c728a6117946294130d07cf6c0cae"; private static final String REGEX = "(?<steamApiDll>steam_api(?:64)?.dll)$";
private CreamApiDllHandler handler = CreamApiDllHandler.getInstance();
private CreamApiConfig config = CreamApiConfig.getInstance(); private CreamApiConfig config = CreamApiConfig.getInstance();
private SteamAppsListCache cache = new SteamAppsListCache(); private SteamAppsListCache cache = new SteamAppsListCache();
@FXML @FXML
@ -68,18 +71,22 @@ public class Controller {
//retrieveDlcList_button.setDisable(true); // WIP //retrieveDlcList_button.setDisable(true); // WIP
generate_tooltips(); generate_tooltips();
fix_dlc_textarea_prompt_text(); fix_dlc_textarea_prompt_text();
try {
read(); read();
} catch (NullPointerException e) {
System.err.println("Can't fill out fields, no configuration file set!");
}
} }
private void read() { private void read() {
language_combobox.setItems(FXCollections.observableArrayList(config.getLanguages()));
language_combobox.getSelectionModel().select(config.getLanguage());
appId_textfield.setText(config.getAppId().toString()); appId_textfield.setText(config.getAppId().toString());
dlc_textarea.setText(config.getDlcListAsString()); dlc_textarea.setText(config.getDlcListAsString());
game_name_textfield.setText(cache.getGame(config.getAppId()).getName()); game_name_textfield.setText(cache.getGame(config.getAppId()).getName());
extra_protection_checkbox.setSelected(config.getExtraProtection()); extra_protection_checkbox.setSelected(config.getExtraProtection());
offline_checkbox.setSelected(config.getForceOffline()); offline_checkbox.setSelected(config.getForceOffline());
unlock_all_checkbox.setSelected(config.getUnlockAll()); unlock_all_checkbox.setSelected(config.getUnlockAll());
language_combobox.setItems(FXCollections.observableArrayList(config.getLanguages()));
language_combobox.getSelectionModel().select(config.getLanguage());
unlockAll_disableDlcTextArea(); unlockAll_disableDlcTextArea();
} }
@ -111,26 +118,28 @@ public class Controller {
} }
public void reset() { public void reset() {
try {
config.read(); config.read();
read(); read();
} catch (NullPointerException e) {
System.err.println("Can't fill out fields, no configuration file set!");
}
} }
public void save() { public void save() {
try { try {
setUpCreamApi(); setUpCreamApi();
} catch (IOException e) {
e.printStackTrace();
}
config.setDlcListFromString(dlc_textarea.getText()); config.setDlcListFromString(dlc_textarea.getText());
config.setAppId(Integer.parseInt(appId_textfield.getText())); config.setAppId(Integer.parseInt(appId_textfield.getText()));
config.setExtraProtection(extra_protection_checkbox.isSelected()); config.setExtraProtection(extra_protection_checkbox.isSelected());
config.setForceOffline(offline_checkbox.isSelected()); config.setForceOffline(offline_checkbox.isSelected());
config.setUnlockAll(unlock_all_checkbox.isSelected()); config.setUnlockAll(unlock_all_checkbox.isSelected());
config.setLanguage(language_combobox.getValue()); config.setLanguage(language_combobox.getValue());
try {
config.sync(); config.sync();
} catch (ConfigurationException e) { } catch (IOException | ConfigurationException e) {
e.printStackTrace(); 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); 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (NullPointerException e) { } catch (NullPointerException e) {
@ -233,19 +246,18 @@ public class Controller {
} }
InputStream is = Files.newInputStream(path); InputStream is = Files.newInputStream(path);
String md5 = DigestUtils.md5Hex(is); 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) { if (!isSameFile) {
String pathOrigString = steamApiPathString; String pathOrigString = steamApiPathString;
pathOrigString = pathOrigString =
pathOrigString pathOrigString
.replaceFirst("(?<steamApiDll>steam_api(?:64)?.dll)$", .replaceFirst(REGEX, is64Bit ? "steam_api64_o.dll" : "steam_api_o.dll");
is64Bit ? "steam_api64_o.dll" : "steam_api_o.dll");
Path pathOrig = Paths.get(pathOrigString); Path pathOrig = Paths.get(pathOrigString);
if (!Files.exists(pathOrig)) { if (!Files.exists(pathOrig)) {
Files.move(path, pathOrig); Files.move(path, pathOrig);
} }
Files.deleteIfExists(path); Files.deleteIfExists(path);
Files.copy(Paths.get(is64Bit ? "steam_api64.dll" : "steam_api.dll"), path); Files.copy(handler.getDllPath(is64Bit), path);
} }
} }
} }

View File

@ -28,7 +28,8 @@ public class CreamApiConfig {
config = CONFIGS.ini(path); config = CONFIGS.ini(path);
config.setCommentLeadingCharsUsedInInput(";"); config.setCommentLeadingCharsUsedInInput(";");
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
e.printStackTrace(); System.err.println("No config file found in default location!");
//e.printStackTrace();
} }
File langFile = new File("languages.txt"); File langFile = new File("languages.txt");
@ -38,7 +39,12 @@ public class CreamApiConfig {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try {
read(); read();
} catch (NullPointerException e) {
System.err.println("Can't fill out fields, no configuration file set!");
}
} }
public static synchronized CreamApiConfig getInstance() { public static synchronized CreamApiConfig getInstance() {
@ -48,7 +54,7 @@ public class CreamApiConfig {
return configInstance; return configInstance;
} }
public void read() { public void read() throws NullPointerException {
appId = config.getInt("steam.appid"); appId = config.getInt("steam.appid");
language = config.getString("steam.language"); language = config.getString("steam.language");
if (language == null) { if (language == null) {

View File

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