Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: merge 'games' directory into launcher installation directory #536

Merged
merged 11 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 13 additions & 45 deletions src/main/java/org/terasology/launcher/LauncherInitTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ protected LauncherConfiguration call() {

// init directories
updateMessage(BundleUtils.getLabel("splash_initLauncherDirs"));
final Path launcherDirectory = getLauncherDirectory(os);
final Path installationDirectory = LauncherDirectoryUtils.getInstallationDirectory();
final Path userDataDirectory = getLauncherDirectory(os);

final Path downloadDirectory = getDirectoryFor(org.terasology.launcher.util.LauncherManagedDirectory.DOWNLOAD, launcherDirectory);
final Path tempDirectory = getDirectoryFor(org.terasology.launcher.util.LauncherManagedDirectory.TEMP, launcherDirectory);
final Path cacheDirectory = getDirectoryFor(org.terasology.launcher.util.LauncherManagedDirectory.CACHE, launcherDirectory);
final Path downloadDirectory = getDirectoryFor(LauncherManagedDirectory.DOWNLOAD, userDataDirectory);
final Path tempDirectory = getDirectoryFor(LauncherManagedDirectory.TEMP, userDataDirectory);
final Path cacheDirectory = getDirectoryFor(LauncherManagedDirectory.CACHE, userDataDirectory);

// launcher settings
final BaseLauncherSettings launcherSettings = getLauncherSettings(launcherDirectory);
final BaseLauncherSettings launcherSettings = getLauncherSettings(userDataDirectory);

// validate the settings
LauncherSettingsValidator.validate(launcherSettings);
Expand All @@ -92,14 +93,14 @@ protected LauncherConfiguration call() {

// game directories
updateMessage(BundleUtils.getLabel("splash_initGameDirs"));
final Path gameDirectory = getGameDirectory(os, launcherSettings.getGameDirectory());
final Path gameDirectory = getDirectoryFor(LauncherManagedDirectory.GAMES, installationDirectory);
final Path gameDataDirectory = getGameDataDirectory(os, launcherSettings.getGameDataDirectory());

// TODO: Does this interact with any remote server for fetching/initializing the database?
logger.trace("Setting up Package Manager");
final PackageManager packageManager = new PackageManager();
packageManager.initLocalStorage(gameDirectory, cacheDirectory);
packageManager.initDatabase(launcherDirectory, gameDirectory);
packageManager.initDatabase(userDataDirectory, gameDirectory);
packageManager.syncDatabase();

logger.trace("Change LauncherSettings...");
Expand All @@ -111,7 +112,7 @@ protected LauncherConfiguration call() {

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

return new LauncherConfiguration(launcherDirectory, downloadDirectory, tempDirectory, cacheDirectory, launcherSettings, packageManager);
return new LauncherConfiguration(userDataDirectory, downloadDirectory, tempDirectory, cacheDirectory, launcherSettings, packageManager);
} catch (LauncherStartFailedException e) {
logger.warn("Could not configure launcher.");
}
Expand Down Expand Up @@ -189,9 +190,11 @@ private boolean checkForLauncherUpdates(Path downloadDirectory, Path tempDirecto
updateMessage(BundleUtils.getLabel("splash_launcherUpdateAvailable"));
boolean foundLauncherInstallationDirectory = false;
try {
updater.detectAndCheckLauncherInstallationDirectory();
final Path installationDir = LauncherDirectoryUtils.getInstallationDirectory();
FileUtils.ensureWritableDir(installationDir);
logger.trace("Launcher installation directory: {}", installationDir);
foundLauncherInstallationDirectory = true;
} catch (URISyntaxException | IOException e) {
} catch (IOException e) {
logger.error("The launcher installation directory can not be detected or used!", e);
GuiUtils.showErrorMessageDialog(owner, BundleUtils.getLabel("message_error_launcherInstallationDirectory"));
// Run launcher without an update. Don't throw a LauncherStartFailedException.
Expand Down Expand Up @@ -220,41 +223,6 @@ private void showDownloadPage() {
}
}

private Path getGameDirectory(OperatingSystem os, Path settingsGameDirectory) throws LauncherStartFailedException {
logger.trace("Init GameDirectory...");
Path gameDirectory = settingsGameDirectory;
if (gameDirectory != null) {
try {
FileUtils.ensureWritableDir(gameDirectory);
} catch (IOException e) {
logger.warn("The game directory can not be created or used! '{}'", gameDirectory, e);
GuiUtils.showWarningMessageDialog(owner, BundleUtils.getLabel("message_error_gameDirectory") + "\n" + gameDirectory);

// Set gameDirectory to 'null' -> user has to choose new game directory
gameDirectory = null;
}
}
if (gameDirectory == null) {
logger.trace("Choose installation directory for the game...");
updateMessage(BundleUtils.getLabel("splash_chooseGameDirectory"));
gameDirectory = GuiUtils.chooseDirectoryDialog(owner, LauncherDirectoryUtils.getApplicationDirectory(os, LauncherDirectoryUtils.GAME_APPLICATION_DIR_NAME),
BundleUtils.getLabel("message_dialog_title_chooseGameDirectory"));
if (gameDirectory == null || Files.notExists(gameDirectory)) {
logger.info("The new game directory is not approved. The TerasologyLauncher is terminated.");
Platform.exit();
}
}
try {
FileUtils.ensureWritableDir(gameDirectory);
} catch (IOException e) {
logger.error("The game directory can not be created or used! '{}'", gameDirectory, e);
GuiUtils.showErrorMessageDialog(owner, BundleUtils.getLabel("message_error_gameDirectory") + "\n" + gameDirectory);
throw new LauncherStartFailedException();
}
logger.debug("Game directory: {}", gameDirectory);
return gameDirectory;
}

private Path getGameDataDirectory(OperatingSystem os, Path settingsGameDataDirectory) throws LauncherStartFailedException {
logger.trace("Init GameDataDirectory...");
Path gameDataDirectory = settingsGameDataDirectory;
Expand Down
29 changes: 12 additions & 17 deletions src/main/java/org/terasology/launcher/TerasologyLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
Expand Down Expand Up @@ -101,22 +99,19 @@ public void start(final Stage initialStage) {
showSplashStage(initialStage, launcherInitTask);
Thread initThread = new Thread(launcherInitTask);

launcherInitTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(final WorkerStateEvent workerStateEvent) {
try {
LauncherConfiguration config = launcherInitTask.getValue();
if (config == null) {
throw new LauncherStartFailedException("Launcher configuration was `null`.");
} else if (config instanceof NullLauncherConfiguration) {
logger.info("Closing the launcher ... (empty configuration, probably due to update)");
Platform.exit();
} else {
showMainStage(config);
}
} catch (IOException | LauncherStartFailedException e) {
openCrashReporterAndExit(e);
launcherInitTask.setOnSucceeded(workerStateEvent -> {
try {
LauncherConfiguration config = launcherInitTask.getValue();
if (config == null) {
throw new LauncherStartFailedException("Launcher configuration was `null`.");
} else if (config instanceof NullLauncherConfiguration) {
logger.info("Closing the launcher ... (empty configuration, probably due to update)");
Platform.exit();
} else {
showMainStage(config);
}
} catch (IOException | LauncherStartFailedException e) {
openCrashReporterAndExit(e);
Comment on lines -104 to +114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this does the same?
Previous implementation of EventHandler seems to me as if the workerStateEvent may not yet be there when the launcherInitTask succeeds? But maybe I'm reading this wrong...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just uses a different syntax (lambda expression) for creating the handler for the onSucceeded event. As handle(WorkerStateEvent event) is the only method that needs to be implemented we can use the simplified syntax here.

new EventHandler<WorkerStateEvent>() {
	@Override
    public void handle(final WorkerStateEvent workerStateEvent) {
        // do something
    }
}

Is replaced by

workerStateEvent -> {
    // do something
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, so the new one also kind of "waits" for a workerStateEvent to come in?

}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
public class PackageManager {

private static final Logger logger = LoggerFactory.getLogger(PackageManager.class);
private static final String INSTALL_DIRECTORY = "games";
private static final String SOURCES_FILENAME = "sources.json";
private static final String DATABASE_FILENAME = "packages.db";
private static final String CACHE_DIRECTORY = "cache";
Expand All @@ -57,7 +56,7 @@ public PackageManager() {
/**
* Sets up local storage for working with game packages and cache files.
*
* @param gameDirectory directory path for storing game packages
* @param gameDirectory directory path for storing game packages
jdrueckert marked this conversation as resolved.
Show resolved Hide resolved
* @param cacheDirectory directory path for storing cache files
*/
public void initLocalStorage(Path gameDirectory, Path cacheDirectory) {
Expand Down Expand Up @@ -87,10 +86,10 @@ public void sync() {
}

// TODO: Move to constructor
public void initDatabase(Path launcherDir, Path gameDir) {
cacheDir = launcherDir.resolve(CACHE_DIRECTORY);
installDir = gameDir.resolve(INSTALL_DIRECTORY);
final Path sourcesFile = launcherDir.resolve(SOURCES_FILENAME);
public void initDatabase(final Path userDataDirectory, final Path gameInstallationDir) {
cacheDir = userDataDirectory.resolve(CACHE_DIRECTORY);
installDir = gameInstallationDir;
final Path sourcesFile = userDataDirectory.resolve(SOURCES_FILENAME);

// Copy default sources file if necessary
if (Files.notExists(sourcesFile)) {
Expand All @@ -104,7 +103,7 @@ public void initDatabase(Path launcherDir, Path gameDir) {

database = new PackageDatabase(
sourcesFile,
launcherDir.resolve(DATABASE_FILENAME),
userDataDirectory.resolve(DATABASE_FILENAME),
installDir
);
}
Expand All @@ -118,7 +117,7 @@ public void syncDatabase() {
/**
* Installs the mentioned package into the local repository.
*
* @param target the package to be installed
* @param target the package to be installed
jdrueckert marked this conversation as resolved.
Show resolved Hide resolved
* @param listener the object which is to be informed about task progress
*/
public void install(Package target, ProgressListener listener) throws IOException, DownloadException {
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/org/terasology/launcher/updater/LauncherUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@
import org.terasology.launcher.github.GitHubClient;
import org.terasology.launcher.github.GitHubRelease;
import org.terasology.launcher.util.BundleUtils;
import org.terasology.launcher.util.FileUtils;
import org.terasology.launcher.util.GuiUtils;
import org.terasology.launcher.version.TerasologyLauncherVersionInfo;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

Expand Down Expand Up @@ -83,14 +80,6 @@ public GitHubRelease updateAvailable() {
return null;
}

public void detectAndCheckLauncherInstallationDirectory() throws URISyntaxException, IOException {
final Path launcherLocation = Paths.get(LauncherUpdater.class.getProtectionDomain().getCodeSource().getLocation().toURI());
logger.trace("Launcher location: {}", launcherLocation);
launcherInstallationDirectory = launcherLocation.getParent().getParent();
FileUtils.ensureWritableDir(launcherInstallationDirectory);
logger.trace("Launcher installation directory: {}", launcherInstallationDirectory);
}

public boolean showUpdateDialog(Stage parentStage, final GitHubRelease release) {
FutureTask<Boolean> dialog = getUpdateDialog(parentStage, release);

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/terasology/launcher/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public static void ensureEmptyDir(final Path directory) throws IOException {
}
}


/**
* Checks whether the given path exists and is a readable directory.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.terasology.launcher.util.windows.SavedGamesPathFinder;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -164,4 +166,18 @@ public static Path getGameDataDirectory(OperatingSystem os) {

return gameDataDirectory;
}

public static Path getInstallationDirectory() {
final URL location = LauncherDirectoryUtils.class.getProtectionDomain().getCodeSource().getLocation();
Path installationDirectory = null;
try {
final Path launcherLocation = Paths.get(location.toURI());
logger.trace("Launcher location: {}", launcherLocation);
installationDirectory = launcherLocation.getParent().getParent();
logger.trace("Launcher installation directory: {}", installationDirectory);
} catch (URISyntaxException e) {
logger.error("Could not determine launcher installation directory.", e);
}
return installationDirectory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public enum LauncherManagedDirectory {
//TODO add documentation what these folders are exactly used for
TEMP(FileUtils::ensureEmptyDir, FileUtils::ensureWritableDir),
CACHE(FileUtils::ensureWritableDir),
DOWNLOAD(FileUtils::ensureWritableDir);
DOWNLOAD(FileUtils::ensureWritableDir),
GAMES(FileUtils::ensureWritableDir);

private final DirectoryCreator[] creators;
private final String errorLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ main_no=
main_yes=
message_deleteGame_title=
message_dialog_title_chooseGameDataDirectory=
message_dialog_title_chooseGameDirectory=
message_error_gameDataDirectory=
message_error_gameDirectory=
message_error_gameDownload_downloadExtract=
Expand All @@ -74,7 +73,6 @@ message_error_loadSettings=
message_error_operatingSystem=
message_error_storeSettings=
message_error_tempDirectory=
message_error_cacheDirectory=
message_error_title=
message_information_gameRunning=
message_information_title=
Expand Down Expand Up @@ -130,7 +128,6 @@ settings_launcher_title=
settings_save=
settings_title=
splash_chooseGameDataDirectory=
splash_chooseGameDirectory=
splash_createFrame=
splash_launcherUpdateAvailable=
splash_launcherUpdateCheck=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ main_no=Geen
main_yes=Ja
message_deleteGame_title=Verwyder ge\u00EFnstalleerde spel
message_dialog_title_chooseGameDataDirectory=Kies data directory vir die spel
message_dialog_title_chooseGameDirectory=Kies installasie directory for the game
message_error_gameDataDirectory=
message_error_gameDirectory=
message_error_gameDownload_downloadExtract=
Expand Down Expand Up @@ -127,7 +126,6 @@ settings_launcher_title=
settings_save=
settings_title=
splash_chooseGameDataDirectory=
splash_chooseGameDirectory=
splash_createFrame=
splash_launcherUpdateAvailable=
splash_launcherUpdateCheck=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ main_no=Ne
main_yes=Ano
message_deleteGame_title=Smazat nainstalovanou hru
message_dialog_title_chooseGameDataDirectory=Vyberte adres\u00E1\u0159 um\u00EDst\u011Bn\u00ED hern\u00EDch dat
message_dialog_title_chooseGameDirectory=Vyberte adres\u00E1\u0159 instalace hry
message_error_gameDataDirectory=Nelze vytvo\u0159it nebo pou\u017E\u00EDt adres\u00E1\u0159 hern\u00EDch dat!
message_error_gameDirectory=Nelze vytvo\u0159it nebo pou\u017E\u00EDt adres\u00E1\u0159 instalace hry!
message_error_gameDownload_downloadExtract=Proces sta\u017Een\u00ED a rozbalen\u00ED hry selhal.
Expand Down Expand Up @@ -127,7 +126,6 @@ settings_launcher_title=Spou\u0161t\u011B\u010D
settings_save=Ulo\u017Eit
settings_title=Nastaven\u00ED
splash_chooseGameDataDirectory=Vyberte adres\u00E1\u0159 hern\u00EDch dat
splash_chooseGameDirectory=Vyberte adres\u00E1\u0159 instalace hry
splash_createFrame=Vytv\u00E1\u0159\u00EDm r\u00E1m spou\u0161t\u011B\u010De...
splash_launcherUpdateAvailable=Je dostupn\u00E1 aktualizace spou\u0161t\u011B\u010De!
splash_launcherUpdateCheck=Vyhledat aktualizace spou\u0161t\u011B\u010De...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ main_no=Nein
main_yes=Ja
message_deleteGame_title=Installierte Version deinstallieren und l\u00F6schen
message_dialog_title_chooseGameDataDirectory=Bitte Verzeichnis f\u00FCr die Spielst\u00E4nde ausw\u00E4hlen
message_dialog_title_chooseGameDirectory=Bitte ein Installationsverzeichnis ausw\u00E4hlen
message_error_gameDataDirectory=Das Spielstandverzeichnis konnte leider nicht erzeugt oder genutzt werden!
message_error_gameDirectory=Das Installationsverzeichnis konnte leider nicht erzeugt oder genutzt werden!
message_error_gameDownload_downloadExtract=Das Spiel konnte leider nicht heruntergeladen und entpackt werden.
Expand Down Expand Up @@ -132,7 +131,6 @@ splash_initGameDirs=Spiel-Ordner werden initialisiert ...
splash_retrieveLauncherSettings=Rufe Launchereinstellungen ab ...
splash_storeLauncherSettings=Speichere Launchereinstellungen ...
splash_chooseGameDataDirectory=Datenverzeichnis f\u00FCr das Spiel ausw\u00E4hlen
splash_chooseGameDirectory=Installationsverzeichnis f\u00FCr das Spiel ausw\u00E4hlen
splash_createFrame=Erstelle Launcher-Rahmen ...
splash_launcherUpdateAvailable=Launcher-Aktualisierung verf\u00FCgbar!
splash_launcherUpdateCheck=Nach Launcher-Aktualisierungen suchen ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ main_no=No
main_yes=Yes
message_deleteGame_title=Delete installed game
message_dialog_title_chooseGameDataDirectory=Choose data directory for the game
message_dialog_title_chooseGameDirectory=Choose installation directory for the game
message_error_gameDataDirectory=Cannot create or use game data directory\!
message_error_gameDirectory=Cannot create or use game installation directory\!
message_error_gameDownload_downloadExtract=The game download and extraction process failed.
Expand Down Expand Up @@ -131,7 +130,6 @@ settings_launcher_title=Launcher
settings_save=Save
settings_title=Settings
splash_chooseGameDataDirectory=Choose data directory for the game
splash_chooseGameDirectory=Choose installation directory for the game
splash_createFrame=Creating launcher frame...
splash_launcherUpdateAvailable=Launcher update available\!
splash_launcherUpdateCheck=Check for launcher updates...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ main_no=No
main_yes=Si
message_deleteGame_title=Borrar juego instalado
message_dialog_title_chooseGameDataDirectory=Escoge un directorio de datos para el juego
message_dialog_title_chooseGameDirectory=Escoge un directorio de instalaci\u00F3n para el juego
message_error_gameDataDirectory=\u00A1No se ha podido crear o utilizar el directorio donde se almacena los datos del juego!
message_error_gameDirectory=\u00A1No se ha podido crear o usar el directorio donde se encuentra instalado el juego!
message_error_gameDownload_downloadExtract=La descarga del juego y su proceso de extracci\u00F3n ha fallado.
Expand Down Expand Up @@ -127,7 +126,6 @@ settings_launcher_title=Launcher
settings_save=Guardar
settings_title=Ajustes
splash_chooseGameDataDirectory=Escoge un directorio de datos para el juego
splash_chooseGameDirectory=Escoge un directorio de instalaci\u00F3n para el juego
splash_createFrame=Creando la ventana del launcher...
splash_launcherUpdateAvailable=\u00A1Actualizaci\u00F3n del Launcher disponible!
splash_launcherUpdateCheck=Comprobando actualizaciones del launcher...
Expand Down
Loading