Remove hunk writers & old nbt

This commit is contained in:
cyberpwn
2021-08-24 22:26:22 -04:00
parent 7b6405fba7
commit 401ed0a7a5
24 changed files with 1 additions and 2102 deletions

View File

@@ -21,14 +21,13 @@ package com.volmit.iris.util.hunk;
import com.volmit.iris.engine.object.basic.IrisPosition;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.function.*;
import com.volmit.iris.util.hunk.io.HunkIOAdapter;
import com.volmit.iris.util.hunk.storage.*;
import com.volmit.iris.util.hunk.view.*;
import com.volmit.iris.util.interpolation.InterpolationMethod;
import com.volmit.iris.util.interpolation.InterpolationMethod3D;
import com.volmit.iris.util.interpolation.IrisInterpolation;
import com.volmit.iris.util.math.BlockPosition;
import com.volmit.iris.util.oldnbt.ByteArrayTag;
import com.volmit.iris.util.nbt.tag.ByteArrayTag;
import com.volmit.iris.util.parallel.BurstExecutor;
import com.volmit.iris.util.parallel.MultiBurst;
import com.volmit.iris.util.stream.interpolation.Interpolated;
@@ -229,18 +228,6 @@ public interface Hunk<T> {
return count.get();
}
default void write(OutputStream s, HunkIOAdapter<T> h) throws IOException {
h.write(this, s);
}
default ByteArrayTag writeByteArrayTag(HunkIOAdapter<T> h, String name) throws IOException {
return h.writeByteArrayTag(this, name);
}
default void write(File s, HunkIOAdapter<T> h) throws IOException {
h.write(this, s);
}
default boolean isAtomic() {
return false;
}

View File

@@ -1,81 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.hunk.io;
import com.volmit.iris.Iris;
import com.volmit.iris.util.function.Function3;
import com.volmit.iris.util.hunk.Hunk;
import java.io.*;
import java.util.concurrent.atomic.AtomicBoolean;
public abstract class BasicHunkIOAdapter<T> implements HunkIOAdapter<T> {
@Override
public void write(Hunk<T> t, OutputStream out) throws IOException {
DataOutputStream dos = new DataOutputStream(out);
dos.writeShort(t.getWidth() + Short.MIN_VALUE);
dos.writeShort(t.getHeight() + Short.MIN_VALUE);
dos.writeShort(t.getDepth() + Short.MIN_VALUE);
dos.writeInt(t.getNonNullEntries() + Integer.MIN_VALUE);
AtomicBoolean failure = new AtomicBoolean(false);
t.iterate(0, (x, y, z, w) -> {
if (w != null) {
try {
dos.writeShort(x + Short.MIN_VALUE);
dos.writeShort(y + Short.MIN_VALUE);
dos.writeShort(z + Short.MIN_VALUE);
write(w, dos);
} catch (Throwable e) {
Iris.reportError(e);
e.printStackTrace();
failure.set(true);
}
}
});
dos.close();
}
@Override
public Hunk<T> read(Function3<Integer, Integer, Integer, Hunk<T>> factory, InputStream in) throws IOException {
DataInputStream din = new DataInputStream(in);
int w = din.readShort() - Short.MIN_VALUE;
int h = din.readShort() - Short.MIN_VALUE;
int d = din.readShort() - Short.MIN_VALUE;
int e = din.readInt() - Integer.MIN_VALUE;
Hunk<T> t = factory.apply(w, h, d);
for (int i = 0; i < e; i++) {
int x = din.readShort() - Short.MIN_VALUE;
int y = din.readShort() - Short.MIN_VALUE;
int z = din.readShort() - Short.MIN_VALUE;
T v = read(din);
if (v == null) {
throw new IOException("NULL VALUE AT " + x + " " + y + " " + z);
}
t.setRaw(x, y, z, v);
}
in.close();
return t;
}
}

View File

