| 1 | package net.minecraft.data.structures;
|
| 2 |
|
| 3 | import com.google.common.hash.Hashing;
|
| 4 | import com.google.common.hash.HashingOutputStream;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.io.ByteArrayOutputStream;
|
| 7 | import java.io.IOException;
|
| 8 | import java.io.InputStream;
|
| 9 | import java.nio.charset.StandardCharsets;
|
| 10 | import java.nio.file.Files;
|
| 11 | import java.nio.file.Path;
|
| 12 | import java.util.ArrayList;
|
| 13 | import java.util.Collection;
|
| 14 | import java.util.List;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.concurrent.CompletionStage;
|
| 17 | import java.util.stream.Stream;
|
| 18 | import net.minecraft.data.CachedOutput;
|
| 19 | import net.minecraft.data.DataProvider;
|
| 20 | import net.minecraft.data.PackOutput;
|
| 21 | import net.minecraft.nbt.CompoundTag;
|
| 22 | import net.minecraft.nbt.NbtAccounter;
|
| 23 | import net.minecraft.nbt.NbtIo;
|
| 24 | import net.minecraft.nbt.NbtUtils;
|
| 25 | import net.minecraft.util.FastBufferedInputStream;
|
| 26 | import net.minecraft.util.Util;
|
| 27 | import net.minecraft.util.datafix.DataFixTypes;
|
| 28 | import net.minecraft.util.datafix.DataFixers;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 | import org.slf4j.Logger;
|
| 31 |
|
| 32 | /**
|
| 33 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 34 | */
|
| 35 | public class NbtToSnbt implements DataProvider {
|
| 36 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 37 | private final Iterable<Path> inputFolders;
|
| 38 | private final PackOutput output;
|
| 39 |
|
| 40 | public NbtToSnbt(final PackOutput output, final Collection<Path> inputFolders) {
|
| 41 | this.inputFolders = inputFolders;
|
| 42 | this.output = output;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public CompletableFuture<?> run(final CachedOutput cache) {
|
| 47 | Path output = this.output.getOutputFolder();
|
| 48 | List<CompletableFuture<?>> tasks = new ArrayList<>();
|
| 49 |
|
| 50 | for (Path input : this.inputFolders) {
|
| 51 | tasks.add(
|
| 52 | CompletableFuture.<CompletableFuture<Void>>supplyAsync(
|
| 53 | () -> {
|
| 54 | try (Stream<Path> walk = Files.walk(input)) {
|
| 55 | return CompletableFuture.allOf(
|
| 56 | walk.filter(path -> path.toString().endsWith(".nbt"))
|
| 57 | .map(path -> CompletableFuture.runAsync(() -> convertStructure(cache, path, getName(input, path), output), Util.ioPool()))
|
| 58 | .toArray(CompletableFuture[]::new)
|
| 59 | );
|
| 60 | } catch (IOException e) {
|
| 61 | LOGGER.error("Failed to read structure input directory", e);
|
| 62 | return CompletableFuture.completedFuture(null);
|
| 63 | }
|
| 64 | },
|
| 65 | Util.backgroundExecutor().forName("NbtToSnbt")
|
| 66 | )
|
| 67 | .thenCompose(v -> (CompletionStage<Void>)v)
|
| 68 | );
|
| 69 | }
|
| 70 |
|
| 71 | return CompletableFuture.allOf(tasks.toArray(CompletableFuture[]::new));
|
| 72 | }
|
| 73 |
|
| 74 | /**
|
| 75 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 76 | */
|
| 77 | @Override
|
| 78 | public String getName() {
|
| 79 | return "NBT -> SNBT";
|
| 80 | }
|
| 81 |
|
| 82 | private static String getName(final Path root, final Path path) {
|
| 83 | String name = root.relativize(path).toString().replaceAll("\\\\", "/");
|
| 84 | return name.substring(0, name.length() - ".nbt".length());
|
| 85 | }
|
| 86 |
|
| 87 | public static @Nullable Path convertStructure(final CachedOutput cache, final Path path, final String name, final Path output) {
|
| 88 | try (
|
| 89 | InputStream rawInput = Files.newInputStream(path);
|
| 90 | InputStream input = new FastBufferedInputStream(rawInput);
|
| 91 | ) {
|
| 92 | Path resultPath = output.resolve(name + ".snbt");
|
| 93 | CompoundTag structure = NbtIo.readCompressed(input, NbtAccounter.unlimitedHeap());
|
| 94 | writeSnbt(
|
| 95 | cache,
|
| 96 | resultPath,
|
| 97 | NbtUtils.structureToSnbt(
|
| 98 | NbtUtils.addCurrentDataVersion(DataFixTypes.STRUCTURE.updateToCurrentVersion(DataFixers.getDataFixer(), structure, NbtUtils.getDataVersion(structure)))
|
| 99 | )
|
| 100 | );
|
| 101 | LOGGER.info("Converted {} from NBT to SNBT", name);
|
| 102 | return resultPath;
|
| 103 | } catch (IOException e) {
|
| 104 | LOGGER.error("Couldn't convert {} from NBT to SNBT at {}", name, path, e);
|
| 105 | return null;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | public static void writeSnbt(final CachedOutput cache, final Path destination, final String text) throws IOException {
|
| 110 | ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
| 111 | HashingOutputStream hashedBytes = new HashingOutputStream(Hashing.sha1(), bytes);
|
| 112 | hashedBytes.write(text.getBytes(StandardCharsets.UTF_8));
|
| 113 | hashedBytes.write(10);
|
| 114 | cache.writeIfNeeded(destination, bytes.toByteArray(), hashedBytes.hash());
|
| 115 | }
|
| 116 | }
|
| 117 |
|