Skip to content

Commit

Permalink
chore: restructure Platform (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
skaldarnar authored Jun 29, 2024
1 parent 0b12f8a commit e334afb
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 107 deletions.
10 changes: 5 additions & 5 deletions src/main/java/org/terasology/launcher/LauncherInitTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.slf4j.LoggerFactory;
import org.terasology.launcher.game.GameManager;
import org.terasology.launcher.model.LauncherVersion;
import org.terasology.launcher.platform.UnsupportedPlatformException;
import org.terasology.launcher.repositories.CombinedRepository;
import org.terasology.launcher.settings.LauncherSettingsValidator;
import org.terasology.launcher.settings.Settings;
Expand All @@ -24,7 +25,7 @@
import org.terasology.launcher.util.LauncherDirectoryUtils;
import org.terasology.launcher.util.LauncherManagedDirectory;
import org.terasology.launcher.util.LauncherStartFailedException;
import org.terasology.launcher.util.Platform;
import org.terasology.launcher.platform.Platform;

import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -114,18 +115,17 @@ protected LauncherConfiguration call() {
releaseRepository);
} catch (LauncherStartFailedException e) {
logger.warn("Could not configure launcher.");
} catch (UnsupportedPlatformException e) {
logger.error("Unsupported OS or architecture: {}", e.getMessage());
}

return null;
}

