| 1 | package net.minecraft.world.level.chunk.storage;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 5 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
| 7 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 8 | import java.io.BufferedOutputStream;
|
| 9 | import java.io.IOException;
|
| 10 | import java.io.InputStream;
|
| 11 | import java.io.OutputStream;
|
| 12 | import java.util.zip.DeflaterOutputStream;
|
| 13 | import java.util.zip.GZIPInputStream;
|
| 14 | import java.util.zip.GZIPOutputStream;
|
| 15 | import java.util.zip.InflaterInputStream;
|
| 16 | import net.jpountz.lz4.LZ4BlockInputStream;
|
| 17 | import net.jpountz.lz4.LZ4BlockOutputStream;
|
| 18 | import net.minecraft.util.FastBufferedInputStream;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public class RegionFileVersion {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final Int2ObjectMap<RegionFileVersion> VERSIONS = new Int2ObjectOpenHashMap<>();
|
| 25 | private static final Object2ObjectMap<String, RegionFileVersion> VERSIONS_BY_NAME = new Object2ObjectOpenHashMap<>();
|
| 26 | public static final RegionFileVersion VERSION_GZIP = register(
|
| 27 | new RegionFileVersion(1, null, in -> new FastBufferedInputStream(new GZIPInputStream(in)), out -> new BufferedOutputStream(new GZIPOutputStream(out)))
|
| 28 | );
|
| 29 | public static final RegionFileVersion VERSION_DEFLATE = register(
|
| 30 | new RegionFileVersion(
|
| 31 | 2, "deflate", in -> new FastBufferedInputStream(new InflaterInputStream(in)), out -> new BufferedOutputStream(new DeflaterOutputStream(out))
|
| 32 | )
|
| 33 | );
|
| 34 | public static final RegionFileVersion VERSION_NONE = register(new RegionFileVersion(3, "none", FastBufferedInputStream::new, BufferedOutputStream::new));
|
| 35 | public static final RegionFileVersion VERSION_LZ4 = register(
|
| 36 | new RegionFileVersion(
|
| 37 | 4, "lz4", in -> new FastBufferedInputStream(new LZ4BlockInputStream(in)), out -> new BufferedOutputStream(new LZ4BlockOutputStream(out))
|
| 38 | )
|
| 39 | );
|
| 40 | public static final RegionFileVersion VERSION_CUSTOM = register(new RegionFileVersion(127, null, in -> {
|
| 41 | throw new UnsupportedOperationException();
|
| 42 | }, out -> {
|
| 43 | throw new UnsupportedOperationException();
|
| 44 | }));
|
| 45 | public static final RegionFileVersion DEFAULT = VERSION_DEFLATE;
|
| 46 | private static volatile RegionFileVersion selected = DEFAULT;
|
| 47 | private final int id;
|
| 48 | private final @Nullable String optionName;
|
| 49 | private final RegionFileVersion.StreamWrapper<InputStream> inputWrapper;
|
| 50 | private final RegionFileVersion.StreamWrapper<OutputStream> outputWrapper;
|
| 51 |
|
| 52 | private RegionFileVersion(
|
| 53 | final int id,
|
| 54 | final @Nullable String optionName,
|
| 55 | final RegionFileVersion.StreamWrapper<InputStream> inputWrapper,
|
| 56 | final RegionFileVersion.StreamWrapper<OutputStream> outputWrapper
|
| 57 | ) {
|
| 58 | this.id = id;
|
| 59 | this.optionName = optionName;
|
| 60 | this.inputWrapper = inputWrapper;
|
| 61 | this.outputWrapper = outputWrapper;
|
| 62 | }
|
| 63 |
|
| 64 | private static RegionFileVersion register(final RegionFileVersion version) {
|
| 65 | VERSIONS.put(version.id, version);
|
| 66 | if (version.optionName != null) {
|
| 67 | VERSIONS_BY_NAME.put(version.optionName, version);
|
| 68 | }
|
| 69 |
|
| 70 | return version;
|
| 71 | }
|
| 72 |
|
| 73 | public static @Nullable RegionFileVersion fromId(final int id) {
|
| 74 | return VERSIONS.get(id);
|
| 75 | }
|
| 76 |
|
| 77 | public static void configure(final String optionName) {
|
| 78 | RegionFileVersion version = VERSIONS_BY_NAME.get(optionName);
|
| 79 | if (version != null) {
|
| 80 | selected = version;
|
| 81 | } else {
|
| 82 | LOGGER.error(
|
| 83 | "Invalid `region-file-compression` value `{}` in server.properties. Please use one of: {}", optionName, String.join(", ", VERSIONS_BY_NAME.keySet())
|
| 84 | );
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | public static RegionFileVersion getSelected() {
|
| 89 | return selected;
|
| 90 | }
|
| 91 |
|
| 92 | public static boolean isValidVersion(final int version) {
|
| 93 | return VERSIONS.containsKey(version);
|
| 94 | }
|
| 95 |
|
| 96 | public int getId() {
|
| 97 | return this.id;
|
| 98 | }
|
| 99 |
|
| 100 | public OutputStream wrap(final OutputStream is) throws IOException {
|
| 101 | return this.outputWrapper.wrap(is);
|
| 102 | }
|
| 103 |
|
| 104 | public InputStream wrap(final InputStream is) throws IOException {
|
| 105 | return this.inputWrapper.wrap(is);
|
| 106 | }
|
| 107 |
|
| 108 | @FunctionalInterface
|
| 109 | private interface StreamWrapper<O> {
|
| 110 | O wrap(O stream) throws IOException;
|
| 111 | }
|
| 112 | }
|
| 113 |
|