Use Guava multimap for resource finder instead of custom "multi"map
This commit is contained in:
parent
4f186c03f5
commit
9fbb97d0fa
|
|
@ -1,5 +1,6 @@
|
||||||
package org.embeddedt.modernfix.forge.classloading;
|
package org.embeddedt.modernfix.forge.classloading;
|
||||||
|
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
import com.google.common.collect.*;
|
import com.google.common.collect.*;
|
||||||
import net.minecraftforge.fml.loading.FMLLoader;
|
import net.minecraftforge.fml.loading.FMLLoader;
|
||||||
import net.minecraftforge.fml.loading.LoadingModList;
|
import net.minecraftforge.fml.loading.LoadingModList;
|
||||||
|
|
@ -22,7 +23,7 @@ import java.util.regex.Pattern;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class ModernFixResourceFinder {
|
public class ModernFixResourceFinder {
|
||||||
private static Map<String, List<Pair<String, String>>> urlsForClass = null;
|
private static Multimap<String, String> urlsForClass = null;
|
||||||
private static final Class<? extends IModLocator> MINECRAFT_LOCATOR;
|
private static final Class<? extends IModLocator> MINECRAFT_LOCATOR;
|
||||||
private static Field explodedDirModsField = null;
|
private static Field explodedDirModsField = null;
|
||||||
private static final Logger LOGGER = LogManager.getLogger("ModernFixResourceFinder");
|
private static final Logger LOGGER = LogManager.getLogger("ModernFixResourceFinder");
|
||||||
|
|
@ -35,8 +36,10 @@ public class ModernFixResourceFinder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Joiner SLASH_JOINER = Joiner.on('/');
|
||||||
|
|
||||||
public static synchronized void init() throws ReflectiveOperationException {
|
public static synchronized void init() throws ReflectiveOperationException {
|
||||||
urlsForClass = new HashMap<>();
|
ImmutableMultimap.Builder<String, String> urlBuilder = ImmutableMultimap.builder();
|
||||||
Interner<String> pathInterner = Interners.newStrongInterner();
|
Interner<String> pathInterner = Interners.newStrongInterner();
|
||||||
//LOGGER.info("Start building list of class locations...");
|
//LOGGER.info("Start building list of class locations...");
|
||||||
for(ModFileInfo fileInfo : LoadingModList.get().getModFiles()) {
|
for(ModFileInfo fileInfo : LoadingModList.get().getModFiles()) {
|
||||||
|
|
@ -50,33 +53,15 @@ public class ModernFixResourceFinder {
|
||||||
stream
|
stream
|
||||||
.map(root::relativize)
|
.map(root::relativize)
|
||||||
.forEach(path -> {
|
.forEach(path -> {
|
||||||
String strPath = path.toString();
|
String strPath = pathInterner.intern(SLASH_JOINER.join(path));
|
||||||
Pair<String, String> pathPair = Pair.of(fileInfo.getMods().get(0).getModId(), pathInterner.intern(strPath));
|
urlBuilder.put(strPath, fileInfo.getMods().get(0).getModId());
|
||||||
List<Pair<String, String>> urlList = urlsForClass.get(strPath);
|
|
||||||
if(urlList != null) {
|
|
||||||
if(urlList.size() > 1)
|
|
||||||
urlList.add(pathPair);
|
|
||||||
else {
|
|
||||||
/* Convert singleton to real list */
|
|
||||||
ArrayList<Pair<String, String>> newList = new ArrayList<>(urlList);
|
|
||||||
newList.add(pathPair);
|
|
||||||
urlsForClass.put(strPath, newList);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* Use a singleton list initially to keep memory usage down */
|
|
||||||
urlsForClass.put(strPath, Collections.singletonList(pathPair));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(List<Pair<String, String>> list : urlsForClass.values()) {
|
urlsForClass = urlBuilder.build();
|
||||||
if(list instanceof ArrayList)
|
|
||||||
((ArrayList<Pair<String, String>>)list).trimToSize();
|
|
||||||
}
|
|
||||||
urlsForClass = ImmutableMap.copyOf(urlsForClass);
|
|
||||||
//LOGGER.info("Finish building");
|
//LOGGER.info("Finish building");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,12 +90,12 @@ public class ModernFixResourceFinder {
|
||||||
private static final Pattern SLASH_REPLACER = Pattern.compile("/+");
|
private static final Pattern SLASH_REPLACER = Pattern.compile("/+");
|
||||||
|
|
||||||
public static Enumeration<URL> findAllURLsForResource(String input) {
|
public static Enumeration<URL> findAllURLsForResource(String input) {
|
||||||
input = SLASH_REPLACER.matcher(input).replaceAll("/");
|
String pathInput = SLASH_REPLACER.matcher(input).replaceAll("/");
|
||||||
List<Pair<String, String>> urlList = urlsForClass.get(input);
|
Collection<String> urlList = urlsForClass.get(pathInput);
|
||||||
if(urlList != null) {
|
if(!urlList.isEmpty()) {
|
||||||
return Iterators.asEnumeration(urlList.stream().map(pair -> {
|
return Iterators.asEnumeration(urlList.stream().map(modId -> {
|
||||||
try {
|
try {
|
||||||
return new URL("modjar://" + pair.getLeft() + "/" + pair.getRight());
|
return new URL("modjar://" + modId + "/" + pathInput);
|
||||||
} catch(MalformedURLException e) {
|
} catch(MalformedURLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user