Skip to content

Commit

Permalink
Fix texture-gallery not preserving textures that are missing after a …
Browse files Browse the repository at this point in the history
…resource(pack) change
  • Loading branch information
TBlueF committed Dec 11, 2023
1 parent ef728de commit a0b47f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private TextureGallery loadTextureGallery() throws IOException {

private void saveTextureGallery() {
try (OutputStream out = storage.writeMeta(id, META_FILE_TEXTURES)) {
this.textureGallery.writeTexturesFile(this.resourcePack, out);
this.textureGallery.writeTexturesFile(out);
} catch (IOException ex) {
Logger.global.logError("Failed to save textures for map '" + getId() + "'!", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,49 +42,53 @@
@DebugDump
public class TextureGallery {

private final Map<ResourcePath<Texture>, Integer> ordinalMap;
private final Map<ResourcePath<Texture>, TextureMapping> textureMappings;
private int nextId;

public TextureGallery() {
this.ordinalMap = new HashMap<>();
this.textureMappings = new HashMap<>();
this.nextId = 0;
}

public void clear() {
this.ordinalMap.clear();
this.textureMappings.clear();
this.nextId = 0;
}

public int get(@Nullable ResourcePath<Texture> textureResourcePath) {
if (textureResourcePath == null) textureResourcePath = ResourcePack.MISSING_TEXTURE;
Integer ordinal = ordinalMap.get(textureResourcePath);
return ordinal != null ? ordinal : 0;
TextureMapping mapping = textureMappings.get(textureResourcePath);
return mapping != null ? mapping.getId() : 0;
}

public synchronized int put(ResourcePath<Texture> textureResourcePath) {
Integer ordinal = ordinalMap.putIfAbsent(textureResourcePath, nextId);
if (ordinal == null) return nextId++;
return ordinal;
public synchronized void put(ResourcePath<Texture> textureResourcePath) {
textureMappings.compute(textureResourcePath, (r, mapping) -> {
if (mapping == null)
return new TextureMapping(nextId++, textureResourcePath.getResource());

Texture texture = textureResourcePath.getResource();
if (texture != null) mapping.setTexture(texture);
return mapping;
});
}

public synchronized void put(ResourcePack resourcePack) {
this.put(ResourcePack.MISSING_TEXTURE); // put this first
resourcePack.getTextures().keySet()
.stream()
.sorted(Comparator.comparing(Key::getFormatted))
.forEach(this::put);
}

public void writeTexturesFile(ResourcePack resourcePack, OutputStream out) throws IOException {
public void writeTexturesFile(OutputStream out) throws IOException {
Texture[] textures = new Texture[nextId];
Arrays.fill(textures, Texture.MISSING);

ordinalMap.forEach((textureResourcePath, ordinal) -> {
Texture texture = textureResourcePath.getResource(resourcePack::getTexture);
if (texture != null) textures[ordinal] = texture;

// make sure the resource-path doesn't get lost
if (textures[ordinal].getResourcePath().equals(ResourcePack.MISSING_TEXTURE))
textures[ordinal] = Texture.missing(textureResourcePath);
this.textureMappings.forEach((textureResourcePath, mapping) -> {
int ordinal = mapping.getId();
Texture texture = mapping.getTexture();
if (texture == null) texture = Texture.missing(textureResourcePath);
textures[ordinal] = texture;
});

try (Writer writer = new OutputStreamWriter(out)) {
Expand All @@ -103,7 +107,7 @@ public static TextureGallery readTexturesFile(InputStream in) throws IOException
for (int ordinal = 0; ordinal < textures.length; ordinal++) {
Texture texture = textures[ordinal];
if (texture != null) {
gallery.ordinalMap.put(texture.getResourcePath(), ordinal);
gallery.textureMappings.put(texture.getResourcePath(), new TextureMapping(ordinal, texture));
}
}
} catch (JsonIOException ex) {
Expand All @@ -112,4 +116,27 @@ public static TextureGallery readTexturesFile(InputStream in) throws IOException
return gallery;
}

static class TextureMapping {
private final int id;
private @Nullable Texture texture;

public TextureMapping(int id, @Nullable Texture texture) {
this.id = id;
this.texture = texture;
}

public int getId() {
return id;
}

public @Nullable Texture getTexture() {
return texture;
}

public void setTexture(@Nullable Texture texture) {
this.texture = texture;
}

}

}

0 comments on commit a0b47f1

Please sign in to comment.