Skip to content

Commit

Permalink
fix(headless-fixes): remove InputSystem and NuiManager from headlesses (
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkWeird authored Sep 29, 2021
1 parent 8f6ef15 commit c62e776
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ private void verifyInitialisation() {
verifyRequiredSystemIsRegistered(Time.class);
verifyRequiredSystemIsRegistered(DisplayDevice.class);
verifyRequiredSystemIsRegistered(RenderingSubsystemFactory.class);
verifyRequiredSystemIsRegistered(InputSystem.class);
}

/**
Expand Down Expand Up @@ -588,7 +587,9 @@ private void switchState(GameState newState) {
newState.init(this);
stateChangeSubscribers.forEach(StateChangeSubscriber::onStateChange);
InputSystem inputSystem = rootContext.get(InputSystem.class);
inputSystem.drainQueues();
if (inputSystem != null) {
inputSystem.drainQueues();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class AbstractState implements GameState {
protected EventSystem eventSystem;
protected ComponentSystemManager componentSystemManager;

protected void initEntityAndComponentManagers() {
protected void initEntityAndComponentManagers(boolean isHeadless) {
verifyNotNull(context);
CoreRegistry.setContext(context);

Expand All @@ -41,9 +41,10 @@ protected void initEntityAndComponentManagers() {

eventSystem = context.get(EventSystem.class);
context.put(Console.class, new ConsoleImpl(context));

NUIManager nuiManager = new NUIManagerInternal((TerasologyCanvasRenderer) context.get(CanvasRenderer.class), context);
context.put(NUIManager.class, nuiManager);
if (!isHeadless) {
NUIManager nuiManager = new NUIManagerInternal((TerasologyCanvasRenderer) context.get(CanvasRenderer.class), context);
context.put(NUIManager.class, nuiManager);
}

componentSystemManager = new ComponentSystemManager(context);
context.put(ComponentSystemManager.class, componentSystemManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,28 @@ public void init(GameEngine engine) {
componentSystemManager = context.get(ComponentSystemManager.class);
entityManager = context.get(EngineEntityManager.class);
cameraTargetSystem = context.get(CameraTargetSystem.class);
inputSystem = context.get(InputSystem.class);
eventSystem.registerEventHandler(nuiManager);
if (nuiManager != null) {
inputSystem = context.get(InputSystem.class);
eventSystem.registerEventHandler(nuiManager);
}
networkSystem = context.get(NetworkSystem.class);
storageManager = context.get(StorageManager.class);
storageServiceWorker = context.get(StorageServiceWorker.class);
console = context.get(Console.class);

// Show or hide the HUD according to the settings
nuiManager.getHUD().bindVisible(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return !context.get(Config.class).getRendering().getDebug().isHudHidden();
}
});
if (nuiManager != null) {
// Show or hide the HUD according to the settings
nuiManager.getHUD().bindVisible(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return !context.get(Config.class).getRendering().getDebug().isHudHidden();
}
});
}

if (networkSystem.getMode() == NetworkMode.CLIENT) {
String motd = networkSystem.getServer().getInfo().getMOTD();
if (motd != null && motd.length() != 0) {
if (nuiManager != null && motd != null && motd.length() != 0) {
nuiManager.pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Server MOTD", motd);
}
}
Expand Down Expand Up @@ -133,7 +137,9 @@ public void dispose(boolean shuttingDown) {
// TODO: Shutdown background threads
eventSystem.process();
GameThread.processWaitingProcesses();
nuiManager.clear();
if (nuiManager != null) {
nuiManager.clear();
}

context.get(AudioManager.class).stopAllSounds();

Expand Down Expand Up @@ -162,11 +168,13 @@ public void dispose(boolean shuttingDown) {
console.dispose();
GameThread.clearWaitingProcesses();

/*
* Clear the binding as otherwise the complete ingame state would be
* referenced.
*/
nuiManager.getHUD().clearVisibleBinding();
if (nuiManager != null) {
/*
* Clear the binding as otherwise the complete ingame state would be
* referenced.
*/
nuiManager.getHUD().clearVisibleBinding();
}
}

@Override
Expand All @@ -187,8 +195,9 @@ public void update(float delta) {
storageManager.update();
}


updateUserInterface(delta);
if (nuiManager != null) {
updateUserInterface(delta);
}

