Skip to content

Commit b367a18

Browse files
authored
fix(facade): fix broken --load-last-game (#5053)
1 parent 40db450 commit b367a18

File tree

1 file changed

+65
-28
lines changed

1 file changed

+65
-28
lines changed

facades/PC/src/main/java/org/terasology/engine/Terasology.java

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 The Terasology Foundation
1+
// Copyright 2022 The Terasology Foundation
22
// SPDX-License-Identifier: Apache-2.0
33
package org.terasology.engine;
44

@@ -9,12 +9,13 @@
99
import org.terasology.crashreporter.CrashReporter;
1010
import org.terasology.engine.config.Config;
1111
import org.terasology.engine.config.SystemConfig;
12-
import org.terasology.engine.core.GameScheduler;
12+
import org.terasology.engine.core.GameEngine;
1313
import org.terasology.engine.core.LoggingContext;
1414
import org.terasology.engine.core.PathManager;
1515
import org.terasology.engine.core.StandardGameStatus;
1616
import org.terasology.engine.core.TerasologyEngine;
1717
import org.terasology.engine.core.TerasologyEngineBuilder;
18+
import org.terasology.engine.core.modes.GameState;
1819
import org.terasology.engine.core.modes.StateLoading;
1920
import org.terasology.engine.core.modes.StateMainMenu;
2021
import org.terasology.engine.core.subsystem.EngineSubsystem;
@@ -177,43 +178,79 @@ public Integer call() throws IOException {
177178

178179
if (isHeadless) {
179180
engine.subscribeToStateChange(new HeadlessStateChangeListener(engine));
180-
engine.run(new StateHeadlessSetup());
181-
} else if (loadLastGame) {
182-
engine.initialize(); //initialize the managers first
183-
GameScheduler.scheduleParallel("loadGame", () -> {
184-
GameManifest gameManifest = getLatestGameManifest();
185-
if (gameManifest != null) {
186-
engine.changeState(new StateLoading(gameManifest, NetworkMode.NONE));
187-
}
188-
});
189-
} else {
190-
if (createLastGame) {
191-
engine.initialize();
192-
GameScheduler.scheduleParallel("createLastGame", () -> {
193-
GameManifest gameManifest = getLatestGameManifest();
194-
if (gameManifest != null) {
195-
String title = gameManifest.getTitle();
196-
if (!title.startsWith("New Created")) { //if first time run
197-
gameManifest.setTitle("New Created " + title + " 1");
198-
} else { //if not first time run
199-
gameManifest.setTitle(getNewTitle(title));
200-
}
201-
engine.changeState(new StateLoading(gameManifest, NetworkMode.NONE));
202-
}
203-
});
204-
}
181+
}
182+
183+
engine.initialize(); //initialize the managers first
205184

206-
engine.run(new StateMainMenu());
185+
GameState nextState = chainMainMenuToWorkAroundBug1127(selectNextGameState());
186+
if (nextState == null) {
187+
return 1;
207188
}
189+
190+
engine.run(nextState);
208191
} catch (Throwable e) {
209192
// also catch Errors such as UnsatisfiedLink, NoSuchMethodError, etc.
210193
splashScreen.close();
211194
reportException(e);
195+
return 1;
212196
}
213197

214198
return 0;
215199
}
216200

201+
private GameState selectNextGameState() {
202+
GameState nextState;
203+
204+
GameManifest gameManifest = getLatestGameManifest();
205+
206+
if (isHeadless) {
207+
nextState = new StateHeadlessSetup();
208+
} else if (loadLastGame) {
209+
if (gameManifest == null) {
210+
logger.error("Failed --load-last-game: last game not found.");
211+
return null;
212+
}
213+
nextState = new StateLoading(gameManifest, NetworkMode.NONE);
214+
} else if (createLastGame) {
215+
if (gameManifest == null) {
216+
logger.error("Failed --create-last-game: last game not found.");
217+
return null;
218+
}
219+
String title = gameManifest.getTitle();
220+
if (!title.startsWith("New Created")) { //if first time run
221+
gameManifest.setTitle("New Created " + title + " 1");
222+
} else { //if not first time run
223+
gameManifest.setTitle(getNewTitle(title));
224+
}
225+
nextState = new StateLoading(gameManifest, NetworkMode.NONE);
226+
} else {
227+
nextState = new StateMainMenu();
228+
}
229+
230+
return nextState;
231+
}
232+
233+
/**
234+
* Chain states to load after MainMenu.
235+
* <p>
236+
* Things are broken when we're not headless and try to skip MainMenu entirely.
237+
*
238+
* @see <a href="https://github.com/MovingBlocks/Terasology/issues/1126">#1126</a>
239+
* @see <a href="https://github.com/MovingBlocks/Terasology/issues/1127">#1127</a>
240+
*/
241+
private GameState chainMainMenuToWorkAroundBug1127(GameState nextState) {
242+
if (isHeadless || nextState == null || nextState instanceof StateMainMenu) {
243+
return nextState;
244+
}
245+
return new StateMainMenu() {
246+
@Override
247+
public void init(GameEngine gameEngine) {
248+
super.init(gameEngine);
249+
gameEngine.changeState(nextState);
250+
}
251+
};
252+
}
253+
217254
private static String getNewTitle(String title) {
218255
String newTitle = title.substring(0, getPositionOfLastDigit(title));
219256
int fileNumber = getLastNumber(title);

0 commit comments

Comments
 (0)