Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package de.bluecolored.bluemap.core.map;

import com.google.gson.*;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.resources.adapter.ResourcesGson;
import de.bluecolored.bluemap.core.resources.pack.ResourcePool;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.ResourcePack;
Expand All @@ -46,7 +45,7 @@ public class TextureGallery {
.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
.create();

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

public TextureGallery() {
Expand All @@ -59,34 +58,33 @@ public void clear() {
this.nextId = 0;
}

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

public synchronized void put(ResourcePath<Texture> textureResourcePath) {
textureMappings.compute(textureResourcePath, (r, mapping) -> {
public synchronized void put(Key key, Texture texture) {
textureMappings.compute(key, (r, mapping) -> {
if (mapping == null)
return new TextureMapping(nextId++, textureResourcePath.getResource());

Texture texture = textureResourcePath.getResource();
if (texture != null) mapping.setTexture(texture);
return new TextureMapping(nextId++, texture);
else if (texture != null) mapping.setTexture(texture);
return mapping;
});
}

public synchronized void put(ResourcePool<Texture> texturePool) {
this.put(ResourcePack.MISSING_TEXTURE); // put this first
texturePool.paths()
this.put(ResourcePack.MISSING_TEXTURE, ResourcePack.MISSING_TEXTURE.getResource()); // put this first
texturePool.entrySet()
.stream()
.sorted(Comparator
.comparing((ResourcePath<Texture> r) -> {
Texture texture = r.getResource(texturePool::get);
.comparing((Map.Entry<Key, Texture> entry) -> {
Texture texture = entry.getValue();
return texture != null && texture.getColorPremultiplied().a < 1f;
})
.thenComparing(Key::getFormatted))
.forEach(this::put);
.thenComparing(entry -> entry.getKey().getFormatted())
)
.forEach(entry -> put(entry.getKey(), entry.getValue()));
}

public void writeTexturesFile(OutputStream out) throws IOException {
Expand Down Expand Up @@ -115,8 +113,8 @@ public static TextureGallery readTexturesFile(InputStream in) throws IOException
gallery.nextId = textures.length;
for (int ordinal = 0; ordinal < textures.length; ordinal++) {
Texture texture = textures[ordinal];
if (texture != null) {
gallery.textureMappings.put(texture.getKey(), new TextureMapping(ordinal, texture));
if (texture != null && texture.getKey() != null) {
gallery.textureMappings.putIfAbsent(texture.getKey(), new TextureMapping(ordinal, texture));
}
}
} catch (JsonParseException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.TextureVariable;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
import de.bluecolored.bluemap.core.util.Direction;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluemap.core.util.math.Color;
import de.bluecolored.bluemap.core.util.math.MatrixM3f;
import de.bluecolored.bluemap.core.util.math.VectorM2f;
Expand All @@ -61,8 +62,8 @@ public class LiquidModelRenderer implements BlockRenderer {
.scale(0.5f, 0.5f, 1)
.translate(0.5f, 0.5f);

@Getter private final Function<ResourcePath<Model>, Model> modelProvider;
@Getter private final Function<ResourcePath<Texture>, Texture> textureProvider;
@Getter private final Function<Key, Model> modelProvider;
@Getter private final Function<Key, Texture> textureProvider;
@Getter private final TextureGallery textureGallery;
@Getter private final RenderSettings renderSettings;
@Getter private final BlockColorCalculatorFactory.BlockColorCalculator blockColorCalculator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Model;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
import de.bluecolored.bluemap.core.util.Direction;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluemap.core.util.math.Color;
import de.bluecolored.bluemap.core.util.math.MatrixM4f;
import de.bluecolored.bluemap.core.util.math.VectorM2f;
Expand All @@ -60,8 +61,8 @@
public class ResourceModelRenderer implements BlockRenderer {
private static final float BLOCK_SCALE = 1f / 16f;

@Getter private final Function<ResourcePath<Model>, Model> modelProvider;
@Getter private final Function<ResourcePath<Texture>, Texture> textureProvider;
@Getter private final Function<Key, Model> modelProvider;
@Getter private final Function<Key, Texture> textureProvider;
@Getter private final TextureGallery textureGallery;
@Getter private final RenderSettings renderSettings;
@Getter private final BlockColorCalculatorFactory.BlockColorCalculator blockColorCalculator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import de.bluecolored.bluemap.core.resources.pack.resourcepack.model.Model;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
import de.bluecolored.bluemap.core.util.Direction;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluemap.core.util.math.Color;
import de.bluecolored.bluemap.core.util.math.MatrixM4f;
import de.bluecolored.bluemap.core.util.math.VectorM2f;
Expand All @@ -56,7 +57,7 @@
public class ResourceModelRenderer implements EntityRenderer {
private static final float SCALE = 1f / 16f;

@Getter private final Function<ResourcePath<Model>, Model> modelProvider;
@Getter private final Function<Key, Model> modelProvider;
@Getter private final TextureGallery textureGallery;
@Getter private final RenderSettings renderSettings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public ResourcePath(String formatted) {
}

public ResourcePath(String namespace, String value) {
super(namespace.toLowerCase(Locale.ROOT), value.toLowerCase(Locale.ROOT));
super(namespace, value);
}

public ResourcePath(Key key) {
super(key.getNamespace(), key.getValue());
}

public ResourcePath(Path filePath, int namespacePos, int valuePos) {
super(parsePath(filePath, namespacePos, valuePos).toLowerCase(Locale.ROOT));
super(parsePath(filePath, namespacePos, valuePos));
}

@Nullable
Expand All @@ -66,7 +66,7 @@ public T getResource() {
}

@Nullable
public T getResource(Function<ResourcePath<T>, T> supplier) {
public T getResource(Function<Key, T> supplier) {
if (resource == null) resource = supplier.apply(this);
return resource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,75 +25,54 @@
package de.bluecolored.bluemap.core.resources.pack;

import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.util.Key;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;

public class ResourcePool<T> {

private final Map<Key, T> pool = new HashMap<>();
private final Map<Key, ResourcePath<T>> paths = new HashMap<>();
private final Map<Key, T> resources = new HashMap<>();

public void put(Key path, T value) {
put(new ResourcePath<>(path), value);
public T get(Key key) {
return resources.get(key);
}

public synchronized void put(ResourcePath<T> path, T value) {
pool.put(path, value);
paths.put(path, path);
path.setResource(value);
public void put(Key key, T value) {
Objects.requireNonNull(key, "key can not be null");
resources.put(key, value);
}

public void putIfAbsent(Key path, T value) {
putIfAbsent(new ResourcePath<>(path), value);
public void putIfAbsent(Key key, T value) {
Objects.requireNonNull(key, "key can not be null");
resources.putIfAbsent(key, value);
}

public synchronized void putIfAbsent(ResourcePath<T> path, T value) {
if (pool.putIfAbsent(path, value) == null) {
paths.put(path, path);
path.setResource(value);
}
public boolean containsKey(Key key) {
return resources.containsKey(key);
}

public Collection<ResourcePath<T>> paths() {
return paths.values();
public void remove(Key key) {
resources.remove(key);
}

public Collection<T> values() {
return pool.values();
}

public boolean contains(Key path) {
return paths.containsKey(path);
}

public @Nullable T get(ResourcePath<T> path) {
return pool.get(path);
}

public @Nullable T get(Key path) {
ResourcePath<T> rp = paths.get(path);
return rp == null ? null : rp.getResource(this::get);
return resources.values();
}

public synchronized void remove(Key path) {
ResourcePath<T> removed = paths.remove(path);
if (removed != null) pool.remove(removed);
public Set<Map.Entry<Key, T>> entrySet() {
return resources.entrySet();
}

public @Nullable ResourcePath<T> getPath(Key path) {
return paths.get(path);
public Set<Key> keySet() {
return resources.keySet();
}

public void load(ResourcePath<T> path, Loader<T> loader) {
public void load(Key path, Loader<T> loader) {
try {
if (contains(path)) return; // don't load already present resources
if (this.containsKey(path)) return; // don't load already present resources

T resource = loader.load(path);
if (resource == null) return; // don't load missing resources
Expand All @@ -104,7 +83,7 @@ public void load(ResourcePath<T> path, Loader<T> loader) {
}
}

public void load(ResourcePath<T> path, Loader<T> loader, BinaryOperator<T> mergeFunction) {
public void load(Key path, Loader<T> loader, BinaryOperator<T> mergeFunction) {
try {
T resource = loader.load(path);
if (resource == null) return; // don't load missing resources
Expand All @@ -120,7 +99,7 @@ public void load(ResourcePath<T> path, Loader<T> loader, BinaryOperator<T> merge
}

public interface Loader<T> {
T load(ResourcePath<T> resourcePath) throws IOException;
T load(Key resourcePath) throws IOException;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package de.bluecolored.bluemap.core.resources.pack.resourcepack.atlas;

import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.resources.pack.ResourcePool;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
Expand Down Expand Up @@ -83,7 +82,7 @@ public void load(Path root, ResourcePool<Texture> textures, Predicate<Key> textu

@Override
@SneakyThrows
protected @Nullable Texture loadTexture(ResourcePath<Texture> key, Path file) {
protected @Nullable Texture loadTexture(Key key, Path file) {
return super.loadTexture(key, file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.google.gson.annotations.SerializedName;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.resources.pack.ResourcePool;
import de.bluecolored.bluemap.core.resources.pack.resourcepack.texture.Texture;
import de.bluecolored.bluemap.core.util.BufferedImageUtil;
Expand All @@ -53,11 +52,11 @@
@SuppressWarnings({"unused", "FieldMayBeFinal"})
public class PalettedPermutationsSource extends Source {

private Set<ResourcePath<Texture>> textures;
private Set<Key> textures;
private String separator = "_";
@SerializedName("palette_key")
private ResourcePath<Texture> paletteKey;
private Map<String, ResourcePath<Texture>> permutations;
private Key paletteKey;
private Map<String, Key> permutations;

@Override
public void load(Path root, ResourcePool<Texture> texturePool, Predicate<Key> textureFilter) throws IOException {
Expand All @@ -66,7 +65,7 @@ public void load(Path root, ResourcePool<Texture> texturePool, Predicate<Key> te
if (permutations == null || permutations.isEmpty()) return;

// textures
for (ResourcePath<Texture> resource : textures) {
for (Key resource : textures) {
texturePool.load(resource, rp -> {
Path file = getFile(root, resource);
return loadTexture(resource, file);
Expand All @@ -80,7 +79,7 @@ public void load(Path root, ResourcePool<Texture> texturePool, Predicate<Key> te
});

// permutations
for (ResourcePath<Texture> resource : permutations.values()) {
for (Key resource : permutations.values()) {
texturePool.load(resource, rp -> {
Path file = getFile(root, resource);
return loadTexture(resource, file);
Expand Down Expand Up @@ -118,7 +117,7 @@ public void bake(ResourcePool<Texture> texturePool, Predicate<Key> textureFilter

// generate textures
Color tempColor = new Color();
for (ResourcePath<Texture> resource : textures) {
for (Key resource : textures) {
Texture texture = texturePool.get(resource);
if (texture == null) continue;
BufferedImage image = texture.getTextureImage();
Expand All @@ -127,11 +126,11 @@ public void bake(ResourcePool<Texture> texturePool, Predicate<Key> textureFilter
String suffix = paletteEntry.getKey();
PaletteMap palette = paletteEntry.getValue();

ResourcePath<Texture> sprite = new ResourcePath<>(
Key sprite = new Key(
resource.getNamespace(),
resource.getValue() + separator + suffix
);
if (texturePool.contains(sprite)) continue;
if (texturePool.containsKey(sprite)) continue;
if (!textureFilter.test(sprite)) continue;

BufferedImage resultImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@
@SuppressWarnings("unused")
public class SingleSource extends Source {

private ResourcePath<Texture> resource;
private @Nullable ResourcePath<Texture> sprite;
private Key resource;
private @Nullable Key sprite;

public SingleSource(ResourcePath<Texture> resource) {
public SingleSource(Key resource) {
this.resource = resource;
}

@Override
public void load(Path root, ResourcePool<Texture> textures, Predicate<Key> textureFilter) throws IOException {
if (resource == null) return;

ResourcePath<Texture> sprite = getSprite();
if (textures.contains(sprite)) return;
Key sprite = getSprite();
if (textures.containsKey(sprite)) return;
if (!textureFilter.test(sprite)) return;

Path file = getFile(root, resource);
Expand All @@ -68,7 +68,7 @@ public void load(Path root, ResourcePool<Texture> textures, Predicate<Key> textu
if (texture != null) textures.put(sprite, texture);
}

public ResourcePath<Texture> getSprite() {
public Key getSprite() {
return sprite == null ? resource : sprite;
}

Expand Down
Loading
Loading