Skip to content

Commit c20366d

Browse files
authored
Remove never-working caching and fix bugs in region code (#2716)
Fixes #2684
1 parent 8a13912 commit c20366d

4 files changed

Lines changed: 59 additions & 38 deletions

File tree

verification/src/changes/accepted-core-public-api-changes.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,42 @@
2828
"CONSTRUCTOR_REMOVED"
2929
]
3030
}
31+
],
32+
"No one should be using these fields outside the class anyways": [
33+
{
34+
"type": "com.sk89q.worldedit.world.storage.FileMcRegionChunkStore",
35+
"member": "Class com.sk89q.worldedit.world.storage.FileMcRegionChunkStore",
36+
"changes": [
37+
"FIELD_REMOVED_IN_SUPERCLASS"
38+
]
39+
},
40+
{
41+
"type": "com.sk89q.worldedit.world.storage.McRegionChunkStore",
42+
"member": "Field cachedReader",
43+
"changes": [
44+
"FIELD_REMOVED"
45+
]
46+
},
47+
{
48+
"type": "com.sk89q.worldedit.world.storage.McRegionChunkStore",
49+
"member": "Field curFilename",
50+
"changes": [
51+
"FIELD_REMOVED"
52+
]
53+
},
54+
{
55+
"type": "com.sk89q.worldedit.world.storage.TrueZipMcRegionChunkStore",
56+
"member": "Class com.sk89q.worldedit.world.storage.TrueZipMcRegionChunkStore",
57+
"changes": [
58+
"FIELD_REMOVED_IN_SUPERCLASS"
59+
]
60+
},
61+
{
62+
"type": "com.sk89q.worldedit.world.storage.ZippedMcRegionChunkStore",
63+
"member": "Class com.sk89q.worldedit.world.storage.ZippedMcRegionChunkStore",
64+
"changes": [
65+
"FIELD_REMOVED_IN_SUPERCLASS"
66+
]
67+
}
3168
]
3269
}

worldedit-core/src/main/java/com/sk89q/worldedit/world/snapshot/SnapshotRestore.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.sk89q.worldedit.world.chunk.Chunk;
3030
import com.sk89q.worldedit.world.storage.ChunkStore;
3131
import com.sk89q.worldedit.world.storage.MissingChunkException;
32+
import org.apache.logging.log4j.LogManager;
33+
import org.apache.logging.log4j.Logger;
3234

