use of org.spongepowered.vanilla.launch.plugin.VanillaDummyPluginContainer 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!");
}
}
Aggregations