Make DependencyLoader implementations non-static and use #getDataFolder

The data directory location for plugins might be configured to another path, so instead we the
plugin's *DataFolder* as a basis for our Core's data directory
This commit is contained in:
Christian Koop 2024-02-02 18:05:32 +01:00
parent 7441a1e9e8
commit f7e46be0d9
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
3 changed files with 22 additions and 13 deletions

View File

@ -133,7 +133,7 @@ public abstract class SongodaPlugin extends JavaPlugin {
);
//Load plugin dependencies
DependencyLoader.loadDependencies(dependencies);
new DependencyLoader(this).loadDependencies(dependencies);
this.config = new Config(this);
onPluginLoad();

View File

@ -112,7 +112,7 @@ public final class PluginInfo {
return coreLibraryVersion;
}
public String getDependencyVersion() {
public int getDependencyVersion() {
return DependencyLoader.DEPENDENCY_VERSION;
}
}

View File

@ -1,9 +1,12 @@
package com.craftaro.core.dependency;
import com.craftaro.core.CraftaroCoreConstants;
import com.craftaro.core.SongodaCore;
import com.georgev22.api.libraryloader.LibraryLoader;
import me.lucko.jarrelocator.JarRelocator;
import me.lucko.jarrelocator.Relocation;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.ApiStatus;
import java.io.File;
import java.io.FileOutputStream;
@ -19,20 +22,26 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class DependencyLoader {
public static final String DEPENDENCY_VERSION = "v1";
private static final LibraryLoader libraryLoader = new LibraryLoader(DependencyLoader.class.getClassLoader(), new File("plugins/CraftaroCore/dependencies/" + DEPENDENCY_VERSION), SongodaCore.getLogger());
@ApiStatus.Internal
public static final int DEPENDENCY_VERSION = 1;
public static LibraryLoader getLibraryLoader() {
return libraryLoader;
private final LibraryLoader libraryLoader;
public DependencyLoader(Plugin plugin) {
this.libraryLoader = new LibraryLoader(
DependencyLoader.class.getClassLoader(),
new File(plugin.getDataFolder().getParentFile(), CraftaroCoreConstants.getProjectName() + "/dependencies/v" + DEPENDENCY_VERSION),
SongodaCore.getLogger()
);
}
public static void loadDependencies(Set<Dependency> dependencies) {
public void loadDependencies(Set<Dependency> dependencies) {
for (Dependency dependency : dependencies) {
loadDependency(dependency);
}
}
public static void loadDependency(Dependency dependency) {
public void loadDependency(Dependency dependency) {
String repositoryUrl = dependency.getRepositoryUrl();
String groupId = dependency.getGroupId();
String artifactId = dependency.getArtifactId();
@ -41,7 +50,7 @@ public class DependencyLoader {
//Check if we have the dependency downloaded already
String name = dependency.getArtifactId() + "-" + dependency.getVersion();
File outputFile = new File(libraryLoader.getLibFolder(), dependency.getGroupId().replace(".", File.separator) + File.separator + dependency.getArtifactId().replace(".", File.separator) + File.separator + dependency.getVersion() + File.separator + "raw-" + name + ".jar");
File outputFile = new File(this.libraryLoader.getLibFolder(), dependency.getGroupId().replace(".", File.separator) + File.separator + dependency.getArtifactId().replace(".", File.separator) + File.separator + dependency.getVersion() + File.separator + "raw-" + name + ".jar");
File relocatedFile = new File(outputFile.getParentFile(), name.replace("raw-", "") + ".jar");
if (relocatedFile.exists()) {
//Check if the file is already loaded to the classpath
@ -91,7 +100,7 @@ public class DependencyLoader {
}
}
public static void loadJarIntoClasspath(File file, Dependency dependency) {
public void loadJarIntoClasspath(File file, Dependency dependency) {
if (!isRelocated(file) && dependency.shouldRelocate()) {
SongodaCore.getLogger().info("Loading dependency for relocation " + file);
//relocate package to com.craftaro.core.third_party to avoid conflicts
@ -121,14 +130,14 @@ public class DependencyLoader {
}
try {
libraryLoader.load(new LibraryLoader.Dependency(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getRepositoryUrl()), true);
this.libraryLoader.load(new LibraryLoader.Dependency(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getRepositoryUrl()), true);
} catch (Exception ignored) {
//already loaded
}
SongodaCore.getLogger().info("----------------------------");
}
private static boolean isRelocated(File file) {
private boolean isRelocated(File file) {
try (ZipFile zipFile = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
@ -144,7 +153,7 @@ public class DependencyLoader {
return false;
}
private static boolean isLoaded(File file) {
private boolean isLoaded(File file) {
//Find the first class file in the jar and try Class.forName
try (ZipFile zipFile = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();