Skip to content

Commit

Permalink
Prevent repeated queries for the same model file if it's determined t…
Browse files Browse the repository at this point in the history
…o not exist
  • Loading branch information
Sollace committed Apr 20, 2023
1 parent b75430f commit f8a353c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/com/minelittlepony/mson/impl/ModelFoundry.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ModelFoundry implements ModelLoader {

private final MsonImpl mson;

private static final CompletableFuture<FileContent<?>> EMPTY_FILE = CompletableFuture.completedFuture(FileContent.empty());

public ModelFoundry(MsonImpl mson) {
this.mson = mson;
}
Expand Down Expand Up @@ -95,7 +97,6 @@ public CompletableFuture<FileContent<?>> loadModel(Identifier id, Identifier fil

public CompletableFuture<FileContent<?>> loadModel(Map.Entry<Identifier, Resource> entry) {
String extension = Files.getFileExtension(entry.getKey().getPath());
System.out.println("Loading Model: " + entry.getKey());
return loadModel(
entry.getKey().withPath(p -> p.replace("models/entity/", "").replace("." + extension, "")),
entry.getKey(),
Expand All @@ -105,6 +106,11 @@ public CompletableFuture<FileContent<?>> loadModel(Map.Entry<Identifier, Resourc

@Override
public CompletableFuture<FileContent<?>> loadModel(Identifier modelId, @Nullable ModelFormat<?> preferredFormat) {
synchronized (loadedFiles) {
if (loadedFiles.containsKey(modelId)) {
return loadedFiles.get(modelId);
}
}
Identifier file = new Identifier(modelId.getNamespace(), "models/entity/" + modelId.getPath());

Map<Identifier, Resource> resources = getResourceManager().findResources("models/entity", id -> {
Expand All @@ -115,7 +121,10 @@ public CompletableFuture<FileContent<?>> loadModel(Identifier modelId, @Nullable
MsonImpl.LOGGER.warn("Ambiguous reference: {} could refer to [{}]", file, String.join(",", resources.keySet().stream().map(Identifier::toString).toArray(String[]::new)));
}
if (resources.isEmpty()) {
return CompletableFuture.completedFuture(FileContent.empty());
synchronized (loadedFiles) {
loadedFiles.put(modelId, EMPTY_FILE);
return EMPTY_FILE;
}
}
Identifier first = resources.keySet().stream().sorted().toList().get(0);
if (preferredFormat != null) {
Expand Down

0 comments on commit f8a353c

Please sign in to comment.