Skip to content

Commit

Permalink
add a remapper for ExpansionManager
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Nov 9, 2024
1 parent 06a5eaf commit 2c3ab05
Showing 1 changed file with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,6 +80,11 @@ public class ExpansionManager {
@NotNull
private UnaryOperator<Map<String, ExpansionClassLoader>> sortAndFilterFunction = UnaryOperator.identity();

/**
* The function to remap the expansion file
*/
private BiFunction<File, ExpansionDescription, Optional<File>> remapper = (file, description) -> Optional.empty();

/**
* Create a new expansion manager
*
Expand Down Expand Up @@ -197,6 +199,15 @@ public void setExpansionFactory(@NotNull final Function<ExpansionClassLoader, Ex
this.expansionFactory = expansionFactory;
}

/**
* Get the function to remap the expansion file
*
* @param remapper the remapper
*/
public void setRemapper(BiFunction<File, ExpansionDescription, Optional<File>> remapper) {
this.remapper = remapper;
}

/**
* Load all expansions from the expansion directory. Also call {@link Expansion#onLoad()}
*/
Expand All @@ -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);
});

Expand Down

0 comments on commit 2c3ab05

Please sign in to comment.