|
| 1 | +/* |
| 2 | + * This file is part of BlueMap, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package de.bluecolored.bluemap.core.mca.region; |
| 26 | + |
| 27 | +import com.flowpowered.math.vector.Vector2i; |
| 28 | +import com.github.luben.zstd.ZstdInputStream; |
| 29 | +import de.bluecolored.bluemap.core.logger.Logger; |
| 30 | +import de.bluecolored.bluemap.core.mca.MCAChunk; |
| 31 | +import de.bluecolored.bluemap.core.mca.MCAWorld; |
| 32 | +import de.bluecolored.bluemap.core.world.Chunk; |
| 33 | +import de.bluecolored.bluemap.core.world.EmptyChunk; |
| 34 | +import de.bluecolored.bluemap.core.world.Region; |
| 35 | +import net.querz.nbt.CompoundTag; |
| 36 | +import net.querz.nbt.Tag; |
| 37 | + |
| 38 | +import java.io.*; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.Collection; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.List; |
| 45 | + |
| 46 | +public class LinearRegion implements Region { |
| 47 | + |
| 48 | + public static final String FILE_SUFFIX = ".linear"; |
| 49 | + |
| 50 | + private static final long SUPERBLOCK = -4323716122432332390L; |
| 51 | + private static final byte VERSION = 1; |
| 52 | + private static final int HEADER_SIZE = 32; |
| 53 | + private static final int FOOTER_SIZE = 8; |
| 54 | + |
| 55 | + private final MCAWorld world; |
| 56 | + private final Path regionFile; |
| 57 | + private final Vector2i regionPos; |
| 58 | + |
| 59 | + |
| 60 | + public LinearRegion(MCAWorld world, Path regionFile) throws IllegalArgumentException { |
| 61 | + this.world = world; |
| 62 | + this.regionFile = regionFile; |
| 63 | + |
| 64 | + String[] filenameParts = regionFile.getFileName().toString().split("\\."); |
| 65 | + int rX = Integer.parseInt(filenameParts[1]); |
| 66 | + int rZ = Integer.parseInt(filenameParts[2]); |
| 67 | + |
| 68 | + this.regionPos = new Vector2i(rX, rZ); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Chunk loadChunk(int chunkX, int chunkZ, boolean ignoreMissingLightData) throws IOException { |
| 73 | + if (Files.notExists(regionFile)) return EmptyChunk.INSTANCE; |
| 74 | + |
| 75 | + long fileLength = Files.size(regionFile); |
| 76 | + if (fileLength == 0) return EmptyChunk.INSTANCE; |
| 77 | + |
| 78 | + try (InputStream inputStream = Files.newInputStream(regionFile); |
| 79 | + DataInputStream rawDataStream = new DataInputStream(inputStream)) { |
| 80 | + |
| 81 | + long superBlock = rawDataStream.readLong(); |
| 82 | + if (superBlock != SUPERBLOCK) |
| 83 | + throw new RuntimeException("Superblock invalid: " + superBlock + " file " + regionFile); |
| 84 | + |
| 85 | + byte version = rawDataStream.readByte(); |
| 86 | + if (version != VERSION) |
| 87 | + throw new RuntimeException("Version invalid: " + version + " file " + regionFile); |
| 88 | + |
| 89 | + rawDataStream.skipBytes(11); // newestTimestamp + compression level + chunk count |
| 90 | + |
| 91 | + int dataCount = rawDataStream.readInt(); |
| 92 | + if (fileLength != HEADER_SIZE + dataCount + FOOTER_SIZE) |
| 93 | + throw new RuntimeException("File length invalid " + this.regionFile + " " + fileLength + " " + (HEADER_SIZE + dataCount + FOOTER_SIZE)); |
| 94 | + |
| 95 | + rawDataStream.skipBytes(8); // Data Hash |
| 96 | + |
| 97 | + byte[] rawCompressed = new byte[dataCount]; |
| 98 | + rawDataStream.readFully(rawCompressed, 0, dataCount); |
| 99 | + |
| 100 | + superBlock = rawDataStream.readLong(); |
| 101 | + if (superBlock != SUPERBLOCK) |
| 102 | + throw new RuntimeException("Footer superblock invalid " + this.regionFile); |
| 103 | + |
| 104 | + try (DataInputStream dis = new DataInputStream(new ZstdInputStream(new ByteArrayInputStream(rawCompressed)))) { |
| 105 | + int x = chunkX - (regionPos.getX() << 5); |
| 106 | + int z = chunkZ - (regionPos.getY() << 5); |
| 107 | + int pos = (z << 5) + x; |
| 108 | + int skip = 0; |
| 109 | + |
| 110 | + for (int i = 0; i < pos; i++) { |
| 111 | + skip += dis.readInt(); // size of the chunk (bytes) to skip |
| 112 | + dis.skipBytes(4); // skip 0 (will be timestamps) |
| 113 | + } |
| 114 | + |
| 115 | + int size = dis.readInt(); |
| 116 | + if (size <= 0) return EmptyChunk.INSTANCE; |
| 117 | + |
| 118 | + dis.skipBytes(((1024 - pos - 1) << 3) + 4); // Skip current chunk 0 and unneeded other chunks zero/size |
| 119 | + dis.skipBytes(skip); // Skip unneeded chunks data |
| 120 | + |
| 121 | + Tag<?> tag = Tag.deserialize(dis, Tag.DEFAULT_MAX_DEPTH); |
| 122 | + if (tag instanceof CompoundTag) { |
| 123 | + MCAChunk chunk = MCAChunk.create(world, (CompoundTag) tag); |
| 124 | + if (!chunk.isGenerated()) return EmptyChunk.INSTANCE; |
| 125 | + return chunk; |
| 126 | + } else { |
| 127 | + throw new IOException("Invalid data tag: " + (tag == null ? "null" : tag.getClass().getName())); |
| 128 | + } |
| 129 | + } |
| 130 | + } catch (RuntimeException e) { |
| 131 | + throw new IOException(e); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Collection<Vector2i> listChunks(long modifiedSince) { |
| 137 | + if (Files.notExists(regionFile)) return Collections.emptyList(); |
| 138 | + |
| 139 | + try { |
| 140 | + long fileLength = Files.size(regionFile); |
| 141 | + if (fileLength == 0) return Collections.emptyList(); |
| 142 | + } catch (IOException ex) { |
| 143 | + Logger.global.logWarning("Failed to read file-size for file: " + regionFile); |
| 144 | + return Collections.emptyList(); |
| 145 | + } |
| 146 | + |
| 147 | + List<Vector2i> chunks = new ArrayList<>(1024); //1024 = 32 x 32 chunks per region-file |
| 148 | + try (InputStream inputStream = Files.newInputStream(regionFile); |
| 149 | + DataInputStream rawDataStream = new DataInputStream(inputStream)) { |
| 150 | + |
| 151 | + long superBlock = rawDataStream.readLong(); |
| 152 | + if (superBlock != SUPERBLOCK) throw new RuntimeException("Superblock invalid: " + superBlock + " file " + regionFile); |
| 153 | + |
| 154 | + byte version = rawDataStream.readByte(); |
| 155 | + if (version != VERSION) throw new RuntimeException("Version invalid: " + version + " file " + regionFile); |
| 156 | + |
| 157 | + long newestTimestamp = rawDataStream.readLong(); |
| 158 | + |
| 159 | + // If whole region is the same - skip. |
| 160 | + if (newestTimestamp < modifiedSince / 1000) return Collections.emptyList(); |
| 161 | + |
| 162 | + // Linear files store whole region timestamp, not chunk timestamp. We need to render the while region file. |
| 163 | + // TODO: Add per-chunk timestamps when .linear add support for per-chunk timestamps (soon) |
| 164 | + for(int i = 0 ; i < 1024; i++) |
| 165 | + chunks.add(new Vector2i((regionPos.getX() << 5) + (i & 31), (regionPos.getY() << 5) + (i >> 5))); |
| 166 | + return chunks; |
| 167 | + } catch (RuntimeException | IOException ex) { |
| 168 | + Logger.global.logWarning("Failed to read .linear file: " + regionFile + " (" + ex + ")"); |
| 169 | + } |
| 170 | + return chunks; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public Path getRegionFile() { |
| 175 | + return regionFile; |
| 176 | + } |
| 177 | + |
| 178 | + public static String getRegionFileName(int regionX, int regionZ) { |
| 179 | + return "r." + regionX + "." + regionZ + FILE_SUFFIX; |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments