| 1 | package net.minecraft.data.structures;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.hash.HashCode;
|
| 5 | import com.google.common.hash.Hashing;
|
| 6 | import com.google.common.hash.HashingOutputStream;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import java.io.BufferedReader;
|
| 9 | import java.io.ByteArrayOutputStream;
|
| 10 | import java.io.IOException;
|
| 11 | import java.nio.file.Files;
|
| 12 | import java.nio.file.Path;
|
| 13 | import java.util.List;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import java.util.concurrent.CompletionStage;
|
| 16 | import java.util.stream.Stream;
|
| 17 | import net.minecraft.data.CachedOutput;
|
| 18 | import net.minecraft.data.DataProvider;
|
| 19 | import net.minecraft.data.PackOutput;
|
| 20 | import net.minecraft.nbt.CompoundTag;
|
| 21 | import net.minecraft.nbt.NbtIo;
|
| 22 | import net.minecraft.nbt.NbtUtils;
|
| 23 | import net.minecraft.util.Util;
|
| 24 | import org.apache.commons.io.IOUtils;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | /**
|
| 28 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 29 | */
|
| 30 | public class SnbtToNbt implements DataProvider {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private final PackOutput output;
|
| 33 | private final Iterable<Path> inputFolders;
|
| 34 | private final List<SnbtToNbt.Filter> filters = Lists.newArrayList();
|
| 35 |
|
| 36 | public SnbtToNbt(final PackOutput output, final Path inputFolder) {
|
| 37 | this(output, List.of(inputFolder));
|
| 38 | }
|
| 39 |
|
| 40 | public SnbtToNbt(final PackOutput output, final Iterable<Path> inputFolders) {
|
| 41 | this.output = output;
|
| 42 | this.inputFolders = inputFolders;
|
| 43 | }
|
| 44 |
|
| 45 | public SnbtToNbt addFilter(final SnbtToNbt.Filter filter) {
|
| 46 | this.filters.add(filter);
|
| 47 | return this;
|
| 48 | }
|
| 49 |
|
| 50 | private CompoundTag applyFilters(final String name, final CompoundTag input) {
|
| 51 | CompoundTag result = input;
|
| 52 |
|
| 53 | for (SnbtToNbt.Filter filter : this.filters) {
|
| 54 | result = filter.apply(name, result);
|
| 55 | }
|
| 56 |
|
| 57 | return result;
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public CompletableFuture<?> run(final CachedOutput cache) {
|
| 62 | Path output = this.output.getOutputFolder();
|
| 63 | List<CompletableFuture<?>> tasks = Lists.newArrayList();
|
| 64 |
|
| 65 | for (Path input : this.inputFolders) {
|
| 66 | tasks.add(CompletableFuture.<CompletableFuture<Void>>supplyAsync(() -> {
|
| 67 | try (Stream<Path> files = Files.walk(input)) {
|
| 68 | return CompletableFuture.allOf(files.filter(path -> path.toString().endsWith(".snbt")).map(path -> CompletableFuture.runAsync(() -> {
|
| 69 | SnbtToNbt.TaskResult structure = this.readStructure(path, this.getName(input, path));
|
| 70 | this.storeStructureIfChanged(cache, structure, output);
|
| 71 | }, Util.backgroundExecutor().forName("SnbtToNbt"))).toArray(CompletableFuture[]::new));
|
| 72 | } catch (Exception e) {
|
| 73 | throw new RuntimeException("Failed to read structure input directory, aborting", e);
|
| 74 | }
|
| 75 | }, Util.backgroundExecutor().forName("SnbtToNbt")).thenCompose(v -> (CompletionStage<Void>)v));
|
| 76 | }
|
| 77 |
|
| 78 | return Util.sequenceFailFast(tasks);
|
| 79 | }
|
| 80 |
|
| 81 | /**
|
| 82 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 83 | */
|
| 84 | @Override
|
| 85 | public String getName() {
|
| 86 | return "SNBT -> NBT";
|
| 87 | }
|
| 88 |
|
| 89 | private String getName(final Path root, final Path path) {
|
| 90 | String name = root.relativize(path).toString().replaceAll("\\\\", "/");
|
| 91 | return name.substring(0, name.length() - ".snbt".length());
|
| 92 | }
|
| 93 |
|
| 94 | private SnbtToNbt.TaskResult readStructure(final Path path, final String name) {
|
| 95 | try (BufferedReader reader = Files.newBufferedReader(path)) {
|
| 96 | String input = IOUtils.toString(reader);
|
| 97 | CompoundTag updated = this.applyFilters(name, NbtUtils.snbtToStructure(input));
|
| 98 | ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
| 99 | HashingOutputStream hos = new HashingOutputStream(Hashing.sha1(), bos);
|
| 100 | NbtIo.writeCompressed(updated, hos);
|
| 101 | byte[] bytes = bos.toByteArray();
|
| 102 | HashCode hash = hos.hash();
|
| 103 | return new SnbtToNbt.TaskResult(name, bytes, hash);
|
| 104 | } catch (Throwable t) {
|
| 105 | throw new SnbtToNbt.StructureConversionException(path, t);
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | private void storeStructureIfChanged(final CachedOutput cache, final SnbtToNbt.TaskResult task, final Path output) {
|
| 110 | Path destination = output.resolve(task.name + ".nbt");
|
| 111 |
|
| 112 | try {
|
| 113 | cache.writeIfNeeded(destination, task.payload, task.hash);
|
| 114 | } catch (IOException e) {
|
| 115 | LOGGER.error("Couldn't write structure {} at {}", task.name, destination, e);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | @FunctionalInterface
|
| 120 | public interface Filter {
|
| 121 | CompoundTag apply(final String name, final CompoundTag input);
|
| 122 | }
|
| 123 |
|
| 124 | private static class StructureConversionException extends RuntimeException {
|
| 125 | public StructureConversionException(final Path path, final Throwable t) {
|
| 126 | super(path.toAbsolutePath().toString(), t);
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | private record TaskResult(String name, byte[] payload, HashCode hash) {
|
| 131 | }
|
| 132 | }
|
| 133 |
|