mirror of
https://github.com/webbukkit/dynmap.git
synced 2024-11-24 11:15:21 +01:00
fabric-1.15.2: backport GenericChunkCache from fabric-1.16.4
This commit is contained in:
parent
2f3f2350b3
commit
ce3cb3ab6b
@ -1,304 +0,0 @@
|
||||
package org.dynmap.fabric_1_15_2;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
//import net.minecraft.util.collection.PackedIntegerArray;
|
||||
import net.minecraft.util.PackedIntegerArray;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
//import net.minecraft.util.math.WordPackedArray;
|
||||
import org.dynmap.Log;
|
||||
import org.dynmap.renderer.DynmapBlockState;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Represents a static, thread-safe snapshot of chunk of blocks
|
||||
* Purpose is to allow clean, efficient copy of a chunk data to be made, and then handed off for processing in another thread (e.g. map rendering)
|
||||
*/
|
||||
public class ChunkSnapshot {
|
||||
private static interface Section {
|
||||
public DynmapBlockState getBlockType(int x, int y, int z);
|
||||
|
||||
public int getBlockSkyLight(int x, int y, int z);
|
||||
|
||||
public int getBlockEmittedLight(int x, int y, int z);
|
||||
|
||||
public boolean isEmpty();
|
||||
}
|
||||
|
||||
private final int x, z;
|
||||
private final Section[] section;
|
||||
private final int[] hmap; // Height map
|
||||
private final int[] biome;
|
||||
private final long captureFulltime;
|
||||
private final int sectionCnt;
|
||||
private final long inhabitedTicks;
|
||||
|
||||
private static final int BLOCKS_PER_SECTION = 16 * 16 * 16;
|
||||
private static final int COLUMNS_PER_CHUNK = 16 * 16;
|
||||
private static final byte[] emptyData = new byte[BLOCKS_PER_SECTION / 2];
|
||||
private static final byte[] fullData = new byte[BLOCKS_PER_SECTION / 2];
|
||||
|
||||
static {
|
||||
Arrays.fill(fullData, (byte) 0xFF);
|
||||
}
|
||||
|
||||
private static class EmptySection implements Section {
|
||||
@Override
|
||||
public DynmapBlockState getBlockType(int x, int y, int z) {
|
||||
return DynmapBlockState.AIR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockSkyLight(int x, int y, int z) {
|
||||
return 15;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockEmittedLight(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static final EmptySection empty_section = new EmptySection();
|
||||
|
||||
private static class StdSection implements Section {
|
||||
DynmapBlockState[] states;
|
||||
byte[] skylight;
|
||||
byte[] emitlight;
|
||||
|
||||
public StdSection() {
|
||||
states = new DynmapBlockState[BLOCKS_PER_SECTION];
|
||||
Arrays.fill(states, DynmapBlockState.AIR);
|
||||
skylight = emptyData;
|
||||
emitlight = emptyData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DynmapBlockState getBlockType(int x, int y, int z) {
|
||||
return states[((y & 0xF) << 8) | (z << 4) | x];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockSkyLight(int x, int y, int z) {
|
||||
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
|
||||
return (skylight[off] >> (4 * (x & 1))) & 0xF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockEmittedLight(int x, int y, int z) {
|
||||
int off = ((y & 0xF) << 7) | (z << 3) | (x >> 1);
|
||||
return (emitlight[off] >> (4 * (x & 1))) & 0xF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct empty chunk snapshot
|
||||
*
|
||||
* @param x
|
||||
* @param z
|
||||
*/
|
||||
public ChunkSnapshot(int worldheight, int x, int z, long captime, long inhabitedTime) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.captureFulltime = captime;
|
||||
this.biome = new int[COLUMNS_PER_CHUNK];
|
||||
this.sectionCnt = worldheight / 16;
|
||||
/* Allocate arrays indexed by section */
|
||||
this.section = new Section[this.sectionCnt+1];
|
||||
|
||||
/* Fill with empty data */
|
||||
for (int i = 0; i <= this.sectionCnt; i++) {
|
||||
this.section[i] = empty_section;
|
||||
}
|
||||
|
||||
/* Create empty height map */
|
||||
this.hmap = new int[16 * 16];
|
||||
|
||||
this.inhabitedTicks = inhabitedTime;
|
||||
}
|
||||
|
||||
public static class StateListException extends Exception {
|
||||
private static boolean loggedOnce = false;
|
||||
|
||||
public StateListException(int x, int z, int actualLength, int expectedLength, int expectedLegacyLength) {
|
||||
if (Log.verbose || !loggedOnce) {
|
||||
loggedOnce = true;
|
||||
Log.info("Skipping chunk at x=" + x + ",z=" + z + ". Expected statelist of length " + expectedLength + " or " + expectedLegacyLength + " but got " + actualLength + ". This can happen if the chunk was not yet converted to the 1.16 format which can be fixed by visiting the chunk.");
|
||||
if (!Log.verbose) {
|
||||
Log.info("You will only see this message once. Turn on verbose logging in the configuration to see all messages.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ChunkSnapshot(CompoundTag nbt, int worldheight) throws StateListException {
|
||||
this.x = nbt.getInt("xPos");
|
||||
this.z = nbt.getInt("zPos");
|
||||
this.captureFulltime = 0;
|
||||
this.hmap = nbt.getIntArray("HeightMap");
|
||||
this.sectionCnt = worldheight / 16;
|
||||
if (nbt.contains("InhabitedTime")) {
|
||||
this.inhabitedTicks = nbt.getLong("InhabitedTime");
|
||||
} else {
|
||||
this.inhabitedTicks = 0;
|
||||
}
|
||||
/* Allocate arrays indexed by section */
|
||||
this.section = new Section[this.sectionCnt+1];
|
||||
/* Fill with empty data */
|
||||
for (int i = 0; i <= this.sectionCnt; i++) {
|
||||
this.section[i] = empty_section;
|
||||
}
|
||||
/* Get sections */
|
||||
ListTag sect = nbt.getList("Sections", 10);
|
||||
for (int i = 0; i < sect.size(); i++) {
|
||||
CompoundTag sec = sect.getCompound(i);
|
||||
int secnum = sec.getByte("Y");
|
||||
if (secnum >= this.sectionCnt) {
|
||||
//Log.info("Section " + (int) secnum + " above world height " + worldheight);
|
||||
continue;
|
||||
}
|
||||
if (secnum < 0)
|
||||
continue;
|
||||
//Log.info("section(" + secnum + ")=" + sec.asString());
|
||||
// Create normal section to initialize
|
||||
StdSection cursect = new StdSection();
|
||||
this.section[secnum] = cursect;
|
||||
DynmapBlockState[] states = cursect.states;
|
||||
DynmapBlockState[] palette = null;
|
||||
// If we've got palette and block states list, process non-empty section
|
||||
if (sec.contains("Palette", 9) && sec.contains("BlockStates", 12)) {
|
||||
ListTag plist = sec.getList("Palette", 10);
|
||||
long[] statelist = sec.getLongArray("BlockStates");
|
||||
palette = new DynmapBlockState[plist.size()];
|
||||
for (int pi = 0; pi < plist.size(); pi++) {
|
||||
CompoundTag tc = plist.getCompound(pi);
|
||||
String pname = tc.getString("Name");
|
||||
if (tc.contains("Properties")) {
|
||||
StringBuilder statestr = new StringBuilder();
|
||||
CompoundTag prop = tc.getCompound("Properties");
|
||||
for (String pid : prop.getKeys()) {
|
||||
if (statestr.length() > 0) statestr.append(',');
|
||||
statestr.append(pid).append('=').append(prop.get(pid).asString());
|
||||
}
|
||||
palette[pi] = DynmapBlockState.getStateByNameAndState(pname, statestr.toString());
|
||||
}
|
||||
if (palette[pi] == null) {
|
||||
palette[pi] = DynmapBlockState.getBaseStateByName(pname);
|
||||
}
|
||||
if (palette[pi] == null) {
|
||||
palette[pi] = DynmapBlockState.AIR;
|
||||
}
|
||||
}
|
||||
|
||||
PackedIntegerArray db = null;
|
||||
WordPackedArray dbp = null;
|
||||
|
||||
int bitsperblock = (statelist.length * 64) / 4096;
|
||||
// PackedIntegerArray db = new PackedIntegerArray(bitsperblock, 4096, statelist);
|
||||
int expectedStatelistLength = (4096 + (64 / bitsperblock) - 1) / (64 / bitsperblock);
|
||||
if (statelist.length == expectedStatelistLength) {
|
||||
db = new PackedIntegerArray(bitsperblock, 4096, statelist);
|
||||
} else {
|
||||
int expectedLegacyStatelistLength = MathHelper.roundUp(bitsperblock * 4096, 64) / 64;
|
||||
if (statelist.length == expectedLegacyStatelistLength) {
|
||||
dbp = new WordPackedArray(bitsperblock, 4096, statelist);
|
||||
} else {
|
||||
throw new StateListException(x, z, statelist.length, expectedStatelistLength, expectedLegacyStatelistLength);
|
||||
}
|
||||
}
|
||||
|
||||
if (bitsperblock > 8) { // Not palette
|
||||
for (int j = 0; j < 4096; j++) {
|
||||
int v = db != null ? db.get(j) : dbp.get(j);
|
||||
states[j] = DynmapBlockState.getStateByGlobalIndex(v);
|
||||
}
|
||||
} else {
|
||||
for (int j = 0; j < 4096; j++) {
|
||||
int v = db != null ? db.get(j) : dbp.get(j);
|
||||
states[j] = (v < palette.length) ? palette[v] : DynmapBlockState.AIR;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sec.contains("BlockLight")) {
|
||||
cursect.emitlight = sec.getByteArray("BlockLight");
|
||||
}
|
||||
if (sec.contains("SkyLight")) {
|
||||
cursect.skylight = sec.getByteArray("SkyLight");
|
||||
}
|
||||
}
|
||||
/* Get biome data */
|
||||
this.biome = new int[COLUMNS_PER_CHUNK];
|
||||
if (nbt.contains("Biomes")) {
|
||||
int[] bb = nbt.getIntArray("Biomes");
|
||||
if (bb != null) {
|
||||
// If v1.15+ format
|
||||
if (bb.length > COLUMNS_PER_CHUNK) {
|
||||
// For now, just pad the grid with the first 16
|
||||
for (int i = 0; i < COLUMNS_PER_CHUNK; i++) {
|
||||
int off = ((i >> 4) & 0xC) + ((i >> 2) & 0x3);
|
||||
int bv = bb[off + 64]; // Offset to y=64
|
||||
if (bv < 0) bv = 0;
|
||||
this.biome[i] = bv;
|
||||
}
|
||||
} else { // Else, older chunks
|
||||
for (int i = 0; i < bb.length; i++) {
|
||||
int bv = bb[i];
|
||||
if (bv < 0) bv = 0;
|
||||
this.biome[i] = bv;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public DynmapBlockState getBlockType(int x, int y, int z) {
|
||||
return section[y >> 4].getBlockType(x, y, z);
|
||||
}
|
||||
|
||||
public int getBlockSkyLight(int x, int y, int z) {
|
||||
return section[y >> 4].getBlockSkyLight(x, y, z);
|
||||
}
|
||||
|
||||
public int getBlockEmittedLight(int x, int y, int z) {
|
||||
return section[y >> 4].getBlockEmittedLight(x, y, z);
|
||||
}
|
||||
|
||||
public int getHighestBlockYAt(int x, int z) {
|
||||
return hmap[z << 4 | x];
|
||||
}
|
||||
|
||||
public int getBiome(int x, int z) {
|
||||
return biome[z << 4 | x];
|
||||
}
|
||||
|
||||
public final long getCaptureFullTime() {
|
||||
return captureFulltime;
|
||||
}
|
||||
|
||||
public boolean isSectionEmpty(int sy) {
|
||||
return section[sy].isEmpty();
|
||||
}
|
||||
|
||||
public long getInhabitedTicks() {
|
||||
return inhabitedTicks;
|
||||
}
|
||||
}
|
@ -43,6 +43,7 @@ import org.dynmap.common.BiomeMap;
|
||||
import org.dynmap.common.DynmapCommandSender;
|
||||
import org.dynmap.common.DynmapListenerManager;
|
||||
import org.dynmap.common.DynmapPlayer;
|
||||
import org.dynmap.common.chunk.GenericChunkCache;
|
||||
import org.dynmap.fabric_1_15_2.command.DmapCommand;
|
||||
import org.dynmap.fabric_1_15_2.event.BlockEvents;
|
||||
import org.dynmap.fabric_1_15_2.event.ChunkDataEvents;
|
||||
@ -68,7 +69,7 @@ public class DynmapPlugin {
|
||||
DynmapCore core;
|
||||
private PermissionProvider permissions;
|
||||
private boolean core_enabled;
|
||||
public SnapshotCache sscache;
|
||||
public GenericChunkCache sscache;
|
||||
public PlayerList playerList;
|
||||
MapManager mapManager;
|
||||
/**
|
||||
@ -460,7 +461,6 @@ public class DynmapPlugin {
|
||||
core.setMinecraftVersion(mcver);
|
||||
core.setDataFolder(dataDirectory);
|
||||
core.setServer(fserver);
|
||||
FabricMapChunkCache.init();
|
||||
core.setTriggerDefault(TRIGGER_DEFAULTS);
|
||||
core.setBiomeNames(getBiomeNames());
|
||||
|
||||
@ -513,7 +513,7 @@ public class DynmapPlugin {
|
||||
}
|
||||
|
||||
playerList = core.playerList;
|
||||
sscache = new SnapshotCache(core.getSnapShotCacheSize(), core.useSoftRefInSnapShotCache());
|
||||
sscache = new GenericChunkCache(core.getSnapShotCacheSize(), core.useSoftRefInSnapShotCache());
|
||||
/* Get map manager from core */
|
||||
mapManager = core.getMapManager();
|
||||
|
||||
@ -835,7 +835,7 @@ public class DynmapPlugin {
|
||||
FabricWorld fw = null;
|
||||
if (add_if_not_found) {
|
||||
/* Add to list if not found */
|
||||
fw = new FabricWorld(w);
|
||||
fw = new FabricWorld(plugin, w);
|
||||
worlds.put(fw.getName(), fw);
|
||||
}
|
||||
last_world = w;
|
||||
@ -894,7 +894,7 @@ public class DynmapPlugin {
|
||||
boolean theend = (Boolean) world.get("the_end");
|
||||
String title = (String) world.get("title");
|
||||
if (name != null) {
|
||||
FabricWorld fw = new FabricWorld(name, height, sealevel, nether, theend, title);
|
||||
FabricWorld fw = new FabricWorld(plugin, name, height, sealevel, nether, theend, title);
|
||||
fw.setWorldUnloaded();
|
||||
core.processWorldLoad(fw);
|
||||
worlds.put(fw.getName(), fw);
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -19,6 +19,7 @@ public class FabricWorld extends DynmapWorld {
|
||||
// TODO: Store this relative to World saves for integrated server
|
||||
public static final String SAVED_WORLDS_FILE = "fabricworlds.yml";
|
||||
private IWorld world;
|
||||
private final DynmapPlugin plugin;
|
||||
private final boolean skylight;
|
||||
private final boolean isnether;
|
||||
private final boolean istheend;
|
||||
@ -47,8 +48,8 @@ public class FabricWorld extends DynmapWorld {
|
||||
}
|
||||
}
|
||||
|
||||
public FabricWorld(IWorld w) {
|
||||
this(getWorldName(w), w.getWorld().getHeight(),
|
||||
public FabricWorld(DynmapPlugin plugin, IWorld w) {
|
||||
this(plugin, getWorldName(w), w.getWorld().getHeight(),
|
||||
w.getWorld().getSeaLevel(),
|
||||
w.getDimension().getType() == DimensionType.THE_NETHER,
|
||||
w.getDimension().getType() == DimensionType.THE_END, //DimensionType
|
||||
@ -56,8 +57,9 @@ public class FabricWorld extends DynmapWorld {
|
||||
setWorldLoaded(w);
|
||||
}
|
||||
|
||||
public FabricWorld(String name, int height, int sealevel, boolean nether, boolean the_end, String deftitle) {
|
||||
public FabricWorld(DynmapPlugin plugin, String name, int height, int sealevel, boolean nether, boolean the_end, String deftitle) {
|
||||
super(name, (height > maxWorldHeight) ? maxWorldHeight : height, sealevel);
|
||||
this.plugin = plugin;
|
||||
world = null;
|
||||
setTitle(deftitle);
|
||||
isnether = nether;
|
||||
@ -193,7 +195,7 @@ public class FabricWorld extends DynmapWorld {
|
||||
@Override
|
||||
public MapChunkCache getChunkCache(List<DynmapChunk> chunks) {
|
||||
if (world != null) {
|
||||
FabricMapChunkCache c = new FabricMapChunkCache();
|
||||
FabricMapChunkCache c = new FabricMapChunkCache(plugin);
|
||||
c.setChunks(this, chunks);
|
||||
return c;
|
||||
}
|
||||
|
120
fabric-1.15.2/src/main/java/org/dynmap/fabric_1_15_2/NBT.java
Normal file
120
fabric-1.15.2/src/main/java/org/dynmap/fabric_1_15_2/NBT.java
Normal file
@ -0,0 +1,120 @@
|
||||
package org.dynmap.fabric_1_15_2;
|
||||
|
||||
import org.dynmap.common.chunk.GenericBitStorage;
|
||||
import org.dynmap.common.chunk.GenericNBTCompound;
|
||||
import org.dynmap.common.chunk.GenericNBTList;
|
||||
|
||||
import java.util.Set;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.util.PackedIntegerArray;
|
||||
|
||||
public class NBT {
|
||||
|
||||
public static class NBTCompound implements GenericNBTCompound {
|
||||
private final CompoundTag obj;
|
||||
public NBTCompound(CompoundTag t) {
|
||||
this.obj = t;
|
||||
}
|
||||
@Override
|
||||
public Set<String> getAllKeys() {
|
||||
return obj.getKeys();
|
||||
}
|
||||
@Override
|
||||
public boolean contains(String s) {
|
||||
return obj.contains(s);
|
||||
}
|
||||
@Override
|
||||
public boolean contains(String s, int i) {
|
||||
return obj.contains(s, i);
|
||||
}
|
||||
@Override
|
||||
public byte getByte(String s) {
|
||||
return obj.getByte(s);
|
||||
}
|
||||
@Override
|
||||
public short getShort(String s) {
|
||||
return obj.getShort(s);
|
||||
}
|
||||
@Override
|
||||
public int getInt(String s) {
|
||||
return obj.getInt(s);
|
||||
}
|
||||
@Override
|
||||
public long getLong(String s) {
|
||||
return obj.getLong(s);
|
||||
}
|
||||
@Override
|
||||
public float getFloat(String s) {
|
||||
return obj.getFloat(s);
|
||||
}
|
||||
@Override
|
||||
public double getDouble(String s) {
|
||||
return obj.getDouble(s);
|
||||
}
|
||||
@Override
|
||||
public String getString(String s) {
|
||||
return obj.getString(s);
|
||||
}
|
||||
@Override
|
||||
public byte[] getByteArray(String s) {
|
||||
return obj.getByteArray(s);
|
||||
}
|
||||
@Override
|
||||
public int[] getIntArray(String s) {
|
||||
return obj.getIntArray(s);
|
||||
}
|
||||
@Override
|
||||
public long[] getLongArray(String s) {
|
||||
return obj.getLongArray(s);
|
||||
}
|
||||
@Override
|
||||
public GenericNBTCompound getCompound(String s) {
|
||||
return new NBTCompound(obj.getCompound(s));
|
||||
}
|
||||
@Override
|
||||
public GenericNBTList getList(String s, int i) {
|
||||
return new NBTList(obj.getList(s, i));
|
||||
}
|
||||
@Override
|
||||
public boolean getBoolean(String s) {
|
||||
return obj.getBoolean(s);
|
||||
}
|
||||
@Override
|
||||
public String getAsString(String s) {
|
||||
return obj.get(s).asString();
|
||||
}
|
||||
@Override
|
||||
public GenericBitStorage makeBitStorage(int bits, int count, long[] data) {
|
||||
return new OurBitStorage(bits, count, data);
|
||||
}
|
||||
}
|
||||
public static class NBTList implements GenericNBTList {
|
||||
private final ListTag obj;
|
||||
public NBTList(ListTag t) {
|
||||
obj = t;
|
||||
}
|
||||
@Override
|
||||
public int size() {
|
||||
return obj.size();
|
||||
}
|
||||
@Override
|
||||
public String getString(int idx) {
|
||||
return obj.getString(idx);
|
||||
}
|
||||
@Override
|
||||
public GenericNBTCompound getCompound(int idx) {
|
||||
return new NBTCompound(obj.getCompound(idx));
|
||||
}
|
||||
}
|
||||
public static class OurBitStorage implements GenericBitStorage {
|
||||
private final PackedIntegerArray bs;
|
||||
public OurBitStorage(int bits, int count, long[] data) {
|
||||
bs = new PackedIntegerArray(bits, count, data);
|
||||
}
|
||||
@Override
|
||||
public int get(int idx) {
|
||||
return bs.get(idx);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,201 +0,0 @@
|
||||
package org.dynmap.fabric_1_15_2;
|
||||
|
||||
import org.dynmap.utils.DynIntHashMap;
|
||||
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SnapshotCache {
|
||||
public static class SnapshotRec {
|
||||
public ChunkSnapshot ss;
|
||||
public DynIntHashMap tileData;
|
||||
}
|
||||
|
||||
private CacheHashMap snapcache;
|
||||
private ReferenceQueue<SnapshotRec> refqueue;
|
||||
private long cache_attempts;
|
||||
private long cache_success;
|
||||
private boolean softref;
|
||||
|
||||
private static class CacheRec {
|
||||
Reference<SnapshotRec> ref;
|
||||
boolean hasbiome;
|
||||
boolean hasrawbiome;
|
||||
boolean hasblockdata;
|
||||
boolean hashighesty;
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class CacheHashMap extends LinkedHashMap<String, CacheRec> {
|
||||
private int limit;
|
||||
private IdentityHashMap<Reference<SnapshotRec>, String> reverselookup;
|
||||
|
||||
public CacheHashMap(int lim) {
|
||||
super(16, (float) 0.75, true);
|
||||
limit = lim;
|
||||
reverselookup = new IdentityHashMap<Reference<SnapshotRec>, String>();
|
||||
}
|
||||
|
||||
protected boolean removeEldestEntry(Map.Entry<String, CacheRec> last) {
|
||||
boolean remove = (size() >= limit);
|
||||
if (remove && (last != null) && (last.getValue() != null)) {
|
||||
reverselookup.remove(last.getValue().ref);
|
||||
}
|
||||
return remove;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create snapshot cache
|
||||
*/
|
||||
public SnapshotCache(int max_size, boolean softref) {
|
||||
snapcache = new CacheHashMap(max_size);
|
||||
refqueue = new ReferenceQueue<SnapshotRec>();
|
||||
this.softref = softref;
|
||||
}
|
||||
|
||||
private String getKey(String w, int cx, int cz) {
|
||||
return w + ":" + cx + ":" + cz;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate cached snapshot, if in cache
|
||||
*/
|
||||
public void invalidateSnapshot(String w, int x, int y, int z) {
|
||||
String key = getKey(w, x >> 4, z >> 4);
|
||||
synchronized (snapcache) {
|
||||
CacheRec rec = snapcache.remove(key);
|
||||
if (rec != null) {
|
||||
snapcache.reverselookup.remove(rec.ref);
|
||||
rec.ref.clear();
|
||||
}
|
||||
}
|
||||
//processRefQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate cached snapshot, if in cache
|
||||
*/
|
||||
public void invalidateSnapshot(String w, int x0, int y0, int z0, int x1, int y1, int z1) {
|
||||
for (int xx = (x0 >> 4); xx <= (x1 >> 4); xx++) {
|
||||
for (int zz = (z0 >> 4); zz <= (z1 >> 4); zz++) {
|
||||
String key = getKey(w, xx, zz);
|
||||
synchronized (snapcache) {
|
||||
CacheRec rec = snapcache.remove(key);
|
||||
if (rec != null) {
|
||||
snapcache.reverselookup.remove(rec.ref);
|
||||
rec.ref.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//processRefQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for chunk snapshot in cache
|
||||
*/
|
||||
public SnapshotRec getSnapshot(String w, int chunkx, int chunkz,
|
||||
boolean blockdata, boolean biome, boolean biomeraw, boolean highesty) {
|
||||
String key = getKey(w, chunkx, chunkz);
|
||||
processRefQueue();
|
||||
SnapshotRec ss = null;
|
||||
CacheRec rec;
|
||||
synchronized (snapcache) {
|
||||
rec = snapcache.get(key);
|
||||
if (rec != null) {
|
||||
ss = rec.ref.get();
|
||||
if (ss == null) {
|
||||
snapcache.reverselookup.remove(rec.ref);
|
||||
snapcache.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ss != null) {
|
||||
if ((blockdata && (!rec.hasblockdata)) ||
|
||||
(biome && (!rec.hasbiome)) ||
|
||||
(biomeraw && (!rec.hasrawbiome)) ||
|
||||
(highesty && (!rec.hashighesty))) {
|
||||
ss = null;
|
||||
}
|
||||
}
|
||||
cache_attempts++;
|
||||
if (ss != null) cache_success++;
|
||||
|
||||
return ss;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add chunk snapshot to cache
|
||||
*/
|
||||
public void putSnapshot(String w, int chunkx, int chunkz, SnapshotRec ss,
|
||||
boolean blockdata, boolean biome, boolean biomeraw, boolean highesty) {
|
||||
String key = getKey(w, chunkx, chunkz);
|
||||
processRefQueue();
|
||||
CacheRec rec = new CacheRec();
|
||||
rec.hasblockdata = blockdata;
|
||||
rec.hasbiome = biome;
|
||||
rec.hasrawbiome = biomeraw;
|
||||
rec.hashighesty = highesty;
|
||||
if (softref)
|
||||
rec.ref = new SoftReference<SnapshotRec>(ss, refqueue);
|
||||
else
|
||||
rec.ref = new WeakReference<SnapshotRec>(ss, refqueue);
|
||||
synchronized (snapcache) {
|
||||
CacheRec prevrec = snapcache.put(key, rec);
|
||||
if (prevrec != null) {
|
||||
snapcache.reverselookup.remove(prevrec.ref);
|
||||
}
|
||||
snapcache.reverselookup.put(rec.ref, key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process reference queue
|
||||
*/
|
||||
private void processRefQueue() {
|
||||
Reference<? extends SnapshotRec> ref;
|
||||
while ((ref = refqueue.poll()) != null) {
|
||||
synchronized (snapcache) {
|
||||
String k = snapcache.reverselookup.remove(ref);
|
||||
if (k != null) {
|
||||
snapcache.remove(k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get hit rate (percent)
|
||||
*/
|
||||
public double getHitRate() {
|
||||
if (cache_attempts > 0) {
|
||||
return (100.0 * cache_success) / (double) cache_attempts;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset cache stats
|
||||
*/
|
||||
public void resetStats() {
|
||||
cache_attempts = cache_success = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup
|
||||
*/
|
||||
public void cleanup() {
|
||||
if (snapcache != null) {
|
||||
snapcache.clear();
|
||||
snapcache.reverselookup.clear();
|
||||
snapcache.reverselookup = null;
|
||||
snapcache = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
package org.dynmap.fabric_1_15_2;
|
||||
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
public class WordPackedArray {
|
||||
private final long[] array;
|
||||
private final int unitSize;
|
||||
private final long maxValue;
|
||||
private final int length;
|
||||
|
||||
public WordPackedArray(int unitSize, int length) {
|
||||
this(unitSize, length, new long[MathHelper.roundUp(length * unitSize, 64) / 64]);
|
||||
}
|
||||
|
||||
public WordPackedArray(int unitSize, int length, long[] array) {
|
||||
Validate.inclusiveBetween(1L, 32L, (long)unitSize);
|
||||
this.length = length;
|
||||
this.unitSize = unitSize;
|
||||
this.array = array;
|
||||
this.maxValue = (1L << unitSize) - 1L;
|
||||
int i = MathHelper.roundUp(length * unitSize, 64) / 64;
|
||||
if (array.length != i) {
|
||||
throw new IllegalArgumentException("Invalid length given for storage, got: " + array.length + " but expected: " + i);
|
||||
}
|
||||
}
|
||||
|
||||
public void set(int index, int value) {
|
||||
Validate.inclusiveBetween(0L, (long)(this.length - 1), (long)index);
|
||||
Validate.inclusiveBetween(0L, this.maxValue, (long)value);
|
||||
int i = index * this.unitSize;
|
||||
int j = i >> 6;
|
||||
int k = (index + 1) * this.unitSize - 1 >> 6;
|
||||
int l = i ^ j << 6;
|
||||
this.array[j] = this.array[j] & ~(this.maxValue << l) | ((long)value & this.maxValue) << l;
|
||||
if (j != k) {
|
||||
int m = 64 - l;
|
||||
int n = this.unitSize - m;
|
||||
this.array[k] = this.array[k] >>> n << n | ((long)value & this.maxValue) >> m;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int get(int index) {
|
||||
Validate.inclusiveBetween(0L, (long)(this.length - 1), (long)index);
|
||||
int i = index * this.unitSize;
|
||||
int j = i >> 6;
|
||||
int k = (index + 1) * this.unitSize - 1 >> 6;
|
||||
int l = i ^ j << 6;
|
||||
if (j == k) {
|
||||
return (int)(this.array[j] >>> l & this.maxValue);
|
||||
} else {
|
||||
int m = 64 - l;
|
||||
return (int)((this.array[j] >>> l | this.array[k] << m) & this.maxValue);
|
||||
}
|
||||
}
|
||||
|
||||
public long[] getAlignedArray() {
|
||||
return this.array;
|
||||
}
|
||||
|
||||
public int getUnitSize() {
|
||||
return this.unitSize;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user