Skip to content

Commit

Permalink
Also capture recursively loaded textures
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Aug 26, 2023
1 parent 2c82194 commit 832f2a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class DynamicModelProvider implements IRegistry<ResourceLocation, IModel>
.softValues()
.build();

public static Set<ResourceLocation> textureCapturer = null;

private final Map<ResourceLocation, ResourceLocation> sideChannelAliases = new Object2ObjectOpenHashMap<>();
public static final boolean HIDE_MODEL_ERRORS = MixinConfigPlugin.isMixinClassApplied("mixin.dynamic_resources.hide_model_exceptions.ModelErrorMixin");

Expand Down Expand Up @@ -65,13 +67,20 @@ public IModel getObject(ResourceLocation location) {
} catch(Exception e) {
throw new RuntimeException(e);
}
if(textureCapturer != null && opt.isPresent()) {
textureCapturer.addAll(opt.get().getTextures());
}
loadedModels.put(location, opt);
}
}
}
return opt.orElse(null);
}

public void clearCache() {
loadedModels.invalidateAll();
}

public IModel getModelOrMissing(ResourceLocation location) {
try {
return getObject(location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private void tryStitchAndDropTexture(Stitcher stitcher) {
while(true) {
stitchSuccess = false;
try {
if(!haveTriedFallbackPass)
throw new StitcherException(null, "force fallback pass");
stitcher.doStitch();
stitchSuccess = true;
} catch(StitcherException ignored) {}
Expand All @@ -101,8 +103,11 @@ private void tryStitchAndDropTexture(Stitcher stitcher) {
if(!haveTriedFallbackPass) {
VintageFix.LOGGER.warn("Failed to fit all textures using greedy approach! Will try slow, scan-all-models fallback now...");
Set<ResourceLocation> allTextures = new HashSet<>();
// capture textures into this set
DynamicModelProvider.textureCapturer = allTextures;
DynamicModelProvider.instance.clearCache();
ProgressManager.ProgressBar bar = ProgressManager.push("Fallback texture gathering", 1);
/* try to load all models, gather expected list of textures, and drop all not matching */
/* try to load all models */
Set<ResourceLocation> loaded = new HashSet<>();
ConcurrentLinkedQueue<ResourceLocation> toLoad = new ConcurrentLinkedQueue<>(ModelLocationInformation.allKnownModelLocations);
ResourceLocation nextLoad;
Expand All @@ -113,7 +118,6 @@ private void tryStitchAndDropTexture(Stitcher stitcher) {
try {
IModel theModel = DynamicModelProvider.instance.getObject(nextLoad);
if(theModel != null) {
allTextures.addAll(theModel.getTextures());
for(ResourceLocation dep : theModel.getDependencies()) {
if(!loaded.contains(dep))
toLoad.add(dep);
Expand All @@ -124,7 +128,10 @@ private void tryStitchAndDropTexture(Stitcher stitcher) {
}
bar.step("");
ProgressManager.pop(bar);
// stop capturing
DynamicModelProvider.textureCapturer = null;
haveTriedFallbackPass = true;
// drop all other textures
((IDroppingStitcher)stitcher).retainAllSprites(allTextures);
} else {
/* drop largest sprite */
Expand Down

0 comments on commit 832f2a2

Please sign in to comment.