Skip to content

Commit

Permalink
fix(headless-execution): MainMenuState can handle headless now. Corre…
Browse files Browse the repository at this point in the history
…ct loadingstate for headless clients. (#4962)

Co-authored-by: jdrueckert <jd.rueckert@googlemail.com>
  • Loading branch information
DarkWeird and jdrueckert authored Nov 29, 2021
1 parent 43e1b90 commit 269f2b0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.terasology.engine.core.modes.loadProcesses.SetupLocalPlayer;
import org.terasology.engine.core.modes.loadProcesses.SetupRemotePlayer;
import org.terasology.engine.core.modes.loadProcesses.StartServer;
import org.terasology.engine.core.subsystem.DisplayDevice;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.game.Game;
import org.terasology.engine.game.GameManifest;
Expand All @@ -64,11 +65,10 @@
public class StateLoading implements GameState {

private static final Logger logger = LoggerFactory.getLogger(StateLoading.class);

private Context context;
private final GameManifest gameManifest;
private final NetworkMode netMode;
private final Queue<LoadProcess> loadProcesses = Queues.newArrayDeque();
private Context context;
private LoadProcess current;
private JoinStatus joinStatus;

Expand All @@ -83,6 +83,7 @@ public class StateLoading implements GameState {

private boolean chunkGenerationStarted;
private long timeLastChunkGenerated;
private boolean headless;

/**
* Constructor for server or single player games
Expand All @@ -106,11 +107,12 @@ public StateLoading(JoinStatus joinStatus) {
@Override
public void init(GameEngine engine) {
this.context = engine.createChildContext();
headless = context.get(DisplayDevice.class).isHeadless();

CoreRegistry.setContext(context);

systemConfig = context.get(SystemConfig.class);

if (netMode.hasLocalClient()) {
if (!headless) {
this.nuiManager = new NUIManagerInternal((TerasologyCanvasRenderer) context.get(CanvasRenderer.class), context);
context.put(NUIManager.class, nuiManager);
}
Expand Down Expand Up @@ -145,15 +147,21 @@ public void init(GameEngine engine) {

private void initClient() {
loadProcesses.add(new JoinServer(context, gameManifest, joinStatus));
loadProcesses.add(new InitialiseRendering(context));
if (!headless) {
loadProcesses.add(new InitialiseRendering(context));
}
loadProcesses.add(new InitialiseEntitySystem(context));
loadProcesses.add(new RegisterBlocks(context, gameManifest));
loadProcesses.add(new InitialiseGraphics(context));
if (!headless) {
loadProcesses.add(new InitialiseGraphics(context));
}
loadProcesses.add(new LoadPrefabs(context));
loadProcesses.add(new ProcessBlockPrefabs(context));
loadProcesses.add(new LoadExtraBlockData(context));
loadProcesses.add(new InitialiseComponentSystemManager(context));
loadProcesses.add(new RegisterInputSystem(context));
if (!headless) {
loadProcesses.add(new RegisterInputSystem(context));
}
loadProcesses.add(new RegisterSystems(context, netMode));
loadProcesses.add(new InitialiseCommandSystem(context));
loadProcesses.add(new InitialiseRemoteWorld(context, gameManifest));
Expand All @@ -170,18 +178,18 @@ private void initClient() {

private void initHost() {
loadProcesses.add(new RegisterMods(context, gameManifest));
if (netMode.hasLocalClient()) {
if (!headless) {
loadProcesses.add(new InitialiseRendering(context));
}
loadProcesses.add(new InitialiseEntitySystem(context));
loadProcesses.add(new RegisterBlocks(context, gameManifest));
if (netMode.hasLocalClient()) {
if (!headless) {
loadProcesses.add(new InitialiseGraphics(context));
}
loadProcesses.add(new LoadPrefabs(context));
loadProcesses.add(new ProcessBlockPrefabs(context));
loadProcesses.add(new InitialiseComponentSystemManager(context));
if (netMode.hasLocalClient()) {
if (!headless) {
loadProcesses.add(new RegisterInputSystem(context));
}
loadProcesses.add(new RegisterSystems(context, netMode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.terasology.engine.core.GameEngine;
import org.terasology.engine.core.LoggingContext;
import org.terasology.engine.core.modes.loadProcesses.RegisterInputSystem;
import org.terasology.engine.core.subsystem.DisplayDevice;
import org.terasology.engine.i18n.TranslationSystem;
import org.terasology.engine.identity.storageServiceClient.StorageServiceWorker;
import org.terasology.engine.input.InputSystem;
Expand All @@ -33,6 +34,7 @@ public class StateMainMenu extends AbstractState {
private StorageServiceWorker storageServiceWorker;

private String messageOnLoad = "";
private boolean headless;


public StateMainMenu() {
Expand All @@ -45,38 +47,50 @@ public StateMainMenu(String showMessageOnLoad) {
@Override
public void init(GameEngine gameEngine) {
context = gameEngine.createChildContext();
initEntityAndComponentManagers(false);
headless = context.get(DisplayDevice.class).isHeadless();

initEntityAndComponentManagers(headless);

createLocalPlayer(context);

// TODO: REMOVE this and handle refreshing of core game state at the engine level - see Issue #1127
new RegisterInputSystem(context).step();
if (!headless) {
// TODO: REMOVE this and handle refreshing of core game state at the engine level - see Issue #1127
new RegisterInputSystem(context).step();

nuiManager = context.get(NUIManager.class);
eventSystem.registerEventHandler(nuiManager);
NUIEditorSystem nuiEditorSystem = new NUIEditorSystem();
context.put(NUIEditorSystem.class, nuiEditorSystem);
componentSystemManager.register(nuiEditorSystem, "engine:NUIEditorSystem");
nuiManager = context.get(NUIManager.class);
eventSystem.registerEventHandler(nuiManager);
NUIEditorSystem nuiEditorSystem = new NUIEditorSystem();
context.put(NUIEditorSystem.class, nuiEditorSystem);
componentSystemManager.register(nuiEditorSystem, "engine:NUIEditorSystem");

NUISkinEditorSystem nuiSkinEditorSystem = new NUISkinEditorSystem();
context.put(NUISkinEditorSystem.class, nuiSkinEditorSystem);
componentSystemManager.register(nuiSkinEditorSystem, "engine:NUISkinEditorSystem");
NUISkinEditorSystem nuiSkinEditorSystem = new NUISkinEditorSystem();
context.put(NUISkinEditorSystem.class, nuiSkinEditorSystem);
componentSystemManager.register(nuiSkinEditorSystem, "engine:NUISkinEditorSystem");

inputSystem = context.get(InputSystem.class);
inputSystem = context.get(InputSystem.class);
}

componentSystemManager.initialise();

console = context.get(Console.class);
storageServiceWorker = context.get(StorageServiceWorker.class);

playBackgroundMusic();

//guiManager.openWindow("main");
context.get(NUIManager.class).pushScreen("engine:mainMenuScreen");
if (!headless) {
//guiManager.openWindow("main");
context.get(NUIManager.class).pushScreen("engine:mainMenuScreen");
}
if (!messageOnLoad.isEmpty()) {
TranslationSystem translationSystem = context.get(TranslationSystem.class);
MessagePopup popup = nuiManager.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
popup.setMessage("Error", translationSystem.translate(messageOnLoad));
if (headless) {
throw new RuntimeException(
String.format(
"Game could not be started, server attempted to return to main menu: [%s]. See logs before",
translationSystem.translate(messageOnLoad)
));
} else {
MessagePopup popup = nuiManager.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
popup.setMessage("Error", translationSystem.translate(messageOnLoad));
}
}

// TODO: enable it when exposing the telemetry to users
Expand Down Expand Up @@ -106,7 +120,7 @@ private void pushLaunchPopup() {
appender.stop();
});
telemetryConfirmPopup.setOptionButtonText(translationSystem.translate("${engine:menu#telemetry-button}"));
telemetryConfirmPopup.setOptionHandler(()-> {
telemetryConfirmPopup.setOptionHandler(() -> {
nuiManager.pushScreen(TelemetryScreen.ASSET_URI, TelemetryScreen.class);
});
}
Expand All @@ -131,7 +145,9 @@ private void stopBackgroundMusic() {

@Override
public void handleInput(float delta) {
inputSystem.update(delta);
if (inputSystem != null) {
inputSystem.update(delta);
}
}

@Override
Expand All @@ -140,12 +156,13 @@ public void update(float delta) {

eventSystem.process();
storageServiceWorker.flushNotificationsToConsole(console);

}

@Override
public void render() {
nuiManager.render();
if (nuiManager != null) {
nuiManager.render();
}
}

@Override
Expand All @@ -155,11 +172,13 @@ public String getLoggingPhase() {

@Override
public boolean isHibernationAllowed() {
return true;
return !headless;
}

private void updateUserInterface(float delta) {
nuiManager.update(delta);
if (nuiManager != null) {
nuiManager.update(delta);
}
}

@Override
Expand Down

0 comments on commit 269f2b0

Please sign in to comment.