|
1 | | -// Copyright 2021 The Terasology Foundation |
| 1 | +// Copyright 2022 The Terasology Foundation |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | package org.terasology.engine; |
4 | 4 |
|
|
9 | 9 | import org.terasology.crashreporter.CrashReporter; |
10 | 10 | import org.terasology.engine.config.Config; |
11 | 11 | import org.terasology.engine.config.SystemConfig; |
12 | | -import org.terasology.engine.core.GameScheduler; |
| 12 | +import org.terasology.engine.core.GameEngine; |
13 | 13 | import org.terasology.engine.core.LoggingContext; |
14 | 14 | import org.terasology.engine.core.PathManager; |
15 | 15 | import org.terasology.engine.core.StandardGameStatus; |
16 | 16 | import org.terasology.engine.core.TerasologyEngine; |
17 | 17 | import org.terasology.engine.core.TerasologyEngineBuilder; |
| 18 | +import org.terasology.engine.core.modes.GameState; |
18 | 19 | import org.terasology.engine.core.modes.StateLoading; |
19 | 20 | import org.terasology.engine.core.modes.StateMainMenu; |
20 | 21 | import org.terasology.engine.core.subsystem.EngineSubsystem; |
@@ -177,43 +178,79 @@ public Integer call() throws IOException { |
177 | 178 |
|
178 | 179 | if (isHeadless) { |
179 | 180 | 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 |
205 | 184 |
|
206 | | - engine.run(new StateMainMenu()); |
| 185 | + GameState nextState = chainMainMenuToWorkAroundBug1127(selectNextGameState()); |
| 186 | + if (nextState == null) { |
| 187 | + return 1; |
207 | 188 | } |
| 189 | + |
| 190 | + engine.run(nextState); |
208 | 191 | } catch (Throwable e) { |
209 | 192 | // also catch Errors such as UnsatisfiedLink, NoSuchMethodError, etc. |
210 | 193 | splashScreen.close(); |
211 | 194 | reportException(e); |
| 195 | + return 1; |
212 | 196 | } |
213 | 197 |
|
214 | 198 | return 0; |
215 | 199 | } |
216 | 200 |
|
| 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 | + |
217 | 254 | private static String getNewTitle(String title) { |
218 | 255 | String newTitle = title.substring(0, getPositionOfLastDigit(title)); |
219 | 256 | int fileNumber = getLastNumber(title); |
|
0 commit comments