-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
RegionFileCache.java
74 lines (64 loc) · 2.13 KB
/
RegionFileCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package me.nallar.tickthreading.minecraft.storage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import cpw.mods.fml.common.FMLLog;
import net.minecraft.world.chunk.storage.RegionFile;
public class RegionFileCache {
private final Map<Long, RegionFile> regionFileMap = new HashMap<Long, RegionFile>();
private final File regionDir;
private boolean closed = false;
public RegionFileCache(File worldDir) {
regionDir = new File(worldDir, "region").getAbsoluteFile();
if (!regionDir.isDirectory() && !regionDir.mkdirs()) {
throw new Error("Failed to create region directory: " + regionDir);
}
}
private static long hash(int x, int z) {
return (((long) x) << 32) | (z & 0xffffffffL);
}
public RegionFile get(int x, int z) {
if (closed) {
throw new IllegalStateException("RegionFileCache already closed");
}
long hash = hash(x, z);
RegionFile regionFile = regionFileMap.get(hash);
if (regionFile != null) {
return regionFile;
}
synchronized (this) {
regionFile = regionFileMap.get(hash);
if (regionFile != null) {
return regionFile;
}
String location = "r." + x + '.' + z + ".mca";
File regionFileFile = new File(regionDir, location);
regionFile = RegionFileFixer.newRegionFile(regionFileFile);
if (regionFileMap.put(hash, regionFile) != null) {
throw new Error("Region file recreated concurrently. This should never happen.");
}
return regionFile;
}
}
public void close() {
closed = true;
for (RegionFile regionFile : regionFileMap.values()) {
try {
regionFile.close();
} catch (IOException e) {
FMLLog.log(Level.WARNING, e, "Failed to close regionFile");
}
}
regionFileMap.clear();
}
public DataOutputStream getChunkOutputStream(int x, int z) {
return get(x >> 5, z >> 5).getChunkDataOutputStream(x & 31, z & 31);
}
public DataInputStream getChunkInputStream(int x, int z) {
return get(x >> 5, z >> 5).getChunkDataInputStream(x & 31, z & 31);
}
}