Skip to content

Commit

Permalink
refactoring: Streamline LauncherSettings to ease replacement (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
skaldarnar authored Oct 16, 2020
1 parent 5a20d1a commit d723157
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 102 deletions.
21 changes: 4 additions & 17 deletions src/main/java/org/terasology/launcher/LauncherConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.terasology.launcher.packages.PackageManager;
import org.terasology.launcher.settings.BaseLauncherSettings;
import org.terasology.launcher.settings.LauncherSettings;

import java.nio.file.Path;

Expand All @@ -36,21 +37,15 @@ public class LauncherConfiguration {

private final Path launcherDirectory;
private final Path downloadDirectory;
private final Path tempDirectory;
private final Path cacheDirectory;
private final BaseLauncherSettings launcherSettings;
private final LauncherSettings launcherSettings;
private final PackageManager packageManager;

public LauncherConfiguration(final Path launcherDirectory,
final Path downloadDirectory,
final Path tempDirectory,
final Path cacheDirectory,
final BaseLauncherSettings launcherSettings,
final LauncherSettings launcherSettings,
final PackageManager packageManager) {
this.launcherDirectory = launcherDirectory;
this.downloadDirectory = downloadDirectory;
this.tempDirectory = tempDirectory;
this.cacheDirectory = cacheDirectory;
this.launcherSettings = launcherSettings;
this.packageManager = packageManager;
}
Expand All @@ -63,15 +58,7 @@ public Path getDownloadDirectory() {
return downloadDirectory;
}

public Path getTempDirectory() {
return tempDirectory;
}

public Path getCacheDirectory() {
return cacheDirectory;
}

public BaseLauncherSettings getLauncherSettings() {
public LauncherSettings getLauncherSettings() {
return launcherSettings;
}

Expand Down
37 changes: 19 additions & 18 deletions src/main/java/org/terasology/launcher/LauncherInitTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.slf4j.LoggerFactory;
import org.terasology.launcher.packages.PackageManager;
import org.terasology.launcher.settings.BaseLauncherSettings;
import org.terasology.launcher.settings.LauncherSettings;
import org.terasology.launcher.settings.LauncherSettingsValidator;
import org.terasology.launcher.settings.Settings;
import org.terasology.launcher.updater.LauncherUpdater;
import org.terasology.launcher.util.BundleUtils;
import org.terasology.launcher.util.DirectoryCreator;
Expand All @@ -44,6 +46,7 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class LauncherInitTask extends Task<LauncherConfiguration> {
Expand Down Expand Up @@ -81,7 +84,8 @@ protected LauncherConfiguration call() {
final Path cacheDirectory = getDirectoryFor(LauncherManagedDirectory.CACHE, userDataDirectory);

// launcher settings
final BaseLauncherSettings launcherSettings = getLauncherSettings(userDataDirectory);
final Path settingsFile = userDataDirectory.resolve(Settings.DEFAULT_FILE_NAME);
final LauncherSettings launcherSettings = getLauncherSettings(settingsFile);

// validate the settings
LauncherSettingsValidator.validate(launcherSettings);
Expand Down Expand Up @@ -119,11 +123,11 @@ protected LauncherConfiguration call() {
launcherSettings.setGameDataDirectory(gameDataDirectory);
// TODO: Rewrite gameVersions.fixSettingsBuildVersion(launcherSettings);

storeLauncherSettingsAfterInit(launcherSettings);
storeLauncherSettingsAfterInit(launcherSettings, settingsFile);

logger.trace("Creating launcher frame...");

return new LauncherConfiguration(userDataDirectory, downloadDirectory, tempDirectory, cacheDirectory, launcherSettings, packageManager);
return new LauncherConfiguration(userDataDirectory, downloadDirectory, launcherSettings, packageManager);
} catch (LauncherStartFailedException e) {
logger.warn("Could not configure launcher.");
}
Expand Down Expand Up @@ -172,20 +176,16 @@ private Path getLauncherDirectory(Platform platform) throws LauncherStartFailedE
return launcherDirectory;
}

private BaseLauncherSettings getLauncherSettings(Path launcherDirectory) throws LauncherStartFailedException {
private LauncherSettings getLauncherSettings(Path settingsFile) throws LauncherStartFailedException {
logger.trace("Init LauncherSettings...");
updateMessage(BundleUtils.getLabel("splash_retrieveLauncherSettings"));
final BaseLauncherSettings launcherSettings = new BaseLauncherSettings(launcherDirectory);
try {
launcherSettings.load();
launcherSettings.init();
} catch (IOException e) {
logger.error("The launcher settings can not be loaded or initialized! '{}'", launcherSettings.getLauncherSettingsFilePath(), e);
Dialogs.showError(owner, BundleUtils.getLabel("message_error_loadSettings") + "\n" + launcherSettings.getLauncherSettingsFilePath());
throw new LauncherStartFailedException();
}
logger.debug("Launcher Settings: {}", launcherSettings);
return launcherSettings;

final LauncherSettings settings = Optional.ofNullable(Settings.load(settingsFile)).orElse(Settings.getDefault());
settings.init();

logger.debug("Launcher Settings: {}", settings);

return settings;
}

private boolean checkForLauncherUpdates(Path downloadDirectory, Path tempDirectory, boolean saveDownloadedFiles) {
Expand Down Expand Up @@ -289,14 +289,15 @@ private boolean confirmSourcesOverwrite() {
}, javafx.application.Platform::runLater).join();
}

private void storeLauncherSettingsAfterInit(BaseLauncherSettings launcherSettings) throws LauncherStartFailedException {
private void storeLauncherSettingsAfterInit(LauncherSettings launcherSettings, final Path settingsFile) throws LauncherStartFailedException {
logger.trace("Store LauncherSettings...");
updateMessage(BundleUtils.getLabel("splash_storeLauncherSettings"));
try {
launcherSettings.store();
Settings.store(launcherSettings, settingsFile);
} catch (IOException e) {
logger.error("The launcher settings can not be stored! '{}'", launcherSettings.getLauncherSettingsFilePath(), e);
logger.error("The launcher settings cannot be stored! '{}'", settingsFile, e);
Dialogs.showError(owner, BundleUtils.getLabel("message_error_storeSettings"));
//TODO: should we fail here, or is it fine to work with in-memory settings?
throw new LauncherStartFailedException();
}
logger.debug("Launcher Settings stored: {}", launcherSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class NullLauncherConfiguration extends LauncherConfiguration {
private static NullLauncherConfiguration instance;

private NullLauncherConfiguration() {
super(null, null, null, null, null, null);
super(null, null, null, null);
}

public static NullLauncherConfiguration getInstance() {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/terasology/launcher/TerasologyLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,13 @@ private void showMainStage(final LauncherConfiguration launcherConfiguration) th
root = (Parent) fxmlLoader.load();
}
final ApplicationController controller = fxmlLoader.getController();
controller.update(launcherConfiguration.getLauncherDirectory(), launcherConfiguration.getDownloadDirectory(),
launcherConfiguration.getLauncherSettings(), launcherConfiguration.getPackageManager(), mainStage, hostServices);
controller.update(
launcherConfiguration.getLauncherDirectory(),
launcherConfiguration.getDownloadDirectory(),
launcherConfiguration.getLauncherSettings(),
launcherConfiguration.getPackageManager(),
mainStage,
hostServices);

Scene scene = new Scene(root);
scene.getStylesheets().add(BundleUtils.getStylesheet("css_terasology"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.terasology.launcher.packages.Package;
import org.terasology.launcher.packages.PackageManager;
import org.terasology.launcher.settings.BaseLauncherSettings;
import org.terasology.launcher.settings.LauncherSettings;
import org.terasology.launcher.settings.Settings;
import org.terasology.launcher.tasks.DeleteTask;
import org.terasology.launcher.tasks.DownloadTask;
import org.terasology.launcher.util.BundleUtils;
Expand All @@ -73,7 +75,7 @@ public class ApplicationController {
private static final long MINIMUM_FREE_SPACE = 200 * MB;

private Path launcherDirectory;
private BaseLauncherSettings launcherSettings;
private LauncherSettings launcherSettings;
private PackageManager packageManager;
private GameStarter gameStarter;
private Stage stage;
Expand Down Expand Up @@ -280,7 +282,7 @@ protected void deleteAction() {
});
}

public void update(final Path newLauncherDirectory, final Path newDownloadDirectory, final BaseLauncherSettings newLauncherSettings,
public void update(final Path newLauncherDirectory, final Path newDownloadDirectory, final LauncherSettings newLauncherSettings,
final PackageManager newPackageManager, final Stage newStage, final HostServices hostServices) {
this.launcherDirectory = newLauncherDirectory;
this.launcherSettings = newLauncherSettings;
Expand Down Expand Up @@ -452,8 +454,9 @@ private void initButtons() {
*/
private void close() {
logger.debug("Dispose launcher frame...");
final Path settingsFile = launcherDirectory.resolve(Settings.DEFAULT_FILE_NAME);
try {
launcherSettings.store();
Settings.store(launcherSettings, settingsFile);
} catch (IOException e) {
logger.warn("Could not store current launcher settings!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.slf4j.event.Level;
import org.terasology.launcher.packages.PackageManager;
import org.terasology.launcher.settings.BaseLauncherSettings;
import org.terasology.launcher.settings.LauncherSettings;
import org.terasology.launcher.settings.Settings;
import org.terasology.launcher.util.BundleUtils;
import org.terasology.launcher.util.JavaHeapSize;
import org.terasology.launcher.util.Languages;
Expand All @@ -49,7 +51,7 @@ public class SettingsController {
private static final Logger logger = LoggerFactory.getLogger(SettingsController.class);

private Path launcherDirectory;
private BaseLauncherSettings launcherSettings;
private LauncherSettings launcherSettings;
private PackageManager packageManager;
private ApplicationController appController;

Expand Down Expand Up @@ -163,10 +165,12 @@ protected void saveSettingsAction(ActionEvent event) {
}

// store changed settings
final Path settingsFile = launcherDirectory.resolve(Settings.DEFAULT_FILE_NAME);
try {
launcherSettings.store();
Settings.store(launcherSettings, settingsFile);
} catch (IOException e) {
logger.error("The launcher settings can not be stored! '{}'", launcherSettings.getLauncherSettingsFilePath(), e);
//TODO: unify error handling, probably to Settings a.k.a. SettingsController?
logger.error("The launcher settings cannot be stored! '{}'", settingsFile, e);
Dialogs.showError(stage, BundleUtils.getLabel("message_error_storeSettings"));
} finally {
((Node) event.getSource()).getScene().getWindow().hide();
Expand Down Expand Up @@ -206,7 +210,7 @@ protected void updateInitialHeapSizeBox() {
}
}

void initialize(final Path newLauncherDirectory, final BaseLauncherSettings newLauncherSettings,
void initialize(final Path newLauncherDirectory, final LauncherSettings newLauncherSettings,
final PackageManager newPackageManager, final Stage newStage, final ApplicationController newAppController) {
this.launcherDirectory = newLauncherDirectory;
this.launcherSettings = newLauncherSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@
import org.terasology.launcher.util.JavaHeapSize;
import org.terasology.launcher.util.Languages;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Locale;
Expand All @@ -39,7 +35,7 @@
* @deprecated to be replaced by {@link org.terasology.launcher.config.Config}
*/
@Deprecated
public final class BaseLauncherSettings extends AbstractLauncherSettings {
public final class BaseLauncherSettings extends LauncherSettings {

public static final String USER_JAVA_PARAMETERS_DEFAULT = "-XX:MaxGCPauseMillis=20";
public static final String USER_GAME_PARAMETERS_DEFAULT = "";
Expand Down Expand Up @@ -76,49 +72,19 @@ public final class BaseLauncherSettings extends AbstractLauncherSettings {

private static final Logger logger = LoggerFactory.getLogger(BaseLauncherSettings.class);

private static final String COMMENT_SETTINGS = "Terasology Launcher - Settings";

private static final String WARN_MSG_INVALID_VALUE = "Invalid value '{}' for the parameter '{}'!";
private static final Level LOG_LEVEL_DEFAULT = Level.INFO;

private final Path launcherSettingsFile;
private final Properties properties;

public BaseLauncherSettings(Path launcherDirectory) {
launcherSettingsFile = launcherDirectory.resolve(LAUNCHER_SETTINGS_FILE_NAME);
properties = new Properties();
}

public String getLauncherSettingsFilePath() {
return launcherSettingsFile.toString();
}

@Override
public synchronized void load() throws IOException {
if (Files.exists(launcherSettingsFile)) {
logger.debug("Load the launcher settings from the file '{}'.", launcherSettingsFile);

// load settings
try (InputStream inputStream = Files.newInputStream(launcherSettingsFile)) {
properties.load(inputStream);
}
}
BaseLauncherSettings(Properties properties) {
this.properties = properties;
}

@Override
public synchronized void store() throws IOException {
logger.trace("Store the launcher settings into the file '{}'.", launcherSettingsFile);

// create directory
if (Files.notExists(launcherSettingsFile.getParent())) {
Files.createDirectories(launcherSettingsFile.getParent());
}

// store settings
try (OutputStream outputStream = Files.newOutputStream(launcherSettingsFile)) {
properties.store(outputStream, COMMENT_SETTINGS);
logger.trace("Stored settings: {}", this);
}
public Properties getProperties() {
return properties;
}

// --------------------------------------------------------------------- //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

/**
* @deprecated to be replaced by {@link org.terasology.launcher.config.Config}
*/
@Deprecated
public abstract class AbstractLauncherSettings {
public abstract class LauncherSettings {

private static final Logger logger = LoggerFactory.getLogger(AbstractLauncherSettings.class);
private static final Logger logger = LoggerFactory.getLogger(LauncherSettings.class);

public abstract void load() throws IOException;

public abstract void store() throws IOException;
public abstract Properties getProperties();

public synchronized void init() {
logger.trace("Init launcher settings ...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ private static String removeUnsupportedJvmParameters(final List<String> params)
}

/**
* Validates an {@link AbstractLauncherSettings} instance against a list of rules.
* Validates an {@link LauncherSettings} instance against a list of rules.
* Also applies a correction to the settings if it breaks any rule.
*
* @param settings the settings to be validated
*/
public static void validate(AbstractLauncherSettings settings) {
public static void validate(LauncherSettings settings) {
for (SettingsValidationRule rule : RULES) {
if (rule.isBrokenBy(settings)) {
logger.warn(rule.getInvalidationMessage());
Expand Down
Loading

0 comments on commit d723157

Please sign in to comment.