save and getdlclist functions as services

added state label at bottom
ripple fill color are consistent now
This commit is contained in:
Jeddunk 2019-11-26 11:19:32 +01:00
parent c5ddd55fec
commit d33c14bab8
3 changed files with 126 additions and 70 deletions

View File

@ -1,7 +1,10 @@
import com.jfoenix.controls.*; import com.jfoenix.controls.*;
import javafx.beans.value.ChangeListener; import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip; 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;
@ -35,6 +38,8 @@ public class Controller {
private CreamApiConfig config = CreamApiConfig.getInstance(); private CreamApiConfig config = CreamApiConfig.getInstance();
private SteamAppsListCache cache = new SteamAppsListCache(); private SteamAppsListCache cache = new SteamAppsListCache();
@FXML @FXML
public Label state_label;
@FXML
public JFXTextField path_textfield; public JFXTextField path_textfield;
@FXML @FXML
public JFXTextField appId_textfield; public JFXTextField appId_textfield;
@ -127,20 +132,40 @@ public class Controller {
} }
public void save() { public void save() {
try { Service<Void> s = new Service<Void>() {
setUpCreamApi(); @Override
config.setDlcListFromString(dlc_textarea.getText()); protected Task<Void> createTask() {
config.setAppId(Integer.parseInt(appId_textfield.getText())); return new Task<Void>() {
config.setExtraProtection(extra_protection_checkbox.isSelected()); @Override
config.setForceOffline(offline_checkbox.isSelected()); protected Void call() {
config.setUnlockAll(unlock_all_checkbox.isSelected()); try {
config.setLanguage(language_combobox.getValue()); setUpCreamApi();
config.sync(); config.setDlcListFromString(dlc_textarea.getText());
} catch (IOException | ConfigurationException e) { config.setAppId(Integer.parseInt(appId_textfield.getText()));
e.printStackTrace(); config.setExtraProtection(extra_protection_checkbox.isSelected());
} catch (NullPointerException e) { config.setForceOffline(offline_checkbox.isSelected());
System.err.println("No configuration file set!"); config.setUnlockAll(unlock_all_checkbox.isSelected());
} config.setLanguage(language_combobox.getValue());
config.sync();
} catch (IOException | ConfigurationException e) {
e.printStackTrace();
} catch (NullPointerException e) {
System.err.println("No configuration file set!");
}
return null;
}
};
}
};
s.setOnRunning(event -> {
setDisableAllButtons(true);
state_label.setText("Saving...");
});
s.setOnSucceeded(event -> {
setDisableAllButtons(false);
state_label.setText("Saved successfully!");
});
s.start();
} }
public void getAppId() { public void getAppId() {
@ -162,56 +187,76 @@ public class Controller {
* also lists DLC not available for purchase. * also lists DLC not available for purchase.
*/ */
public void getDlcList() { public void getDlcList() {
Map<Integer, String> steamStoreDLCs = new HashMap<>(); Service<Void> s = new Service<Void>() {
Map<Integer, String> steamDbDLCs = new HashMap<>(); @Override
//StringBuilder sb = new StringBuilder(); protected Task<Void> createTask() {
try { return new Task<Void>() {
// Steam Store @Override
Document steamDoc = Jsoup protected Void call() {
.connect("https://store.steampowered.com/app/" + appId_textfield.getText() + "/") Map<Integer, String> steamStoreDLCs = new HashMap<>();
.get(); Map<Integer, String> steamDbDLCs = new HashMap<>();
Elements steamDLCs = steamDoc.getElementsByClass("game_area_dlc_row"); //StringBuilder sb = new StringBuilder();
for (Element dlc : steamDLCs) { try {
String dlc_id = dlc.attr("data-ds-appid"); // Steam Store
String dlc_name = dlc Document steamDoc = Jsoup
.getElementsByClass("game_area_dlc_name") .connect("https://store.steampowered.com/app/" + appId_textfield.getText() + "/")
.text().replace("\n", "").trim(); .get();
steamStoreDLCs.put(Integer.parseInt(dlc_id), dlc_name); Elements steamDLCs = steamDoc.getElementsByClass("game_area_dlc_row");
} for (Element dlc : steamDLCs) {
// SteamDB String dlc_id = dlc.attr("data-ds-appid");
Document steamDbDoc = Jsoup String dlc_name = dlc
.connect("https://steamdb.info/app/" + appId_textfield.getText() + "/dlc/") .getElementsByClass("game_area_dlc_name")
.get(); .text().replace("\n", "").trim();
Element steamDbDlcSection = steamDbDoc.getElementById("dlc"); steamStoreDLCs.put(Integer.parseInt(dlc_id), dlc_name);
Elements steamDbDLCElements = steamDbDlcSection.getElementsByClass("app"); }
for (Element dlc : steamDbDLCElements) { // SteamDB
String dlc_id = dlc.attr("data-appid"); Document steamDbDoc = Jsoup
String dlc_name = "Unknown DLC " + dlc_id; .connect("https://steamdb.info/app/" + appId_textfield.getText() + "/dlc/")
Elements td = dlc.getElementsByTag("td"); .get();
if (!td.isEmpty()) { Element steamDbDlcSection = steamDbDoc.getElementById("dlc");
dlc_name = td.get(1).text().replace("\n", "").trim(); Elements steamDbDLCElements = steamDbDlcSection.getElementsByClass("app");
} for (Element dlc : steamDbDLCElements) {
steamDbDLCs.put(Integer.parseInt(dlc_id), dlc_name); String dlc_id = dlc.attr("data-appid");
} String dlc_name = "Unknown DLC " + dlc_id;
} catch (HttpStatusException e) { Elements td = dlc.getElementsByTag("td");
if (e.getStatusCode() == 404) { if (!td.isEmpty()) {
System.err.println("App ID empty or not found! (HTTP Status Code: 404)"); dlc_name = td.get(1).text().replace("\n", "").trim();
} }
} catch (IOException e) { steamDbDLCs.put(Integer.parseInt(dlc_id), dlc_name);
e.printStackTrace(); }
} catch (NullPointerException e) { } catch (HttpStatusException e) {
// ignore 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) {
// ignore
}
Map<Integer, String> allDLCs = new HashMap<>(steamStoreDLCs); Map<Integer, String> allDLCs = new HashMap<>(steamStoreDLCs);
steamDbDLCs.forEach(allDLCs::putIfAbsent); steamDbDLCs.forEach(allDLCs::putIfAbsent);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
LinkedHashMap<Number, String> collect = allDLCs.entrySet().stream() LinkedHashMap<Number, String> collect = allDLCs.entrySet().stream()
.sorted(Map.Entry.comparingByKey()) .sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new)); (oldValue, newValue) -> oldValue, LinkedHashMap::new));
collect.forEach((k, v) -> sb.append(k).append("=").append(v).append("\n")); collect.forEach((k, v) -> sb.append(k).append("=").append(v).append("\n"));
dlc_textarea.setText(sb.toString()); dlc_textarea.setText(sb.toString());
return null;
}
};
}
};
s.setOnRunning(event -> {
setDisableAllButtons(true);
state_label.setText("Getting list of DLCs...");
});
s.setOnSucceeded(event -> {
setDisableAllButtons(false);
state_label.setText("Got list of DLCs successfully!");
});
s.start();
} }
public void openFileChooser() { public void openFileChooser() {
@ -262,4 +307,12 @@ public class Controller {
} }
} }
} }
private void setDisableAllButtons(boolean b) {
reset_button.setDisable(b);
save_button.setDisable(b);
getAppId_button.setDisable(b);
path_button.setDisable(b);
retrieveDlcList_button.setDisable(b);
}
} }

