Введение настроек на стороне Java часть 1

Не комплируется
This commit is contained in:
Gravit 2018-10-21 19:12:36 +07:00
parent ac643bf322
commit 67849bc1be
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 66 additions and 146 deletions

View file

@ -1,117 +1,4 @@
var settings = { var settingsClass = Java.extend(LauncherSettingsClass.static, {
file: DirBridge.dir.resolve("settings.bin"), // Settings file
login: null, rsaPassword: null, profile: 0, // Auth
updatesDir: null, autoEnter: false, fullScreen: false, ram: 0, // Client
// Offline cache
offline: false,
lastSign: null,
lastProfiles: new java.util.LinkedList(),
lastHDirs: new java.util.HashMap(16),
/* Settings and overlay functions */
load: function() {
LogHelper.debug("Loading settings file");
try {
tryWithResources(new HInput(IOHelper.newInput(settings.file)), settings.read);
} catch(e) {
LogHelper.error(e);
settings.setDefault();
}
},
save: function() {
LogHelper.debug("Saving settings file");
try {
tryWithResources(new HOutput(IOHelper.newOutput(settings.file)), settings.write);
} catch(e) {
LogHelper.error(e);
}
},
// Internal functions
read: function(input) {
var magic = input.readInt();
if (magic != config.settingsMagic) {
throw new java.io.IOException("Settings magic mismatch: " + java.lang.Integer.toString(magic, 16));
}
// Launcher settings
var debug = input.readBoolean();
if (!LogHelper.isDebugEnabled() && debug) {
LogHelper.setDebugEnabled(true);
}
// Auth settings
settings.login = input.readBoolean() ? input.readString(255) : null;
settings.rsaPassword = input.readBoolean() ? input.readByteArray(IOHelper.BUFFER_SIZE) : null;
settings.profile = input.readLength(0);
// Client settings
settings.updatesDir = IOHelper.toPath(input.readString(0));
settings.autoEnter = input.readBoolean();
settings.fullScreen = input.readBoolean();
settings.setRAM(input.readLength(JVMHelper.RAM));
// Offline cache
var publicKey = Launcher.getConfig().publicKey;
settings.lastSign = input.readBoolean() ? input.readByteArray(-SecurityHelper.RSA_KEY_LENGTH) : null;
settings.lastProfiles.clear();
var lastProfilesCount = input.readLength(0);
for (var i = 0; i < lastProfilesCount; i++) {
settings.lastProfiles.add(new SignedObjectHolder(input, publicKey, ClientProfile.RO_ADAPTER));
}
settings.lastHDirs.clear();
var lastHDirsCount = input.readLength(0);
for (var i = 0; i < lastHDirsCount; i++) {
var name = IOHelper.verifyFileName(input.readString(255));
VerifyHelper.putIfAbsent(settings.lastHDirs, name, new SignedObjectHolder(input, publicKey, function(i) new HashedDir(i)),
java.lang.String.format("Duplicate offline hashed dir: '%s'", name));
}
// Apply CLI params
cliParams.applySettings();
},
write: function(output) {
output.writeInt(config.settingsMagic);
// Launcher settings
output.writeBoolean(LogHelper.isDebugEnabled());
// Auth settings
output.writeBoolean(settings.login !== null);
if (settings.login !== null) {
output.writeString(settings.login, 255);
}
output.writeBoolean(settings.rsaPassword !== null);
if (settings.rsaPassword !== null) {
output.writeByteArray(settings.rsaPassword, IOHelper.BUFFER_SIZE);
}
output.writeLength(settings.profile, 0);
// Client settings
output.writeString(IOHelper.toString(settings.updatesDir), 0);
output.writeBoolean(settings.autoEnter);
output.writeBoolean(settings.fullScreen);
output.writeLength(settings.ram, JVMHelper.RAM);
// Offline cache
output.writeBoolean(settings.lastSign !== null);
if (settings.lastSign !== null) {
output.writeByteArray(settings.lastSign, -SecurityHelper.RSA_KEY_LENGTH);
}
output.writeLength(settings.lastProfiles.size(), 0);
for each (var profile in settings.lastProfiles) {
profile.write(output);
}
output.writeLength(settings.lastHDirs.size(), 0);
for each (var entry in settings.lastHDirs.entrySet()) {
output.writeString(entry.getKey(), 0);
entry.getValue().write(output);
}
},
setDefault: function() { setDefault: function() {
// Auth settings // Auth settings
settings.login = null; settings.login = null;
@ -137,13 +24,12 @@ var settings = {
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;
}, }
setRAM: function(ram) {
settings.ram = java.lang.Math["min(int,int)"](((ram / 256) | 0) * 256, JVMHelper.RAM);
},
/* ===================== OVERLAY ===================== */ });
var settingsOverlay = {
/* ===================== OVERLAY ===================== */
overlay: null, ramLabel: null, dirLabel: null, overlay: null, ramLabel: null, dirLabel: null,
deleteDirPressedAgain: false, deleteDirPressedAgain: false,
@ -248,9 +134,8 @@ var settings = {
settings.dirLabel.setText(IOHelper.toString(settings.updatesDir)); settings.dirLabel.setText(IOHelper.toString(settings.updatesDir));
} }
}; };
/* ====================== CLI PARAMS ===================== */ /* ====================== CLI PARAMS ===================== */
var cliParams = { var cliParamsClass = Java.extend(CliParamsInterface.static, {
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
@ -324,4 +209,7 @@ var cliParams = {
settings.offline = cliParams.offline; settings.offline = cliParams.offline;
} }
} }
}; });
var cliParams = new cliParamsClass;
var settings = new settingsClass;
settings.cliParams = cliParams;

View file