@@ -1,56 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.hunk.io;
import com.volmit.iris.util.data.IOAdapter;
import com.volmit.iris.util.function.Function3;
import com.volmit.iris.util.hunk.Hunk;
import com.volmit.iris.util.io.CustomOutputStream;
import com.volmit.iris.util.oldnbt.ByteArrayTag;
import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public interface HunkIOAdapter<T> extends IOAdapter<T> {
void write(Hunk<T> t, OutputStream out) throws IOException;
Hunk<T> read(Function3<Integer, Integer, Integer, Hunk<T>> factory, InputStream in) throws IOException;
default void write(Hunk<T> t, File f) throws IOException {
f.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(f);
GZIPOutputStream gzo = new CustomOutputStream(fos, 6);
write(t, gzo);
}
default Hunk<T> read(Function3<Integer, Integer, Integer, Hunk<T>> factory, File f) throws IOException {
return read(factory, new GZIPInputStream(new FileInputStream(f)));
}
default Hunk<T> read(Function3<Integer, Integer, Integer, Hunk<T>> factory, ByteArrayTag f) throws IOException {
return read(factory, new ByteArrayInputStream(f.getValue()));
}
default ByteArrayTag writeByteArrayTag(Hunk<T> tHunk, String name) throws IOException {
ByteArrayOutputStream boas = new ByteArrayOutputStream();
write(tHunk, boas);
return new ByteArrayTag(name, boas.toByteArray());
}
}

View File

