diff --git a/expansion/common/src/main/java/me/hsgamer/hscore/expansion/common/ExpansionManager.java b/expansion/common/src/main/java/me/hsgamer/hscore/expansion/common/ExpansionManager.java index 57387de01a..644ab90a42 100644 --- a/expansion/common/src/main/java/me/hsgamer/hscore/expansion/common/ExpansionManager.java +++ b/expansion/common/src/main/java/me/hsgamer/hscore/expansion/common/ExpansionManager.java @@ -7,10 +7,7 @@ import java.io.File; import java.util.*; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.UnaryOperator; +import java.util.function.*; import java.util.jar.JarFile; import java.util.stream.Collectors; @@ -83,6 +80,11 @@ public class ExpansionManager { @NotNull private UnaryOperator> sortAndFilterFunction = UnaryOperator.identity(); + /** + * The function to remap the expansion file + */ + private BiFunction> remapper = (file, description) -> Optional.empty(); + /** * Create a new expansion manager * @@ -197,6 +199,15 @@ public void setExpansionFactory(@NotNull final Function> remapper) { + this.remapper = remapper; + } + /** * Load all expansions from the expansion directory. Also call {@link Expansion#onLoad()} */ @@ -208,17 +219,27 @@ public void loadExpansions() { .filter(file -> file.isFile() && file.getName().toLowerCase(Locale.ROOT).endsWith(".jar")) .forEach(file -> { ExpansionDescription description; - ExpansionClassLoader loader; try (final JarFile jar = new JarFile(file)) { description = descriptionFactory.apply(jar); - if (initClassLoaders.containsKey(description.getName())) { - return; - } - loader = new ExpansionClassLoader(this, file, description, this.parentClassLoader); } catch (final Exception e) { exceptionHandler.accept(new InvalidExpansionFileException("Cannot load expansion file " + file.getName(), file, e)); return; } + + if (initClassLoaders.containsKey(description.getName())) { + return; + } + + File finalFile = this.remapper.apply(file, description).orElse(file); + + ExpansionClassLoader loader; + try { + loader = new ExpansionClassLoader(this, finalFile, description, this.parentClassLoader); + } catch (final Exception e) { + exceptionHandler.accept(new InvalidExpansionFileException("Cannot load expansion file " + file.getName(), file, e)); + return; + } + initClassLoaders.put(description.getName(), loader); });