@ -67,6 +67,7 @@ var DigestAlgorithm = DigestAlgorithmClass.static;
var VerifyHelper = VerifyHelperClass.static; var VerifyHelper = VerifyHelperClass.static;
var RingProgressIndicator = RingProgressIndicatorClass.static var RingProgressIndicator = RingProgressIndicatorClass.static
var RingProgressIndicatorSkin = RingProgressIndicatorSkinClass.static var RingProgressIndicatorSkin = RingProgressIndicatorSkinClass.static
var LauncherSettings = LauncherSettingsClass.static
// Helper JS class API imports // Helper JS class API imports
var JSApplication = null; var JSApplication = null;

View file

@ -134,6 +134,8 @@ public static void addLauncherClassBindings(Map<String, Object> bindings) {
bindings.put("VerifyHelperClass", VerifyHelper.class); bindings.put("VerifyHelperClass", VerifyHelper.class);
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("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

@ -0,0 +1,10 @@
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

@ -1,13 +1,12 @@
package ru.gravit.launcher.client; package ru.gravit.launcher.client;
import ru.gravit.launcher.Launcher; import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.launcher.hasher.HashedDir;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.serialize.HInput; import ru.gravit.launcher.serialize.HInput;
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.utils.event.EventManager;
import ru.gravit.utils.helper.*; import ru.gravit.utils.helper.*;
import java.io.IOException; import java.io.IOException;
@ -20,18 +19,34 @@
public class LauncherSettings { public class LauncherSettings {
public static int settingsMagic; public static int settingsMagic;
Path file = DirBridge.dir.resolve("settings.bin"); @LauncherAPI
String login; public Path file = DirBridge.dir.resolve("settings.bin");
byte[] rsaPassword; @LauncherAPI
int profile; public String login;
Path updatesDir; @LauncherAPI
boolean autoEnter; public byte[] rsaPassword;
boolean fullScreen; @LauncherAPI
int ram; public int profile;
@LauncherAPI
public Path updatesDir;
@LauncherAPI
public boolean autoEnter;
@LauncherAPI
public boolean fullScreen;
@LauncherAPI
public boolean offline;
@LauncherAPI
public int ram;
@LauncherAPI
public CliParamsInterface cliParams;
byte[] lastSign; @LauncherAPI
LinkedList<SignedObjectHolder<ClientProfile>> lastProfiles; public byte[] lastSign;
HashMap<String,SignedObjectHolder<HashedDir>> lastHDirs; @LauncherAPI
public LinkedList<SignedObjectHolder<ClientProfile>> lastProfiles;
@LauncherAPI
public HashMap<String,SignedObjectHolder<HashedDir>> lastHDirs;
@LauncherAPI
public void load() public void load()
{ {
LogHelper.debug("Loading settings file"); LogHelper.debug("Loading settings file");
@ -45,6 +60,7 @@ public void load()
setDefault(); setDefault();
} }
} }
@LauncherAPI
public void read(HInput input) throws IOException, SignatureException public void read(HInput input) throws IOException, SignatureException
{ {
int magic = input.readInt(); int magic = input.readInt();
@ -84,8 +100,9 @@ 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(); cliParams.applySettings();
} }
@LauncherAPI
public void write(HOutput output) throws IOException { public void write(HOutput output) throws IOException {
output.writeInt(settingsMagic); output.writeInt(settingsMagic);
@ -115,19 +132,21 @@ public void write(HOutput output) throws IOException {
output.writeByteArray(lastSign, -SecurityHelper.RSA_KEY_LENGTH); output.writeByteArray(lastSign, -SecurityHelper.RSA_KEY_LENGTH);
} }
output.writeLength(lastProfiles.size(), 0); output.writeLength(lastProfiles.size(), 0);
for(SignedObjectHolder<ClientProfile> profile : lastProfiles) { for (SignedObjectHolder<ClientProfile> profile : lastProfiles) {
profile.write(output); profile.write(output);
} }
output.writeLength(lastHDirs.size(), 0); output.writeLength(lastHDirs.size(), 0);
for(Map.Entry<String,SignedObjectHolder<HashedDir>> entry : lastHDirs.entrySet()) { for (Map.Entry<String, SignedObjectHolder<HashedDir>> entry : lastHDirs.entrySet()) {
output.writeString(entry.getKey(), 0); output.writeString(entry.getKey(), 0);
entry.getValue().write(output); entry.getValue().write(output);
} }
} }
@LauncherAPI
public void setRAM(int ram) public void setRAM(int ram)
{ {
ram = java.lang.Math.min(((ram / 256)) * 256, JVMHelper.RAM); ram = java.lang.Math.min(((ram / 256)) * 256, JVMHelper.RAM);
} }
@LauncherAPI
public void setDefault() public void setDefault()
{ {
// Auth settings // Auth settings
@ -149,6 +168,6 @@ public void setDefault()
lastHDirs.clear(); lastHDirs.clear();
// Apply CLI params // Apply CLI params
//cliParams.applySettings(); cliParams.applySettings();
} }
} }

View file

@ -63,9 +63,9 @@ static int readBuildNumber() {
private static final Pattern UUID_PATTERN = Pattern.compile("-", Pattern.LITERAL); private static final Pattern UUID_PATTERN = Pattern.compile("-", Pattern.LITERAL);
public static int MAJOR = 4; public static int MAJOR = 4;
public static int MINOR = 0; public static int MINOR = 0;
public static int PATCH = 3; public static int PATCH = 4;
public static int BUILD = readBuildNumber(); public static int BUILD = readBuildNumber();
public static Version.Type RELEASE = Version.Type.BETA; public static Version.Type RELEASE = Version.Type.DEV;
@LauncherAPI @LauncherAPI
public static LauncherConfig getConfig() { public static LauncherConfig getConfig() {