some cleanup in save handler

This commit is contained in:
Florian Nücke 2014-03-16 08:00:46 +01:00
parent 067664dd3e
commit b3a737c57c

View File

@ -3,52 +3,46 @@ package li.cil.oc.common
import java.io import java.io
import java.util.logging.Level import java.util.logging.Level
import li.cil.oc.{OpenComputers, Settings} import li.cil.oc.{OpenComputers, Settings}
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.world.ChunkCoordIntPair import net.minecraft.world.ChunkCoordIntPair
import net.minecraftforge.common.DimensionManager import net.minecraftforge.common.DimensionManager
import net.minecraftforge.event.ForgeSubscribe import net.minecraftforge.event.ForgeSubscribe
import net.minecraftforge.event.world.WorldEvent import net.minecraftforge.event.world.WorldEvent
import scala.collection.mutable import scala.collection.mutable
// TODO Save all data to an NBT compound and save it as a single file, to improve file I/O performance.
object SaveHandler { object SaveHandler {
val saveData = mutable.Map.empty[ChunkCoordIntPair, mutable.Map[String, Array[Byte]]] val saveData = mutable.Map.empty[ChunkCoordIntPair, mutable.Map[String, Array[Byte]]]
var cachedNbt = new NBTTagCompound()
def savePath = new io.File(DimensionManager.getCurrentSaveRootDirectory, Settings.savePath + "state") def savePath = new io.File(DimensionManager.getCurrentSaveRootDirectory, Settings.savePath + "state")
def scheduleSave(chunk: ChunkCoordIntPair, name: String, data: Array[Byte]) = saveData.synchronized { def scheduleSave(chunk: ChunkCoordIntPair, name: String, data: Array[Byte]) = saveData.synchronized {
if (chunk == null) OpenComputers.log.warning("Cannot save machines with non tile entity owners.") if (chunk == null) throw new IllegalArgumentException("chunk is null")
else saveData.getOrElseUpdate(chunk, mutable.Map.empty[String, Array[Byte]]) += name -> data else saveData.getOrElseUpdate(chunk, mutable.Map.empty[String, Array[Byte]]) += name -> data
} }
def load(chunk: ChunkCoordIntPair, name: String): Array[Byte] = { def load(chunk: ChunkCoordIntPair, name: String): Array[Byte] = {
if (chunk == null) null if (chunk == null) throw new IllegalArgumentException("chunk is null")
else { val path = savePath
val path = savePath val chunkPath = new io.File(path, s"${chunk.chunkXPos}.${chunk.chunkZPos}")
val chunkPath = new io.File(path, s"${chunk.chunkXPos}.${chunk.chunkZPos}") val file = new io.File(chunkPath, name)
val file = new io.File(chunkPath, name) try {
try { // val bis = new io.BufferedInputStream(new GZIPInputStream(new io.FileInputStream(file)))
// val bis = new io.BufferedInputStream(new GZIPInputStream(new io.FileInputStream(file))) val bis = new io.BufferedInputStream(new io.FileInputStream(file))
val bis = new io.BufferedInputStream(new io.FileInputStream(file)) val bos = new io.ByteArrayOutputStream
val bos = new io.ByteArrayOutputStream val buffer = new Array[Byte](8 * 1024)
val buffer = new Array[Byte](8 * 1024) var read = 0
var read = 0 do {
do { read = bis.read(buffer)
read = bis.read(buffer) if (read > 0) {
if (read > 0) { bos.write(buffer, 0, read)
bos.write(buffer, 0, read) }
} } while (read >= 0)
} while (read >= 0) bis.close()
bis.close() bos.toByteArray
bos.toByteArray }
} catch {
catch { case e: io.IOException =>
case e: io.IOException => OpenComputers.log.log(Level.WARNING, "Error loading auxiliary tile entity data.", e)
OpenComputers.log.log(Level.WARNING, "Error loading auxiliary tile entity data.", e) Array.empty[Byte]
Array.empty[Byte]
}
} }
} }