use of org.terasology.engine.bootstrap.EnvironmentSwitchHandler in project Terasology by MovingBlocks.
the class PreviewWorldScreen method resetEnvironment.
private void resetEnvironment() {
CoreRegistry.setContext(context);
if (environment != null) {
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
environmentSwitchHandler.handleSwitchBackFromPreviewEnvironment(subContext);
environment.close();
environment = null;
}
previewGen.close();
WorldConfigurator worldConfig = worldGenerator.getConfigurator();
Map<String, Component> params = worldConfig.getProperties();
if (params != null) {
config.setModuleConfigs(worldGenerator.getUri(), params);
}
}
use of org.terasology.engine.bootstrap.EnvironmentSwitchHandler in project Terasology by MovingBlocks.
the class TerasologyEngine method initialize.
public void initialize() {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Stopwatch totalInitTime = Stopwatch.createStarted();
try {
logger.info("Initializing Terasology...");
logEnvironmentInfo();
// TODO: Need to get everything thread safe and get rid of the concept of "GameThread" as much as possible.
GameThread.setToCurrentThread();
preInitSubsystems();
initManagers();
initSubsystems();
changeStatus(TerasologyEngineStatus.INITIALIZING_ASSET_MANAGEMENT);
initAssets();
EnvironmentSwitchHandler environmentSwitcher = new EnvironmentSwitchHandler();
rootContext.put(EnvironmentSwitchHandler.class, environmentSwitcher);
environmentSwitcher.handleSwitchToGameEnvironment(rootContext);
postInitSubsystems();
verifyInitialisation();
/**
* Prevent objects being put in engine context after init phase. Engine states should use/create a
* child context.
*/
CoreRegistry.setContext(null);
} catch (RuntimeException e) {
logger.error("Failed to initialise Terasology", e);
cleanup();
throw e;
}
double seconds = 0.001 * totalInitTime.elapsed(TimeUnit.MILLISECONDS);
logger.info("Initialization completed in {}sec.", String.format("%.2f", seconds));
}
use of org.terasology.engine.bootstrap.EnvironmentSwitchHandler in project Terasology by MovingBlocks.
the class PreviewWorldScreen method setEnvironment.
public void setEnvironment() throws Exception {
// TODO: pass world gen and module list directly rather than using the config
SimpleUri worldGenUri = config.getWorldGeneration().getDefaultGenerator();
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(config.getDefaultModSelection().listModules());
if (result.isSuccess()) {
subContext = new ContextImpl(context);
CoreRegistry.setContext(subContext);
environment = moduleManager.loadEnvironment(result.getModules(), false);
subContext.put(WorldGeneratorPluginLibrary.class, new TempWorldGeneratorPluginLibrary(environment, subContext));
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
environmentSwitchHandler.handleSwitchToPreviewEnvironment(subContext, environment);
genTexture();
worldGenerator = WorldGeneratorManager.createWorldGenerator(worldGenUri, subContext, environment);
worldGenerator.setWorldSeed(seed.getText());
List<Zone> previewZones = Lists.newArrayList(worldGenerator.getZones()).stream().filter(z -> !z.getPreviewLayers().isEmpty()).collect(Collectors.toList());
if (previewZones.isEmpty()) {
zoneSelector.setVisible(false);
previewGen = new FacetLayerPreview(environment, worldGenerator);
} else {
zoneSelector.setVisible(true);
zoneSelector.setOptions(previewZones);
zoneSelector.setSelection(previewZones.get(0));
}
configureProperties();
} else {
throw new UnresolvedDependencyException("Unable to resolve depencencies for " + worldGenUri);
}
}
use of org.terasology.engine.bootstrap.EnvironmentSwitchHandler in project Terasology by MovingBlocks.
the class JoinServer method step.
@Override
public boolean step() {
if (applyModuleThread != null) {
if (!applyModuleThread.isAlive()) {
if (oldEnvironment != null) {
oldEnvironment.close();
}
return true;
}
return false;
} else if (joinStatus.getStatus() == JoinStatus.Status.COMPLETE) {
Server server = networkSystem.getServer();
ServerInfoMessage serverInfo = networkSystem.getServer().getInfo();
// If no GameName, use Server IP Address
if (serverInfo.getGameName().length() > 0) {
gameManifest.setTitle(serverInfo.getGameName());
} else {
gameManifest.setTitle(server.getRemoteAddress());
}
for (WorldInfo worldInfo : serverInfo.getWorldInfoList()) {
gameManifest.addWorld(worldInfo);
}
Map<String, Short> blockMap = Maps.newHashMap();
for (Entry<Integer, String> entry : serverInfo.getBlockIds().entrySet()) {
String name = entry.getValue();
short id = entry.getKey().shortValue();
Short oldId = blockMap.put(name, id);
if (oldId != null && oldId != id) {
logger.warn("Overwriting Id {} for {} with Id {}", oldId, name, id);
}
}
Map<String, Short> biomeMap = Maps.newHashMap();
for (Entry<Short, String> entry : serverInfo.getBiomeIds().entrySet()) {
String name = entry.getValue();
short id = entry.getKey();
Short oldId = biomeMap.put(name, id);
if (oldId != null && oldId != id) {
logger.warn("Overwriting Biome Id {} for {} with Id {}", oldId, name, id);
}
}
gameManifest.setRegisteredBlockFamilies(serverInfo.getRegisterBlockFamilyList());
gameManifest.setBlockIdMap(blockMap);
gameManifest.setBiomeIdMap(biomeMap);
gameManifest.setTime(networkSystem.getServer().getInfo().getTime());
ModuleManager moduleManager = context.get(ModuleManager.class);
Set<Module> moduleSet = Sets.newLinkedHashSet();
for (NameVersion moduleInfo : networkSystem.getServer().getInfo().getModuleList()) {
Module module = moduleManager.getRegistry().getModule(moduleInfo.getName(), moduleInfo.getVersion());
if (module == null) {
StateMainMenu mainMenu = new StateMainMenu("Missing required module: " + moduleInfo);
context.get(GameEngine.class).changeState(mainMenu);
return false;
} else {
logger.info("Activating module: {}:{}", moduleInfo.getName(), moduleInfo.getVersion());
gameManifest.addModule(module.getId(), module.getVersion());
moduleSet.add(module);
}
}
oldEnvironment = moduleManager.getEnvironment();
moduleManager.loadEnvironment(moduleSet, true);
context.get(Game.class).load(gameManifest);
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
applyModuleThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
applyModuleThread.start();
return false;
} else if (joinStatus.getStatus() == JoinStatus.Status.FAILED) {
StateMainMenu mainMenu = new StateMainMenu("Failed to connect to server: " + joinStatus.getErrorMessage());
context.get(GameEngine.class).changeState(mainMenu);
networkSystem.shutdown();
}
return false;
}
use of org.terasology.engine.bootstrap.EnvironmentSwitchHandler in project Terasology by MovingBlocks.
the class RegisterMods method step.
@Override
public boolean step() {
if (applyModulesThread != null) {
if (!applyModulesThread.isAlive()) {
if (oldEnvironment != null) {
oldEnvironment.close();
}
return true;
}
return false;
} else {
ModuleManager moduleManager = context.get(ModuleManager.class);
List<Name> moduleIds = gameManifest.getModules().stream().map(NameVersion::getName).collect(Collectors.toCollection(ArrayList::new));
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(moduleIds);
if (result.isSuccess()) {
oldEnvironment = moduleManager.getEnvironment();
ModuleEnvironment env = moduleManager.loadEnvironment(result.getModules(), true);
for (Module moduleInfo : env.getModulesOrderedByDependencies()) {
logger.info("Activating module: {}:{}", moduleInfo.getId(), moduleInfo.getVersion());
}
EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
applyModulesThread = new Thread(() -> environmentSwitchHandler.handleSwitchToGameEnvironment(context));
applyModulesThread.start();
return false;
} else {
logger.warn("Missing at least one required module or dependency: {}", moduleIds);
context.get(GameEngine.class).changeState(new StateMainMenu("Missing required module or dependency"));
return true;
}
}
}
Aggregations