use of org.spongepowered.plugin.meta.PluginMetadata in project SpongeAPI by SpongePowered.
the class PluginProcessor method finish.
private void finish() {
if (!this.meta.isEmpty()) {
getMessager().printMessage(WARNING, "The following extra plugin IDs were not found: " + this.meta.keySet());
}
List<PluginMetadata> meta = this.plugins.values().stream().map(PluginElement::getMetadata).collect(Collectors.toList());
meta.addAll(this.meta.values());
try (BufferedWriter writer = createWriter()) {
McModInfo.DEFAULT.write(writer, meta);
} catch (IOException e) {
throw new PluginProcessException("Failed to write plugin metadata", e);
}
}
use of org.spongepowered.plugin.meta.PluginMetadata in project SpongeAPI by SpongePowered.
the class PluginProcessor method init.
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
String extraFiles = processingEnv.getOptions().get(EXTRA_FILES_OPTION);
if (extraFiles != null && !extraFiles.isEmpty()) {
for (String file : FILE_SPLITTER.split(extraFiles)) {
Path path = Paths.get(file);
getMessager().printMessage(NOTE, "Reading extra plugin metadata from " + path);
try {
for (PluginMetadata meta : McModInfo.DEFAULT.read(path)) {
PluginMetadata base = this.meta.putIfAbsent(meta.getId(), meta);
if (base != null) {
base.accept(meta);
}
}
} catch (IOException e) {
throw new PluginProcessException("Failed to read extra plugin metadata from " + path, e);
}
}
}
String outputFile = processingEnv.getOptions().get(OUTPUT_FILE_OPTION);
if (outputFile != null && !outputFile.isEmpty()) {
this.outputPath = Paths.get(outputFile);
}
}
use of org.spongepowered.plugin.meta.PluginMetadata in project LanternServer by LanternPowered.
the class LanternPluginManager method loadPlugin.
private void loadPlugin(PluginCandidate candidate) {
final String id = candidate.getId();
final LanternClassLoader classLoader = LanternClassLoader.get();
if (candidate.getSource().isPresent()) {
try {
classLoader.addBaseURL(candidate.getSource().get().toUri().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException("Failed to add plugin '" + id + "' from " + candidate.getDisplaySource() + " to classpath", e);
}
}
final PluginMetadata metadata = candidate.getMetadata();
checkNotNull(metadata, "metadata");
final String name = firstNonNull(metadata.getName(), id);
final String version = firstNonNull(metadata.getVersion(), "unknown");
try {
final Class<?> pluginClass = Class.forName(candidate.getPluginClass());
final PluginContainer container = new LanternPluginContainer(id, this.injector, pluginClass, metadata, candidate.getSource().orElse(null));
registerPlugin(container);
registerPluginInstance(container);
this.eventManager.registerListeners(container, container.getInstance().get());
this.logger.info("Loaded plugin: {} {} (from {})", name, version, candidate.getDisplaySource());
} catch (Throwable e) {
this.logger.error("Failed to load plugin: {} {} (from {})", name, version, candidate.getDisplaySource(), e);
}
}
use of org.spongepowered.plugin.meta.PluginMetadata in project LanternServer by LanternPowered.
the class PluginScanner method scanJar.
private void scanJar(Path path, boolean classpath) {
logger.trace("Scanning {} for plugins", path);
final List<PluginCandidate> candidates = new ArrayList<>();
List<PluginMetadata> metadata = null;
// Open the zip file so we can scan it for plugins
try (JarInputStream jar = new JarInputStream(new BufferedInputStream(Files.newInputStream(path)))) {
ZipEntry entry = jar.getNextEntry();
if (entry == null) {
return;
}
Manifest manifest = jar.getManifest();
if (manifest == null) {
// Try harder to find it anyway - Wtf Java?
try (JarFile jarFile = new JarFile(path.toFile())) {
manifest = jarFile.getManifest();
}
}
if (manifest == null && !classpath) {
// TODO
logger.warn("Missing JAR manifest in {}", path);
}
do {
if (entry.isDirectory()) {
continue;
}
final String name = entry.getName();
if (!name.endsWith(CLASS_EXTENSION)) {
if (name.equals(METADATA_FILE)) {
try {
metadata = McModInfo.DEFAULT.read(jar);
} catch (IOException e) {
logger.error("Failed to read plugin metadata from " + METADATA_FILE + " in {}", path, e);
return;
}
}
continue;
}
final PluginCandidate candidate = scanClassFile(jar, path);
if (candidate != null) {
candidates.add(candidate);
}
} while ((entry = jar.getNextEntry()) != null);
} catch (IOException e) {
logger.error("Failed to scan plugin JAR: {}", path, e);
return;
}
if (!candidates.isEmpty()) {
boolean success = false;
for (PluginCandidate candidate : candidates) {
success |= addCandidate(candidate);
// Find matching plugin metadata
if (metadata != null) {
boolean found = false;
for (PluginMetadata meta : metadata) {
if (candidate.getId().equals(meta.getId())) {
found = true;
candidate.setMetadata(meta);
break;
}
}
if (!found) {
logger.warn("No matching metadata found for plugin '{}' in " + METADATA_FILE + " from {}", candidate.getId(), path);
}
}
}
if (!success) {
logger.warn("{} is missing a valid {} file." + "This is not a problem when testing plugins, however it is recommended to include one in public plugins.\n" + "Please see https://docs.spongepowered.org/master/en/plugin/plugin-meta.html for details.", path, METADATA_FILE);
}
} else if (!classpath) {
logger.error("No valid plugins found in {}. Is the file actually a plugin JAR? Please keep in" + "mind that Lantern can only load Sponge plugins.", path);
}
}
use of org.spongepowered.plugin.meta.PluginMetadata in project LanternServer by LanternPowered.
the class PluginScanner method scanClassFile.
private PluginCandidate scanClassFile(InputStream in, @Nullable Path source) throws IOException {
final ClassReader reader = new ClassReader(in);
final PluginClassVisitor visitor = new PluginClassVisitor();
try {
reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
final PluginMetadata metadata = visitor.getMetadata();
if (metadata == null) {
// Not a plugin class
return null;
}
return new PluginCandidate(visitor.getClassName().replace('/', '.'), source, metadata);
} catch (InvalidPluginException e) {
logger.error("Skipping invalid plugin {} from {}", visitor.getClassName(), source, e);
}
return null;
}
Aggregations