@@ -1,101 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.hunk.io;
import com.volmit.iris.Iris;
import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.oldnbt.CompoundTag;
import com.volmit.iris.util.oldnbt.NBTInputStream;
import com.volmit.iris.util.oldnbt.NBTOutputStream;
import com.volmit.iris.util.oldnbt.Tag;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
@SuppressWarnings("SynchronizeOnNonFinalField")
public class HunkRegion {
private final File folder;
private CompoundTag compound;
private final int x;
private final int z;
public HunkRegion(File folder, int x, int z, CompoundTag compound) {
this.compound = fix(compound);
this.folder = folder;
this.x = x;
this.z = z;
folder.mkdirs();
}
public HunkRegion(File folder, int x, int z) {
this(folder, x, z, new CompoundTag(x + "." + z, new KMap<>()));
File f = getFile();
if (f.exists()) {
try {
NBTInputStream in = new NBTInputStream(new FileInputStream(f));
compound = fix((CompoundTag) in.readTag());
in.close();
} catch (Throwable e) {
Iris.reportError(e);
}
}
}
public CompoundTag getCompound() {
return compound;
}
private CompoundTag fix(CompoundTag readTag) {
Map<String, Tag> v = readTag.getValue();
if (!(v instanceof KMap)) {
return new CompoundTag(readTag.getName(), new KMap<>(v));
}
return readTag;
}
public File getFile() {
return new File(folder, x + "." + z + ".dat");
}
public void save() throws IOException {
synchronized (compound) {
File f = getFile();
FileOutputStream fos = new FileOutputStream(f);
NBTOutputStream out = new NBTOutputStream(fos);
out.writeTag(compound);
out.close();
}
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
}

View File

@@ -1,262 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.hunk.io;
import com.volmit.iris.Iris;
import com.volmit.iris.engine.object.tile.TileData;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.collection.KSet;
import com.volmit.iris.util.function.Function2;
import com.volmit.iris.util.function.Function3;
import com.volmit.iris.util.hunk.Hunk;
import com.volmit.iris.util.math.M;
import com.volmit.iris.util.math.Position2;
import com.volmit.iris.util.oldnbt.ByteArrayTag;
import com.volmit.iris.util.oldnbt.CompoundTag;
import com.volmit.iris.util.oldnbt.Tag;
import com.volmit.iris.util.parallel.MultiBurst;
import org.bukkit.block.TileState;
import org.bukkit.block.data.BlockData;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
public class HunkRegionSlice<T> {
public static final Function2<Integer, CompoundTag, HunkRegionSlice<BlockData>> BLOCKDATA = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new BlockDataHunkIOAdapter(), c, "blockdata");
public static final Function2<Integer, CompoundTag, HunkRegionSlice<TileData<? extends TileState>>> TILE = (h, c) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new TileDataHunkIOAdapter(), c, "tile");
public static final Function3<Integer, CompoundTag, String, HunkRegionSlice<String>> STRING = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new StringHunkIOAdapter(), c, t);
public static final Function3<Integer, CompoundTag, String, HunkRegionSlice<Boolean>> BOOLEAN = (h, c, t) -> new HunkRegionSlice<>(h, Hunk::newMappedHunk, new BooleanHunkIOAdapter(), c, t);
private final Function3<Integer, Integer, Integer, Hunk<T>> factory;
private final HunkIOAdapter<T> adapter;
private final CompoundTag compound;
private final String key;
private final KMap<Position2, Hunk<T>> loadedChunks;
private final KMap<Position2, Long> lastUse;
private final KSet<Position2> save;
private final int height;
public HunkRegionSlice(int height, Function3<Integer, Integer, Integer, Hunk<T>> factory, HunkIOAdapter<T> adapter, CompoundTag compound, String key) {
this.height = height;
this.loadedChunks = new KMap<>();
this.factory = factory;
this.adapter = adapter;
this.compound = compound;
this.save = new KSet<>();
this.key = key;
this.lastUse = new KMap<>();
}
public synchronized int cleanup(long t) {
int v = 0;
if (loadedChunks.size() != lastUse.size()) {
Iris.warn("Incorrect chunk use counts in " + key + " region slice.");
for (Position2 i : lastUse.k()) {
if (!loadedChunks.containsKey(i)) {
Iris.warn(" Missing LoadChunkKey " + i);
}
}
}
for (Position2 i : lastUse.k()) {
Long l = lastUse.get(i);
if (l == null || M.ms() - l > t) {
v++;
MultiBurst.burst.lazy(() -> {
unload(i.getX(), i.getZ());
});
}
}
return v;
}
public synchronized void clear() {
for (String i : new KList<>(compound.getValue().keySet())) {
if (i.startsWith(key + ".")) {
compound.getValue().remove(i);
}
}
}
public synchronized void save(MultiBurst burst) {
try {
for (Position2 i : save.copy()) {
if (i == null) {
continue;
}
save(i.getX(), i.getZ());
try {
save.remove(i);
} catch (Throwable eer) {
Iris.reportError(eer);
}
}
} catch (Throwable ee) {
Iris.reportError(ee);
}
}
public boolean contains(int x, int z) {
return compound.getValue().containsKey(key(x, z));
}
public void delete(int x, int z) {
compound.getValue().remove(key(x, z));
}
public Hunk<T> read(int x, int z) throws IOException {
AtomicReference<IOException> e = new AtomicReference<>();
Hunk<T> xt = null;
Tag t = compound.getValue().get(key(x, z));
if ((t instanceof ByteArrayTag)) {
try {
xt = adapter.read(factory, (ByteArrayTag) t);
} catch (IOException xe) {
Iris.reportError(xe);
e.set(xe);
}
}
if (xt != null) {
return xt;
}
if (e.get() != null) {
throw e.get();
}
return null;
}
public void write(Hunk<T> hunk, int x, int z) throws IOException {
compound.getValue().put(key(x, z), hunk.writeByteArrayTag(adapter, key(x, z)));
}
public synchronized int unloadAll() {
int v = 0;
for (Position2 i : loadedChunks.k()) {
unload(i.getX(), i.getZ());
v++;
}
save.clear();
loadedChunks.clear();
lastUse.clear();
return v;
}
public void save(Hunk<T> region, int x, int z) {
try {
write(region, x, z);
} catch (IOException e) {
Iris.reportError(e);
e.printStackTrace();
}
}
public boolean isLoaded(int x, int z) {
return loadedChunks.containsKey(new Position2(x, z));
}
public void save(int x, int z) {
if (isLoaded(x, z)) {
save(get(x, z), x, z);
}
}
public void unload(int x, int z) {
Position2 key = new Position2(x, z);
if (isLoaded(x, z)) {
if (save.contains(key)) {
save(x, z);
save.remove(key);
}
lastUse.remove(key);
loadedChunks.remove(key);
}
}
public Hunk<T> load(int x, int z) {
if (isLoaded(x, z)) {
return loadedChunks.get(new Position2(x, z));
}
Hunk<T> v = null;
if (contains(x, z)) {
try {
v = read(x, z);
} catch (IOException e) {
Iris.reportError(e);
e.printStackTrace();
}
}
if (v == null) {
v = factory.apply(16, height, 16);
}
loadedChunks.put(new Position2(x, z), v);
return v;
}
public Hunk<T> get(int x, int z) {
Position2 key = new Position2(x, z);
Hunk<T> c = loadedChunks.get(key);
if (c == null) {
c = load(x, z);
}
lastUse.put(new Position2(x, z), M.ms());
return c;
}
public Hunk<T> getR(int x, int z) {
return get(x, z).readOnly();
}
public Hunk<T> getRW(int x, int z) {
save.add(new Position2(x, z));
return get(x, z);
}
private String key(int x, int z) {
if (x < 0 || x >= 32 || z < 0 || z >= 32) {
throw new IndexOutOfBoundsException("The chunk " + x + " " + z + " is out of bounds max is 31x31");
}
return key + "." + x + "." + z;
}
public int getLoadCount() {
return loadedChunks.size();
}
}

