use of org.terasology.module.ModuleMetadata in project Terasology by MovingBlocks.
the class SelectModulesScreen method initialise.
@Override
public void initialise() {
setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
remoteModuleRegistryUpdater = Executors.newSingleThreadExecutor().submit(moduleManager.getInstallManager().updateRemoteRegistry());
selectModulesConfig = config.getSelectModulesConfig();
resolver = new DependencyResolver(moduleManager.getRegistry());
modulesLookup = Maps.newHashMap();
sortedModules = Lists.newArrayList();
for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
Module latestVersion = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
if (!latestVersion.isOnClasspath()) {
ModuleSelectionInfo info = ModuleSelectionInfo.local(latestVersion);
modulesLookup.put(info.getMetadata().getId(), info);
sortedModules.add(info);
}
}
Collections.sort(sortedModules, moduleInfoComparator);
allSortedModules = new ArrayList<>(sortedModules);
final UIList<ModuleSelectionInfo> moduleList = find("moduleList", UIList.class);
if (moduleList != null) {
moduleList.setList(sortedModules);
moduleList.setItemRenderer(new AbstractItemRenderer<ModuleSelectionInfo>() {
public String getString(ModuleSelectionInfo value) {
return value.getMetadata().getDisplayName().toString();
}
@Override
public void draw(ModuleSelectionInfo value, Canvas canvas) {
if (isSelectedGameplayModule(value) && value.isValidToSelect()) {
canvas.setMode("gameplay");
} else if (value.isSelected() && value.isExplicitSelection()) {
canvas.setMode("enabled");
} else if (value.isSelected()) {
canvas.setMode("dependency");
} else if (!value.isPresent()) {
canvas.setMode("disabled");
} else if (!value.isValidToSelect()) {
canvas.setMode("invalid");
} else {
canvas.setMode("available");
}
canvas.drawText(getString(value), canvas.getRegion());
}
@Override
public Vector2i getPreferredSize(ModuleSelectionInfo value, Canvas canvas) {
String text = getString(value);
return new Vector2i(canvas.getCurrentStyle().getFont().getWidth(text), canvas.getCurrentStyle().getFont().getLineHeight());
}
});
// ItemActivateEventListener is triggered by double clicking
moduleList.subscribe((widget, item) -> {
if (item.isSelected() && moduleList.getSelection().isExplicitSelection()) {
deselect(item);
} else if (item.isValidToSelect()) {
select(item);
}
});
ResettableUIText moduleSearch = find("moduleSearch", ResettableUIText.class);
if (moduleSearch != null) {
moduleSearch.subscribe(new TextChangeEventListener() {
@Override
public void onTextChange(String oldText, String newText) {
filterText(newText);
}
});
}
final Binding<ModuleMetadata> moduleInfoBinding = new ReadOnlyBinding<ModuleMetadata>() {
@Override
public ModuleMetadata get() {
if (moduleList.getSelection() != null) {
return moduleList.getSelection().getMetadata();
}
return null;
}
};
UILabel name = find("name", UILabel.class);
if (name != null) {
name.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
if (moduleInfoBinding.get() != null) {
return moduleInfoBinding.get().getDisplayName().toString();
}
return "";
}
});
}
UILabel installedVersion = find("installedVersion", UILabel.class);
if (installedVersion != null) {
installedVersion.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo sel = moduleList.getSelection();
if (sel == null) {
return "";
}
return sel.isPresent() ? sel.getMetadata().getVersion().toString() : translationSystem.translate("${engine:menu#module-version-installed-none}");
}
});
}
UILabel onlineVersion = find("onlineVersion", UILabel.class);
if (onlineVersion != null) {
onlineVersion.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo sel = moduleList.getSelection();
if (sel == null) {
return "";
}
return (sel.getOnlineVersion() != null) ? sel.getOnlineVersion().getVersion().toString() : "none";
}
});
}
UILabel description = find("description", UILabel.class);
if (description != null) {
description.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleMetadata moduleMetadata = moduleInfoBinding.get();
if (moduleMetadata != null) {
String dependenciesNames;
List<DependencyInfo> dependencies = moduleMetadata.getDependencies();
if (dependencies != null && dependencies.size() > 0) {
dependenciesNames = translationSystem.translate("${engine:menu#module-dependencies-exist}") + ":" + '\n';
for (DependencyInfo dependency : dependencies) {
dependenciesNames += " " + dependency.getId().toString() + '\n';
}
} else {
dependenciesNames = translationSystem.translate("${engine:menu#module-dependencies-empty}") + ".";
}
return moduleMetadata.getDescription().toString() + '\n' + '\n' + dependenciesNames;
}
return "";
}
});
}
UILabel status = find("status", UILabel.class);
if (status != null) {
status.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo info = moduleList.getSelection();
if (info != null) {
if (isSelectedGameplayModule(info)) {
return translationSystem.translate("${engine:menu#module-status-activegameplay}");
} else if (info.isSelected() && info.isExplicitSelection()) {
return translationSystem.translate("${engine:menu#module-status-activated}");
} else if (info.isSelected()) {
return translationSystem.translate("${engine:menu#module-status-dependency}");
} else if (!info.isPresent()) {
return translationSystem.translate("${engine:menu#module-status-notpresent}");
} else if (info.isValidToSelect()) {
return translationSystem.translate("${engine:menu#module-status-available}");
} else {
return translationSystem.translate("${engine:menu#module-status-error}");
}
}
return "";
}
});
}
UIButton toggleActivate = find("toggleActivation", UIButton.class);
if (toggleActivate != null) {
toggleActivate.subscribe(button -> {
ModuleSelectionInfo info = moduleList.getSelection();
if (info != null) {
// Toggle
if (info.isSelected() && info.isExplicitSelection()) {
deselect(info);
} else if (info.isValidToSelect()) {
select(info);
}
}
});
toggleActivate.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
ModuleSelectionInfo info = moduleList.getSelection();
return info != null && info.isPresent() && !isSelectedGameplayModule(info) && (info.isSelected() || info.isValidToSelect());
}
});
toggleActivate.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
if (moduleList.getSelection() != null) {
if (moduleList.getSelection().isExplicitSelection()) {
return translationSystem.translate("${engine:menu#deactivate-module}");
} else {
return translationSystem.translate("${engine:menu#activate-module}");
}
}
// button should be disabled
return translationSystem.translate("${engine:menu#activate-module}");
}
});
}
UIButton downloadButton = find("download", UIButton.class);
if (downloadButton != null) {
downloadButton.subscribe(button -> {
if (moduleList.getSelection() != null) {
ModuleSelectionInfo info = moduleList.getSelection();
startDownloadingNewestModulesRequiredFor(info);
}
});
downloadButton.bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
try {
return moduleList.getSelection().getOnlineVersion() != null;
} catch (NullPointerException e) {
return false;
}
}
});
downloadButton.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
ModuleSelectionInfo info = moduleList.getSelection();
if (info != null && !info.isPresent()) {
return translationSystem.translate("${engine:menu#download-module}");
} else {
return translationSystem.translate("${engine:menu#update-module}");
}
}
});
}
UIButton disableAll = find("disableAll", UIButton.class);
if (disableAll != null) {
disableAll.subscribe(button -> sortedModules.stream().filter(info -> info.isSelected() && info.isExplicitSelection()).forEach(this::deselect));
}
localOnlyCheckbox = find("localOnly", UICheckbox.class);
localOnlyCheckbox.bindChecked(new Binding<Boolean>() {
@Override
public Boolean get() {
filterText(moduleSearch.getText());
prepareModuleList(selectModulesConfig.isChecked());
return selectModulesConfig.isChecked();
}
@Override
public void set(Boolean value) {
selectModulesConfig.setIsChecked(value);
filterText(moduleSearch.getText());
prepareModuleList(value);
}
});
}
WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
}
use of org.terasology.module.ModuleMetadata in project Terasology by MovingBlocks.
the class ModuleInstaller method getDownloadUrls.
private Map<URL, Path> getDownloadUrls(Iterable<Module> modules) {
Map<URL, Path> result = new HashMap<>();
for (Module module : modules) {
ModuleMetadata metadata = module.getMetadata();
String version = metadata.getVersion().toString();
String id = metadata.getId().toString();
URL url = RemoteModuleExtension.getDownloadUrl(metadata);
String fileName = String.format("%s-%s.jar", id, version);
Path folder = PathManager.getInstance().getHomeModPath().normalize();
Path target = folder.resolve(fileName);
result.put(url, target);
}
return result;
}
use of org.terasology.module.ModuleMetadata 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.ModuleMetadata in project Terasology by MovingBlocks.
the class ModuleManagerFactory method create.
public static ModuleManager create() throws Exception {
ModuleManager moduleManager = new ModuleManagerImpl("");
try (Reader reader = new InputStreamReader(ModuleManagerFactory.class.getResourceAsStream("/module.txt"), TerasologyConstants.CHARSET)) {
ModuleMetadata metadata = new ModuleMetadataReader().read(reader);
moduleManager.getRegistry().add(ClasspathModule.create(metadata, ModuleManagerFactory.class));
}
moduleManager.loadEnvironment(Sets.newHashSet(moduleManager.getRegistry().getLatestModuleVersion(new Name("engine"))), true);
return moduleManager;
}
use of org.terasology.module.ModuleMetadata in project Terasology by MovingBlocks.
the class ModuleDownloadListGeneratorTest method buildSimpleModule.
private Module buildSimpleModule(String id, String version) {
ModuleMetadata metadata = new ModuleMetadata();
metadata.setId(new Name(id));
if (version != null) {
metadata.setVersion(new Version(version));
}
return new BaseModule(Collections.emptyList(), metadata) {
@Override
public ImmutableList<URL> getClasspaths() {
return null;
}
@Override
public boolean isOnClasspath() {
return false;
}
@Override
public boolean isCodeModule() {
return false;
}
};
}
Aggregations