diff --git a/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java b/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java index 05a2a14..e589e35 100644 --- a/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java +++ b/src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java @@ -16,6 +16,7 @@ package net.fabricmc.loader.impl; +import com.google.common.base.Suppliers; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import cpw.mods.modlauncher.ArgumentHandler; @@ -41,6 +42,7 @@ import java.nio.file.Path; import java.util.*; import java.util.function.Consumer; +import java.util.function.Supplier; @SuppressWarnings("deprecation") public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader { @@ -50,7 +52,7 @@ public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader { private final List mods = new ArrayList<>(); private final Multimap modAliases = HashMultimap.create(); - private final Map adapterMap = new HashMap<>(); + private final Map> adapterMap = new HashMap<>(); private final EntrypointStorage entrypointStorage = new EntrypointStorage(); private final ObjectShare objectShare = new ObjectShareImpl(); @@ -229,7 +231,7 @@ public void setup() { } private void setupLanguageAdapters() { - adapterMap.put("default", DefaultLanguageAdapter.INSTANCE); + adapterMap.put("default", () -> DefaultLanguageAdapter.INSTANCE); for (ModContainerImpl mod : mods) { // add language adapters @@ -238,11 +240,14 @@ private void setupLanguageAdapters() { throw new RuntimeException("Duplicate language adapter key: " + laEntry.getKey() + "! (" + laEntry.getValue() + ", " + adapterMap.get(laEntry.getKey()).getClass().getName() + ")"); } - try { - adapterMap.put(laEntry.getKey(), (LanguageAdapter) Class.forName(laEntry.getValue(), true, Thread.currentThread().getContextClassLoader()).getDeclaredConstructor().newInstance()); - } catch (Exception e) { - throw new RuntimeException("Failed to instantiate language adapter: " + laEntry.getKey(), e); - } + Supplier supplier = Suppliers.memoize(() -> { + try { + return (LanguageAdapter) Class.forName(laEntry.getValue(), true, Thread.currentThread().getContextClassLoader()).getDeclaredConstructor().newInstance(); + } catch (Exception e) { + throw new RuntimeException("Failed to instantiate language adapter: " + laEntry.getKey(), e); + } + }); + adapterMap.put(laEntry.getKey(), supplier); } } } diff --git a/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java b/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java index 29f2db7..4030b90 100644 --- a/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java +++ b/src/main/java/net/fabricmc/loader/impl/entrypoint/EntrypointStorage.java @@ -25,13 +25,8 @@ import net.fabricmc.loader.impl.util.log.Log; import net.fabricmc.loader.impl.util.log.LogCategory; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.function.Supplier; public final class EntrypointStorage { interface Entry { @@ -98,11 +93,11 @@ public Collection getInstances() { private static final class NewEntry implements Entry { private final ModContainerImpl mod; - private final LanguageAdapter adapter; + private final Supplier adapter; private final String value; private final Map, Object> instanceMap; - NewEntry(ModContainerImpl mod, LanguageAdapter adapter, String value) { + NewEntry(ModContainerImpl mod, Supplier adapter, String value) { this.mod = mod; this.adapter = adapter; this.value = value; @@ -121,7 +116,7 @@ public synchronized T getOrCreate(Class type) throws Exception { T ret = (T) instanceMap.get(type); if (ret == null) { - ret = adapter.create(mod, value, type); + ret = adapter.get().create(mod, value, type); assert ret != null; T prev = (T) instanceMap.putIfAbsent(type, ret); if (prev != null) ret = prev; @@ -173,7 +168,7 @@ public void addDeprecated(ModContainerImpl modContainer, String adapter, String getOrCreateEntries("server").add(oe); } - public void add(ModContainerImpl modContainer, String key, EntrypointMetadata metadata, Map adapterMap) throws Exception { + public void add(ModContainerImpl modContainer, String key, EntrypointMetadata metadata, Map> adapterMap) throws Exception { if (!adapterMap.containsKey(metadata.getAdapter())) { throw new Exception("Could not find adapter '" + metadata.getAdapter() + "' (mod " + modContainer.getMetadata().getId() + "!)"); }