3335
import java.io.IOException;
3436
import java.util.ArrayList;
@@ -41,6 +43,8 @@
4143
*/
4244
public class SnapshotRestore {
4345

46+
private static final Logger LOGGER = LogManager.getLogger();
47+
4448
private final Map<BlockVector2, ArrayList<BlockVector3>> neededChunks = new LinkedHashMap<>();
4549
private final ChunkStore chunkStore;
4650
private final EditSession editSession;
@@ -154,6 +158,7 @@ public void restore() throws MaxChangedBlocksException {
154158
} catch (MissingChunkException me) {
155159
missingChunks.add(chunkPos);
156160
} catch (IOException | DataException me) {
161+
LOGGER.info(() -> "Failed to load chunk at " + chunkPos, me);
157162
errorChunks.add(chunkPos);
158163
lastErrorMessage = me.getMessage();
159164
}

worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/McRegionChunkStore.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232

3333
public abstract class McRegionChunkStore extends ChunkStore {
3434

35-
protected String curFilename = null;
36-
protected McRegionReader cachedReader = null;
37-
3835
/**
3936
* Get the filename of a region file.
4037
*
@@ -50,26 +47,14 @@ public static String getFilename(BlockVector2 position) {
5047

5148
protected McRegionReader getReader(BlockVector2 pos, String worldname) throws DataException, IOException {
5249
String filename = getFilename(pos);
53-
if (curFilename != null) {
54-
if (curFilename.equals(filename)) {
55-
return cachedReader;
56-
} else {
57-
try {
58-
cachedReader.close();
59-
} catch (IOException ignored) {
60-
}
61-
}
62-
}
6350
InputStream stream = getInputStream(filename, worldname);
64-
cachedReader = new McRegionReader(stream);
65-
//curFilename = filename;
66-
return cachedReader;
51+
return new McRegionReader(stream);
6752
}
6853

6954
@Override
7055
public LinCompoundTag getChunkData(BlockVector2 position, World world) throws DataException, IOException {
71-
McRegionReader reader = getReader(position, world.getName());
72-
try (var chunkStream = new DataInputStream(reader.getChunkInputStream(position))) {
56+
try (McRegionReader reader = getReader(position, world.getName());
57+
var chunkStream = new DataInputStream(reader.getChunkInputStream(position))) {
7358
return LinBinaryIO.readUsing(chunkStream, LinRootEntry::readFrom).value();
7459
}
7560
}
@@ -84,11 +69,4 @@ public LinCompoundTag getChunkData(BlockVector2 position, World world) throws Da
8469
*/
8570
protected abstract InputStream getInputStream(String name, String worldName) throws IOException, DataException;
8671

87-
@Override
88-
public void close() throws IOException {
89-
if (cachedReader != null) {
90-
cachedReader.close();
91-
}
92-
}
93-
9472
}

worldedit-core/src/main/java/com/sk89q/worldedit/world/storage/McRegionReader.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ counts. The chunk offset for a chunk (x, z) begins at byte 4*(x+z*32) in the
6060
import com.sk89q.worldedit.world.DataException;
6161

6262
import java.io.ByteArrayInputStream;
63+
import java.io.Closeable;
6364
import java.io.DataInputStream;
65+
import java.io.EOFException;
6466
import java.io.IOException;
6567
import java.io.InputStream;
6668
import java.util.zip.GZIPInputStream;
@@ -70,7 +72,7 @@ counts. The chunk offset for a chunk (x, z) begins at byte 4*(x+z*32) in the
7072
* Reader for a MCRegion file. This reader works on input streams, meaning
7173
* that it can be used to read files from non-file based sources.
7274
*/
73-
public class McRegionReader {
75+
public class McRegionReader implements Closeable {
7476

7577
protected static final int VERSION_GZIP = 1;
7678
protected static final int VERSION_DEFLATE = 2;
@@ -129,10 +131,10 @@ public synchronized InputStream getChunkInputStream(BlockVector2 position) throw
129131
throw new DataException("The chunk at " + position + " is not generated");
130132
}
131133

132-
int sectorNumber = offset >> 8;
134+
long sectorNumber = offset >> 8;
133135
int numSectors = offset & 0xFF;
134136

135-
stream.seek((long) sectorNumber * SECTOR_BYTES);
137+
stream.seek(sectorNumber * SECTOR_BYTES);
136138
int length = dataStream.readInt();
137139

138140
if (length > SECTOR_BYTES * numSectors) {
@@ -142,19 +144,17 @@ public synchronized InputStream getChunkInputStream(BlockVector2 position) throw
142144

143145
byte version = dataStream.readByte();
144146

147+
byte[] data = new byte[length - 1];
148+
try {
149+
dataStream.readFully(data);
150+
} catch (EOFException e) {
151+
throw new DataException("MCRegion file does not contain "
152+
+ x + "," + z + " in full");
153+
}
154+
145155
if (version == VERSION_GZIP) {
146-
byte[] data = new byte[length - 1];
147-
if (dataStream.read(data) < length - 1) {
148-
throw new DataException("MCRegion file does not contain "
149-
+ x + "," + z + " in full");
150-
}
151156
return new GZIPInputStream(new ByteArrayInputStream(data));
152157
} else if (version == VERSION_DEFLATE) {
153-
byte[] data = new byte[length - 1];
154-
if (dataStream.read(data) < length - 1) {
155-
throw new DataException("MCRegion file does not contain "
156-
+ x + "," + z + " in full");
157-
}
158158
return new InflaterInputStream(new ByteArrayInputStream(data));
159159
} else {
160160
throw new DataException("MCRegion chunk at "
@@ -187,6 +187,7 @@ public boolean hasChunk(int x, int z) {
187187
/**
188188
* Close the stream.
189189
*/
190+
@Override
190191
public void close() throws IOException {
191192
stream.close();
192193
}

0 commit comments

Comments
 (0)