Skip to content

Commit e334afb

Browse files
authored
chore: restructure Platform (#714)
1 parent 0b12f8a commit e334afb

File tree

10 files changed

+173
-107
lines changed

10 files changed

+173
-107
lines changed

src/main/java/org/terasology/launcher/LauncherInitTask.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.slf4j.LoggerFactory;
1313
import org.terasology.launcher.game.GameManager;
1414
import org.terasology.launcher.model.LauncherVersion;
15+
import org.terasology.launcher.platform.UnsupportedPlatformException;
1516
import org.terasology.launcher.repositories.CombinedRepository;
1617
import org.terasology.launcher.settings.LauncherSettingsValidator;
1718
import org.terasology.launcher.settings.Settings;
@@ -24,7 +25,7 @@
2425
import org.terasology.launcher.util.LauncherDirectoryUtils;
2526
import org.terasology.launcher.util.LauncherManagedDirectory;
2627
import org.terasology.launcher.util.LauncherStartFailedException;
27-
import org.terasology.launcher.util.Platform;
28+
import org.terasology.launcher.platform.Platform;
2829

2930
import java.io.IOException;
3031
import java.net.URI;
@@ -114,18 +115,17 @@ protected LauncherConfiguration call() {
114115
releaseRepository);
115116
} catch (LauncherStartFailedException e) {
116117
logger.warn("Could not configure launcher.");
118+
} catch (UnsupportedPlatformException e) {
119+
logger.error("Unsupported OS or architecture: {}", e.getMessage());
117120
}
118121

119122
return null;
120123
}
121124

