Java реализация settings часть 2

This commit is contained in:
Gravit 2018-10-22 00:01:23 +07:00
parent 67849bc1be
commit d84193d5a4
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 56 additions and 54 deletions

View file

@ -20,7 +20,7 @@ function initDialog() {
// Init overlays // Init overlays
debug.initOverlay(); debug.initOverlay();
processing.initOverlay(); processing.initOverlay();
settings.initOverlay(); settingsOverlay.initOverlay();
update.initOverlay(); update.initOverlay();
// Verify launcher & make request // Verify launcher & make request
@ -135,7 +135,7 @@ function goSettings(event) {
} }
// Show settings overlay // Show settings overlay
overlay.show(settings.overlay, null); overlay.show(settingsOverlay.overlay, null);
} }
/* ======== Processing functions ======== */ /* ======== Processing functions ======== */

View file

@ -22,7 +22,7 @@ var settingsClass = Java.extend(LauncherSettingsClass.static, {
setPassword: function(password) { setPassword: function(password) {
var encrypted = SecurityHelper.newRSAEncryptCipher(Launcher.getConfig().publicKey).doFinal(IOHelper.encode(password)); var encrypted = SecurityHelper.newRSAEncryptCipher(Launcher.getConfig().publicKey).doFinal(IOHelper.encode(password));
settings.password = encrypted; //settings.password = encrypted;
return encrypted; return encrypted;
} }
@ -34,26 +34,26 @@ var settingsOverlay = {
deleteDirPressedAgain: false, deleteDirPressedAgain: false,
initOverlay: function() { initOverlay: function() {
settings.overlay = loadFXML("dialog/overlay/settings/settings.fxml"); settingsOverlay.overlay = loadFXML("dialog/overlay/settings/settings.fxml");
// Lookup autoEnter checkbox // Lookup autoEnter checkbox
var autoEnterBox = settings.overlay.lookup("#autoEnter"); var autoEnterBox = settingsOverlay.overlay.lookup("#autoEnter");
autoEnterBox.setSelected(settings.autoEnter); autoEnterBox.setSelected(settings.autoEnter);
autoEnterBox.selectedProperty()["addListener(javafx.beans.value.ChangeListener)"]( autoEnterBox.selectedProperty()["addListener(javafx.beans.value.ChangeListener)"](
function(o, ov, nv) settings.autoEnter = nv); function(o, ov, nv) settings.autoEnter = nv);
// Lookup fullScreen checkbox // Lookup fullScreen checkbox
var fullScreenBox = settings.overlay.lookup("#fullScreen"); var fullScreenBox = settingsOverlay.overlay.lookup("#fullScreen");
fullScreenBox.setSelected(settings.fullScreen); fullScreenBox.setSelected(settings.fullScreen);
fullScreenBox.selectedProperty()["addListener(javafx.beans.value.ChangeListener)"]( fullScreenBox.selectedProperty()["addListener(javafx.beans.value.ChangeListener)"](
function(o, ov, nv) settings.fullScreen = nv); function(o, ov, nv) settings.fullScreen = nv);
// Lookup RAM label // Lookup RAM label
settings.ramLabel = settings.overlay.lookup("#ramLabel"); settingsOverlay.ramLabel = settingsOverlay.overlay.lookup("#ramLabel");
settings.updateRAMLabel(); settingsOverlay.updateRAMLabel();
// Lookup RAM slider options // Lookup RAM slider options
var ramSlider = settings.overlay.lookup("#ramSlider"); var ramSlider = settingsOverlay.overlay.lookup("#ramSlider");
ramSlider.setMin(0); ramSlider.setMin(0);
ramSlider.setMax(JVMHelper.RAM); ramSlider.setMax(JVMHelper.RAM);
ramSlider.setSnapToTicks(true); ramSlider.setSnapToTicks(true);
@ -65,17 +65,17 @@ var settingsOverlay = {
ramSlider.setValue(settings.ram); ramSlider.setValue(settings.ram);
ramSlider.valueProperty()["addListener(javafx.beans.value.ChangeListener)"](function(o, ov, nv) { ramSlider.valueProperty()["addListener(javafx.beans.value.ChangeListener)"](function(o, ov, nv) {
settings.setRAM(nv); settings.setRAM(nv);
settings.updateRAMLabel(); settingsOverlay.updateRAMLabel();
}); });
// Lookup dir label // Lookup dir label
settings.dirLabel = settings.overlay.lookup("#dirLabel"); settingsOverlay.dirLabel = settingsOverlay.overlay.lookup("#dirLabel");
settings.dirLabel.setOnAction(function(event) settingsOverlay.dirLabel.setOnAction(function(event)
app.getHostServices().showDocument(settings.updatesDir.toUri())); app.getHostServices().showDocument(settings.updatesDir.toUri()));
settings.updateDirLabel(); settingsOverlay.updateDirLabel();
// Lookup change dir button // Lookup change dir button
settings.overlay.lookup("#changeDir").setOnAction(function(event) { settingsOverlay.overlay.lookup("#changeDir").setOnAction(function(event) {
var chooser = new javafx.stage.DirectoryChooser(); var chooser = new javafx.stage.DirectoryChooser();
chooser.setTitle("Сменить директорию загрузок"); chooser.setTitle("Сменить директорию загрузок");
chooser.setInitialDirectory(dir.toFile()); chooser.setInitialDirectory(dir.toFile());
@ -89,53 +89,54 @@ var settingsOverlay = {
}); });
// Lookup delete dir button // Lookup delete dir button
var deleteDirButton = settings.overlay.lookup("#deleteDir"); var deleteDirButton = settingsOverlay.overlay.lookup("#deleteDir");
deleteDirButton.setOnAction(function(event) { deleteDirButton.setOnAction(function(event) {
if (!settings.deleteDirPressedAgain) { if (!settingsOverlay.deleteDirPressedAgain) {
settings.deleteDirPressedAgain = true; settingsOverlay.deleteDirPressedAgain = true;
deleteDirButton.setText("Подтвердить вменяемость"); deleteDirButton.setText("Подтвердить вменяемость");
return; return;
} }
// Delete dir! // Delete dir!
settings.deleteUpdatesDir(); settingsOverlay.deleteUpdatesDir();
settings.deleteDirPressedAgain = false; settingsOverlay.deleteDirPressedAgain = false;
deleteDirButton.setText("Ещё раз попробовать"); deleteDirButton.setText("Ещё раз попробовать");
}); });
// Lookup debug checkbox // Lookup debug checkbox
var debugBox = settings.overlay.lookup("#debug"); var debugBox = settingsOverlay.overlay.lookup("#debug");
debugBox.setSelected(LogHelper.isDebugEnabled()); debugBox.setSelected(LogHelper.isDebugEnabled());
debugBox.selectedProperty()["addListener(javafx.beans.value.ChangeListener)"]( debugBox.selectedProperty()["addListener(javafx.beans.value.ChangeListener)"](
function(o, ov, nv) LogHelper.setDebugEnabled(nv)); function(o, ov, nv) LogHelper.setDebugEnabled(nv));
// Lookup apply settings button // Lookup apply settings button
settings.overlay.lookup("#apply").setOnAction(function(event) overlay.hide(0, null)); settingsOverlay.overlay.lookup("#apply").setOnAction(function(event) overlay.hide(0, null));
}, },
updateRAMLabel: function() { updateRAMLabel: function() {
settings.ramLabel.setText(settings.ram <= 0 ? "Автоматически" : settings.ram + " MiB"); settingsOverlay.ramLabel.setText(settings.ram <= 0 ? "Автоматически" : settings.ram + " MiB");
}, },
deleteUpdatesDir: function() { deleteUpdatesDir: function() {
processing.description.setText("Удаление директории загрузок"); processing.description.setText("Удаление директории загрузок");
overlay.swap(0, processing.overlay, function(event) { overlay.swap(0, processing.overlay, function(event) {
var task = newTask(function() IOHelper.deleteDir(settings.updatesDir, false)); var task = newTask(function() IOHelper.deleteDir(settings.updatesDir, false));
task.setOnSucceeded(function(event) overlay.swap(0, settings.overlay, null)); task.setOnSucceeded(function(event) overlay.swap(0, settingsOverlay.overlay, null));
task.setOnFailed(function(event) { task.setOnFailed(function(event) {
processing.setError(task.getException()); processing.setError(task.getException());
overlay.swap(2500, settings.overlay, null); overlay.swap(2500, settingsOverlay.overlay, null);
}); });
startTask(task); startTask(task);
}); });
}, },
updateDirLabel: function() { updateDirLabel: function() {
settings.dirLabel.setText(IOHelper.toString(settings.updatesDir)); settingsOverlay.dirLabel.setText(IOHelper.toString(settings.updatesDir));
} }
}; };
var settings = new settingsClass;
/* ====================== CLI PARAMS ===================== */ /* ====================== CLI PARAMS ===================== */
var cliParamsClass = Java.extend(CliParamsInterface.static, { var cliParams = {
login: null, password: null, profile: -1, autoLogin: false, // Auth login: null, password: null, profile: -1, autoLogin: false, // Auth
updatesDir: null, autoEnter: null, fullScreen: null, ram: -1, // Client updatesDir: null, autoEnter: null, fullScreen: null, ram: -1, // Client
offline: false, // Offline offline: false, // Offline
@ -192,7 +193,7 @@ var cliParamsClass = Java.extend(CliParamsInterface.static, {
// Apply client params // Apply client params
if (cliParams.updatesDir !== null) { if (cliParams.updatesDir !== null) {
settings.updatesDir = cliParams.updatesDir; //settings.updatesDir = cliParams.updatesDir;
} }
if (cliParams.autoEnter !== null) { if (cliParams.autoEnter !== null) {
settings.autoLogin = cliParams.autoEnter; settings.autoLogin = cliParams.autoEnter;
@ -209,7 +210,4 @@ var cliParamsClass = Java.extend(CliParamsInterface.static, {
settings.offline = cliParams.offline; settings.offline = cliParams.offline;
} }
} }
}); };
var cliParams = new cliParamsClass;
var settings = new settingsClass;
settings.cliParams = cliParams;

View file

@ -6,6 +6,7 @@ var LauncherApp = Java.extend(JSApplication, {
app = JSApplication.getInstance(); app = JSApplication.getInstance();
cliParams.init(app.getParameters()); cliParams.init(app.getParameters());
settings.load(); settings.load();
cliParams.applySettings();
}, start: function(primaryStage) { }, start: function(primaryStage) {
stage = primaryStage; stage = primaryStage;
stage.setTitle(config.title); stage.setTitle(config.title);

View file

@ -135,7 +135,6 @@ public static void addLauncherClassBindings(Map<String, Object> bindings) {
bindings.put("DirBridgeClass", DirBridge.class); bindings.put("DirBridgeClass", DirBridge.class);
bindings.put("FunctionalBridgeClass",FunctionalBridge.class); bindings.put("FunctionalBridgeClass",FunctionalBridge.class);
bindings.put("LauncherSettingsClass",LauncherSettings.class); bindings.put("LauncherSettingsClass",LauncherSettings.class);
bindings.put("CliParamsInterface",CliParamsInterface.class);
// Load JS API if available // Load JS API if available
bindings.put("RingProgressIndicatorClass", RingProgressIndicator.class); bindings.put("RingProgressIndicatorClass", RingProgressIndicator.class);

View file

@ -1,10 +0,0 @@
package ru.gravit.launcher.client;
import ru.gravit.launcher.LauncherAPI;
public interface CliParamsInterface {
@LauncherAPI
void applySettings();
@LauncherAPI
void init(javafx.application.Application.Parameters params);
}

View file

@ -9,16 +9,19 @@
import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.utils.helper.*; import ru.gravit.utils.helper.*;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.SignatureException; import java.security.SignatureException;
import java.security.interfaces.RSAPublicKey; import java.security.interfaces.RSAPublicKey;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
public class LauncherSettings { public class LauncherSettings {
public static int settingsMagic; public static int settingsMagic = 0xc0de5;
@LauncherAPI @LauncherAPI
public Path file = DirBridge.dir.resolve("settings.bin"); public Path file = DirBridge.dir.resolve("settings.bin");
@LauncherAPI @LauncherAPI
@ -37,23 +40,33 @@ public class LauncherSettings {
public boolean offline; public boolean offline;
@LauncherAPI @LauncherAPI
public int ram; public int ram;
@LauncherAPI
public CliParamsInterface cliParams;
@LauncherAPI @LauncherAPI
public byte[] lastSign; public byte[] lastSign;
@LauncherAPI @LauncherAPI
public LinkedList<SignedObjectHolder<ClientProfile>> lastProfiles; public List<SignedObjectHolder<ClientProfile>> lastProfiles = new LinkedList<>();
@LauncherAPI @LauncherAPI
public HashMap<String,SignedObjectHolder<HashedDir>> lastHDirs; public Map<String,SignedObjectHolder<HashedDir>> lastHDirs = new HashMap<>(16);
@LauncherAPI @LauncherAPI
public void load() public void load() throws SignatureException {
{
LogHelper.debug("Loading settings file"); LogHelper.debug("Loading settings file");
try { try {
try(HInput input = new HInput(IOHelper.newInput(file))) try(HInput input = new HInput(IOHelper.newInput(file)))
{ {
read(input);
}
} catch(IOException e) {
LogHelper.error(e);
setDefault();
}
}
@LauncherAPI
public void save() throws SignatureException {
LogHelper.debug("Save settings file");
try {
try(HOutput output = new HOutput(IOHelper.newOutput(file)))
{
write(output);
} }
} catch(IOException e) { } catch(IOException e) {
LogHelper.error(e); LogHelper.error(e);
@ -100,7 +113,6 @@ public void read(HInput input) throws IOException, SignatureException
VerifyHelper.putIfAbsent(lastHDirs, name, new SignedObjectHolder<>(input, publicKey, HashedDir::new), VerifyHelper.putIfAbsent(lastHDirs, name, new SignedObjectHolder<>(input, publicKey, HashedDir::new),
java.lang.String.format("Duplicate offline hashed dir: '%s'", name)); java.lang.String.format("Duplicate offline hashed dir: '%s'", name));
} }
cliParams.applySettings();
} }
@LauncherAPI @LauncherAPI
public void write(HOutput output) throws IOException { public void write(HOutput output) throws IOException {
@ -166,8 +178,10 @@ public void setDefault()
lastSign = null; lastSign = null;
lastProfiles.clear(); lastProfiles.clear();
lastHDirs.clear(); lastHDirs.clear();
}
// Apply CLI params @LauncherAPI
cliParams.applySettings(); public byte[] setPassword(String password) throws BadPaddingException, IllegalBlockSizeException {
byte[] encrypted = SecurityHelper.newRSAEncryptCipher(Launcher.getConfig().publicKey).doFinal(IOHelper.encode(password));
return encrypted;
} }
} }