View File

@ -11,7 +11,7 @@ public class Main extends Application {
Parent root = FXMLLoader.load(ClassLoader.getSystemResource("mainWindow.fxml"));//getClass().getResource("/mainWindow.fxml") Parent root = FXMLLoader.load(ClassLoader.getSystemResource("mainWindow.fxml"));//getClass().getResource("/mainWindow.fxml")
primaryStage.setTitle("Auto CreamAPI"); primaryStage.setTitle("Auto CreamAPI");
primaryStage.setMinWidth(655 + 25); primaryStage.setMinWidth(655 + 25);
primaryStage.setMinHeight(360 + 50); primaryStage.setMinHeight(400 + 50);
primaryStage.setScene(new Scene(root, 1200, 600)); primaryStage.setScene(new Scene(root, 1200, 600));
primaryStage.show(); primaryStage.show();
} }

View File

@ -7,11 +7,12 @@
<?import com.jfoenix.controls.JFXTextField?> <?import com.jfoenix.controls.JFXTextField?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?> <?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="10.0" minHeight="360.0" minWidth="655.0" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller"> <GridPane hgap="10.0" minHeight="400.0" minWidth="655.0" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="-Infinity" prefWidth="375.0" /> <ColumnConstraints hgrow="SOMETIMES" maxWidth="1.7976931348623157E308" minWidth="-Infinity" prefWidth="375.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="120.0" /> <ColumnConstraints hgrow="SOMETIMES" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="120.0" />
@ -26,18 +27,19 @@
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="-Infinity" prefHeight="60.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="-Infinity" prefHeight="60.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="-Infinity" minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints> </rowConstraints>
<padding> <padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding> </padding>
<JFXTextField fx:id="path_textfield" promptText="Path to game's steam_api(64).dll..." GridPane.columnSpan="2" /> <JFXTextField fx:id="path_textfield" promptText="Path to game's steam_api(64).dll..." GridPane.columnSpan="2" />
<JFXButton fx:id="path_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#openFileChooser" GridPane.columnIndex="2"> <JFXButton fx:id="path_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#openFileChooser" ripplerFill="BLACK" style="-fx-background-color: #ddd;" GridPane.columnIndex="2">
<graphic> <graphic>
<FontAwesomeIconView glyphName="FOLDER_OPEN" glyphSize="24" /> <FontAwesomeIconView glyphName="FOLDER_OPEN" glyphSize="24" />
</graphic> </graphic>
</JFXButton> </JFXButton>
<JFXTextField fx:id="game_name_textfield" promptText="Game..." GridPane.rowIndex="1" /> <JFXTextField fx:id="game_name_textfield" promptText="Game..." GridPane.rowIndex="1" />
<JFXButton fx:id="getAppId_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#getAppId" GridPane.columnIndex="1" GridPane.rowIndex="1"> <JFXButton fx:id="getAppId_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#getAppId" ripplerFill="BLACK" style="-fx-background-color: #ddd;" GridPane.columnIndex="1" GridPane.rowIndex="1">
<graphic> <graphic>
<FontAwesomeIconView glyphName="SEARCH" glyphSize="24" /> <FontAwesomeIconView glyphName="SEARCH" glyphSize="24" />
</graphic> </graphic>
@ -48,7 +50,8 @@
<JFXCheckBox fx:id="extra_protection_checkbox" text="Try to bypass game-specific protection" GridPane.columnSpan="2147483647" GridPane.rowIndex="4" /> <JFXCheckBox fx:id="extra_protection_checkbox" text="Try to bypass game-specific protection" GridPane.columnSpan="2147483647" GridPane.rowIndex="4" />
<JFXCheckBox fx:id="unlock_all_checkbox" onAction="#unlockAll_disableDlcTextArea" text="Unlock all DLC (if possible)" GridPane.columnSpan="2147483647" GridPane.rowIndex="5" /> <JFXCheckBox fx:id="unlock_all_checkbox" onAction="#unlockAll_disableDlcTextArea" text="Unlock all DLC (if possible)" GridPane.columnSpan="2147483647" GridPane.rowIndex="5" />
<JFXTextArea fx:id="dlc_textarea" promptText="List of DLC..." GridPane.columnSpan="2147483647" GridPane.rowIndex="6" /> <JFXTextArea fx:id="dlc_textarea" promptText="List of DLC..." GridPane.columnSpan="2147483647" GridPane.rowIndex="6" />
<JFXButton fx:id="retrieveDlcList_button" maxHeight="1.7976931348623157E308" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onAction="#getDlcList" text="Get DLCs for AppID" GridPane.rowIndex="7" /> <JFXButton fx:id="retrieveDlcList_button" maxHeight="1.7976931348623157E308" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onAction="#getDlcList" ripplerFill="BLACK" style="-fx-background-color: #ddd;" text="Get DLCs for AppID" GridPane.rowIndex="7" />
<JFXButton fx:id="save_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#save" text="Save" GridPane.columnIndex="1" GridPane.rowIndex="7" /> <JFXButton fx:id="save_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#save" ripplerFill="BLACK" style="-fx-background-color: #ddd;" text="Save" GridPane.columnIndex="1" GridPane.rowIndex="7" />
<JFXButton fx:id="reset_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#reset" text="Reset" GridPane.columnIndex="2" GridPane.rowIndex="7" /> <JFXButton fx:id="reset_button" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onAction="#reset" ripplerFill="BLACK" style="-fx-background-color: #ddd;" text="Reset" GridPane.columnIndex="2" GridPane.rowIndex="7" />
<Label fx:id="state_label" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" GridPane.columnSpan="2147483647" GridPane.rowIndex="8" />
</GridPane> </GridPane>