122-
private Platform getPlatform() {
125+
private Platform getPlatform() throws UnsupportedPlatformException {
123126
logger.trace("Init Platform...");
124127
updateMessage(I18N.getLabel("splash_checkOS"));
125128
final Platform platform = Platform.getPlatform();
126-
if (!platform.isLinux() && !platform.isMac() && !platform.isWindows()) {
127-
logger.warn("Detected unexpected platform: {}", platform);
128-
}
129129
logger.debug("Platform: {}", platform);
130130
return platform;
131131
}

src/main/java/org/terasology/launcher/game/GameService.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javafx.concurrent.Worker;
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
11+
import org.terasology.launcher.platform.UnsupportedPlatformException;
1112
import org.terasology.launcher.settings.Settings;
1213

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

5051
public GameService() {
5152
setExecutor(Executors.newSingleThreadExecutor(
52-
new ThreadFactoryBuilder()
53-
.setNameFormat("GameService-%d")
54-
.setDaemon(true)
55-
.setUncaughtExceptionHandler(this::exceptionHandler)
56-
.build()
53+
new ThreadFactoryBuilder()
54+
.setNameFormat("GameService-%d")
55+
.setDaemon(true)
56+
.setUncaughtExceptionHandler(this::exceptionHandler)
57+
.build()
5758
));
5859
}
5960

6061
/**
6162
* Start a new game process with these settings.
63+
*
6264
* @param gameInstallation the directory under which we will find libs/Terasology.jar, also used as the process's
63-
* working directory
64-
* @param settings supplies other settings relevant to configuring a process
65+
* working directory
66+
* @param settings supplies other settings relevant to configuring a process
6567
*/
6668
@SuppressWarnings("checkstyle:HiddenField")
6769
public void start(GameInstallation gameInstallation, Settings settings) {
@@ -115,7 +117,7 @@ public void restart() {
115117
* This class's configuration fields <em>must</em> be set before this is called.
116118
*
117119
* @throws com.google.common.base.VerifyException when fields are unset
118-
* @throws RuntimeException when required files in the game directory are missing or inaccessible
120+
* @throws RuntimeException when required files in the game directory are missing or inaccessible
119121
*/
120122
@Override
121123
protected RunGameTask createTask() throws GameVersionNotSupportedException {
@@ -128,19 +130,23 @@ protected RunGameTask createTask() throws GameVersionNotSupportedException {
128130
settings.userJavaParameters.get(),
129131
settings.userGameParameters.get(),
130132
settings.logLevel.get());
131-
} catch (IOException e) {
133+
} catch (IOException | UnsupportedPlatformException e) {
132134
throw new RuntimeException("Error using this as a game directory: " + gamePath, e);
133135
}
134136
return new RunGameTask(starter);
135137
}
136138

137-
/** After a task completes, reset to ready for the next. */
139+
/**
140+
* After a task completes, reset to ready for the next.
141+
*/
138142
@Override
139143
protected void succeeded() {
140144
reset(); // Ready to go again!
141145
}
142146

143-
/** Checks to see if the failure left any exceptions behind, then resets to ready. */
147+
/**
148+
* Checks to see if the failure left any exceptions behind, then resets to ready.
149+
*/
144150
@Override
145151
protected void failed() {
146152
// "Uncaught" exceptions from javafx's Task are actually caught and kept in a property,

src/main/java/org/terasology/launcher/game/GameStarter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99
import org.slf4j.event.Level;
10+
import org.terasology.launcher.platform.UnsupportedPlatformException;
1011
import org.terasology.launcher.util.JavaHeapSize;
11-
import org.terasology.launcher.util.Platform;
12+
import org.terasology.launcher.platform.Platform;
1213

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2023 The Terasology Foundation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package org.terasology.launcher.platform;
5+
6+
public enum Arch {
7+
X64,
8+
X86,
9+
ARM64
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2023 The Terasology Foundation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package org.terasology.launcher.platform;
5+
6+
public enum OS {
7+
WINDOWS,
8+
MAC,
9+
LINUX
10+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2023 The Terasology Foundation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package org.terasology.launcher.platform;
5+
6+
/**
7+
* A simplified representation of a computer platform as `os` and `arch`
8+
*/
9+
public enum Platform {
10+
11+
// unsupported platforms commented out, but might be useful for local development
12+
// MACOS_X64(OS.MAC, Arch.X64),
13+
// supported platforms by both the game and the launcher
14+
WINDOWS_X64(OS.WINDOWS, Arch.X64),
15+
LINUX_X64(OS.LINUX, Arch.X64);
16+
17+
/**
18+
* The simplified operating system identifier.
19+
*/
20+
public final OS os;
21+
/**
22+
* The simplified architecture identifier.
23+
*/
24+
public final Arch arch;
25+
26+
Platform(OS os, Arch arch) {
27+
this.os = os;
28+
this.arch = arch;
29+
}
30+
31+
public boolean isLinux() {
32+
return os == OS.LINUX;
33+
}
34+
35+
public boolean isMac() {
36+
return os == OS.MAC;
37+
}
38+
39+
public boolean isWindows() {
40+
return os == OS.WINDOWS;
41+
}
42+
43+
public String toString() {
44+
return "OS '" + os + "', arch '" + arch + "'";
45+
}
46+
47+
/**
48+
* Get information on the host platform the launcher is currently running on.
49+
*
50+
* @return the platform
51+
*/
52+
public static Platform getPlatform() throws UnsupportedPlatformException {
53+
final String platformOs = System.getProperty("os.name").toLowerCase();
54+
final OS os;
55+
if (platformOs.startsWith("linux")) {
56+
os = OS.LINUX;
57+
} else if (platformOs.startsWith("mac os")) {
58+
os = OS.MAC;
59+
} else if (platformOs.startsWith("windows")) {
60+
os = OS.WINDOWS;
61+
} else {
62+
throw new UnsupportedPlatformException("Unsupported OS: " + platformOs);
63+
}
64+
65+
final String platformArch = System.getProperty("os.arch");
66+
final Arch arch;
67+
switch (platformArch) {
68+
case "x86_64":
69+
case "amd64":
70+
arch = Arch.X64;
71+
break;
72+
case "x86":
73+
case "i386":
74+
arch = Arch.X86;
75+
break;
76+
case "aarch64":
77+
case "arm64":
78+
arch = Arch.ARM64;
79+
break;
80+
default:
81+
throw new UnsupportedPlatformException("Architecture not supported: " + platformArch);
82+
}
83+
84+
return fromOsAndArch(os, arch);
85+
}
86+
87+
/**
88+
* Derive the {@link Platform} from the given {@link OS} and {@link Arch}
89+
*
90+
* @throws UnsupportedPlatformException if the given OS and Arch combination is not supported
91+
*/
92+
public static Platform fromOsAndArch(OS os, Arch arch) throws UnsupportedPlatformException {
93+
if (os.equals(OS.WINDOWS) && arch.equals(Arch.X64)) {
94+
return WINDOWS_X64;
95+
} else if (os.equals(OS.LINUX) && arch.equals(Arch.X64)) {
96+
return LINUX_X64;
97+
// } else if (os.equals(OS.MAC) && arch.equals(Arch.X64)) {
98+
// return MACOS_X64;
99+
} else {
100+
throw new UnsupportedPlatformException("Unsupported platform: " + os + " " + arch);
101+
}
102+
}
103+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2023 The Terasology Foundation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package org.terasology.launcher.platform;
5+
6+
public class UnsupportedPlatformException extends Exception {
7+
8+
public UnsupportedPlatformException() {
9+
super();
10+
}
11+
12+
public UnsupportedPlatformException(String message) {
13+
super(message);
14+
}
15+
}

src/main/java/org/terasology/launcher/util/LauncherDirectoryUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8+
import org.terasology.launcher.platform.Platform;
89

910
import java.io.IOException;
1011
import java.net.URISyntaxException;

src/main/java/org/terasology/launcher/util/Platform.java

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)