storageServiceWorker.flushNotificationsToConsole(console);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.terasology.engine.core.modes.loadProcesses.InitialisePhysics;
import org.terasology.engine.core.modes.loadProcesses.InitialiseRecordAndReplay;
import org.terasology.engine.core.modes.loadProcesses.InitialiseRemoteWorld;
import org.terasology.engine.core.modes.loadProcesses.InitialiseRendering;
import org.terasology.engine.core.modes.loadProcesses.InitialiseSystems;
import org.terasology.engine.core.modes.loadProcesses.InitialiseWorld;
import org.terasology.engine.core.modes.loadProcesses.InitialiseWorldGenerator;
import org.terasology.engine.core.modes.loadProcesses.InitialiseRendering;
import org.terasology.engine.core.modes.loadProcesses.JoinServer;
import org.terasology.engine.core.modes.loadProcesses.LoadEntities;
import org.terasology.engine.core.modes.loadProcesses.LoadExtraBlockData;
Expand Down Expand Up @@ -110,8 +110,10 @@ public void init(GameEngine engine) {

systemConfig = context.get(SystemConfig.class);

this.nuiManager = new NUIManagerInternal((TerasologyCanvasRenderer) context.get(CanvasRenderer.class), context);
context.put(NUIManager.class, nuiManager);
if (netMode.hasLocalClient()) {
this.nuiManager = new NUIManagerInternal((TerasologyCanvasRenderer) context.get(CanvasRenderer.class), context);
context.put(NUIManager.class, nuiManager);
}

EngineTime time = (EngineTime) context.get(Time.class);
time.setPaused(true);
Expand All @@ -134,9 +136,10 @@ public void init(GameEngine engine) {
}

popStep();
loadingScreen = nuiManager.pushScreen("engine:loadingScreen", LoadingScreen.class);
loadingScreen.updateStatus(current.getMessage(), current.getProgress());

if (nuiManager != null) {
loadingScreen = nuiManager.pushScreen("engine:loadingScreen", LoadingScreen.class);
loadingScreen.updateStatus(current.getMessage(), current.getProgress());
}
chunkGenerationStarted = false;
}

