Search in sources :

Example 1 with EnvironmentSwitchHandler

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);
    }
}
Also used : WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Component(org.terasology.entitySystem.Component) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Example 2 with EnvironmentSwitchHandler

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));
}
Also used : Stopwatch(com.google.common.base.Stopwatch) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Example 3 with EnvironmentSwitchHandler

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);
    }
}
Also used : CoreScreenLayer(org.terasology.rendering.nui.CoreScreenLayer) Zone(org.terasology.world.zones.Zone) ContextImpl(org.terasology.context.internal.ContextImpl) DependencyResolver(org.terasology.module.DependencyResolver) UISlider(org.terasology.rendering.nui.widgets.UISlider) LoggerFactory(org.slf4j.LoggerFactory) Config(org.terasology.config.Config) Property(org.terasology.rendering.nui.properties.Property) ByteBuffer(java.nio.ByteBuffer) WorldConfigurator(org.terasology.world.generator.WorldConfigurator) Map(java.util.Map) PropertyLayout(org.terasology.rendering.nui.layouts.PropertyLayout) SimpleUri(org.terasology.engine.SimpleUri) TeraMath(org.terasology.math.TeraMath) Context(org.terasology.context.Context) ModuleAwareAssetTypeManager(org.terasology.assets.module.ModuleAwareAssetTypeManager) WidgetUtil(org.terasology.rendering.nui.WidgetUtil) ModuleEnvironment(org.terasology.module.ModuleEnvironment) ComponentLibrary(org.terasology.entitySystem.metadata.ComponentLibrary) Collectors(java.util.stream.Collectors) PropertyProvider(org.terasology.rendering.nui.properties.PropertyProvider) UIImage(org.terasology.rendering.nui.widgets.UIImage) Objects(java.util.Objects) List(java.util.List) UnresolvedDependencyException(org.terasology.module.exceptions.UnresolvedDependencyException) CoreRegistry(org.terasology.registry.CoreRegistry) TextureData(org.terasology.rendering.assets.texture.TextureData) ResourceUrn(org.terasology.assets.ResourceUrn) In(org.terasology.registry.In) MenuAnimationSystems(org.terasology.rendering.nui.animation.MenuAnimationSystems) Texture(org.terasology.rendering.assets.texture.Texture) Callable(java.util.concurrent.Callable) FacetLayerPreview(org.terasology.rendering.nui.layers.mainMenu.preview.FacetLayerPreview) Lists(com.google.common.collect.Lists) WorldGeneratorPluginLibrary(org.terasology.world.generator.plugin.WorldGeneratorPluginLibrary) ResolutionResult(org.terasology.module.ResolutionResult) NUIManager(org.terasology.rendering.nui.NUIManager) ModuleManager(org.terasology.engine.module.ModuleManager) Assets(org.terasology.utilities.Assets) Logger(org.slf4j.Logger) FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) TempWorldGeneratorPluginLibrary(org.terasology.world.generator.plugin.TempWorldGeneratorPluginLibrary) UIText(org.terasology.rendering.nui.widgets.UIText) WorldGeneratorManager(org.terasology.world.generator.internal.WorldGeneratorManager) UIDropdown(org.terasology.rendering.nui.widgets.UIDropdown) WorldGenerator(org.terasology.world.generator.WorldGenerator) Binding(org.terasology.rendering.nui.databinding.Binding) PreviewGenerator(org.terasology.rendering.nui.layers.mainMenu.preview.PreviewGenerator) Component(org.terasology.entitySystem.Component) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler) UIButton(org.terasology.rendering.nui.widgets.UIButton) PropertyOrdering(org.terasology.rendering.nui.properties.PropertyOrdering) Zone(org.terasology.world.zones.Zone) ResolutionResult(org.terasology.module.ResolutionResult) SimpleUri(org.terasology.engine.SimpleUri) TempWorldGeneratorPluginLibrary(org.terasology.world.generator.plugin.TempWorldGeneratorPluginLibrary) UnresolvedDependencyException(org.terasology.module.exceptions.UnresolvedDependencyException) ContextImpl(org.terasology.context.internal.ContextImpl) FacetLayerPreview(org.terasology.rendering.nui.layers.mainMenu.preview.FacetLayerPreview) DependencyResolver(org.terasology.module.DependencyResolver) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Example 4 with EnvironmentSwitchHandler

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;
}
Also used : Set(java.util.Set) Server(org.terasology.network.Server) NameVersion(org.terasology.naming.NameVersion) GameEngine(org.terasology.engine.GameEngine) ModuleManager(org.terasology.engine.module.ModuleManager) Entry(java.util.Map.Entry) StateMainMenu(org.terasology.engine.modes.StateMainMenu) WorldInfo(org.terasology.world.internal.WorldInfo) ServerInfoMessage(org.terasology.network.ServerInfoMessage) Module(org.terasology.module.Module) Map(java.util.Map) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Example 5 with EnvironmentSwitchHandler

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;
        }
    }
}
Also used : ModuleEnvironment(org.terasology.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.modes.StateMainMenu) ResolutionResult(org.terasology.module.ResolutionResult) GameEngine(org.terasology.engine.GameEngine) ModuleManager(org.terasology.engine.module.ModuleManager) Module(org.terasology.module.Module) Name(org.terasology.naming.Name) DependencyResolver(org.terasology.module.DependencyResolver) EnvironmentSwitchHandler(org.terasology.engine.bootstrap.EnvironmentSwitchHandler)

Aggregations

EnvironmentSwitchHandler (org.terasology.engine.bootstrap.EnvironmentSwitchHandler)5 ModuleManager (org.terasology.engine.module.ModuleManager)3 Map (java.util.Map)2 GameEngine (org.terasology.engine.GameEngine)2 StateMainMenu (org.terasology.engine.modes.StateMainMenu)2 Component (org.terasology.entitySystem.Component)2 DependencyResolver (org.terasology.module.DependencyResolver)2 Module (org.terasology.module.Module)2 ModuleEnvironment (org.terasology.module.ModuleEnvironment)2 ResolutionResult (org.terasology.module.ResolutionResult)2 Stopwatch (com.google.common.base.Stopwatch)1 Lists (com.google.common.collect.Lists)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1