private Platform getPlatform() {
private Platform getPlatform() throws UnsupportedPlatformException {
logger.trace("Init Platform...");
updateMessage(I18N.getLabel("splash_checkOS"));
final Platform platform = Platform.getPlatform();
if (!platform.isLinux() && !platform.isMac() && !platform.isWindows()) {
logger.warn("Detected unexpected platform: {}", platform);
}
logger.debug("Platform: {}", platform);
return platform;
}
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/org/terasology/launcher/game/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javafx.concurrent.Worker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.launcher.platform.UnsupportedPlatformException;
import org.terasology.launcher.settings.Settings;

import java.io.IOException;
Expand Down Expand Up @@ -49,19 +50,20 @@ public class GameService extends Service<Boolean> {

public GameService() {
setExecutor(Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder()
.setNameFormat("GameService-%d")
.setDaemon(true)
.setUncaughtExceptionHandler(this::exceptionHandler)
.build()
new ThreadFactoryBuilder()
.setNameFormat("GameService-%d")
.setDaemon(true)
.setUncaughtExceptionHandler(this::exceptionHandler)
.build()
));
}

/**
* Start a new game process with these settings.
*
* @param gameInstallation the directory under which we will find libs/Terasology.jar, also used as the process's
* working directory
* @param settings supplies other settings relevant to configuring a process
* working directory
* @param settings supplies other settings relevant to configuring a process
*/
@SuppressWarnings("checkstyle:HiddenField")
public void start(GameInstallation gameInstallation, Settings settings) {
Expand Down Expand Up @@ -115,7 +117,7 @@ public void restart() {
* This class's configuration fields <em>must</em> be set before this is called.
*
* @throws com.google.common.base.VerifyException when fields are unset
* @throws RuntimeException when required files in the game directory are missing or inaccessible
* @throws RuntimeException when required files in the game directory are missing or inaccessible
*/
@Override
protected RunGameTask createTask() throws GameVersionNotSupportedException {
Expand All @@ -128,19 +130,23 @@ protected RunGameTask createTask() throws GameVersionNotSupportedException {
settings.userJavaParameters.get(),
settings.userGameParameters.get(),
settings.logLevel.get());
} catch (IOException e) {
} catch (IOException | UnsupportedPlatformException e) {
throw new RuntimeException("Error using this as a game directory: " + gamePath, e);
}
return new RunGameTask(starter);
}

/** After a task completes, reset to ready for the next. */
/**
* After a task completes, reset to ready for the next.
*/
@Override
protected void succeeded() {
reset(); // Ready to go again!
}

/** Checks to see if the failure left any exceptions behind, then resets to ready. */
/**
* Checks to see if the failure left any exceptions behind, then resets to ready.
*/
@Override
protected void failed() {
// "Uncaught" exceptions from javafx's Task are actually caught and kept in a property,
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/terasology/launcher/game/GameStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import org.terasology.launcher.platform.UnsupportedPlatformException;
import org.terasology.launcher.util.JavaHeapSize;
import org.terasology.launcher.util.Platform;
import org.terasology.launcher.platform.Platform;

import java.io.IOException;
import java.nio.file.Path;
Expand Down Expand Up @@ -38,7 +39,8 @@ final class GameStarter implements Callable<Process> {
* @param logLevel the minimum level of log events Terasology will include on its output stream to us
*/
GameStarter(GameInstallation gameInstallation, Path gameDataDirectory, JavaHeapSize heapMin, JavaHeapSize heapMax,
List<String> javaParams, List<String> gameParams, Level logLevel) throws IOException, GameVersionNotSupportedException {
List<String> javaParams, List<String> gameParams, Level logLevel)
throws IOException, GameVersionNotSupportedException, UnsupportedPlatformException {
Semver engineVersion = gameInstallation.getEngineVersion();
var gamePath = gameInstallation.getPath();

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/terasology/launcher/platform/Arch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2023 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.launcher.platform;

public enum Arch {
X64,
X86,
ARM64
}
10 changes: 10 additions & 0 deletions src/main/java/org/terasology/launcher/platform/OS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2023 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.launcher.platform;

public enum OS {
WINDOWS,
MAC,
LINUX
}
103 changes: 103 additions & 0 deletions src/main/java/org/terasology/launcher/platform/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2023 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.launcher.platform;

/**
* A simplified representation of a computer platform as `os` and `arch`
*/
public enum Platform {

// unsupported platforms commented out, but might be useful for local development
// MACOS_X64(OS.MAC, Arch.X64),
// supported platforms by both the game and the launcher
WINDOWS_X64(OS.WINDOWS, Arch.X64),
LINUX_X64(OS.LINUX, Arch.X64);

/**
* The simplified operating system identifier.
*/
public final OS os;
/**
* The simplified architecture identifier.
*/
public final Arch arch;

Platform(OS os, Arch arch) {
this.os = os;
this.arch = arch;
}

public boolean isLinux() {
return os == OS.LINUX;
}

public boolean isMac() {
return os == OS.MAC;
}

public boolean isWindows() {
return os == OS.WINDOWS;
}

public String toString() {
return "OS '" + os + "', arch '" + arch + "'";
}

/**
* Get information on the host platform the launcher is currently running on.
*
* @return the platform
*/
public static Platform getPlatform() throws UnsupportedPlatformException {
final String platformOs = System.getProperty("os.name").toLowerCase();
final OS os;
if (platformOs.startsWith("linux")) {
os = OS.LINUX;
} else if (platformOs.startsWith("mac os")) {
os = OS.MAC;
} else if (platformOs.startsWith("windows")) {
os = OS.WINDOWS;
} else {
throw new UnsupportedPlatformException("Unsupported OS: " + platformOs);
}

final String platformArch = System.getProperty("os.arch");
final Arch arch;
switch (platformArch) {
case "x86_64":
case "amd64":
arch = Arch.X64;
break;
case "x86":
case "i386":
arch = Arch.X86;
break;
case "aarch64":
case "arm64":
arch = Arch.ARM64;
break;
default:
throw new UnsupportedPlatformException("Architecture not supported: " + platformArch);
}

return fromOsAndArch(os, arch);
}

/**
* Derive the {@link Platform} from the given {@link OS} and {@link Arch}
*
* @throws UnsupportedPlatformException if the given OS and Arch combination is not supported
*/
public static Platform fromOsAndArch(OS os, Arch arch) throws UnsupportedPlatformException {
if (os.equals(OS.WINDOWS) && arch.equals(Arch.X64)) {
return WINDOWS_X64;
} else if (os.equals(OS.LINUX) && arch.equals(Arch.X64)) {
return LINUX_X64;
// } else if (os.equals(OS.MAC) && arch.equals(Arch.X64)) {
// return MACOS_X64;
} else {
throw new UnsupportedPlatformException("Unsupported platform: " + os + " " + arch);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.launcher.platform;

public class UnsupportedPlatformException extends Exception {

public UnsupportedPlatformException() {
super();
}

public UnsupportedPlatformException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.launcher.platform.Platform;

import java.io.IOException;
import java.net.URISyntaxException;
Expand Down
82 changes: 0 additions & 82 deletions src/main/java/org/terasology/launcher/util/Platform.java

This file was deleted.

Loading

0 comments on commit e334afb

Please sign in to comment.