Skip to content

Commit 9fbb97d

Browse files
committed
Use Guava multimap for resource finder instead of custom "multi"map
1 parent 4f186c0 commit 9fbb97d

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

forge/src/main/java/org/embeddedt/modernfix/forge/classloading/ModernFixResourceFinder.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.embeddedt.modernfix.forge.classloading;
22

3+
import com.google.common.base.Joiner;
34
import com.google.common.collect.*;
45
import net.minecraftforge.fml.loading.FMLLoader;
56
import net.minecraftforge.fml.loading.LoadingModList;
@@ -22,7 +23,7 @@
2223
import java.util.stream.Stream;
2324

2425
public class ModernFixResourceFinder {
25-
private static Map<String, List<Pair<String, String>>> urlsForClass = null;
26+
private static Multimap<String, String> urlsForClass = null;
2627
private static final Class<? extends IModLocator> MINECRAFT_LOCATOR;
2728
private static Field explodedDirModsField = null;
2829
private static final Logger LOGGER = LogManager.getLogger("ModernFixResourceFinder");
@@ -35,8 +36,10 @@ public class ModernFixResourceFinder {
3536
}
3637
}
3738

39+
private static final Joiner SLASH_JOINER = Joiner.on('/');
40+
3841
public static synchronized void init() throws ReflectiveOperationException {
39-
urlsForClass = new HashMap<>();
42+
ImmutableMultimap.Builder<String, String> urlBuilder = ImmutableMultimap.builder();
4043
Interner<String> pathInterner = Interners.newStrongInterner();
4144
//LOGGER.info("Start building list of class locations...");
4245
for(ModFileInfo fileInfo : LoadingModList.get().getModFiles()) {
@@ -50,33 +53,15 @@ public static synchronized void init() throws ReflectiveOperationException {
5053
stream
5154
.map(root::relativize)
5255
.forEach(path -> {
53-
String strPath = path.toString();
54-
Pair<String, String> pathPair = Pair.of(fileInfo.getMods().get(0).getModId(), pathInterner.intern(strPath));
55-
List<Pair<String, String>> urlList = urlsForClass.get(strPath);
56-
if(urlList != null) {
57-
if(urlList.size() > 1)
58-
urlList.add(pathPair);
59-
else {
60-
/* Convert singleton to real list */
61-
ArrayList<Pair<String, String>> newList = new ArrayList<>(urlList);
62-
newList.add(pathPair);
63-
urlsForClass.put(strPath, newList);
64-
}
65-
} else {
66-
/* Use a singleton list initially to keep memory usage down */
67-
urlsForClass.put(strPath, Collections.singletonList(pathPair));
68-
}
56+
String strPath = pathInterner.intern(SLASH_JOINER.join(path));
57+
urlBuilder.put(strPath, fileInfo.getMods().get(0).getModId());
6958
});
7059
} catch(IOException e) {
7160
throw new RuntimeException(e);
7261
}
7362
}
7463
}
75-
for(List<Pair<String, String>> list : urlsForClass.values()) {
76-
if(list instanceof ArrayList)
77-
((ArrayList<Pair<String, String>>)list).trimToSize();
78-
}
79-
urlsForClass = ImmutableMap.copyOf(urlsForClass);
64+
urlsForClass = urlBuilder.build();
8065
//LOGGER.info("Finish building");
8166
}
8267

@@ -105,12 +90,12 @@ private static Iterable<Path> getRootPathForLocator(IModLocator locator, ModFile
10590
private static final Pattern SLASH_REPLACER = Pattern.compile("/+");
10691

10792
public static Enumeration<URL> findAllURLsForResource(String input) {
108-
input = SLASH_REPLACER.matcher(input).replaceAll("/");
109-
List<Pair<String, String>> urlList = urlsForClass.get(input);
110-
if(urlList != null) {
111-
return Iterators.asEnumeration(urlList.stream().map(pair -> {
93+
String pathInput = SLASH_REPLACER.matcher(input).replaceAll("/");
94+
Collection<String> urlList = urlsForClass.get(pathInput);
95+
if(!urlList.isEmpty()) {
96+
return Iterators.asEnumeration(urlList.stream().map(modId -> {
11297
try {
113-
return new URL("modjar://" + pair.getLeft() + "/" + pair.getRight());
98+
return new URL("modjar://" + modId + "/" + pathInput);
11499
} catch(MalformedURLException e) {
115100
throw new RuntimeException(e);
116101
}

0 commit comments

Comments
 (0)