From ae0e178d437d57834acfc5baacbbe05dec3276ab Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 13 Apr 2019 22:31:22 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=AD=D0=BA=D1=81=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B8=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=84=D0=B8=D1=82=D1=87=D0=B0=20-=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=82=D1=81=D1=80=D0=BE=D0=B9=D0=BA=D0=B8=20=D0=B2=20jso?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Launcher/runtime/dialog/dialog.js | 3 +- .../dialog/overlay/settings/settings.js | 53 ++++++------------- Launcher/runtime/engine/api.js | 4 +- Launcher/runtime/engine/settings.js | 21 ++++++++ .../gravit/launcher/NewLauncherSettings.java | 38 +++++++++++++ .../launcher/gui/JSRuntimeProvider.java | 8 +-- .../launcher/managers/SettingsManager.java | 41 ++++++++++++++ .../gravit/utils/config/JsonConfigurable.java | 30 ++++++----- 8 files changed, 144 insertions(+), 54 deletions(-) create mode 100644 Launcher/runtime/engine/settings.js create mode 100644 Launcher/src/main/java/ru/gravit/launcher/NewLauncherSettings.java create mode 100644 Launcher/src/main/java/ru/gravit/launcher/managers/SettingsManager.java diff --git a/Launcher/runtime/dialog/dialog.js b/Launcher/runtime/dialog/dialog.js index e85546bf..1761a8f5 100644 --- a/Launcher/runtime/dialog/dialog.js +++ b/Launcher/runtime/dialog/dialog.js @@ -180,7 +180,7 @@ function goAuth(event) { if (!passwordField.isDisable()) { var password = passwordField.getText(); if (password !== null && !password.isEmpty()) { - rsaPassword = settings.setPassword(password); + rsaPassword = settingsOverlay.setPassword(password); } else if (settings.rsaPassword !== null) { rsaPassword = settings.rsaPassword; } else { @@ -443,6 +443,7 @@ var serverHolder = { }; /* ======== Scenes scripts ======== */ +launcher.loadScript("api/settings.js"); launcher.loadScript("dialog/overlay/debug/debug.js"); launcher.loadScript("dialog/overlay/processing/processing.js"); launcher.loadScript("dialog/overlay/settings/settings.js"); diff --git a/Launcher/runtime/dialog/overlay/settings/settings.js b/Launcher/runtime/dialog/overlay/settings/settings.js index 959b68c5..fcedc189 100644 --- a/Launcher/runtime/dialog/overlay/settings/settings.js +++ b/Launcher/runtime/dialog/overlay/settings/settings.js @@ -1,35 +1,3 @@ -var settingsClass = Java.extend(LauncherSettingsClass.static, { - setDefault: function() { - settings.login = null; - settings.rsaPassword = null; - settings.profile = 0; - - settings.updatesDir = DirBridge.defaultUpdatesDir; - settings.autoEnter = config.autoEnterDefault; - settings.fullScreen = config.fullScreenDefault; - settings.setRAM(config.ramDefault); - - settings.lastDigest = null; - settings.lastProfiles.clear(); - settings.lastHDirs.clear(); - - cliParams.applySettings(); - }, - - setPassword: function(password) { - var encrypted = SecurityHelper.newRSAEncryptCipher(Launcher.getConfig().publicKey).doFinal(IOHelper.encode(password)); - return encrypted; - }, - - - setRAM: function(ram) { - if (ram>762&&ram<1024){ - settings.ram = java.lang.Math["min(int,int)"](ram, FunctionalBridge.getJVMTotalMemory()); - }else{ - settings.ram = java.lang.Math["min(int,int)"](((ram / 256) | 0) * 256, FunctionalBridge.getJVMTotalMemory()); - } - }, -}); var settingsOverlay = { /* ===================== OVERLAY ===================== */ overlay: null, ramLabel: null, dirLabel: null, transferDialog: null, @@ -83,7 +51,7 @@ var settingsOverlay = { ramSlider.setBlockIncrement(1024); ramSlider.setValue(settings.ram); ramSlider.valueProperty()["addListener(javafx.beans.value.ChangeListener)"](function(o, ov, nv) { - settings.setRAM(nv); + settingsOverlay.setRAM(nv); settingsOverlay.updateRAMLabel(); }); @@ -153,12 +121,25 @@ var settingsOverlay = { }); }, + setPassword: function(password) { + var encrypted = SecurityHelper.newRSAEncryptCipher(Launcher.getConfig().publicKey).doFinal(IOHelper.encode(password)); + return encrypted; + }, + + + setRAM: function(ram) { + if (ram>762&&ram<1024){ + settings.ram = java.lang.Math["min(int,int)"](ram, FunctionalBridge.getJVMTotalMemory()); + }else{ + settings.ram = java.lang.Math["min(int,int)"](((ram / 256) | 0) * 256, FunctionalBridge.getJVMTotalMemory()); + } + }, + updateDirLabel: function() { settingsOverlay.dirLabel.setText(IOHelper.toString(settings.updatesDir)); } }; LogHelper.debug("Dir: %s", DirBridge.dir); -var settings = new settingsClass; /* ====================== CLI PARAMS ===================== */ var cliParams = { @@ -206,7 +187,7 @@ var cliParams = { settings.login = cliParams.login; } if (cliParams.password !== null) { - settings.setPassword(cliParams.password); + settingsOverlay.setPassword(cliParams.password); } if (cliParams.profile >= 0) { settings.profile = cliParams.profile; @@ -221,7 +202,7 @@ var cliParams = { settings.fullScreen = cliParams.fullScreen; } if (cliParams.ram >= 0) { - settings.setRAM(cliParams.ram); + settingsOverlay.setRAM(cliParams.ram); } if (cliParams.offline !== null) { diff --git a/Launcher/runtime/engine/api.js b/Launcher/runtime/engine/api.js index 603fd8b4..a11bde9d 100644 --- a/Launcher/runtime/engine/api.js +++ b/Launcher/runtime/engine/api.js @@ -56,6 +56,8 @@ var SecurityHelper = SecurityHelperClass.static; var DigestAlgorithm = DigestAlgorithmClass.static; var VerifyHelper = VerifyHelperClass.static; var LauncherSettings = LauncherSettingsClass.static; +var NewLauncherSettings = NewLauncherSettingsClass.static; +var SettingsManager = SettingsManagerClass.static; // Helper JS class API imports var JSApplication = null; @@ -86,4 +88,4 @@ function startTask(task) { function openURL(url) { app.getHostServices().showDocument(url.toURI()); -} +} \ No newline at end of file diff --git a/Launcher/runtime/engine/settings.js b/Launcher/runtime/engine/settings.js new file mode 100644 index 00000000..4428e350 --- /dev/null +++ b/Launcher/runtime/engine/settings.js @@ -0,0 +1,21 @@ +var settingsManagerClass = Java.extend(SettingsManagerClass.static, { + getDefaultConfig: function() { + var new_settings = new NewLauncherSettings; + new_settings.login = null; + new_settings.rsaPassword = null; + new_settings.profile = 0; + + new_settings.updatesDir = DirBridge.defaultUpdatesDir; + new_settings.autoEnter = config.autoEnterDefault; + new_settings.fullScreen = config.fullScreenDefault; + new_settings.ram = config.ramDefault; + + new_settings.lastDigest = null; + new_settings.lastProfiles.clear(); + new_settings.lastHDirs.clear(); + return new_settings; + }, +}); + +var settingsManager = new settingsManager; +var settings = SettingsManager.settings; \ No newline at end of file diff --git a/Launcher/src/main/java/ru/gravit/launcher/NewLauncherSettings.java b/Launcher/src/main/java/ru/gravit/launcher/NewLauncherSettings.java new file mode 100644 index 00000000..2824c1be --- /dev/null +++ b/Launcher/src/main/java/ru/gravit/launcher/NewLauncherSettings.java @@ -0,0 +1,38 @@ +package ru.gravit.launcher; + +import ru.gravit.launcher.hasher.HashedDir; +import ru.gravit.launcher.profiles.ClientProfile; + +import java.nio.file.Path; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class NewLauncherSettings { + @LauncherAPI + public String login; + @LauncherAPI + public byte[] rsaPassword; + @LauncherAPI + public int profile; + @LauncherAPI + public transient Path updatesDir; + @LauncherAPI + public String updatesDirPath; + @LauncherAPI + public boolean autoEnter; + @LauncherAPI + public boolean fullScreen; + @LauncherAPI + public boolean offline; + @LauncherAPI + public int ram; + + @LauncherAPI + public byte[] lastDigest; + @LauncherAPI + public List lastProfiles = new LinkedList<>(); + @LauncherAPI + public Map lastHDirs = new HashMap<>(16); +} diff --git a/Launcher/src/main/java/ru/gravit/launcher/gui/JSRuntimeProvider.java b/Launcher/src/main/java/ru/gravit/launcher/gui/JSRuntimeProvider.java index 20ca7393..e0fa8dbf 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/gui/JSRuntimeProvider.java +++ b/Launcher/src/main/java/ru/gravit/launcher/gui/JSRuntimeProvider.java @@ -1,14 +1,12 @@ package ru.gravit.launcher.gui; -import ru.gravit.launcher.JSApplication; -import ru.gravit.launcher.Launcher; -import ru.gravit.launcher.LauncherAPI; -import ru.gravit.launcher.LauncherConfig; +import ru.gravit.launcher.*; import ru.gravit.launcher.client.*; import ru.gravit.launcher.hasher.FileNameMatcher; import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedFile; +import ru.gravit.launcher.managers.SettingsManager; import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.PlayerProfile; import ru.gravit.launcher.profiles.Texture; @@ -52,6 +50,8 @@ public static void addLauncherClassBindings(Map bindings) { bindings.put("LauncherClass", Launcher.class); bindings.put("LauncherConfigClass", LauncherConfig.class); bindings.put("HTTPRequestClass", HTTPRequest.class); + bindings.put("SettingsManagerClass", SettingsManager.class); + bindings.put("NewLauncherSettingsClass", NewLauncherSettings.class); // Set client class bindings bindings.put("PlayerProfileClass", PlayerProfile.class); diff --git a/Launcher/src/main/java/ru/gravit/launcher/managers/SettingsManager.java b/Launcher/src/main/java/ru/gravit/launcher/managers/SettingsManager.java new file mode 100644 index 00000000..3d03f3c2 --- /dev/null +++ b/Launcher/src/main/java/ru/gravit/launcher/managers/SettingsManager.java @@ -0,0 +1,41 @@ +package ru.gravit.launcher.managers; + +import ru.gravit.launcher.LauncherAPI; +import ru.gravit.launcher.NewLauncherSettings; +import ru.gravit.launcher.client.DirBridge; +import ru.gravit.utils.config.JsonConfigurable; + +import java.lang.reflect.Type; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class SettingsManager extends JsonConfigurable { + @LauncherAPI + public static NewLauncherSettings settings; + + public SettingsManager() { + super(NewLauncherSettings.class, DirBridge.dir.resolve("settings.json")); + } + @LauncherAPI + @Override + public NewLauncherSettings getConfig() { + settings.updatesDirPath = settings.updatesDir.toString(); + return settings; + } + @LauncherAPI + @Override + public NewLauncherSettings getDefaultConfig() { + return new NewLauncherSettings(); + } + @LauncherAPI + @Override + public void setConfig(NewLauncherSettings config) { + settings = config; + settings.updatesDir = Paths.get(settings.updatesDirPath); + } + + @Override + public void setType(Type type) { + super.setType(type); + } +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java b/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java index 2a0bbc2f..d2e646d1 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java +++ b/libLauncher/src/main/java/ru/gravit/utils/config/JsonConfigurable.java @@ -1,6 +1,8 @@ package ru.gravit.utils.config; import ru.gravit.launcher.Launcher; +import ru.gravit.launcher.LauncherAPI; +import ru.gravit.utils.Version; import ru.gravit.utils.helper.IOHelper; import java.io.BufferedReader; @@ -12,60 +14,64 @@ public abstract class JsonConfigurable { private Type type; protected Path configPath; - + @LauncherAPI public void saveConfig() throws IOException { saveConfig(configPath); } - + @LauncherAPI public void loadConfig() throws IOException { loadConfig(configPath); } - + @LauncherAPI public JsonConfigurable(Type type, Path configPath) { this.type = type; this.configPath = configPath; } - + @LauncherAPI public void saveConfig(Path configPath) throws IOException { try (BufferedWriter writer = IOHelper.newWriter(configPath)) { Launcher.gson.toJson(getConfig(), type, writer); } } - + @LauncherAPI public void loadConfig(Path configPath) throws IOException { if (generateConfigIfNotExists(configPath)) return; try (BufferedReader reader = IOHelper.newReader(configPath)) { setConfig(Launcher.gson.fromJson(reader, type)); } } - + @LauncherAPI public void resetConfig() throws IOException { setConfig(getDefaultConfig()); saveConfig(); } - + @LauncherAPI public void resetConfig(Path newPath) throws IOException { setConfig(getDefaultConfig()); saveConfig(newPath); } - + @LauncherAPI public boolean generateConfigIfNotExists(Path path) throws IOException { if (IOHelper.isFile(path)) return false; resetConfig(path); return true; } - + @LauncherAPI public boolean generateConfigIfNotExists() throws IOException { if (IOHelper.isFile(configPath)) return false; resetConfig(); return true; } - + protected void setType(Type type) + { + this.type = type; + } + @LauncherAPI public abstract T getConfig(); - + @LauncherAPI public abstract T getDefaultConfig(); - + @LauncherAPI public abstract void setConfig(T config); }