Skip to content
Merged
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 @@ -26,14 +26,15 @@

package org.geysermc.pack.converter.bootstrap;

import org.geysermc.pack.converter.util.DefaultLogListener;
import org.geysermc.pack.converter.util.LogListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.PrintWriter;
import java.io.StringWriter;

public class BootstrapLogListener implements LogListener {
public class BootstrapLogListener extends DefaultLogListener {
private final ThunderGUI gui;

public BootstrapLogListener(ThunderGUI gui) {
Expand All @@ -45,21 +46,26 @@ public void debug(@NotNull String message) {
if (gui.debugMode.get()) {
appendText("DEBUG: " + message);
}

super.debug(message);
}

@Override
public void info(@NotNull String message) {
appendText(message);
super.info(message);
}

@Override
public void warn(@NotNull String message) {
appendText("WARNING: " + message);
super.warn(message);
}

@Override
public void error(@NotNull String message) {
appendText("ERROR: " + message);
super.error(message);
}

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

appendText(writer.toString());
}
super.error(message, exception);
}

private void appendText(String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public final class AssetConverters {
public static final ConverterPipeline<Model, BedrockModel> MODEL = create(ModelConverter.INSTANCE);
public static final ConverterPipeline<SoundRegistry, Map<String, SoundDefinitions>> SOUND_REGISTRY = create(
(pack, context) -> pack.soundRegistries(), SoundRegistryConverter.INSTANCE);
public static final ConverterPipeline<Sound, Sound> SOUND = create(extractor(SoundSerializer.CATEGORY), SoundConverter.INSTANCE);
public static final ConverterPipeline<Sound, Sound> SOUND = create(SoundConverter.INSTANCE);
public static final ConverterPipeline<Texture, TransformedTexture> TEXTURE = create(TextureConverter.INSTANCE);

private static <JavaAsset, BedrockAsset> ConverterPipeline<JavaAsset, BedrockAsset> createSingle(BiFunction<ResourcePack, ExtractionContext, JavaAsset> extractor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
package org.geysermc.pack.converter.type.sound;

import org.geysermc.pack.bedrock.resource.BedrockResourcePack;
import org.geysermc.pack.converter.pipeline.AssetCombiner;
import org.geysermc.pack.converter.pipeline.AssetConverter;
import org.geysermc.pack.converter.pipeline.CombineContext;
import org.geysermc.pack.converter.pipeline.ConversionContext;
import org.geysermc.pack.converter.pipeline.*;
import org.geysermc.pack.converter.util.JsonMappings;
import org.jetbrains.annotations.Nullable;
import team.unnamed.creative.ResourcePack;
import team.unnamed.creative.sound.Sound;

import java.io.FileOutputStream;
Expand All @@ -40,39 +39,50 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

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

@Override
public Collection<Sound> extract(ResourcePack pack, ExtractionContext context) {
return pack.sounds();
}

@Override
public @Nullable Sound convert(Sound sound, ConversionContext context) throws Exception {
return sound;
}

@Override
public void include(BedrockResourcePack pack, List<Sound> sounds, CombineContext context) {
JsonMappings mappings = JsonMappings.getMapping("sounds");

List<String> exported = new ArrayList<>();
Path output = pack.directory().resolve(SoundRegistryConverter.BEDROCK_SOUNDS_LOCATION);

for (Sound sound : sounds) {
String path = sound.key().value();
if (exported.contains(path)) {
context.warn("Conflicting sound file " + sound.key() + "!");
continue;
}
Path file = output.resolve(path + ".ogg");
Path directory = file.getParent();
try {
Files.createDirectories(directory);
try (OutputStream outputStream = new FileOutputStream(file.toFile())) {
sound.data().write(outputStream);
String javaPath = sound.key().value();
List<String> paths = mappings.map(javaPath);
for (String path : paths) {
if (exported.contains(path)) {
context.warn("Conflicting sound file " + sound.key() + "!");
continue;
}
Path file = output.resolve(path + ".ogg");
Path directory = file.getParent();
try {
Files.createDirectories(directory);
try (OutputStream outputStream = new FileOutputStream(file.toFile())) {
sound.data().write(outputStream);
}
} catch (IOException exception) {
context.error("Failed to write sound file " + sound.key() + "!", exception);
continue;
}
} catch (IOException exception) {
context.error("Failed to write sound file " + sound.key() + "!", exception);
continue;
exported.add(path);
}
exported.add(path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.geysermc.pack.converter.type.texture.transformer.TransformContext;
import org.geysermc.pack.converter.type.texture.transformer.TransformedTexture;
import org.geysermc.pack.converter.util.ImageUtil;
import org.geysermc.pack.converter.util.JsonMappings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import team.unnamed.creative.ResourcePack;
import team.unnamed.creative.texture.Texture;
Expand Down Expand Up @@ -78,7 +80,7 @@ public Collection<Texture> extract(ResourcePack pack, ExtractionContext context)
// TODO ideally textures should be transformed individually in the convert process, and not together in the extraction process, but this is hard to achieve,
// TODO and will need another big refactor to the texture transformation code
// TODO for now this will work, but for library users it might be nice to be able to properly convert singular textures with transformations
TextureMappings mappings = TextureMappings.textureMappings();
JsonMappings mappings = JsonMappings.getMapping("textures");
List<Texture> textures = new ArrayList<>(pack.textures());

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

@Override
public @Nullable TransformedTexture convert(Texture texture, ConversionContext context) throws Exception {
TextureMappings mappings = TextureMappings.textureMappings();
public @NotNull TransformedTexture convert(Texture texture, ConversionContext context) throws Exception {
JsonMappings mappings = JsonMappings.getMapping("textures");
TransformedTexture transformed = new TransformedTexture(texture);

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

Object mappingObject = mappings.textures(relativePath);

if (mappingObject == null) {
mappingObject = mappings.textures(rootPath);
List<String> mapping = mappings.map(relativePath);
List<String> transformedOutputs = new ArrayList<>();
for (String item : mapping) {
transformedOutputs.add(bedrockRoot + item.substring(item.indexOf('/')) + ".png");
}

String fallbackPath = bedrockRoot + "/" + relativePath.substring(relativePath.indexOf('/') + 1) + ".png";
if (mappingObject instanceof Map<?,?> keyMappings) { // Handles common subdirectories
String sanitizedName = input.substring(input.indexOf('/') + 1);
if (sanitizedName.endsWith(".png")) sanitizedName = sanitizedName.substring(0, sanitizedName.length() - 4);

Object bedrockOutput = keyMappings.get(sanitizedName);
if (bedrockOutput instanceof String bedrockPath) {
transformed.output(bedrockRoot + "/" + bedrockPath + ".png");
} else if (bedrockOutput instanceof List<?> paths) {
for (String bedrockPath : (List<String>) paths) {
transformed.output(bedrockRoot + "/" + bedrockPath + ".png");
}
} else { // Fallback
transformed.output(fallbackPath);
}
} else if (mappingObject instanceof String str) { // Direct mappings
transformed.output(str + ".png");
} else if (mappingObject instanceof List<?> paths) { // Mappings where duplicate code paths exist
for (String path : (List<String>) paths) {
transformed.output(path + ".png");
}
} else { // Fallback
transformed.output(fallbackPath);
}
transformed.output(transformedOutputs);

return transformed;
}
Expand Down Expand Up @@ -210,6 +189,8 @@ public void include(BedrockResourcePack pack, List<TransformedTexture> transform
}
}

Files.createDirectories(output.getParent());

try (OutputStream stream = Files.newOutputStream(output)) {
ImageIO.write(bedrockImage, "png", stream);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

import net.kyori.adventure.key.Key;
import org.geysermc.pack.bedrock.resource.BedrockResourcePack;
import org.geysermc.pack.converter.type.texture.TextureMappings;
import org.geysermc.pack.converter.util.ImageUtil;
import org.geysermc.pack.converter.util.JsonMappings;
import org.geysermc.pack.converter.util.LogListener;
import org.geysermc.pack.converter.util.LogListenerHelper;
import org.jetbrains.annotations.NotNull;
Expand All @@ -46,7 +46,7 @@
import java.util.Optional;

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

public TransformContext(
TextureMappings mappings,
JsonMappings mappings,
Collection<Texture> textures,
BedrockResourcePack bedrockPack,
ResourcePack javaPack,
Expand All @@ -76,7 +76,7 @@ public TransformContext(
}
}

public TextureMappings mappings() {
public JsonMappings mappings() {
return this.mappings;
}

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

public class TransformedTexture {
private final Texture texture;
private final List<String> outputs = new ArrayList<>();
private List<String> outputs = new ArrayList<>();

public TransformedTexture(@NotNull Texture texture) {
this.texture = texture;
Expand All @@ -50,7 +50,7 @@ public List<String> output() {
return outputs;
}

public void output(@NotNull String output) {
outputs.add(output);
public void output(@NotNull List<String> output) {
outputs = new ArrayList<>(output);
}
}
Loading