Skip to content

Commit 37f5a55

Browse files
authored
Abstract JsonMappings (#47)
1 parent 493384c commit 37f5a55

9 files changed

Lines changed: 166 additions & 118 deletions

File tree

bootstrap/src/main/java/org/geysermc/pack/converter/bootstrap/BootstrapLogListener.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626

2727
package org.geysermc.pack.converter.bootstrap;
2828

29+
import org.geysermc.pack.converter.util.DefaultLogListener;
2930
import org.geysermc.pack.converter.util.LogListener;
3031
import org.jetbrains.annotations.NotNull;
3132
import org.jetbrains.annotations.Nullable;
3233

3334
import java.io.PrintWriter;
3435
import java.io.StringWriter;
3536

36-
public class BootstrapLogListener implements LogListener {
37+
public class BootstrapLogListener extends DefaultLogListener {
3738
private final ThunderGUI gui;
3839

3940
public BootstrapLogListener(ThunderGUI gui) {
@@ -45,21 +46,26 @@ public void debug(@NotNull String message) {
4546
if (gui.debugMode.get()) {
4647
appendText("DEBUG: " + message);
4748
}
49+
50+
super.debug(message);
4851
}
4952

5053
@Override
5154
public void info(@NotNull String message) {
5255
appendText(message);
56+
super.info(message);
5357
}
5458

5559
@Override
5660
public void warn(@NotNull String message) {
5761
appendText("WARNING: " + message);
62+
super.warn(message);
5863
}
5964

6065
@Override
6166
public void error(@NotNull String message) {
6267
appendText("ERROR: " + message);
68+
super.error(message);
6369
}
6470

6571
@Override
@@ -73,6 +79,7 @@ public void error(@NotNull String message, @Nullable Throwable exception) {
7379

7480
appendText(writer.toString());
7581
}
82+
super.error(message, exception);
7683
}
7784

7885
private void appendText(String text) {

converter/src/main/java/org/geysermc/pack/converter/pipeline/AssetConverters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public final class AssetConverters {
8080
public static final ConverterPipeline<Model, BedrockModel> MODEL = create(ModelConverter.INSTANCE);
8181
public static final ConverterPipeline<SoundRegistry, Map<String, SoundDefinitions>> SOUND_REGISTRY = create(
8282
(pack, context) -> pack.soundRegistries(), SoundRegistryConverter.INSTANCE);
83-
public static final ConverterPipeline<Sound, Sound> SOUND = create(extractor(SoundSerializer.CATEGORY), SoundConverter.INSTANCE);
83+
public static final ConverterPipeline<Sound, Sound> SOUND = create(SoundConverter.INSTANCE);
8484
public static final ConverterPipeline<Texture, TransformedTexture> TEXTURE = create(TextureConverter.INSTANCE);
8585

8686
private static <JavaAsset, BedrockAsset> ConverterPipeline<JavaAsset, BedrockAsset> createSingle(BiFunction<ResourcePack, ExtractionContext, JavaAsset> extractor,

converter/src/main/java/org/geysermc/pack/converter/type/sound/SoundConverter.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
package org.geysermc.pack.converter.type.sound;
2828

2929
import org.geysermc.pack.bedrock.resource.BedrockResourcePack;
30-
import org.geysermc.pack.converter.pipeline.AssetCombiner;
31-
import org.geysermc.pack.converter.pipeline.AssetConverter;
32-
import org.geysermc.pack.converter.pipeline.CombineContext;
33-
import org.geysermc.pack.converter.pipeline.ConversionContext;
30+
import org.geysermc.pack.converter.pipeline.*;
31+
import org.geysermc.pack.converter.util.JsonMappings;
3432
import org.jetbrains.annotations.Nullable;
33+
import team.unnamed.creative.ResourcePack;
3534
import team.unnamed.creative.sound.Sound;
3635

3736
import java.io.FileOutputStream;
@@ -40,39 +39,50 @@
4039
import java.nio.file.Files;
4140
import java.nio.file.Path;
4241
import java.util.ArrayList;
42+
import java.util.Collection;
4343
import java.util.List;
4444

45-
public class SoundConverter implements AssetConverter<Sound, Sound>, AssetCombiner<Sound> {
45+
public class SoundConverter implements AssetExtractor<Sound>, AssetConverter<Sound, Sound>, AssetCombiner<Sound> {
4646
public static final SoundConverter INSTANCE = new SoundConverter();
4747

48+
@Override
49+
public Collection<Sound> extract(ResourcePack pack, ExtractionContext context) {
50+
return pack.sounds();
51+
}
52+
4853
@Override
4954
public @Nullable Sound convert(Sound sound, ConversionContext context) throws Exception {
5055
return sound;
5156
}
5257

5358
@Override
5459
public void include(BedrockResourcePack pack, List<Sound> sounds, CombineContext context) {
60+
JsonMappings mappings = JsonMappings.getMapping("sounds");
61+
5562
List<String> exported = new ArrayList<>();
5663
Path output = pack.directory().resolve(SoundRegistryConverter.BEDROCK_SOUNDS_LOCATION);
5764

5865
for (Sound sound : sounds) {
59-
String path = sound.key().value();
60-
if (exported.contains(path)) {
61-
context.warn("Conflicting sound file " + sound.key() + "!");
62-
continue;
63-
}
64-
Path file = output.resolve(path + ".ogg");
65-
Path directory = file.getParent();
66-
try {
67-
Files.createDirectories(directory);
68-
try (OutputStream outputStream = new FileOutputStream(file.toFile())) {
69-
sound.data().write(outputStream);
66+
String javaPath = sound.key().value();
67+
List<String> paths = mappings.map(javaPath);
68+
for (String path : paths) {
69+
if (exported.contains(path)) {
70+
context.warn("Conflicting sound file " + sound.key() + "!");
71+
continue;
72+
}
73+
Path file = output.resolve(path + ".ogg");
74+
Path directory = file.getParent();
75+
try {
76+
Files.createDirectories(directory);
77+
try (OutputStream outputStream = new FileOutputStream(file.toFile())) {
78+
sound.data().write(outputStream);
79+
}
80+
} catch (IOException exception) {
81+
context.error("Failed to write sound file " + sound.key() + "!", exception);
82+
continue;
7083
}
71-
} catch (IOException exception) {
72-
context.error("Failed to write sound file " + sound.key() + "!", exception);
73-
continue;
84+
exported.add(path);
7485
}
75-
exported.add(path);
7686
}
7787
}
7888
}

converter/src/main/java/org/geysermc/pack/converter/type/texture/TextureConverter.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.geysermc.pack.converter.type.texture.transformer.TransformContext;
3838
import org.geysermc.pack.converter.type.texture.transformer.TransformedTexture;
3939
import org.geysermc.pack.converter.util.ImageUtil;
40+
import org.geysermc.pack.converter.util.JsonMappings;
41+
import org.jetbrains.annotations.NotNull;
4042
import org.jetbrains.annotations.Nullable;
4143
import team.unnamed.creative.ResourcePack;
4244
import team.unnamed.creative.texture.Texture;
@@ -78,7 +80,7 @@ public Collection<Texture> extract(ResourcePack pack, ExtractionContext context)
7880
// TODO ideally textures should be transformed individually in the convert process, and not together in the extraction process, but this is hard to achieve,
7981
// TODO and will need another big refactor to the texture transformation code
8082
// TODO for now this will work, but for library users it might be nice to be able to properly convert singular textures with transformations
81-
TextureMappings mappings = TextureMappings.textureMappings();
83+
JsonMappings mappings = JsonMappings.getMapping("textures");
8284
List<Texture> textures = new ArrayList<>(pack.textures());
8385

8486
context.info("Transforming textures...");
@@ -103,8 +105,8 @@ public Collection<Texture> extract(ResourcePack pack, ExtractionContext context)
103105
}
104106

105107
@Override
106-
public @Nullable TransformedTexture convert(Texture texture, ConversionContext context) throws Exception {
107-
TextureMappings mappings = TextureMappings.textureMappings();
108+
public @NotNull TransformedTexture convert(Texture texture, ConversionContext context) throws Exception {
109+
JsonMappings mappings = JsonMappings.getMapping("textures");
108110
TransformedTexture transformed = new TransformedTexture(texture);
109111

110112
String input = texture.key().value();
@@ -113,36 +115,13 @@ public Collection<Texture> extract(ResourcePack pack, ExtractionContext context)
113115
String rootPath = relativePath.substring(0, relativePath.indexOf('/'));
114116
String bedrockRoot = DIRECTORY_LOCATIONS.getOrDefault(rootPath, rootPath);
115117

116-
Object mappingObject = mappings.textures(relativePath);
117-
118-
if (mappingObject == null) {
119-
mappingObject = mappings.textures(rootPath);
118+
List<String> mapping = mappings.map(relativePath);
119+
List<String> transformedOutputs = new ArrayList<>();
120+
for (String item : mapping) {
121+
transformedOutputs.add(bedrockRoot + item.substring(item.indexOf('/')) + ".png");
120122
}
121123

122-
String fallbackPath = bedrockRoot + "/" + relativePath.substring(relativePath.indexOf('/') + 1) + ".png";
123-
if (mappingObject instanceof Map<?,?> keyMappings) { // Handles common subdirectories
124-
String sanitizedName = input.substring(input.indexOf('/') + 1);
125-
if (sanitizedName.endsWith(".png")) sanitizedName = sanitizedName.substring(0, sanitizedName.length() - 4);
126-
127-
Object bedrockOutput = keyMappings.get(sanitizedName);
128-
if (bedrockOutput instanceof String bedrockPath) {
129-
transformed.output(bedrockRoot + "/" + bedrockPath + ".png");
130-
} else if (bedrockOutput instanceof List<?> paths) {
131-
for (String bedrockPath : (List<String>) paths) {
132-
transformed.output(bedrockRoot + "/" + bedrockPath + ".png");
133-
}
134-
} else { // Fallback
135-
transformed.output(fallbackPath);
136-
}
137-
} else if (mappingObject instanceof String str) { // Direct mappings
138-
transformed.output(str + ".png");
139-
} else if (mappingObject instanceof List<?> paths) { // Mappings where duplicate code paths exist
140-
for (String path : (List<String>) paths) {
141-
transformed.output(path + ".png");
142-
}
143-
} else { // Fallback
144-
transformed.output(fallbackPath);
145-
}
124+
transformed.output(transformedOutputs);
146125

147126
return transformed;
148127
}
@@ -210,6 +189,8 @@ public void include(BedrockResourcePack pack, List<TransformedTexture> transform
210189
}
211190
}
212191

192+
Files.createDirectories(output.getParent());
193+
213194
try (OutputStream stream = Files.newOutputStream(output)) {
214195
ImageIO.write(bedrockImage, "png", stream);
215196
}

converter/src/main/java/org/geysermc/pack/converter/type/texture/TextureMappings.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

converter/src/main/java/org/geysermc/pack/converter/type/texture/transformer/TransformContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
import net.kyori.adventure.key.Key;
3030
import org.geysermc.pack.bedrock.resource.BedrockResourcePack;
31-
import org.geysermc.pack.converter.type.texture.TextureMappings;
3231
import org.geysermc.pack.converter.util.ImageUtil;
32+
import org.geysermc.pack.converter.util.JsonMappings;
3333
import org.geysermc.pack.converter.util.LogListener;
3434
import org.geysermc.pack.converter.util.LogListenerHelper;
3535
import org.jetbrains.annotations.NotNull;
@@ -46,7 +46,7 @@
4646
import java.util.Optional;
4747

4848
public class TransformContext implements LogListenerHelper {
49-
private final TextureMappings mappings;
49+
private final JsonMappings mappings;
5050
private final Collection<Texture> textures;
5151
// TODO figure out how to handle this, this is executed in the extraction phase and ideally bedrock pack wouldn't be accessed then
5252
@Deprecated(forRemoval = true)
@@ -57,7 +57,7 @@ public class TransformContext implements LogListenerHelper {
5757
private final Map<Key, Texture> byKey = new HashMap<>();
5858

5959
public TransformContext(
60-
TextureMappings mappings,
60+
JsonMappings mappings,
6161
Collection<Texture> textures,
6262
BedrockResourcePack bedrockPack,
6363
ResourcePack javaPack,
@@ -76,7 +76,7 @@ public TransformContext(
7676
}
7777
}
7878

79-
public TextureMappings mappings() {
79+
public JsonMappings mappings() {
8080
return this.mappings;
8181
}
8282

converter/src/main/java/org/geysermc/pack/converter/type/texture/transformer/TransformedTexture.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
public class TransformedTexture {
3636
private final Texture texture;
37-
private final List<String> outputs = new ArrayList<>();
37+
private List<String> outputs = new ArrayList<>();
3838

3939
public TransformedTexture(@NotNull Texture texture) {
4040
this.texture = texture;
@@ -50,7 +50,7 @@ public List<String> output() {
5050
return outputs;
5151
}
5252

53-
public void output(@NotNull String output) {
54-
outputs.add(output);
53+
public void output(@NotNull List<String> output) {
54+
outputs = new ArrayList<>(output);
5555
}
5656
}

0 commit comments

Comments
 (0)