Search in sources :

Example 6 with Module

use of org.terasology.gestalt.module.Module 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) from the following list: {}", moduleIds);
            context.get(GameEngine.class).changeState(new StateMainMenu("Missing required module or dependency"));
            return true;
        }
    }
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) ResolutionResult(org.terasology.gestalt.module.dependencyresolution.ResolutionResult) GameEngine(org.terasology.engine.core.GameEngine) ModuleManager(org.terasology.engine.core.module.ModuleManager) Module(org.terasology.gestalt.module.Module) Name(org.terasology.gestalt.naming.Name) DependencyResolver(org.terasology.gestalt.module.dependencyresolution.DependencyResolver) EnvironmentSwitchHandler(org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler)

Example 7 with Module

use of org.terasology.gestalt.module.Module in project Terasology by MovingBlocks.

the class ModuleManager method loadAndConfigureEngineModule.

/**
 * Load and configure the engine module.
 * <p>
 * The engine module is the parts of the engine which are available to be called directly
 * from other modules. Unlike other modules, engine classes are on the classpath and not
 * restricted by the ModuleClassLoader.
 * <p>
 * This function is static so it can be tested without needing a ModuleManager instance.
 *
 * @param moduleFactory used to create the module
 * @param classesOnClasspathsToAddToEngine added to the module's reflections manifest
 */
static Module loadAndConfigureEngineModule(ModuleFactory moduleFactory, List<Class<?>> classesOnClasspathsToAddToEngine) {
    // Start by creating a gestalt Module for the Java package `org.terasology.engine`.
    Module packageModule = moduleFactory.createPackageModule("org.terasology.engine");
    // We need to add reflections from our subsystems and other classes.
    Reflections packageReflections = packageModule.getModuleManifest();
    ConfigurationBuilder config = reflectionsConfigurationFrom(packageReflections);
    Collection<File> classPaths = new HashSet<>(packageModule.getClasspaths());
    for (Class<?> aClass : classesOnClasspathsToAddToEngine) {
        URL url = ClasspathHelper.forClass(aClass);
        // include this in reflections scan
        config.addUrls(url);
        // also include in Module.moduleClasspaths
        classPaths.add(urlToFile(url));
    }
    if (!config.getUrls().isEmpty()) {
        Reflections reflectionsWithSubsystems = new Reflections(config);
        packageReflections.merge(reflectionsWithSubsystems);
    }
    // TODO: expand the ModuleFactory interface to make this whole thing less awkward
    return new Module(packageModule.getMetadata(), packageModule.getResources(), classPaths, packageReflections, clazz -> packageModule.getClassPredicate().test(clazz) || config.getUrls().contains(ClasspathHelper.forClass(clazz)));
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) Module(org.terasology.gestalt.module.Module) File(java.io.File) URL(java.net.URL) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 8 with Module

use of org.terasology.gestalt.module.Module 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);
            }
        }
        gameManifest.setRegisteredBlockFamilies(serverInfo.getRegisterBlockFamilyList());
        gameManifest.setBlockIdMap(blockMap);
        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.engine.network.Server) NameVersion(org.terasology.gestalt.naming.NameVersion) GameEngine(org.terasology.engine.core.GameEngine) ModuleManager(org.terasology.engine.core.module.ModuleManager) Entry(java.util.Map.Entry) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) WorldInfo(org.terasology.engine.world.internal.WorldInfo) ServerInfoMessage(org.terasology.engine.network.ServerInfoMessage) Module(org.terasology.gestalt.module.Module) Map(java.util.Map) EnvironmentSwitchHandler(org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler)

Example 9 with Module

use of org.terasology.gestalt.module.Module in project Terasology by MovingBlocks.

the class ModuleInstaller method call.

@Override
public List<Module> call() throws Exception {
    Map<URI, Path> filesToDownload = getDownloadUrls(moduleList);
    logger.info("Started downloading {} modules", filesToDownload.size());
    MultiFileDownloader downloader = new MultiFileDownloader(filesToDownload, downloadProgressListener);
    List<Path> downloadedModulesPaths = downloader.call();
    logger.info("Module download completed, loading the new modules...");
    List<Module> newInstalledModules = new ArrayList<>(downloadedModulesPaths.size());
    for (Path filePath : downloadedModulesPaths) {
        try {
            Module module = moduleManager.registerArchiveModule(filePath);
            newInstalledModules.add(module);
        } catch (IOException e) {
            logger.warn("Could not load module {}", filePath.getFileName(), e);
        }
    }
    logger.info("Finished loading the downloaded modules");
    return newInstalledModules;
}
Also used : Path(java.nio.file.Path) MultiFileDownloader(org.terasology.engine.utilities.download.MultiFileDownloader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Module(org.terasology.gestalt.module.Module) URI(java.net.URI)

Example 10 with Module

use of org.terasology.gestalt.module.Module in project Terasology by MovingBlocks.

the class ModuleManager method loadEnvironment.

public ModuleEnvironment loadEnvironment(Set<Module> modules, boolean asPrimary) {
    Set<Module> finalModules = Sets.newLinkedHashSet(modules);
    finalModules.add(engineModule);
    ModuleEnvironment newEnvironment;
    boolean permissiveSecurityEnabled = Boolean.parseBoolean(System.getProperty(SystemConfig.PERMISSIVE_SECURITY_ENABLED_PROPERTY));
    if (permissiveSecurityEnabled) {
        newEnvironment = new ModuleEnvironment(finalModules, wrappingPermissionProviderFactory);
    } else {
        newEnvironment = new ModuleEnvironment(finalModules, permissionProviderFactory);
    }
    if (asPrimary) {
        environment = newEnvironment;
    }
    return newEnvironment;
}
Also used : ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) Module(org.terasology.gestalt.module.Module)

Aggregations

Module (org.terasology.gestalt.module.Module)51 ModuleManager (org.terasology.engine.core.module.ModuleManager)18 Name (org.terasology.gestalt.naming.Name)17 DependencyResolver (org.terasology.gestalt.module.dependencyresolution.DependencyResolver)13 ModuleMetadata (org.terasology.gestalt.module.ModuleMetadata)11 List (java.util.List)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 SimpleUri (org.terasology.engine.core.SimpleUri)9 UILabel (org.terasology.nui.widgets.UILabel)9 UIText (org.terasology.nui.widgets.UIText)9 DependencyInfo (org.terasology.gestalt.module.dependencyresolution.DependencyInfo)8 ResolutionResult (org.terasology.gestalt.module.dependencyresolution.ResolutionResult)8 Canvas (org.terasology.nui.Canvas)8 Collection (java.util.Collection)7 Comparator (java.util.Comparator)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 TranslationSystem (org.terasology.engine.i18n.TranslationSystem)7 In (org.terasology.engine.registry.In)7