use of org.spongepowered.plugin.metadata.builtin.MetadataContainer in project SpongeCommon by SpongePowered.
the class VanillaLaunch method createPlatformPlugins.
protected final void createPlatformPlugins() {
final String metadataFileLocation = this.pluginPlatform.metadataFilePath();
try {
// This is a bit nasty, but allows Sponge to detect builtin platform plugins when it's not the first entry on the classpath.
final URL classUrl = VanillaLaunch.class.getResource("/" + VanillaLaunch.class.getName().replace('.', '/') + ".class");
MetadataContainer read = null;
// In production, let's try to ensure we can find our descriptor even if we're not first on the classpath
if (classUrl.getProtocol().equals("jar")) {
// Extract the path of the underlying jar file, and parse it as a path to normalize it
final String[] classUrlSplit = classUrl.getPath().split("!");
final Path expectedFile = Paths.get(new URL(classUrlSplit[0]).toURI());
// Then go through every possible resource
final Enumeration<URL> manifests = VanillaLaunch.class.getClassLoader().getResources("/" + metadataFileLocation);
while (manifests.hasMoreElements()) {
final URL next = manifests.nextElement();
if (!next.getProtocol().equals("jar")) {
continue;
}
// And stop when the normalized jar in that resource matches the URL of the jar that loaded VanillaLaunch?
final String[] pathSplit = next.getPath().split("!");
if (pathSplit.length == 2) {
final Path vanillaPath = Paths.get(new URL(pathSplit[0]).toURI());
if (vanillaPath.equals(expectedFile)) {
try (final Reader reader = new InputStreamReader(next.openStream(), StandardCharsets.UTF_8)) {
read = MetadataParser.read(reader);
}
break;
}
}
}
}
if (read == null) {
// other measures failed, fall back to directly querying the classpath
final Path vanillaPath = Paths.get(VanillaLaunch.class.getResource("/" + metadataFileLocation).toURI());
try (final Reader reader = Files.newBufferedReader(vanillaPath, StandardCharsets.UTF_8)) {
read = MetadataParser.read(reader);
}
}
if (read == null) {
throw new RuntimeException("Could not determine location for implementation metadata!");
}
for (final PluginMetadata metadata : read.metadata()) {
this.pluginManager().addPlugin(new VanillaDummyPluginContainer(metadata, this.logger(), this));
}
} catch (final IOException | URISyntaxException e) {
throw new RuntimeException("Could not load metadata information for the implementation! This should be impossible!");
}
}
use of org.spongepowered.plugin.metadata.builtin.MetadataContainer in project SpongeCommon by SpongePowered.
the class ModFileParsers method pluginMetadataParser.
public static IModFileInfo pluginMetadataParser(final String fileName, final IModFile iModFile) {
final ModFile modFile = (ModFile) iModFile;
AppLaunch.logger().debug("Considering plugin file candidate {}", modFile.getFilePath());
final Path metadataFile = modFile.getLocator().findPath(modFile, "META-INF/" + fileName + ".json");
if (Files.notExists(metadataFile)) {
AppLaunch.logger().debug("Plugin file '{}' is missing a 'sponge_plugins.json' metadata file in META-INF", modFile);
return null;
}
try {
final MetadataContainer container;
try (final Reader reader = Files.newBufferedReader(metadataFile, StandardCharsets.UTF_8)) {
container = MetadataParser.read(reader);
}
final PluginFileConfigurable configurable = new PluginFileConfigurable(container);
return ModFileParsers.generateModFileMetadata(modFile, configurable);
} catch (final Exception e) {
AppLaunch.logger().warn("Could not read metadata for plugin file '{}'", modFile, e);
return null;
}
}
Aggregations