View File

@@ -1,97 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.hunk.io;
import com.volmit.iris.Iris;
import com.volmit.iris.util.data.DataPalette;
import com.volmit.iris.util.function.Function3;
import com.volmit.iris.util.hunk.Hunk;
import java.io.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public abstract class PaletteHunkIOAdapter<T> implements HunkIOAdapter<T> {
@Override
public void write(Hunk<T> t, OutputStream out) throws IOException {
DataOutputStream dos = new DataOutputStream(out);
dos.writeShort(t.getWidth() + Short.MIN_VALUE);
dos.writeShort(t.getHeight() + Short.MIN_VALUE);
dos.writeShort(t.getDepth() + Short.MIN_VALUE);
AtomicInteger nonNull = new AtomicInteger(0);
DataPalette<T> palette = new DataPalette<>();
t.iterateSync((x, y, z, w) -> {
if (w != null) {
palette.getIndex(w);
nonNull.getAndAdd(1);
}
});
palette.write(this, dos);
dos.writeInt(nonNull.get() + Integer.MIN_VALUE);
AtomicBoolean failure = new AtomicBoolean(false);
t.iterateSync((x, y, z, w) -> {
if (w != null) {
try {
dos.writeShort(x + Short.MIN_VALUE);
dos.writeShort(y + Short.MIN_VALUE);
dos.writeShort(z + Short.MIN_VALUE);
dos.writeShort(palette.getIndex(w) + Short.MIN_VALUE);
} catch (Throwable e) {
Iris.reportError(e);
e.printStackTrace();
failure.set(true);
}
}
});
dos.close();
}
@Override
public Hunk<T> read(Function3<Integer, Integer, Integer, Hunk<T>> factory, InputStream in) throws IOException {
DataInputStream din = new DataInputStream(in);
int w = din.readShort() - Short.MIN_VALUE;
int h = din.readShort() - Short.MIN_VALUE;
int d = din.readShort() - Short.MIN_VALUE;
DataPalette<T> palette = DataPalette.getPalette(this, din);
int e = din.readInt() - Integer.MIN_VALUE;
Hunk<T> t = factory.apply(w, h, d);
for (int i = 0; i < e; i++) {
int x = din.readShort() - Short.MIN_VALUE;
int y = din.readShort() - Short.MIN_VALUE;
int z = din.readShort() - Short.MIN_VALUE;
int vf = din.readShort() - Short.MIN_VALUE;
T v = null;
if (palette.getPalette().hasIndex(vf)) {
v = palette.getPalette().get(vf);
}
if (v != null) {
t.setRaw(x, y, z, v);
}
}
in.close();
return t;
}
}

View File

@@ -1,45 +0,0 @@
/*
* Iris is a World Generator for Minecraft Bukkit Servers
* Copyright (c) 2021 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.volmit.iris.util.hunk.io;
import com.volmit.iris.Iris;
import com.volmit.iris.engine.object.tile.TileData;
import org.bukkit.block.TileState;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class TileDataHunkIOAdapter extends PaletteHunkIOAdapter<TileData<? extends TileState>> {
@Override
public void write(TileData<? extends TileState> data, DataOutputStream dos) throws IOException {
data.toBinary(dos);
}
@Override
public TileData<? extends TileState> read(DataInputStream din) throws IOException {
try {
return TileData.read(din);
} catch (Throwable e) {
Iris.reportError(e);
e.printStackTrace();
throw new IOException();
}
}
}