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);
}
}
}
}
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;
}
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();
}
}
Aggregations