mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
deduplicate dependency loading logic
This commit is contained in:
4 files changed
+83
-80
No files matched your search
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 Josua Parks (Vicarious)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonDependencies {
|
||||
|
||||
private CarbonDependencies() {
|
||||
}
|
||||
|
||||
public static void load(
|
||||
final Path cacheDir,
|
||||
final Exceptions.CheckedConsumer<Path, Throwable> addToClasspath
|
||||
) {
|
||||
final DependencyDownloader downloader = new DependencyDownloader(
|
||||
LogManager.getLogger(CarbonDependencies.class.getSimpleName()),
|
||||
cacheDir
|
||||
);
|
||||
|
||||
try (final InputStream stream = Objects.requireNonNull(
|
||||
CarbonDependencies.class.getClassLoader().getResourceAsStream("carbon-dependencies.list"),
|
||||
"Could not get InputStream for carbon-dependencies.list"
|
||||
)) {
|
||||
downloader.load(stream);
|
||||
} catch (final IOException ex) {
|
||||
throw new RuntimeException("Failed to load dependency list", ex);
|
||||
}
|
||||
|
||||
final Set<Path> resolved = downloader.resolve();
|
||||
|
||||
try {
|
||||
for (final Path dep : resolved) {
|
||||
addToClasspath.accept(dep);
|
||||
}
|
||||
} catch (final Error e) {
|
||||
throw e;
|
||||
} catch (final Throwable thr) {
|
||||
throw new RuntimeException("Failed to add dependencies to classpath", thr);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,23 +20,20 @@
|
||||
package net.draycia.carbon.fabric;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.draycia.carbon.common.util.DependencyDownloader;
|
||||
import net.draycia.carbon.common.util.CarbonDependencies;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.fabricmc.loader.launch.common.FabricLauncherBase;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
public class CarbonFabricBootstrap implements ModInitializer {
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
this.loadDependencies();
|
||||
CarbonDependencies.load(
|
||||
FabricLoader.getInstance().getConfigDir().resolve("carbonchat").resolve("libraries"),
|
||||
path -> FabricLauncherBase.getLauncher().propose(path.toUri().toURL())
|
||||
);
|
||||
|
||||
final CarbonChatFabric carbonChat = Guice.createInjector(new CarbonChatFabricModule())
|
||||
.getInstance(CarbonChatFabric.class);
|
||||
@@ -44,29 +41,4 @@ public class CarbonFabricBootstrap implements ModInitializer {
|
||||
carbonChat.onInitialize();
|
||||
}
|
||||
|
||||
private void loadDependencies() {
|
||||
final DependencyDownloader downloader = new DependencyDownloader(
|
||||
LogManager.getLogger(this.getClass().getSimpleName()),
|
||||
FabricLoader.getInstance().getConfigDir().resolve("carbonchat").resolve("libraries")
|
||||
);
|
||||
|
||||
try (final InputStream stream = Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("carbon-dependencies.list"))) {
|
||||
downloader.load(stream);
|
||||
} catch (final IOException ex) {
|
||||
throw new RuntimeException("Couldn't load dependencies", ex);
|
||||
}
|
||||
|
||||
for (final Path dep : downloader.resolve()) {
|
||||
addJarToClasspath(dep);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addJarToClasspath(final Path file) {
|
||||
try {
|
||||
FabricLauncherBase.getLauncher().propose(file.toUri().toURL());
|
||||
} catch (final MalformedURLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,12 +22,7 @@ package net.draycia.carbon.paper;
|
||||
import io.papermc.paper.plugin.loader.PluginClasspathBuilder;
|
||||
import io.papermc.paper.plugin.loader.PluginLoader;
|
||||
import io.papermc.paper.plugin.loader.library.impl.JarLibrary;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import net.draycia.carbon.common.util.DependencyDownloader;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import net.draycia.carbon.common.util.CarbonDependencies;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
@@ -36,21 +31,10 @@ public class CarbonPaperLoader implements PluginLoader {
|
||||
|
||||
@Override
|
||||
public void classloader(final PluginClasspathBuilder classpathBuilder) {
|
||||
final DependencyDownloader downloader = new DependencyDownloader(
|
||||
LogManager.getLogger(this.getClass().getSimpleName()),
|
||||
classpathBuilder.getContext().getDataDirectory().resolve("libraries")
|
||||
CarbonDependencies.load(
|
||||
classpathBuilder.getContext().getDataDirectory().resolve("libraries"),
|
||||
path -> classpathBuilder.addLibrary(new JarLibrary(path))
|
||||
);
|
||||
try (
|
||||
final InputStream stream = Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("carbon-dependencies.list"))
|
||||
) {
|
||||
downloader.load(stream);
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
for (final Path path : downloader.resolve()) {
|
||||
classpathBuilder.addLibrary(new JarLibrary(path));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,12 +29,8 @@ import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.plugin.PluginContainer;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import net.draycia.carbon.common.util.DependencyDownloader;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import net.draycia.carbon.common.util.CarbonDependencies;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
|
||||
@Plugin(
|
||||
@@ -73,7 +69,11 @@ public final class CarbonVelocityBootstrap {
|
||||
|
||||
@Subscribe
|
||||
public void onProxyInitialize(final ProxyInitializeEvent event) {
|
||||
this.loadDependencies(this.dataDirectory);
|
||||
CarbonDependencies.load(
|
||||
this.dataDirectory.resolve("libraries"),
|
||||
path -> this.proxy.getPluginManager().addToClasspath(this, path)
|
||||
);
|
||||
|
||||
this.injector = this.parentInjector.createChildInjector(
|
||||
new CarbonChatVelocityModule(this.pluginContainer, this.proxy, this.dataDirectory));
|
||||
this.injector.getInstance(CarbonChatVelocity.class).onInitialization(this);
|
||||
@@ -84,25 +84,4 @@ public final class CarbonVelocityBootstrap {
|
||||
this.injector.getInstance(CarbonChatVelocity.class).onShutdown();
|
||||
}
|
||||
|
||||
private void loadDependencies(final Path dataDir) {
|
||||
final DependencyDownloader downloader = new DependencyDownloader(
|
||||
LogManager.getLogger(this.getClass().getSimpleName()),
|
||||
dataDir.resolve("libraries")
|
||||
);
|
||||
|
||||
try (final InputStream stream = Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("carbon-dependencies.list"))) {
|
||||
downloader.load(stream);
|
||||
} catch (final IOException ex) {
|
||||
throw new RuntimeException("Couldn't load dependencies", ex);
|
||||
}
|
||||
|
||||
for (final Path dep : downloader.resolve()) {
|
||||
this.addJarToClasspath(dep);
|
||||
}
|
||||
}
|
||||
|
||||
private void addJarToClasspath(final Path file) {
|
||||
this.proxy.getPluginManager().addToClasspath(this, file);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user