Skip to content

Commit

Permalink
Fix quadratic-time zip file scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
soir20 committed Jan 7, 2024
1 parent a0de177 commit 003b70d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ public void start() throws InvalidPluginException,
cache.load(repository, currentPackIds, "textures", "optifine");
METADATA_REGISTRY.set(cache.get(currentPackIds));


ResourceLocation packIcon = new ResourceLocation(MODID, "pack.png");

return new SpriteFrameSizeFixPack(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@
package io.github.moremcmeta.moremcmeta.impl.client.resource;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand All @@ -40,6 +46,8 @@
public class OrderedResourceRepository {
private final PackType RESOURCE_TYPE;
private final ImmutableList<ResourceCollection> COLLECTIONS;
private final Lock COLLECTIONS_BY_NAMESPACE_LOCK;
private ImmutableMap<String, List<ResourceCollectionResult>> collectionsByNamespace;

/**
* Creates a new ordered group of {@link ResourceCollection}s.
Expand All @@ -55,6 +63,7 @@ public OrderedResourceRepository(PackType resourceType,
}

COLLECTIONS = ImmutableList.copyOf(resourceCollections);
COLLECTIONS_BY_NAMESPACE_LOCK = new ReentrantLock();
}

/**
Expand Down Expand Up @@ -130,13 +139,27 @@ public Set<ResourceLocation> list(String pathStart, Predicate<String> fileFilter
* @return all collections that have resources in the given namespace
*/
private List<ResourceCollectionResult> collectionsByNamespace(String namespace) {
COLLECTIONS_BY_NAMESPACE_LOCK.lock();

// Filtering collections when resources requested, rather than in constructor, avoids inf. recursion with other mods
if (collectionsByNamespace == null) {
Map<String, List<ResourceCollectionResult>> collections = new HashMap<>();

IntStream.range(0, COLLECTIONS.size())
.mapToObj((index) -> new ResourceCollectionResult(COLLECTIONS.get(index), index))
.forEach((collection) -> collection.collection().namespaces(RESOURCE_TYPE)
.forEach((collectorNamespace) -> collections.computeIfAbsent(
collectorNamespace,
(key) -> new ArrayList<>()).add(collection)
)
);

collectionsByNamespace = ImmutableMap.copyOf(collections);
}

// Filter collections when resources requested, rather than constructor, avoids inf. recursion with other mods
return IntStream.range(0, COLLECTIONS.size())
.mapToObj((index) -> new ResourceCollectionResult(COLLECTIONS.get(index), index))
.filter((collection) -> collection.collection().namespaces(RESOURCE_TYPE).contains(namespace))
.toList();
COLLECTIONS_BY_NAMESPACE_LOCK.unlock();

return collectionsByNamespace.getOrDefault(namespace, ImmutableList.of());
}

/**
Expand Down

0 comments on commit 003b70d

Please sign in to comment.