Skip to content

Commit

Permalink
Initialize language adapters lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed May 1, 2024
1 parent c4d7633 commit d870131
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
19 changes: 12 additions & 7 deletions src/main/java/net/fabricmc/loader/impl/FabricLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -50,7 +52,7 @@ public final class FabricLoaderImpl extends net.fabricmc.loader.FabricLoader {
private final List<ModContainerImpl> mods = new ArrayList<>();
private final Multimap<String, String> modAliases = HashMultimap.create();

private final Map<String, LanguageAdapter> adapterMap = new HashMap<>();
private final Map<String, Supplier<LanguageAdapter>> adapterMap = new HashMap<>();
private final EntrypointStorage entrypointStorage = new EntrypointStorage();

private final ObjectShare objectShare = new ObjectShareImpl();
Expand Down Expand Up @@ -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
Expand All @@ -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<LanguageAdapter> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -98,11 +93,11 @@ public Collection<Object> getInstances() {

private static final class NewEntry implements Entry {
private final ModContainerImpl mod;
private final LanguageAdapter adapter;
private final Supplier<LanguageAdapter> adapter;
private final String value;
private final Map<Class<?>, Object> instanceMap;

NewEntry(ModContainerImpl mod, LanguageAdapter adapter, String value) {
NewEntry(ModContainerImpl mod, Supplier<LanguageAdapter> adapter, String value) {
this.mod = mod;
this.adapter = adapter;
this.value = value;
Expand All @@ -121,7 +116,7 @@ public synchronized <T> T getOrCreate(Class<T> 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;
Expand Down Expand Up @@ -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<String, LanguageAdapter> adapterMap) throws Exception {
public void add(ModContainerImpl modContainer, String key, EntrypointMetadata metadata, Map<String, Supplier<LanguageAdapter>> adapterMap) throws Exception {
if (!adapterMap.containsKey(metadata.getAdapter())) {
throw new Exception("Could not find adapter '" + metadata.getAdapter() + "' (mod " + modContainer.getMetadata().getId() + "!)");
}
Expand Down

0 comments on commit d870131

Please sign in to comment.