diff options
5 files changed, 75 insertions, 24 deletions
diff --git a/src/main/java/makamys/lodmod/LODMod.java b/src/main/java/makamys/lodmod/LODMod.java index 34410d4..57f3d41 100644 --- a/src/main/java/makamys/lodmod/LODMod.java +++ b/src/main/java/makamys/lodmod/LODMod.java @@ -18,6 +18,8 @@ import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; import makamys.lodmod.renderer.LODRenderer; import makamys.lodmod.util.SpriteUtil; @@ -70,13 +72,6 @@ public class LODMod } @SubscribeEvent - public void onWorldSave(WorldEvent.Save event) { - if(isActive()) { - renderer.onSave(event.world); - } - } - - @SubscribeEvent public void onRenderOverlay(RenderGameOverlayEvent event) { FontRenderer fontRenderer = RenderManager.instance.getFontRenderer(); if(isActive() && event.type == ElementType.TEXT && fontRenderer != null && Minecraft.getMinecraft().gameSettings.showDebugInfo) diff --git a/src/main/java/makamys/lodmod/renderer/ChunkMesh.java b/src/main/java/makamys/lodmod/renderer/ChunkMesh.java index 45510ef..9d6241f 100644 --- a/src/main/java/makamys/lodmod/renderer/ChunkMesh.java +++ b/src/main/java/makamys/lodmod/renderer/ChunkMesh.java @@ -44,7 +44,7 @@ public class ChunkMesh extends Mesh { public static int usedRAM = 0; public static int instances = 0; - private ChunkMesh(int x, int y, int z, Flags flags, int quadCount, byte[] data, List<String> stringTable) { + public ChunkMesh(int x, int y, int z, Flags flags, int quadCount, byte[] data, List<String> stringTable) { this.x = x; this.y = y; this.z = z; @@ -73,9 +73,9 @@ public class ChunkMesh extends Mesh { usedRAM += buffer.limit(); instances++; } - + private NBTBase toNBT(List<MeshQuad> quads, int quadCount) { - ByteArrayOutputStream byteOut = new ByteArrayOutputStream(quadCount * 6 * 13); + ByteArrayOutputStream byteOut = new ByteArrayOutputStream(quadCount * (2 + 4 * (3 + 2 + 2 + 4))); DataOutputStream out = new DataOutputStream(byteOut); try { for(int pass = 0; pass <= 9; pass++){ diff --git a/src/main/java/makamys/lodmod/renderer/LODChunk.java b/src/main/java/makamys/lodmod/renderer/LODChunk.java index 96c0261..cdf04b7 100644 --- a/src/main/java/makamys/lodmod/renderer/LODChunk.java +++ b/src/main/java/makamys/lodmod/renderer/LODChunk.java @@ -27,6 +27,22 @@ public class LODChunk { this.z = z; } + public LODChunk(NBTTagCompound nbt, List<String> spriteList) { + this.x = nbt.getInteger("x"); + this.z = nbt.getInteger("z"); + + NBTTagCompound chunkMeshesCompound = nbt.getCompoundTag("chunkMeshes"); + for(Object o : chunkMeshesCompound.func_150296_c()) { + String key = (String)o; + int keyInt = Integer.parseInt(key); + + byte[] data = chunkMeshesCompound.getByteArray(key); + + chunkMeshes[keyInt] = new ChunkMesh(x, keyInt / 2, z, new ChunkMesh.Flags(true, true, true, false), data.length / (2 + 4 * (3 + 2 + 2 + 4)), data, spriteList); + } + + } + @Override public String toString() { return "LODChunk(" + x + ", " + z + ")"; diff --git a/src/main/java/makamys/lodmod/renderer/LODRegion.java b/src/main/java/makamys/lodmod/renderer/LODRegion.java index f0b3138..0dc49ea 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRegion.java +++ b/src/main/java/makamys/lodmod/renderer/LODRegion.java @@ -3,16 +3,24 @@ package makamys.lodmod.renderer; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import makamys.lodmod.LODMod; import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.TextureMap; 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; +import net.minecraftforge.common.util.Constants.NBT; public class LODRegion { @@ -31,26 +39,55 @@ public class LODRegion { } } - public static LODRegion load(int regionX, int regionZ) { - return new LODRegion(regionX, regionZ); // TODO + public LODRegion(int regionX, int regionZ, NBTTagCompound nbt) { + this.regionX = regionX; + this.regionZ = regionZ; + + NBTTagList list = nbt.getTagList("chunks", NBT.TAG_COMPOUND); + List<String> stringTable = Arrays.asList(nbt.getString("stringTable").split("\\n")); + + int idx = 0; + for(int i = 0; i < 32; i++) { + for(int j = 0; j < 32; j++) { + data[i][j] = new LODChunk(list.getCompoundTagAt(idx++), stringTable); + if(data[i][j].hasChunkMeshes()) { + LODMod.renderer.setVisible(data[i][j], true); + } + } + } + } + + public static LODRegion load(Path saveDir, int regionX, int regionZ) { + File saveFile = getSavePath(saveDir, regionX, regionZ).toFile(); + if(saveFile.exists()) { + try { + NBTTagCompound nbt = CompressedStreamTools.readCompressed(new FileInputStream(saveFile)); + return new LODRegion(regionX, regionZ, nbt); + } catch (IOException e) { + e.printStackTrace(); + } + } + return new LODRegion(regionX, regionZ); + } + + private static Path getSavePath(Path saveDir, int regionX, int regionZ) { + return saveDir.resolve("lod").resolve(regionX + "," + regionZ + ".lod"); } public void save(Path saveDir) { try { - Path savePath = saveDir.resolve("lod").resolve(regionX + "," + regionZ + ".lod"); - File saveFile = savePath.toFile(); + File saveFile = getSavePath(saveDir, regionX, regionZ).toFile(); saveFile.getParentFile().mkdirs(); NBTTagCompound nbt = new NBTTagCompound(); nbt.setByte("V", (byte)0); + nbt.setString("stringTable", String.join("\n", (List<String>) ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites.keySet().stream().collect(Collectors.toList()))); 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()); - } + list.appendTag(data[i][j].saveToNBT()); } } nbt.setTag("chunks", list); diff --git a/src/main/java/makamys/lodmod/renderer/LODRenderer.java b/src/main/java/makamys/lodmod/renderer/LODRenderer.java index ca661a7..23fb16b 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRenderer.java +++ b/src/main/java/makamys/lodmod/renderer/LODRenderer.java @@ -338,6 +338,8 @@ public class LODRenderer { } public void destroy() { + onSave(); + glDeleteProgram(shaderProgram); glDeleteVertexArrays(VAO); glDeleteBuffers(VBO); @@ -404,7 +406,7 @@ public class LODRenderer { ChunkCoordIntPair key = new ChunkCoordIntPair(Math.floorDiv(chunkX , 32), Math.floorDiv(chunkZ, 32)); LODRegion region = loadedRegionsMap.get(key); if(region == null) { - region = LODRegion.load(Math.floorDiv(chunkX , 32), Math.floorDiv(chunkZ , 32)); + region = LODRegion.load(getSaveDir(), Math.floorDiv(chunkX , 32), Math.floorDiv(chunkZ , 32)); loadedRegionsMap.put(key, region); } return region; @@ -438,7 +440,7 @@ public class LODRenderer { Entity player = (Entity) Minecraft.getMinecraft().getIntegratedServer().getConfigurationManager().playerEntityList.get(0); - setLOD(lodChunk, 1);//lodChunk.distSq(player) < 16 * 16 * 16 * 16 ? 2 : 1); + lodChunk.tick(player); setVisible(lodChunk, true); } @@ -578,11 +580,12 @@ 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 void onSave() { + loadedRegionsMap.forEach((k, v) -> v.save(getSaveDir())); + } + + private Path getSaveDir(){ + return Minecraft.getMinecraft().mcDataDir.toPath().resolve("lodmod").resolve(Minecraft.getMinecraft().getIntegratedServer().getFolderName()); } public static class LODChunkComparator implements Comparator<LODChunk> { |