Skip to content

Commit 9621e21

Browse files
authored
Merge pull request #5322 from SiliconSaga/review/post-di-phase5
review: safe JoinServer handoff, ScreenGrabber visibility, deprecate CoreRegistry constructor
2 parents 68b01f5 + 5bb95e6 commit 9621e21

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

engine/src/main/java/org/terasology/engine/core/modes/loadProcesses/InitialiseWorld.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public boolean step() {
8383
WorldInfo worldInfo = verifyNotNull(gameManifest.getWorldInfo(TerasologyConstants.MAIN_WORLD),
8484
"Game manifest does not contain a MAIN_WORLD");
8585
verify(worldInfo.getWorldGenerator().isValid(), "Game manifest did not specify world type.");
86+
// Seed is carried by the GameManifest from the UI (GameManifestProvider).
87+
// Generate a random fallback if the manifest doesn't specify one.
8688
if (worldInfo.getSeed() == null || worldInfo.getSeed().isEmpty()) {
8789
FastRandom random = new FastRandom();
8890
worldInfo.setSeed(random.nextString(16));

engine/src/main/java/org/terasology/engine/core/modes/loadProcesses/JoinServer.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.Map;
3232
import java.util.Map.Entry;
3333
import java.util.Set;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.FutureTask;
3436

3537
//TODO document this!
3638
public class JoinServer extends VariableStepLoadProcess {
@@ -41,7 +43,7 @@ public class JoinServer extends VariableStepLoadProcess {
4143
private GameManifest gameManifest;
4244
private JoinStatus joinStatus;
4345

44-
private Thread applyModuleThread;
46+
private FutureTask<Context> applyModuleTask;
4547
private ModuleEnvironment oldEnvironment;
4648

4749
public JoinServer(Context context, GameManifest gameManifest, JoinStatus joinStatus) {
@@ -53,7 +55,7 @@ public JoinServer(Context context, GameManifest gameManifest, JoinStatus joinSta
5355

5456
@Override
5557
public String getMessage() {
56-
if (applyModuleThread != null) {
58+
if (applyModuleTask != null) {
5759
return "${engine:menu#scanning-for-assets}";
5860
} else {
5961
return joinStatus.getCurrentActivity();
@@ -62,8 +64,21 @@ public String getMessage() {
6264

6365
@Override
6466
public boolean step() {
65-
if (applyModuleThread != null) {
66-
if (!applyModuleThread.isAlive()) {
67+
if (applyModuleTask != null) {
68+
if (applyModuleTask.isDone()) {
69+
try {
70+
Context gameContext = applyModuleTask.get();
71+
CoreRegistry.setContext(gameContext);
72+
} catch (ExecutionException e) {
73+
logger.error("Failed to apply game environment", e.getCause());
74+
StateMainMenu mainMenu = new StateMainMenu("Failed to apply game environment: " + e.getCause().getMessage());
75+
context.get(GameEngine.class).changeState(mainMenu);
76+
networkSystem.shutdown();
77+
return true;
78+
} catch (InterruptedException e) {
79+
Thread.currentThread().interrupt();
80+
return false;
81+
}
6782
if (oldEnvironment != null) {
6883
oldEnvironment.close();
6984
}
@@ -120,15 +135,17 @@ public boolean step() {
120135
context.get(Game.class).load(gameManifest);
121136

122137
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
123-
ContextImpl modulesContext = new ContextImpl(context,
124-
moduleManager.getEnvironment().getBeans(BeanContext.class).stream().findFirst().get());
138+
BeanContext moduleBeanContext = moduleManager.getEnvironment().getBeans(BeanContext.class).stream()
139+
.findFirst()
140+
.orElseThrow(() -> new IllegalStateException(
141+
"Loaded module environment did not expose a BeanContext"));
142+
ContextImpl modulesContext = new ContextImpl(context, moduleBeanContext);
125143
ServiceRegistry gameContextRegistry = new ServiceRegistry();
126-
applyModuleThread = new Thread(() -> {
144+
applyModuleTask = new FutureTask<>(() -> {
127145
environmentSwitchHandler.handleSwitchToGameEnvironment(modulesContext, gameContextRegistry);
128-
Context gameContext = new ContextImpl(modulesContext, gameContextRegistry);
129-
CoreRegistry.setContext(gameContext);
146+
return new ContextImpl(modulesContext, gameContextRegistry);
130147
});
131-
applyModuleThread.start();
148+
new Thread(applyModuleTask, "apply-module-environment").start();
132149

133150
return false;
134151
} else if (joinStatus.getStatus() == JoinStatus.Status.FAILED) {

engine/src/main/java/org/terasology/engine/persistence/internal/ReadWriteStorageManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ public final class ReadWriteStorageManager extends AbstractStorageManager
116116
private final Game game;
117117
private final BlockManager blockManager;
118118

119+
/**
120+
* @deprecated Use the @Inject constructor instead. This constructor relies on CoreRegistry
121+
* for implicit dependencies which conflicts with the DI migration.
122+
*/
123+
@Deprecated
119124
public ReadWriteStorageManager(Path savePath, ModuleEnvironment environment, EngineEntityManager entityManager, BlockManager blockManager,
120125
ExtraBlockDataManager extraDataManager, RecordAndReplaySerializer recordAndReplaySerializer,
121126
RecordAndReplayUtils recordAndReplayUtils, RecordAndReplayCurrentStatus recordAndReplayCurrentStatus) {

0 commit comments

Comments
 (0)