From 14408cd9a6ff392a187b15ccbe20b4626a5d016b Mon Sep 17 00:00:00 2001 From: Jeddunk Date: Wed, 6 Nov 2019 01:21:43 +0100 Subject: [PATCH] Languages can now be defined in a file. INI properties which are not editable will now be set. Code improvements. --- languages.txt | 16 ++++++ src/main/java/Controller.java | 3 +- src/main/java/CreamApiConfig.java | 80 +++++++-------------------- src/main/java/Main.java | 1 - src/main/java/SteamAppsListCache.java | 15 ++--- 5 files changed, 40 insertions(+), 75 deletions(-) create mode 100644 languages.txt diff --git a/languages.txt b/languages.txt new file mode 100644 index 0000000..f1bee6a --- /dev/null +++ b/languages.txt @@ -0,0 +1,16 @@ +# List of languages +# https://partner.steamgames.com/doc/store/localization#supported_languages + +english +latam +brazilian +german +french +italian +portuguese +spanish +russian +schinese +tchinese +japanese +koreana diff --git a/src/main/java/Controller.java b/src/main/java/Controller.java index abed79c..d29c1bc 100644 --- a/src/main/java/Controller.java +++ b/src/main/java/Controller.java @@ -1,7 +1,6 @@ import com.ibasco.agql.protocols.valve.steam.webapi.pojos.SteamApp; import com.jfoenix.controls.*; import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.event.ActionEvent; import javafx.fxml.FXML; @@ -99,7 +98,7 @@ public class Controller { } public void getAppId() { - final SteamApp game = cache.searchGame(game_name_textfield.getText()); + final SteamApp game = cache.findGame(game_name_textfield.getText()); if (game == null) { appId_textfield.setText("-1"); } else { diff --git a/src/main/java/CreamApiConfig.java b/src/main/java/CreamApiConfig.java index c511fbe..4c9c45d 100644 --- a/src/main/java/CreamApiConfig.java +++ b/src/main/java/CreamApiConfig.java @@ -1,11 +1,9 @@ -import org.apache.commons.configuration2.INIConfiguration; -import org.apache.commons.configuration2.SubnodeConfiguration; +import org.apache.commons.configuration2.*; import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; import org.apache.commons.configuration2.builder.fluent.Configurations; import org.apache.commons.configuration2.ex.ConfigurationException; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.text.MessageFormat; import java.util.*; @@ -16,7 +14,6 @@ public class CreamApiConfig { private INIConfiguration config; private String path = "cream_api.ini"; - //private Ini ini; private Integer appId; private String language; private Boolean unlockAll; @@ -32,27 +29,14 @@ public class CreamApiConfig { } catch (ConfigurationException e) { e.printStackTrace(); } - /*try { - //ini = new Ini(new File("cream_api.ini")); - //Config iniConfig = Config.getGlobal().clone(); + + File langFile = new File("languages.txt"); + try { + BufferedReader fIn = new BufferedReader(new FileReader(langFile)); + fIn.lines().filter(line -> !line.isEmpty() && !line.startsWith("#")).forEach(line -> languages.add(line)); } catch (IOException e) { - System.err.println("Can't open \"cream_api.ini\"!"); e.printStackTrace(); - }*/ - // https://partner.steamgames.com/doc/store/localization#supported_languages - languages.add("english"); - languages.add("latam"); - languages.add("brazilian"); - languages.add("german"); - languages.add("french"); - languages.add("italian"); - languages.add("portuguese"); - languages.add("spanish"); - languages.add("russian"); - languages.add("schinese"); - languages.add("tchinese"); - languages.add("japanese"); - languages.add("koreana"); + } read(); } @@ -64,17 +48,6 @@ public class CreamApiConfig { } public void read() { - /*appId = ini.get("steam", "appid", int.class); - language = ini.get("steam", "language", String.class); - if (language == null) { - language = "english"; - } - unlockAll = ini.get("steam", "unlockall", boolean.class); - extraProtection = ini.get("steam", "extraprotection", boolean.class); - forceOffline = ini.get("steam", "forceoffline", boolean.class); - Map dlc_temp = ini.get("dlc"); - for (Map.Entry e: dlc_temp.entrySet()) - dlc.put(Integer.parseInt(e.getKey()), e.getValue());*/ appId = config.getInt("steam.appid"); language = config.getString("steam.language"); if (language == null) { @@ -84,23 +57,10 @@ public class CreamApiConfig { extraProtection = config.getBoolean("steam.extraprotection"); forceOffline = config.getBoolean("steam.forceoffline"); final SubnodeConfiguration dlc_section = config.getSection("dlc"); - for (Iterator it = dlc_section.getKeys(); it.hasNext(); ) { - String k = it.next(); - final String v = dlc_section.getString(k); - dlc.put(Integer.parseInt(k), v); - } + dlc_section.getKeys().forEachRemaining(k -> dlc.put(Integer.parseInt(k), dlc_section.getString(k))); } public void sync() throws ConfigurationException { - /*ini.put("steam", "appid", appId); - ini.put("steam", "language", language); - ini.put("steam", "unlockall", unlockAll); - ini.put("steam", "extraprotection", extraProtection); - ini.put("steam", "forceoffline", forceOffline); - for (Map.Entry e: dlc.entrySet()) { - ini.put("dlc", e.getKey().toString(), e.getValue()); - } - ini.store();*/ FileBasedConfigurationBuilder builder = CONFIGS.iniBuilder(path); config = builder.getConfiguration(); config.setCommentLeadingCharsUsedInInput(";"); @@ -112,9 +72,13 @@ public class CreamApiConfig { config.setProperty("steam.forceoffline", forceOffline); final SubnodeConfiguration dlc_section = config.getSection("dlc"); dlc_section.clear(); - for (Map.Entry e : this.dlc.entrySet()) { - config.setProperty("dlc." + e.getKey().toString(), e.getValue()); - } + this.dlc.forEach((key, value) -> config.setProperty("dlc." + key.toString(), value)); + + // default settings + config.setProperty("steam.orgapi", "steam_api_o.dll"); + config.setProperty("steam.orgapi64", "steam_api64_o.dll"); + config.setProperty("steam.lowviolence", false); + config.setProperty("steam_misc.disableuserinterface", false); builder.save(); } @@ -123,23 +87,17 @@ public class CreamApiConfig { public String getDlcListAsString() { StringBuilder sb = new StringBuilder(); - for (Map.Entry e: dlc.entrySet()) { - sb.append(e.getKey()); - sb.append("="); - sb.append(e.getValue()); - sb.append("\r\n"); - } + dlc.forEach((key, value) -> sb.append(key).append("=").append(value).append("\r\n")); return sb.toString(); } public void setDlcListFromString(String str) { dlc.clear(); - final String[] lines = str.split("\\R+"); - for (String line : lines) { + Arrays.stream(str.split("\\R+")).forEach(line -> { final String[] split = line.split("\\s*=\\s*", 2); if (split.length == 2) dlc.put(Integer.parseInt(split[0]), split[1]); else System.err.println(MessageFormat.format("Error while splitting line: \"{0}\"", line)); - } + }); } // GETTERS AND SETTERS diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 3974de4..1d2a0cf 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -16,7 +16,6 @@ public class Main extends Application { primaryStage.show(); } - public static void main(String[] args) { launch(args); } diff --git a/src/main/java/SteamAppsListCache.java b/src/main/java/SteamAppsListCache.java index 38fa235..81cbe48 100644 --- a/src/main/java/SteamAppsListCache.java +++ b/src/main/java/SteamAppsListCache.java @@ -11,6 +11,7 @@ import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; @SuppressWarnings("WeakerAccess") public class SteamAppsListCache { @@ -25,7 +26,7 @@ public class SteamAppsListCache { public SteamAppsListCache() { getListFromFile(); - if (Instant.now().isAfter(list.timestamp.plus(Duration.ofDays(14)))) { + if (Instant.now().isAfter(list.timestamp.plus(Duration.ofDays(3)))) { getListFromApi(); saveListToFile(); } @@ -49,7 +50,7 @@ public class SteamAppsListCache { return null; } - public SteamApp searchGame(String name) { + public SteamApp findGame(String name) { for (SteamApp app : list.steamAppsList) { if (app.getName().toLowerCase().contains(name.toLowerCase())) { return app; @@ -91,15 +92,7 @@ public class SteamAppsListCache { private void getListFromFile() { try { BufferedReader fIn = new BufferedReader(new FileReader(cacheFile)); - StringBuilder content = new StringBuilder(); - String line; - - while ((line = fIn.readLine()) != null) { - content.append(line); - //content.append(System.lineSeparator()); - } - - String json = content.toString(); + String json = fIn.lines().collect(Collectors.joining()); final Type type = new TypeToken() {}.getType(); Gson gson = new Gson(); list = gson.fromJson(json, type);