From 2eba035a4b02b0a8a0f4834d16ced168b352fcf3 Mon Sep 17 00:00:00 2001 From: makamys Date: Fri, 7 May 2021 19:40:22 +0200 Subject: Save ChunkMeshes to disk (no loading yet) --- .../java/makamys/lodmod/renderer/ChunkMesh.java | 27 +++++++++++++++- .../java/makamys/lodmod/renderer/LODChunk.java | 17 ++++++++++ .../java/makamys/lodmod/renderer/LODRegion.java | 37 ++++++++++++++++++++++ .../java/makamys/lodmod/renderer/LODRenderer.java | 9 ++++++ src/main/java/makamys/lodmod/renderer/Mesh.java | 3 +- .../makamys/lodmod/renderer/SimpleChunkMesh.java | 1 + 6 files changed, 92 insertions(+), 2 deletions(-) (limited to 'src/main/java/makamys/lodmod/renderer') diff --git a/src/main/java/makamys/lodmod/renderer/ChunkMesh.java b/src/main/java/makamys/lodmod/renderer/ChunkMesh.java index 2251da8..45510ef 100644 --- a/src/main/java/makamys/lodmod/renderer/ChunkMesh.java +++ b/src/main/java/makamys/lodmod/renderer/ChunkMesh.java @@ -1,7 +1,9 @@ package makamys.lodmod.renderer; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.DataInputStream; +import java.io.DataOutputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; @@ -14,6 +16,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.lwjgl.BufferUtils; @@ -24,6 +27,9 @@ import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagByteArray; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; public class ChunkMesh extends Mesh { @@ -33,6 +39,8 @@ public class ChunkMesh extends Mesh { int z; Flags flags; + NBTBase nbtData; + public static int usedRAM = 0; public static int instances = 0; @@ -44,6 +52,7 @@ public class ChunkMesh extends Mesh { this.quadCount = quadCount; this.buffer = createBuffer(data, stringTable); + this.nbtData = new NBTTagByteArray(data); usedRAM += buffer.limit(); instances++; @@ -56,12 +65,28 @@ public class ChunkMesh extends Mesh { this.flags = flags; this.quadCount = quadCount; - this.buffer = createBuffer(quads); + this.nbtData = toNBT(quads, quadCount); + // TODO move this somewhere else + List nameList = (List) ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites.keySet().stream().collect(Collectors.toList()); + this.buffer = createBuffer(((NBTTagByteArray)nbtData).func_150292_c(), nameList); usedRAM += buffer.limit(); instances++; } + private NBTBase toNBT(List quads, int quadCount) { + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(quadCount * 6 * 13); + DataOutputStream out = new DataOutputStream(byteOut); + try { + for(int pass = 0; pass <= 9; pass++){ + for(MeshQuad quad : quads) { + quad.writeToDisk(out, pass); + } + } + } catch(IOException e) {} + return new NBTTagByteArray(byteOut.toByteArray()); + } + void destroy() { usedRAM -= buffer.limit(); instances--; diff --git a/src/main/java/makamys/lodmod/renderer/LODChunk.java b/src/main/java/makamys/lodmod/renderer/LODChunk.java index e1582b1..96c0261 100644 --- a/src/main/java/makamys/lodmod/renderer/LODChunk.java +++ b/src/main/java/makamys/lodmod/renderer/LODChunk.java @@ -4,6 +4,9 @@ import java.util.List; import makamys.lodmod.LODMod; import net.minecraft.entity.Entity; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagEnd; +import net.minecraft.nbt.NBTTagList; import net.minecraft.world.chunk.Chunk; public class LODChunk { @@ -67,4 +70,18 @@ public class LODChunk { } } + public NBTTagCompound saveToNBT() { + NBTTagCompound nbt = new NBTTagCompound(); + nbt.setInteger("x", x); + nbt.setInteger("z", z); + NBTTagCompound chunkMeshesCompound = new NBTTagCompound(); + for(int i = 0; i < chunkMeshes.length; i++) { + if(chunkMeshes[i] != null) { + chunkMeshesCompound.setTag(String.valueOf(i), chunkMeshes[i].nbtData); + } + } + nbt.setTag("chunkMeshes", chunkMeshesCompound); + return nbt; + } + } diff --git a/src/main/java/makamys/lodmod/renderer/LODRegion.java b/src/main/java/makamys/lodmod/renderer/LODRegion.java index 695202d..f0b3138 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRegion.java +++ b/src/main/java/makamys/lodmod/renderer/LODRegion.java @@ -1,6 +1,17 @@ package makamys.lodmod.renderer; +import java.io.BufferedOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.nio.file.Path; + +import net.minecraft.client.Minecraft; import net.minecraft.entity.Entity; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.world.chunk.Chunk; public class LODRegion { @@ -24,6 +35,32 @@ public class LODRegion { return new LODRegion(regionX, regionZ); // TODO } + public void save(Path saveDir) { + try { + Path savePath = saveDir.resolve("lod").resolve(regionX + "," + regionZ + ".lod"); + File saveFile = savePath.toFile(); + saveFile.getParentFile().mkdirs(); + + NBTTagCompound nbt = new NBTTagCompound(); + nbt.setByte("V", (byte)0); + + NBTTagList list = new NBTTagList(); + + for(int i = 0; i < 32; i++) { + for(int j = 0; j < 32; j++) { + if(data[i][j] != null) { + list.appendTag(data[i][j].saveToNBT()); + } + } + } + nbt.setTag("chunks", list); + CompressedStreamTools.writeCompressed(nbt, new FileOutputStream(saveFile)); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public LODChunk getChunkAbsolute(int chunkXAbs, int chunkZAbs) { return getChunk(chunkXAbs - regionX * 32, chunkZAbs - regionZ * 32); } diff --git a/src/main/java/makamys/lodmod/renderer/LODRenderer.java b/src/main/java/makamys/lodmod/renderer/LODRenderer.java index 4c5c50f..ca661a7 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRenderer.java +++ b/src/main/java/makamys/lodmod/renderer/LODRenderer.java @@ -7,11 +7,13 @@ import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.entity.Entity; import net.minecraft.world.ChunkCoordIntPair; +import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.gen.ChunkProviderServer; import java.nio.FloatBuffer; import java.nio.IntBuffer; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -576,6 +578,13 @@ public class LODRenderer { ); } + public void onSave(World world) { + String worldName = world.getWorldInfo().getWorldName(); + Path saveDir = Minecraft.getMinecraft().mcDataDir.toPath().resolve("lodmod").resolve(worldName); + + loadedRegionsMap.forEach((k, v) -> v.save(saveDir)); + } + public static class LODChunkComparator implements Comparator { Entity player; diff --git a/src/main/java/makamys/lodmod/renderer/Mesh.java b/src/main/java/makamys/lodmod/renderer/Mesh.java index 55f2d8b..0f429ec 100644 --- a/src/main/java/makamys/lodmod/renderer/Mesh.java +++ b/src/main/java/makamys/lodmod/renderer/Mesh.java @@ -3,6 +3,8 @@ package makamys.lodmod.renderer; import java.nio.ByteBuffer; import java.nio.IntBuffer; +import net.minecraft.nbt.NBTBase; + public abstract class Mesh { public ByteBuffer buffer; @@ -13,5 +15,4 @@ public abstract class Mesh { public int offset = -1; public abstract int getStride(); - } diff --git a/src/main/java/makamys/lodmod/renderer/SimpleChunkMesh.java b/src/main/java/makamys/lodmod/renderer/SimpleChunkMesh.java index 960c48d..75b4349 100644 --- a/src/main/java/makamys/lodmod/renderer/SimpleChunkMesh.java +++ b/src/main/java/makamys/lodmod/renderer/SimpleChunkMesh.java @@ -20,6 +20,7 @@ import net.minecraft.block.material.Material; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.nbt.NBTTagString; import net.minecraft.util.IIcon; import net.minecraft.world.chunk.Chunk; -- cgit