Minestom/src/main/java/net/minestom/server/extensions/Extension.java

194 lines
5.9 KiB
Java
Raw Normal View History

package net.minestom.server.extensions;
2021-06-08 16:25:46 +02:00
import net.minestom.server.event.Event;
import net.minestom.server.event.EventNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
2021-06-08 16:25:46 +02:00
import java.util.HashSet;
import java.util.Set;
public abstract class Extension {
/**
* List of extensions that depend on this extension.
*/
protected final Set<String> dependents = new HashSet<>();
protected Extension() {
}
public void preInitialize() {
}
public abstract void initialize();
public void postInitialize() {
}
public void preTerminate() {
}
public abstract void terminate();
public void postTerminate() {
}
ExtensionClassLoader getExtensionClassLoader() {
if (getClass().getClassLoader() instanceof ExtensionClassLoader extensionClassLoader) {
return extensionClassLoader;
}
throw new IllegalStateException("Extension class loader is not an ExtensionClassLoader");
}
2020-10-25 10:41:51 +01:00
@NotNull
public DiscoveredExtension getOrigin() {
return getExtensionClassLoader().getDiscoveredExtension();
}
/**
* Gets the logger for the extension
2021-06-08 16:25:46 +02:00
*
* @return The logger for the extension
*/
2020-10-25 10:41:51 +01:00
@NotNull
public Logger getLogger() {
return getExtensionClassLoader().getLogger();
}
2021-06-08 16:25:46 +02:00
public @NotNull EventNode<Event> getEventNode() {
return getExtensionClassLoader().getEventNode();
2021-06-08 16:25:46 +02:00
}
public @NotNull Path getDataDirectory() {
return getOrigin().getDataDirectory();
}
/**
* Gets a resource from the extension directory, or from inside the jar if it does not
* exist in the extension directory.
* <p>
* If it does not exist in the extension directory, it will be copied from inside the jar.
* <p>
* The caller is responsible for closing the returned {@link InputStream}.
*
* @param fileName The file to read
* @return The file contents, or null if there was an issue reading the file.
*/
public @Nullable InputStream getResource(@NotNull String fileName) {
return getResource(Paths.get(fileName));
}
/**
* Gets a resource from the extension directory, or from inside the jar if it does not
* exist in the extension directory.
* <p>
* If it does not exist in the extension directory, it will be copied from inside the jar.
* <p>
* The caller is responsible for closing the returned {@link InputStream}.
*
* @param target The file to read
* @return The file contents, or null if there was an issue reading the file.
*/
public @Nullable InputStream getResource(@NotNull Path target) {
final Path targetFile = getDataDirectory().resolve(target);
try {
// Copy from jar if the file does not exist in the extension data directory.
if (!Files.exists(targetFile)) {
savePackagedResource(target);
}
return Files.newInputStream(targetFile);
} catch (IOException ex) {
getLogger().info("Failed to read resource {}.", target, ex);
return null;
}
}
/**
* Gets a resource from inside the extension jar.
* <p>
* The caller is responsible for closing the returned {@link InputStream}.
*
* @param fileName The file to read
* @return The file contents, or null if there was an issue reading the file.
*/
public @Nullable InputStream getPackagedResource(@NotNull String fileName) {
2021-07-08 20:25:10 +02:00
try {
2021-11-16 18:23:15 +01:00
final URL url = getOrigin().getClassLoader().getResource(fileName);
2021-07-08 20:25:10 +02:00
if (url == null) {
getLogger().debug("Resource not found: {}", fileName);
return null;
}
return url.openConnection().getInputStream();
} catch (IOException ex) {
getLogger().debug("Failed to load resource {}.", fileName, ex);
return null;
}
}
/**
* Gets a resource from inside the extension jar.
* <p>
* The caller is responsible for closing the returned {@link InputStream}.
*
* @param target The file to read
* @return The file contents, or null if there was an issue reading the file.
*/
public @Nullable InputStream getPackagedResource(@NotNull Path target) {
2021-07-08 20:25:10 +02:00
return getPackagedResource(target.toString().replace('\\', '/'));
}
/**
* Copies a resource file to the extension directory, replacing any existing copy.
*
* @param fileName The resource to save
* @return True if the resource was saved successfully, null otherwise
*/
public boolean savePackagedResource(@NotNull String fileName) {
return savePackagedResource(Paths.get(fileName));
}
/**
* Copies a resource file to the extension directory, replacing any existing copy.
*
* @param target The resource to save
* @return True if the resource was saved successfully, null otherwise
*/
public boolean savePackagedResource(@NotNull Path target) {
final Path targetFile = getDataDirectory().resolve(target);
try (InputStream is = getPackagedResource(target)) {
if (is == null) {
return false;
}
Files.createDirectories(targetFile.getParent());
Files.copy(is, targetFile, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException ex) {
getLogger().debug("Failed to save resource {}.", target, ex);
return false;
}
}
/**
* @return A modifiable list of dependents.
*/
public Set<String> getDependents() {
return dependents;
}
}