Search in sources :

Example 11 with ModuleManager

use of org.terasology.engine.module.ModuleManager in project Terasology by MovingBlocks.

the class ModulesMetric method updateModules.

private void updateModules() {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    ModuleEnvironment moduleEnvironment = moduleManager.getEnvironment();
    modules.clear();
    Iterator<Module> iterator = moduleEnvironment.iterator();
    while (iterator.hasNext()) {
        Module next = iterator.next();
        modules.add(next);
    }
}
Also used : ModuleEnvironment(org.terasology.module.ModuleEnvironment) ModuleManager(org.terasology.engine.module.ModuleManager) Module(org.terasology.module.Module)

Example 12 with ModuleManager

use of org.terasology.engine.module.ModuleManager in project Terasology by MovingBlocks.

the class WorldGeneratorManager method refresh.

public void refresh() {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    List<WorldGeneratorInfo> infos = Lists.newArrayList();
    for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
        Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
        if (module.isCodeModule()) {
            DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
            ResolutionResult resolutionResult = resolver.resolve(module.getId());
            if (resolutionResult.isSuccess()) {
                try (ModuleEnvironment tempEnvironment = moduleManager.loadEnvironment(resolutionResult.getModules(), false)) {
                    for (Class<?> generatorClass : tempEnvironment.getTypesAnnotatedWith(RegisterWorldGenerator.class)) {
                        if (tempEnvironment.getModuleProviding(generatorClass).equals(module.getId())) {
                            RegisterWorldGenerator annotation = generatorClass.getAnnotation(RegisterWorldGenerator.class);
                            if (isValidWorldGenerator(generatorClass)) {
                                SimpleUri uri = new SimpleUri(moduleId, annotation.id());
                                infos.add(new WorldGeneratorInfo(uri, annotation.displayName(), annotation.description()));
                            } else {
                                logger.error("{} marked to be registered as a World Generator, but is not a subclass of WorldGenerator or lacks the correct constructor", generatorClass);
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.error("Error loading world generator in module {}, skipping", module.getId(), e);
                }
            } else {
                logger.warn("Could not resolve dependencies for module: {}", module);
            }
        }
    }
    Collections.sort(infos);
    generatorInfo = ImmutableList.copyOf(infos);
}
Also used : RegisterWorldGenerator(org.terasology.world.generator.RegisterWorldGenerator) ModuleEnvironment(org.terasology.module.ModuleEnvironment) ResolutionResult(org.terasology.module.ResolutionResult) SimpleUri(org.terasology.engine.SimpleUri) ModuleManager(org.terasology.engine.module.ModuleManager) Module(org.terasology.module.Module) UnresolvedWorldGeneratorException(org.terasology.world.generator.UnresolvedWorldGeneratorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Name(org.terasology.naming.Name) DependencyResolver(org.terasology.module.DependencyResolver)

Example 13 with ModuleManager

use of org.terasology.engine.module.ModuleManager in project Terasology by MovingBlocks.

the class WorldGeneratorManager method createGenerator.

/**
 * @param uri uri of the world generator to create.
 * @param context objects from this context will be injected into the
 * @return The instantiated world generator.
 */
public static WorldGenerator createGenerator(SimpleUri uri, Context context) throws UnresolvedWorldGeneratorException {
    ModuleManager moduleManager = context.get(ModuleManager.class);
    Module module = moduleManager.getEnvironment().get(uri.getModuleName());
    if (module == null) {
        DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
        ResolutionResult result = resolver.resolve(uri.getModuleName());
        if (!result.isSuccess()) {
            if (moduleManager.getRegistry().getLatestModuleVersion(uri.getModuleName()) == null) {
                throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - not found");
            } else {
                throw new UnresolvedWorldGeneratorException("Unable to resolve world generator '" + uri + "' - unable to resolve module dependencies");
            }
        }
        try (ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), false)) {
            return createWorldGenerator(uri, context, environment);
        }
    } else {
        return createWorldGenerator(uri, context, moduleManager.getEnvironment());
    }
}
Also used : UnresolvedWorldGeneratorException(org.terasology.world.generator.UnresolvedWorldGeneratorException) ModuleEnvironment(org.terasology.module.ModuleEnvironment) ResolutionResult(org.terasology.module.ResolutionResult) ModuleManager(org.terasology.engine.module.ModuleManager) Module(org.terasology.module.Module) DependencyResolver(org.terasology.module.DependencyResolver)

Example 14 with ModuleManager

use of org.terasology.engine.module.ModuleManager 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 15 with ModuleManager

use of org.terasology.engine.module.ModuleManager 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

ModuleManager (org.terasology.engine.module.ModuleManager)25 ModuleEnvironment (org.terasology.module.ModuleEnvironment)10 Name (org.terasology.naming.Name)8 ModuleAwareAssetTypeManager (org.terasology.assets.module.ModuleAwareAssetTypeManager)7 Module (org.terasology.module.Module)7 DependencyResolver (org.terasology.module.DependencyResolver)6 ResolutionResult (org.terasology.module.ResolutionResult)6 ContextImpl (org.terasology.context.internal.ContextImpl)5 Prefab (org.terasology.entitySystem.prefab.Prefab)5 PrefabData (org.terasology.entitySystem.prefab.PrefabData)5 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)5 BeforeClass (org.junit.BeforeClass)4 TypeSerializationLibrary (org.terasology.persistence.typeHandling.TypeSerializationLibrary)4 Before (org.junit.Before)3 Config (org.terasology.config.Config)3 GameEngine (org.terasology.engine.GameEngine)3 Map (java.util.Map)2 ComponentSystemManager (org.terasology.engine.ComponentSystemManager)2 SimpleUri (org.terasology.engine.SimpleUri)2 EnvironmentSwitchHandler (org.terasology.engine.bootstrap.EnvironmentSwitchHandler)2