Search in sources :

Example 1 with ModuleLoader

use of org.terasology.module.ModuleLoader in project Terasology by MovingBlocks.

the class ModuleManagerImpl method loadModulesFromClassPath.

/**
 * Overrides modules in modules/ with those specified via -classpath in the JVM
 */
private void loadModulesFromClassPath() {
    // Only attempt this if we're using the standard URLClassLoader
    if (ClassLoader.getSystemClassLoader() instanceof URLClassLoader) {
        URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        ModuleLoader loader = new ModuleLoader(metadataReader);
        Enumeration<URL> moduleInfosInClassPath;
        loader.setModuleInfoPath(TerasologyConstants.MODULE_INFO_FILENAME);
        // We're looking for jars on the classpath with a module.txt
        try {
            moduleInfosInClassPath = urlClassLoader.findResources(TerasologyConstants.MODULE_INFO_FILENAME.toString());
        } catch (IOException e) {
            logger.warn("Failed to search for classpath modules: {}", e);
            return;
        }
        for (URL url : Collections.list(moduleInfosInClassPath)) {
            if (!url.getProtocol().equalsIgnoreCase("jar")) {
                continue;
            }
            try {
                Reader reader = new InputStreamReader(url.openStream(), TerasologyConstants.CHARSET);
                ModuleMetadata metaData = metadataReader.read(reader);
                String displayName = metaData.getDisplayName().toString();
                Name id = metaData.getId();
                // if the display name is empty or the id is null, this probably isn't a Terasology module
                if (null == id || displayName.equalsIgnoreCase("")) {
                    logger.warn("Found a module-like JAR on the class path with no id or display name. Skipping");
                    logger.warn("{}", url);
                }
                logger.info("Loading module {} from class path at {}", displayName, url.getFile());
                // the url contains a protocol, and points to the module.txt
                // we need to trim both of those away to get the module's path
                Path path = Paths.get(url.getFile().replace("file:", "").replace("!/" + TerasologyConstants.MODULE_INFO_FILENAME, "").replace("/" + TerasologyConstants.MODULE_INFO_FILENAME, ""));
                Module module = loader.load(path);
                registry.add(module);
            } catch (IOException e) {
                logger.warn("Failed to load module.txt for classpath module {}", url);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ModuleLoader(org.terasology.module.ModuleLoader) InputStreamReader(java.io.InputStreamReader) URLClassLoader(java.net.URLClassLoader) ModuleMetadata(org.terasology.module.ModuleMetadata) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ClasspathModule(org.terasology.module.ClasspathModule) Module(org.terasology.module.Module) URL(java.net.URL) Name(org.terasology.naming.Name)

Example 2 with ModuleLoader

use of org.terasology.module.ModuleLoader in project Terasology by MovingBlocks.

the class ModuleInstaller method call.

@Override
public List<Module> call() throws Exception {
    Map<URL, 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());
    ModuleLoader loader = new ModuleLoader(moduleManager.getModuleMetadataReader());
    loader.setModuleInfoPath(TerasologyConstants.MODULE_INFO_FILENAME);
    for (Path filePath : downloadedModulesPaths) {
        try {
            Module module = loader.load(filePath);
            moduleManager.getRegistry().add(module);
            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.utilities.download.MultiFileDownloader) ModuleLoader(org.terasology.module.ModuleLoader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Module(org.terasology.module.Module) URL(java.net.URL)

Example 3 with ModuleLoader

use of org.terasology.module.ModuleLoader in project Terasology by MovingBlocks.

the class ClientConnectionHandler method receiveModule.

private void receiveModule(ChannelHandlerContext channelHandlerContext, NetData.ModuleData moduleData) {
    if (receivingModule == null) {
        joinStatus.setErrorMessage("Module download error");
        channelHandlerContext.getChannel().close();
        return;
    }
    try {
        downloadingModule.write(moduleData.getModule().toByteArray());
        lengthReceived += moduleData.getModule().size();
        joinStatus.setCurrentProgress((float) lengthReceived / receivingModule.getSize());
        if (lengthReceived == receivingModule.getSize()) {
            // finished
            downloadingModule.close();
            String moduleName = String.format("%s-%s.jar", receivingModule.getId(), receivingModule.getVersion());
            Path finalPath = PathManager.getInstance().getHomeModPath().normalize().resolve(moduleName);
            if (finalPath.normalize().startsWith(PathManager.getInstance().getHomeModPath())) {
                if (Files.exists(finalPath)) {
                    logger.error("File already exists at {}", finalPath);
                    joinStatus.setErrorMessage("Module download error");
                    channelHandlerContext.getChannel().close();
                    return;
                }
                Files.copy(tempModuleLocation, finalPath);
                ModuleLoader loader = new ModuleLoader(moduleManager.getModuleMetadataReader());
                loader.setModuleInfoPath(TerasologyConstants.MODULE_INFO_FILENAME);
                moduleManager.getRegistry().add(loader.load(finalPath));
                receivingModule = null;
                if (missingModules.isEmpty()) {
                    sendJoin(channelHandlerContext);
                }
            } else {
                logger.error("Module rejected");
                joinStatus.setErrorMessage("Module download error");
                channelHandlerContext.getChannel().close();
            }
        }
    } catch (IOException e) {
        logger.error("Error saving module", e);
        joinStatus.setErrorMessage("Module download error");
        channelHandlerContext.getChannel().close();
    }
}
Also used : Path(java.nio.file.Path) ModuleLoader(org.terasology.module.ModuleLoader) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 Path (java.nio.file.Path)3 ModuleLoader (org.terasology.module.ModuleLoader)3 URL (java.net.URL)2 Module (org.terasology.module.Module)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 ClasspathModule (org.terasology.module.ClasspathModule)1 ModuleMetadata (org.terasology.module.ModuleMetadata)1 Name (org.terasology.naming.Name)1 MultiFileDownloader (org.terasology.utilities.download.MultiFileDownloader)1