2016-10-04 06:33:25 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2015 Laurent CLOUET
|
|
|
|
|
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
|
|
|
|
|
*
|
2020-05-28 13:28:42 +02:00
|
|
|
* This program is free software; you can redistribute it and/or
|
2016-10-04 06:33:25 +02:00
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; version 2
|
|
|
|
|
* of the License.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-04-08 20:45:54 +01:00
|
|
|
package com.sheepit.client;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
2015-04-22 19:41:50 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.nio.file.attribute.PosixFilePermission;
|
|
|
|
|
import java.util.HashSet;
|
2021-10-02 19:38:04 +00:00
|
|
|
import java.util.List;
|
2015-03-31 00:29:58 +01:00
|
|
|
import java.util.Properties;
|
2015-04-22 19:41:50 +01:00
|
|
|
import java.util.Set;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
import com.sheepit.client.Configuration.ComputeType;
|
|
|
|
|
import com.sheepit.client.hardware.gpu.GPU;
|
|
|
|
|
import com.sheepit.client.hardware.gpu.GPUDevice;
|
2021-02-23 20:25:56 +00:00
|
|
|
import com.sheepit.client.standalone.GuiText;
|
|
|
|
|
import com.sheepit.client.standalone.GuiTextOneLine;
|
2021-12-29 17:08:51 +00:00
|
|
|
import com.sheepit.client.os.OS;
|
2021-07-16 23:37:24 +00:00
|
|
|
import lombok.Data;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
2021-07-16 23:37:24 +00:00
|
|
|
@Data
|
2015-03-31 00:29:58 +01:00
|
|
|
public class SettingsLoader {
|
2021-10-02 19:38:04 +00:00
|
|
|
|
|
|
|
|
private enum PropertyNames {
|
|
|
|
|
|
|
|
|
|
PRIORITY("priority"),
|
|
|
|
|
CACHE_DIR("cache-dir"),
|
2022-05-22 15:59:44 +10:00
|
|
|
SHARED_ZIP("shared-zip"),
|
2021-10-02 19:38:04 +00:00
|
|
|
COMPUTE_METHOD("compute-method"),
|
|
|
|
|
GPU("compute-gpu"),
|
|
|
|
|
CORES("cores"),
|
|
|
|
|
CORES_BACKWARDS_COMPAT("cpu-cores"),
|
|
|
|
|
RAM("ram"),
|
|
|
|
|
RENDER_TIME("rendertime"),
|
|
|
|
|
LOGIN("login"),
|
|
|
|
|
PASSWORD("password"),
|
|
|
|
|
PROXY("proxy"),
|
|
|
|
|
HOSTNAME("hostname"),
|
|
|
|
|
AUTO_SIGNIN("auto-signin"),
|
|
|
|
|
USE_SYSTRAY("use-systray"),
|
|
|
|
|
HEADLESS("headless"),
|
|
|
|
|
UI("ui"),
|
2024-03-26 14:08:46 +00:00
|
|
|
THEME("theme"),
|
2024-04-14 09:27:52 +00:00
|
|
|
LOG_DIR("log-dir"),
|
2024-05-05 10:55:46 +00:00
|
|
|
DEBUG("debug"),
|
2024-06-08 03:21:33 +00:00
|
|
|
INCOMPATIBLE_PROCESS("incompatible-process"),
|
2024-03-26 14:08:46 +00:00
|
|
|
DISABLE_LARGE_DOWNLOADS("disable-large-downloads");
|
2021-10-02 19:38:04 +00:00
|
|
|
|
|
|
|
|
String propertyName;
|
|
|
|
|
|
|
|
|
|
PropertyNames(String prop) {
|
|
|
|
|
this.propertyName = prop;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 14:02:30 +00:00
|
|
|
@Override public String toString() {
|
2021-10-02 19:38:04 +00:00
|
|
|
return propertyName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static final String ARG_SERVER = "-server";
|
|
|
|
|
public static final String ARG_LOGIN = "-login";
|
|
|
|
|
public static final String ARG_PASSWORD = "-password";
|
|
|
|
|
public static final String ARG_CACHE_DIR = "-cache-dir";
|
|
|
|
|
public static final String ARG_SHARED_ZIP = "-shared-zip";
|
|
|
|
|
public static final String ARG_GPU = "-gpu";
|
|
|
|
|
public static final String ARG_NO_GPU = "--no-gpu";
|
|
|
|
|
public static final String ARG_COMPUTE_METHOD = "-compute-method";
|
|
|
|
|
public static final String ARG_CORES = "-cores";
|
|
|
|
|
public static final String ARG_MEMORY = "-memory";
|
|
|
|
|
public static final String ARG_RENDERTIME = "-rendertime";
|
|
|
|
|
public static final String ARG_VERBOSE = "--verbose";
|
2024-05-05 10:55:46 +00:00
|
|
|
public static final String ARG_LOG_STDOUT = "--log-stdout";
|
2021-10-02 19:38:04 +00:00
|
|
|
public static final String ARG_REQUEST_TIME = "-request-time";
|
|
|
|
|
public static final String ARG_SHUTDOWN = "-shutdown";
|
|
|
|
|
public static final String ARG_SHUTDOWN_MODE = "-shutdown-mode";
|
|
|
|
|
public static final String ARG_PROXY = "-proxy";
|
|
|
|
|
public static final String ARG_EXTRAS = "-extras";
|
|
|
|
|
public static final String ARG_UI = "-ui";
|
|
|
|
|
public static final String ARG_CONFIG = "-config";
|
2024-04-14 09:27:52 +00:00
|
|
|
public static final String ARG_LOG_DIRECTORY = "-logdir";
|
2021-10-02 19:38:04 +00:00
|
|
|
public static final String ARG_VERSION = "--version";
|
|
|
|
|
public static final String ARG_SHOW_GPU = "--show-gpu";
|
|
|
|
|
public static final String ARG_NO_SYSTRAY = "--no-systray";
|
|
|
|
|
public static final String ARG_PRIORITY = "-priority";
|
|
|
|
|
public static final String ARG_TITLE = "-title";
|
|
|
|
|
public static final String ARG_THEME = "-theme";
|
|
|
|
|
public static final String ARG_HOSTNAME = "-hostname";
|
|
|
|
|
public static final String ARG_HEADLESS = "--headless";
|
2024-03-26 14:08:46 +00:00
|
|
|
public static final String ARG_DISABLE_LARGE_DOWNLOADS = "--disable-large-downloads";
|
2024-06-08 03:21:33 +00:00
|
|
|
public static final String ARG_INCOMPATIBLE_PROCESS = "-incompatible-process";
|
2021-10-02 19:38:04 +00:00
|
|
|
|
|
|
|
|
|
2015-03-31 00:29:58 +01:00
|
|
|
private String path;
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
private Option<String> login;
|
2019-08-22 21:35:26 +02:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
private Option<String> password;
|
2024-04-14 09:27:52 +00:00
|
|
|
private Option<String> logDir;
|
2024-05-05 10:55:46 +00:00
|
|
|
private Option<String> debug;
|
2021-10-02 19:38:04 +00:00
|
|
|
private Option<String> proxy;
|
|
|
|
|
private Option<String> hostname;
|
|
|
|
|
private Option<String> computeMethod;
|
|
|
|
|
private Option<String> gpu;
|
|
|
|
|
private Option<String> cores;
|
|
|
|
|
private Option<String> ram;
|
|
|
|
|
private Option<String> renderTime;
|
|
|
|
|
private Option<String> cacheDir;
|
2022-05-22 15:59:44 +10:00
|
|
|
private Option<String> sharedZip;
|
2021-10-02 19:38:04 +00:00
|
|
|
private Option<String> autoSignIn;
|
|
|
|
|
private Option<String> useSysTray;
|
|
|
|
|
private Option<String> headless;
|
|
|
|
|
private Option<String> ui;
|
|
|
|
|
private Option<String> theme;
|
|
|
|
|
private Option<Integer> priority;
|
2024-03-26 14:08:46 +00:00
|
|
|
private Option<String> disableLargeDownloads;
|
2024-06-08 03:21:33 +00:00
|
|
|
private Option<String> incompatibleProcess;
|
2015-03-31 00:29:58 +01:00
|
|
|
|
2015-04-07 20:07:53 +01:00
|
|
|
public SettingsLoader(String path_) {
|
2018-12-18 22:53:01 +01:00
|
|
|
if (path_ == null) {
|
2021-12-29 17:08:51 +00:00
|
|
|
path = OS.getOS().getDefaultConfigFilePath();
|
2018-12-18 22:53:01 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
path = path_;
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
public void setSettings(String path_, String login_, String password_, String proxy_, String hostname_,
|
2021-12-03 23:38:57 +00:00
|
|
|
ComputeType computeMethod_, GPUDevice gpu_, Integer cores_, Long maxRam_,
|
2022-05-22 15:59:44 +10:00
|
|
|
Integer maxRenderTime_, String cacheDir_, String sharedZip_, Boolean autoSignIn_, Boolean useSysTray_,
|
2024-06-08 03:21:33 +00:00
|
|
|
Boolean isHeadless, String ui_, String theme_, Integer priority_, Boolean disableLargeDownloads_, Boolean debug_, String incompatibleProcess_) {
|
2018-12-18 22:53:01 +01:00
|
|
|
if (path_ == null) {
|
2021-12-29 17:08:51 +00:00
|
|
|
path = OS.getOS().getDefaultConfigFilePath();
|
2018-12-18 22:53:01 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
path = path_;
|
|
|
|
|
}
|
2021-10-02 19:38:04 +00:00
|
|
|
login = setValue(login_, login, ARG_LOGIN);
|
|
|
|
|
password = setValue(password_, password, ARG_PASSWORD);
|
|
|
|
|
proxy = setValue(proxy_, proxy, ARG_PROXY);
|
|
|
|
|
hostname = setValue(hostname_, hostname, ARG_HOSTNAME);
|
|
|
|
|
cacheDir = setValue(cacheDir_, cacheDir, ARG_CACHE_DIR);
|
2022-05-22 15:59:44 +10:00
|
|
|
sharedZip = setValue(sharedZip_, sharedZip, ARG_SHARED_ZIP);
|
2021-10-02 19:38:04 +00:00
|
|
|
autoSignIn = setValue(autoSignIn_.toString(), autoSignIn, "");
|
|
|
|
|
useSysTray = setValue(useSysTray_.toString(), useSysTray, ARG_NO_SYSTRAY);
|
|
|
|
|
headless = setValue(isHeadless.toString(), headless, ARG_HEADLESS);
|
|
|
|
|
ui = setValue(ui_, ui, ARG_UI);
|
|
|
|
|
priority = setValue(priority_, priority, ARG_PRIORITY);
|
|
|
|
|
theme = setValue(theme_, theme, ARG_THEME);
|
2024-03-26 14:08:46 +00:00
|
|
|
disableLargeDownloads = setValue(disableLargeDownloads_.toString(), disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS);
|
2024-05-05 10:55:46 +00:00
|
|
|
debug = setValue(debug_.toString(), debug, ARG_VERBOSE);
|
2024-06-08 03:21:33 +00:00
|
|
|
incompatibleProcess = setValue(incompatibleProcess_, incompatibleProcess, ARG_INCOMPATIBLE_PROCESS);
|
2024-03-26 14:08:46 +00:00
|
|
|
|
2015-04-20 21:26:07 +01:00
|
|
|
if (cores_ > 0) {
|
2021-10-02 19:38:04 +00:00
|
|
|
cores = setValue(cores_.toString(), cores, ARG_CORES);
|
2015-04-20 21:26:07 +01:00
|
|
|
}
|
2017-03-29 22:13:04 +02:00
|
|
|
if (maxRam_ > 0) {
|
2021-10-02 19:38:04 +00:00
|
|
|
ram = setValue(maxRam_+ "k", ram, ARG_MEMORY);
|
2017-03-29 22:13:04 +02:00
|
|
|
}
|
2017-04-25 13:06:23 +02:00
|
|
|
if (maxRenderTime_ > 0) {
|
2021-10-02 19:38:04 +00:00
|
|
|
renderTime = setValue(maxRenderTime_.toString(), renderTime, ARG_RENDERTIME);
|
2017-04-25 13:06:23 +02:00
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
if (computeMethod_ != null) {
|
|
|
|
|
try {
|
2021-10-02 19:38:04 +00:00
|
|
|
computeMethod = setValue(computeMethod_.name(), computeMethod, ARG_COMPUTE_METHOD);
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gpu_ != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
gpu = setValue(gpu_.getId(), gpu, ARG_GPU);
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
/**
|
|
|
|
|
* sets an option to a given value. If the option being passed on is null it will be created with the given value and returned.
|
|
|
|
|
* @param value The value to be set
|
|
|
|
|
* @param option The {@link Option} object that the value is going to be stored in. Can be null
|
|
|
|
|
* @param launchFlag A flag indicating whether the option was set via a launch argument or not
|
|
|
|
|
* @param <T> The type of the value stored within the option
|
|
|
|
|
* @return The {@link Option} object that has been passed on as a parameter with the value being set, or a newly created object if option was null
|
|
|
|
|
*/
|
|
|
|
|
private <T> Option<T> setValue(T value, Option<T> option, String launchFlag) {
|
|
|
|
|
if (option == null && value != null) {
|
|
|
|
|
option = new Option<>(value, launchFlag);
|
|
|
|
|
}
|
|
|
|
|
else if (value != null){
|
|
|
|
|
option.setValue(value);
|
|
|
|
|
}
|
|
|
|
|
return option;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 20:31:16 +01:00
|
|
|
public String getFilePath() {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
/**
|
|
|
|
|
* Takes the list of launch parameters and marks every config setting corresponding to one of the set values as launch command, ensuring that they wont overwrite
|
|
|
|
|
* the one in the config file
|
|
|
|
|
* @param argsList a list of the launch arguments
|
|
|
|
|
*/
|
|
|
|
|
public void markLaunchSettings(List<String> argsList) {
|
2022-05-22 15:59:44 +10:00
|
|
|
Option options[] = { login, password, proxy, hostname, computeMethod, gpu, cores, ram, renderTime, cacheDir, sharedZip,
|
2024-03-26 14:08:46 +00:00
|
|
|
autoSignIn, useSysTray, headless, ui, theme, priority, disableLargeDownloads };
|
2021-10-02 19:38:04 +00:00
|
|
|
|
|
|
|
|
for (Option option : options) {
|
|
|
|
|
if (option != null && argsList.contains(option.getLaunchFlag())) {
|
|
|
|
|
option.setLaunchCommand(true);
|
2020-06-09 14:47:39 +10:00
|
|
|
}
|
2021-10-02 19:38:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Selects the right setting to store to the config file between the value currently set in the config file and the option the client is working with
|
|
|
|
|
* currently, depending on whether the setting was set via launch argument or not
|
|
|
|
|
* @param saveTo the properties object representing the config file that is going to be written
|
|
|
|
|
* @param configFileProperties the properties object containing the current config file values
|
|
|
|
|
* @param property an enum representing the name of the setting
|
|
|
|
|
* @param option the option containing the currently used value
|
|
|
|
|
*/
|
|
|
|
|
private void setProperty(Properties saveTo, Properties configFileProperties, PropertyNames property, Option<String> option) {
|
|
|
|
|
if (option != null) {
|
|
|
|
|
if (option.isLaunchCommand()) {
|
|
|
|
|
String configValue = configFileProperties.getProperty(property.propertyName);
|
|
|
|
|
if (configValue != null) {
|
|
|
|
|
saveTo.setProperty(property.propertyName, configValue);
|
|
|
|
|
}
|
2021-07-16 23:37:24 +00:00
|
|
|
}
|
2021-10-02 19:38:04 +00:00
|
|
|
else {
|
|
|
|
|
saveTo.setProperty(property.propertyName, option.getValue());
|
2015-04-08 20:37:53 +01:00
|
|
|
}
|
2021-10-02 19:38:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("PointlessBooleanExpression") public void saveFile() {
|
|
|
|
|
|
|
|
|
|
Properties configFileProp = new Properties();
|
|
|
|
|
if (new File(path).exists()) {
|
|
|
|
|
try {
|
2024-06-06 01:05:27 +00:00
|
|
|
InputStream input = new FileInputStream(path);
|
2021-10-02 19:38:04 +00:00
|
|
|
configFileProp.load(input);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
2020-04-14 23:24:28 +10:00
|
|
|
}
|
2021-10-02 19:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Properties prop = new Properties();
|
|
|
|
|
OutputStream output = null;
|
|
|
|
|
try {
|
|
|
|
|
output = new FileOutputStream(path);
|
2015-04-08 20:37:53 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
setProperty(prop, configFileProp, PropertyNames.PRIORITY,
|
|
|
|
|
new Option<>(priority != null ? priority.getValue().toString() : null, priority.isLaunchCommand(), ARG_PRIORITY));
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.CACHE_DIR, cacheDir);
|
2022-05-22 15:59:44 +10:00
|
|
|
setProperty(prop, configFileProp, PropertyNames.SHARED_ZIP, sharedZip);
|
2021-10-02 19:38:04 +00:00
|
|
|
setProperty(prop, configFileProp, PropertyNames.COMPUTE_METHOD, computeMethod);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.GPU, gpu);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.CORES, cores);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.RAM, ram);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.RENDER_TIME, renderTime);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.LOGIN, login);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.PASSWORD, password);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.PROXY, proxy);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.HOSTNAME, hostname);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.AUTO_SIGNIN, autoSignIn);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.USE_SYSTRAY, useSysTray);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.HEADLESS, headless);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.UI, ui);
|
|
|
|
|
setProperty(prop, configFileProp, PropertyNames.THEME, theme);
|
2024-03-26 14:08:46 +00:00
|
|
|
setProperty(prop, configFileProp, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads);
|
2024-04-14 09:27:52 +00:00
|
|
|
setProperty(prop, configFileProp, PropertyNames.LOG_DIR, logDir);
|
2024-05-05 10:55:46 +00:00
|
|
|
setProperty(prop, configFileProp, PropertyNames.DEBUG, debug);
|
2024-06-08 03:21:33 +00:00
|
|
|
setProperty(prop, configFileProp, PropertyNames.INCOMPATIBLE_PROCESS, incompatibleProcess);
|
2015-03-31 00:29:58 +01:00
|
|
|
prop.store(output, null);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException io) {
|
|
|
|
|
io.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
if (output != null) {
|
|
|
|
|
try {
|
|
|
|
|
output.close();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-22 19:41:50 +01:00
|
|
|
|
|
|
|
|
// Set Owner read/write
|
|
|
|
|
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
|
|
|
|
|
perms.add(PosixFilePermission.OWNER_READ);
|
|
|
|
|
perms.add(PosixFilePermission.OWNER_WRITE);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Files.setPosixFilePermissions(Paths.get(path), perms);
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedOperationException e) {
|
|
|
|
|
// most likely because it's MS Windows
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
/**
|
|
|
|
|
* Initializes or sets an option object to the corresponding value from the config file
|
|
|
|
|
* @param config the properties loaded from the config file
|
|
|
|
|
* @param property the name of the property
|
|
|
|
|
* @param option the option to store the property value
|
|
|
|
|
* @param launchFlag the launch argument corresponding to the respective option
|
|
|
|
|
*/
|
|
|
|
|
private Option<String> loadConfigOption(Properties config, PropertyNames property, Option<String> option, String launchFlag) {
|
|
|
|
|
String configValue;
|
|
|
|
|
if (config.containsKey(property.propertyName)) {
|
|
|
|
|
configValue = config.getProperty(property.propertyName);
|
|
|
|
|
if (option == null && configValue != null) {
|
|
|
|
|
option = new Option<>(configValue, launchFlag);
|
|
|
|
|
}
|
|
|
|
|
else if (configValue != null){
|
|
|
|
|
option.setValue(configValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return option;
|
|
|
|
|
}
|
2021-05-11 19:07:47 +02:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
public void loadFile(boolean initialize) throws Exception {
|
2021-05-11 19:07:47 +02:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
if (initialize)
|
|
|
|
|
initWithDefaults();
|
2015-03-31 00:29:58 +01:00
|
|
|
|
|
|
|
|
if (new File(path).exists() == false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Properties prop = new Properties();
|
|
|
|
|
InputStream input = null;
|
|
|
|
|
try {
|
|
|
|
|
input = new FileInputStream(path);
|
|
|
|
|
prop.load(input);
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
cacheDir = loadConfigOption(prop, PropertyNames.CACHE_DIR, cacheDir, ARG_CACHE_DIR);
|
2015-03-31 00:29:58 +01:00
|
|
|
|
2022-05-22 15:59:44 +10:00
|
|
|
sharedZip = loadConfigOption(prop, PropertyNames.SHARED_ZIP, sharedZip, ARG_SHARED_ZIP);
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
computeMethod = loadConfigOption(prop, PropertyNames.COMPUTE_METHOD, computeMethod, ARG_COMPUTE_METHOD);
|
2015-03-31 00:29:58 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
gpu = loadConfigOption(prop, PropertyNames.GPU, gpu, ARG_GPU);
|
2015-03-31 00:29:58 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
cores = loadConfigOption(prop, PropertyNames.CORES_BACKWARDS_COMPAT, cores, ARG_CORES);
|
2015-04-20 21:26:07 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
cores = loadConfigOption(prop, PropertyNames.CORES, cores, ARG_CORES);
|
2018-12-11 07:25:04 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
ram = loadConfigOption(prop, PropertyNames.RAM, ram, ARG_MEMORY);
|
2017-03-29 22:13:04 +02:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
renderTime = loadConfigOption(prop, PropertyNames.RENDER_TIME, renderTime, ARG_RENDERTIME);
|
2017-04-25 13:06:23 +02:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
login = loadConfigOption(prop, PropertyNames.LOGIN, login, ARG_LOGIN);
|
2015-03-31 00:29:58 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
password = loadConfigOption(prop, PropertyNames.PASSWORD, password, ARG_PASSWORD);
|
2015-04-03 19:50:58 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
proxy = loadConfigOption(prop, PropertyNames.PROXY, proxy, ARG_PROXY);
|
2015-07-06 18:41:28 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
hostname = loadConfigOption(prop, PropertyNames.HOSTNAME, hostname, ARG_HOSTNAME);
|
2017-05-08 01:35:08 +02:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
autoSignIn = loadConfigOption(prop, PropertyNames.AUTO_SIGNIN, autoSignIn, "");
|
2015-04-08 20:37:53 +01:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
useSysTray = loadConfigOption(prop, PropertyNames.USE_SYSTRAY, useSysTray, ARG_NO_SYSTRAY);
|
2020-06-09 14:47:39 +10:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
headless = loadConfigOption(prop, PropertyNames.HEADLESS, headless, ARG_HEADLESS);
|
2021-07-16 23:37:24 +00:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
ui = loadConfigOption(prop, PropertyNames.UI, ui, ARG_UI);
|
2016-09-21 23:26:44 +02:00
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
theme = loadConfigOption(prop, PropertyNames.THEME, theme, ARG_THEME);
|
2020-05-28 13:28:42 +02:00
|
|
|
|
2024-03-26 14:08:46 +00:00
|
|
|
disableLargeDownloads = loadConfigOption(prop, PropertyNames.DISABLE_LARGE_DOWNLOADS, disableLargeDownloads, ARG_DISABLE_LARGE_DOWNLOADS);
|
|
|
|
|
|
2024-04-14 09:27:52 +00:00
|
|
|
logDir = loadConfigOption(prop, PropertyNames.LOG_DIR, logDir, ARG_LOG_DIRECTORY);
|
2024-06-08 03:21:33 +00:00
|
|
|
|
|
|
|
|
incompatibleProcess = loadConfigOption(prop, PropertyNames.INCOMPATIBLE_PROCESS, incompatibleProcess, "");
|
2024-04-14 09:27:52 +00:00
|
|
|
|
2024-05-05 10:55:46 +00:00
|
|
|
debug = loadConfigOption(prop, PropertyNames.DEBUG, debug, ARG_VERBOSE);
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
if (prop.containsKey(PropertyNames.PRIORITY.propertyName)) {
|
|
|
|
|
int prio = Integer.parseInt(prop.getProperty(PropertyNames.PRIORITY.propertyName));
|
|
|
|
|
if (priority == null) {
|
|
|
|
|
priority = new Option<>(prio, ARG_PRIORITY);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
priority.setValue(prio);
|
|
|
|
|
}
|
2017-03-19 17:38:22 +01:00
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
2021-05-11 19:07:47 +02:00
|
|
|
catch (Exception e) { //We need the try-catch here to ensure that the input file will be closed though we'll deal with the exception in the calling method
|
|
|
|
|
throw e;
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
if (input != null) {
|
|
|
|
|
try {
|
|
|
|
|
input.close();
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 23:06:21 +01:00
|
|
|
/**
|
|
|
|
|
* Merge the Settings file with the Configuration.
|
|
|
|
|
* The Configuration will have high priority.
|
2021-10-02 19:38:04 +00:00
|
|
|
* @param config the config file
|
|
|
|
|
* @param initialize whether to initialize all fields with default values, should only be true on first call
|
2015-03-31 23:06:21 +01:00
|
|
|
*/
|
2021-10-02 19:38:04 +00:00
|
|
|
public void merge(Configuration config, boolean initialize) {
|
2015-03-31 00:29:58 +01:00
|
|
|
if (config == null) {
|
|
|
|
|
System.out.println("SettingsLoader::merge config is null");
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 19:07:47 +02:00
|
|
|
try {
|
2021-10-02 19:38:04 +00:00
|
|
|
loadFile(initialize);
|
2021-05-11 19:07:47 +02:00
|
|
|
applyConfigFileValues(config);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
System.err.println("Exception while reading the config file. Falling back to defaults");
|
|
|
|
|
initWithDefaults();
|
|
|
|
|
applyConfigFileValues(config);
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
|
2021-05-11 19:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void applyConfigFileValues(Configuration config) {
|
2019-08-07 22:17:59 +02:00
|
|
|
if (config.getLogin().isEmpty() && login != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setLogin(login.getValue());
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
2019-08-07 22:17:59 +02:00
|
|
|
if (config.getPassword().isEmpty() && password != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setPassword(password.getValue());
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-07-06 18:41:28 +01:00
|
|
|
if ((config.getProxy() == null || config.getProxy().isEmpty()) && proxy != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setProxy(proxy.getValue());
|
2015-07-06 18:41:28 +01:00
|
|
|
}
|
|
|
|
|
|
2017-05-08 01:35:08 +02:00
|
|
|
if ((config.getHostname() == null || config.getHostname().isEmpty() || config.getHostname().equals(config.getDefaultHostname())) && hostname != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setHostname(hostname.getValue());
|
2017-05-08 01:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
if (config.isHeadless() == false && headless != null) {
|
|
|
|
|
config.setHeadless(Boolean.parseBoolean(headless.getValue()));
|
2021-07-16 23:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
2017-03-19 17:38:22 +01:00
|
|
|
if (config.getPriority() == 19) { // 19 is default value
|
2023-10-19 11:09:08 +00:00
|
|
|
config.setPriority(priority.getValue());
|
2017-03-19 17:38:22 +01:00
|
|
|
}
|
2024-06-08 03:21:33 +00:00
|
|
|
|
|
|
|
|
if (incompatibleProcess != null) {
|
|
|
|
|
config.setIncompatibleProcess(incompatibleProcess.getValue());
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 20:37:47 +01:00
|
|
|
try {
|
2020-06-21 02:31:31 +10:00
|
|
|
if (config.getComputeMethod() == null && computeMethod == null) {
|
|
|
|
|
config.setComputeMethod(ComputeType.CPU);
|
|
|
|
|
}
|
2024-06-03 14:02:30 +00:00
|
|
|
else if ((config.getComputeMethod() == null && computeMethod != null) || (computeMethod != null && config.getComputeMethod() != ComputeType.valueOf(computeMethod.getValue()))) {
|
2020-06-21 02:31:31 +10:00
|
|
|
if (config.getComputeMethod() == null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setComputeMethod(ComputeType.valueOf(computeMethod.getValue()));
|
2020-06-21 02:31:31 +10:00
|
|
|
}
|
|
|
|
|
|
2015-04-07 20:37:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException e) {
|
|
|
|
|
System.err.println("SettingsLoader::merge failed to handle compute method (raw value: '" + computeMethod + "')");
|
|
|
|
|
computeMethod = null;
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
2015-03-31 23:06:21 +01:00
|
|
|
if (config.getGPUDevice() == null && gpu != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
GPUDevice device = GPU.getGPUDevice(gpu.getValue());
|
2015-03-31 00:29:58 +01:00
|
|
|
if (device != null) {
|
2019-08-07 22:17:59 +02:00
|
|
|
config.setGPUDevice(device);
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
2021-02-23 20:25:56 +00:00
|
|
|
else if (config.getUIType() != null && (config.getUIType().equals(GuiText.type) || config.getUIType().equals(GuiTextOneLine.type))) {
|
|
|
|
|
System.err.println("SettingsLoader::merge could not find specified GPU");
|
|
|
|
|
System.exit(2);
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
2020-05-16 18:20:38 +10:00
|
|
|
|
2017-01-05 09:35:59 +01:00
|
|
|
if (config.getNbCores() == -1 && cores != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setNbCores(Integer.parseInt(cores.getValue()));
|
2015-04-20 21:26:07 +01:00
|
|
|
}
|
2017-03-29 22:13:04 +02:00
|
|
|
|
2021-11-16 14:51:53 +00:00
|
|
|
if (config.getMaxAllowedMemory() == -1 && ram != null) {
|
2022-02-19 16:51:06 +00:00
|
|
|
config.setMaxAllowedMemory(Utils.parseNumber(ram.getValue()) / 1000); // internal ram value is in KiB
|
2017-03-29 22:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-25 13:06:23 +02:00
|
|
|
if (config.getMaxRenderTime() == -1 && renderTime != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setMaxRenderTime(Integer.parseInt(renderTime.getValue()));
|
2017-04-25 13:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-22 15:59:44 +10:00
|
|
|
if (config.getSharedDownloadsDirectory() == null && sharedZip != null) {
|
|
|
|
|
config.setSharedDownloadsDirectory(new File(sharedZip.getValue()));
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 22:17:59 +02:00
|
|
|
if (config.isUserHasSpecifiedACacheDir() == false && cacheDir != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setCacheDir(new File(cacheDir.getValue()));
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
2015-04-03 19:50:58 +01:00
|
|
|
|
2015-04-08 20:37:53 +01:00
|
|
|
if (config.getUIType() == null && ui != null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
config.setUIType(ui.getValue());
|
2015-04-08 20:37:53 +01:00
|
|
|
}
|
2020-05-28 13:28:42 +02:00
|
|
|
|
2020-04-14 23:24:28 +10:00
|
|
|
if (config.getTheme() == null) {
|
2021-10-02 19:38:04 +00:00
|
|
|
if (this.theme != null && (this.theme.getValue().equals("dark") || this.theme.getValue().equals("light"))) {
|
|
|
|
|
config.setTheme(this.theme.getValue());
|
2020-05-28 13:28:42 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2020-04-14 23:24:28 +10:00
|
|
|
config.setTheme("light");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-28 13:28:42 +02:00
|
|
|
|
2024-03-26 14:08:46 +00:00
|
|
|
if (config.isDisableLargeDownloads() == false && disableLargeDownloads != null) {
|
|
|
|
|
config.setDisableLargeDownloads(Boolean.parseBoolean(disableLargeDownloads.getValue()));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 14:47:39 +10:00
|
|
|
// if the user has invoked the app with --no-systray, then we just overwrite the existing configuration with (boolean)false. If no parameter has been
|
|
|
|
|
// specified and the settings file contains use-systray=false, then deactivate as well.
|
2021-10-02 19:38:04 +00:00
|
|
|
if (!config.isUseSysTray() || (config.isUseSysTray() && useSysTray != null && useSysTray.getValue().equals("false"))) {
|
2020-06-09 14:47:39 +10:00
|
|
|
config.setUseSysTray(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 19:38:04 +00:00
|
|
|
if (config.isAutoSignIn() == false && autoSignIn != null) {
|
|
|
|
|
config.setAutoSignIn(Boolean.parseBoolean(autoSignIn.getValue()));
|
|
|
|
|
}
|
2024-04-14 09:27:52 +00:00
|
|
|
|
|
|
|
|
if (config.getLogDirectory() != null && logDir != null) {
|
|
|
|
|
config.setLogDirectory(logDir.getValue());
|
|
|
|
|
}
|
2024-05-05 10:55:46 +00:00
|
|
|
|
|
|
|
|
if (config.isDebugLevel() == false && debug != null) {
|
|
|
|
|
config.setDebugLevel(Boolean.parseBoolean(debug.getValue()));
|
|
|
|
|
}
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-11 19:07:47 +02:00
|
|
|
private void initWithDefaults() {
|
|
|
|
|
Configuration defaultConfigValues = new Configuration(null, null, null);
|
|
|
|
|
this.login = null;
|
|
|
|
|
this.password = null;
|
|
|
|
|
this.proxy = null;
|
|
|
|
|
this.hostname = null;
|
|
|
|
|
this.computeMethod = null;
|
|
|
|
|
this.gpu = null;
|
|
|
|
|
this.cacheDir = null;
|
2022-05-22 15:59:44 +10:00
|
|
|
this.sharedZip = null;
|
2021-05-11 19:07:47 +02:00
|
|
|
this.autoSignIn = null;
|
2021-10-02 19:38:04 +00:00
|
|
|
this.useSysTray = new Option<>(String.valueOf(defaultConfigValues.isUseSysTray()), ARG_NO_SYSTRAY);
|
|
|
|
|
this.headless = new Option<>(String.valueOf(defaultConfigValues.isHeadless()), ARG_HEADLESS);
|
2021-05-11 19:07:47 +02:00
|
|
|
this.ui = null;
|
2021-10-02 19:38:04 +00:00
|
|
|
this.priority = new Option<>(defaultConfigValues.getPriority(), ARG_PRIORITY); // must be the same default as Configuration
|
2021-05-11 19:07:47 +02:00
|
|
|
this.ram = null;
|
|
|
|
|
this.renderTime = null;
|
|
|
|
|
this.theme = null;
|
2021-10-02 19:38:04 +00:00
|
|
|
this.cores = new Option<>(String.valueOf(defaultConfigValues.getNbCores()), ARG_CORES);
|
2024-03-26 14:08:46 +00:00
|
|
|
this.disableLargeDownloads = new Option<>(String.valueOf(defaultConfigValues.isDisableLargeDownloads()), ARG_DISABLE_LARGE_DOWNLOADS);
|
2024-04-14 09:27:52 +00:00
|
|
|
this.logDir = null;
|
2024-05-05 10:55:46 +00:00
|
|
|
this.debug = null;
|
2024-06-08 03:21:33 +00:00
|
|
|
this.incompatibleProcess = null;
|
2021-05-11 19:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-28 13:28:42 +02:00
|
|
|
@Override public String toString() {
|
2020-06-09 14:47:39 +10:00
|
|
|
return String.format(
|
2024-05-05 10:55:46 +00:00
|
|
|
"SettingsLoader [path=%s, login=%s, password=%s, computeMethod=%s, gpu=%s, cacheDir=%s, sharedZip=%s, theme=%s, priority=%d, autosign=%s, usetray=%s, headless=%s, disableLargeDownloads=%s, logDir=%s, debug=%s]",
|
|
|
|
|
path, login, password, computeMethod, gpu, cacheDir, sharedZip, theme, priority, autoSignIn, useSysTray, headless, disableLargeDownloads, logDir, debug);
|
2015-03-31 00:29:58 +01:00
|
|
|
}
|
|
|
|
|
}
|