Expand Down Expand Up @@ -167,16 +170,20 @@ private void initClient() {

private void initHost() {
loadProcesses.add(new RegisterMods(context, gameManifest));
if(netMode.hasLocalClient()) {
if (netMode.hasLocalClient()) {
loadProcesses.add(new InitialiseRendering(context));
}
loadProcesses.add(new InitialiseEntitySystem(context));
loadProcesses.add(new RegisterBlocks(context, gameManifest));
loadProcesses.add(new InitialiseGraphics(context));
if (netMode.hasLocalClient()) {
loadProcesses.add(new InitialiseGraphics(context));
}
loadProcesses.add(new LoadPrefabs(context));
loadProcesses.add(new ProcessBlockPrefabs(context));
loadProcesses.add(new InitialiseComponentSystemManager(context));
loadProcesses.add(new RegisterInputSystem(context));
if (netMode.hasLocalClient()) {
loadProcesses.add(new RegisterInputSystem(context));
}
loadProcesses.add(new RegisterSystems(context, netMode));
loadProcesses.add(new InitialiseCommandSystem(context));
loadProcesses.add(new LoadExtraBlockData(context));
Expand Down Expand Up @@ -252,14 +259,17 @@ public void update(float delta) {
}
}
if (current == null) {
nuiManager.closeScreen(loadingScreen);
nuiManager.setHUDVisible(true);
if (nuiManager != null) {
nuiManager.closeScreen(loadingScreen);
nuiManager.setHUDVisible(true);
}
context.get(GameEngine.class).changeState(new StateIngame(gameManifest, context));
} else {
float progressValue = (progress + current.getExpectedCost() * current.getProgress()) / maxProgress;
loadingScreen.updateStatus(current.getMessage(), progressValue);
nuiManager.update(delta);

if (nuiManager != null) {
loadingScreen.updateStatus(current.getMessage(), progressValue);
nuiManager.update(delta);
}
// chunk generation begins at the AwaitCharacterSpawn step
if (current instanceof AwaitCharacterSpawn && !chunkGenerationStarted) {
chunkGenerationStarted = true;
Expand All @@ -280,7 +290,9 @@ public void update(float delta) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public StateMainMenu(String showMessageOnLoad) {
@Override
public void init(GameEngine gameEngine) {
context = gameEngine.createChildContext();
initEntityAndComponentManagers();
initEntityAndComponentManagers(false);

createLocalPlayer(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.terasology.engine.context.Context;
import org.terasology.engine.core.subsystem.EngineSubsystem;
import org.terasology.engine.input.InputSystem;

public class HeadlessInput implements EngineSubsystem {

Expand All @@ -19,8 +18,7 @@ public void postInitialise(Context context) {
}

private void initControls(Context context) {
InputSystem inputSystem = new InputSystem();
context.put(InputSystem.class, inputSystem);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.terasology.engine.core.module.ModuleManager;
import org.terasology.engine.core.module.StandardModuleExtension;
import org.terasology.engine.game.GameManifest;
import org.terasology.engine.input.InputSystem;
import org.terasology.engine.network.NetworkMode;
import org.terasology.engine.rendering.nui.layers.mainMenu.savedGames.GameInfo;
import org.terasology.engine.rendering.nui.layers.mainMenu.savedGames.GameProvider;
Expand All @@ -40,12 +39,9 @@ public StateHeadlessSetup() {
@Override
public void init(GameEngine gameEngine) {
context = gameEngine.createChildContext();
initEntityAndComponentManagers();
initEntityAndComponentManagers(true);
createLocalPlayer(context);

componentSystemManager.register(context.get(InputSystem.class), "engine:InputSystem");
componentSystemManager.initialise();

GameManifest gameManifest;
List<GameInfo> savedGames = GameProvider.getSavedGames();
if (savedGames.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.joml.Vector3ic;
import org.terasology.engine.config.Config;
import org.terasology.engine.context.Context;
import org.terasology.engine.logic.players.LocalPlayerSystem;
import org.terasology.engine.monitoring.PerformanceMonitor;
import org.terasology.engine.rendering.assets.material.Material;
import org.terasology.engine.rendering.cameras.Camera;
Expand Down Expand Up @@ -48,8 +47,6 @@ public class HeadlessWorldRenderer implements WorldRenderer {
public HeadlessWorldRenderer(Context context) {
this.worldProvider = context.get(WorldProvider.class);
this.chunkProvider = context.get(ChunkProvider.class);
LocalPlayerSystem localPlayerSystem = context.get(LocalPlayerSystem.class);
localPlayerSystem.setPlayerCamera(noCamera);
config = context.get(Config.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.terasology.engine.core.subsystem.config.BindsManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.input.cameraTarget.CameraTargetSystem;
import org.terasology.engine.input.events.CharEvent;
Expand Down Expand Up @@ -61,7 +62,7 @@
* In addition to raw keyboard and mouse input, the system handles Bind Buttons and Bind Axis, which can be mapped to
* one or more inputs.
*/
@RegisterSystem
@RegisterSystem(RegisterMode.CLIENT)
public class InputSystem extends BaseComponentSystem {

@In
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public class ChatSystem extends BaseComponentSystem {

@Override
public void initialise() {
overlay = nuiManager.addOverlay(NotificationOverlay.ASSET_URI, NotificationOverlay.class);
if (nuiManager != null) {
overlay = nuiManager.addOverlay(NotificationOverlay.ASSET_URI, NotificationOverlay.class);
}
}

@ReceiveEvent(components = ClientComponent.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import org.terasology.engine.entitySystem.systems.BaseComponentSystem;
import org.terasology.engine.entitySystem.systems.RegisterMode;
import org.terasology.engine.entitySystem.systems.RegisterSystem;
import org.terasology.engine.input.binds.general.ConsoleButton;
import org.terasology.engine.logic.console.commandSystem.ConsoleCommand;
import org.terasology.engine.logic.console.ui.NotificationOverlay;
import org.terasology.input.ButtonState;
import org.terasology.engine.input.binds.general.ConsoleButton;
import org.terasology.engine.network.ClientComponent;
import org.terasology.engine.registry.In;
import org.terasology.engine.rendering.nui.NUIManager;
import org.terasology.input.ButtonState;

@RegisterSystem
public class ConsoleSystem extends BaseComponentSystem {
Expand All @@ -29,16 +29,18 @@ public class ConsoleSystem extends BaseComponentSystem {

@Override
public void initialise() {
overlay = nuiManager.addOverlay(NotificationOverlay.ASSET_URI, NotificationOverlay.class);
console.subscribe((Message message) -> {
if (!nuiManager.isOpen("engine:console")) {
// make sure the message isn't already shown in the chat overlay
if (message.getType() != CoreMessageType.CHAT && message.getType() != CoreMessageType.NOTIFICATION
|| !nuiManager.isOpen("engine:chat")) {
overlay.setVisible(true);
if (nuiManager != null) {
overlay = nuiManager.addOverlay(NotificationOverlay.ASSET_URI, NotificationOverlay.class);
console.subscribe((Message message) -> {
if (!nuiManager.isOpen("engine:console")) {
// make sure the message isn't already shown in the chat overlay
if (message.getType() != CoreMessageType.CHAT && message.getType() != CoreMessageType.NOTIFICATION
|| !nuiManager.isOpen("engine:chat")) {
overlay.setVisible(true);
}
}
}
});
});
}
}

@ReceiveEvent(components = ClientComponent.class, priority = EventPriority.PRIORITY_CRITICAL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.terasology.engine.core.subsystem.config.BindsSubsystem;
import org.terasology.engine.core.subsystem.headless.HeadlessAudio;
import org.terasology.engine.core.subsystem.headless.HeadlessGraphics;
import org.terasology.engine.core.subsystem.headless.HeadlessInput;
import org.terasology.engine.core.subsystem.headless.HeadlessTimer;
import org.terasology.engine.core.subsystem.headless.mode.HeadlessStateChangeListener;
import org.terasology.engine.core.subsystem.headless.mode.StateHeadlessSetup;
Expand Down Expand Up @@ -270,8 +269,7 @@ private void populateSubsystems(TerasologyEngineBuilder builder) {
if (isHeadless) {
builder.add(new HeadlessGraphics())
.add(new HeadlessTimer())
.add(new HeadlessAudio())
.add(new HeadlessInput());
.add(new HeadlessAudio());
} else {
EngineSubsystem audio = soundEnabled ? new LwjglAudio() : new HeadlessAudio();
builder.add(audio)
Expand Down

0 comments on commit c62e776

Please sign in to comment.