mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-17 11:15:12 -04:00
some cleanup in save handler
This commit is contained in:
parent
067664dd3e
commit
b3a737c57c
@ -3,52 +3,46 @@ package li.cil.oc.common
|
||||
import java.io
|
||||
import java.util.logging.Level
|
||||
import li.cil.oc.{OpenComputers, Settings}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraft.world.ChunkCoordIntPair
|
||||
import net.minecraftforge.common.DimensionManager
|
||||
import net.minecraftforge.event.ForgeSubscribe
|
||||
import net.minecraftforge.event.world.WorldEvent
|
||||
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 {
|
||||
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 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
|
||||
}
|
||||
|
||||
def load(chunk: ChunkCoordIntPair, name: String): Array[Byte] = {
|
||||
if (chunk == null) null
|
||||
else {
|
||||
val path = savePath
|
||||
val chunkPath = new io.File(path, s"${chunk.chunkXPos}.${chunk.chunkZPos}")
|
||||
val file = new io.File(chunkPath, name)
|
||||
try {
|
||||
// val bis = new io.BufferedInputStream(new GZIPInputStream(new io.FileInputStream(file)))
|
||||
val bis = new io.BufferedInputStream(new io.FileInputStream(file))
|
||||
val bos = new io.ByteArrayOutputStream
|
||||
val buffer = new Array[Byte](8 * 1024)
|
||||
var read = 0
|
||||
do {
|
||||
read = bis.read(buffer)
|
||||
if (read > 0) {
|
||||
bos.write(buffer, 0, read)
|
||||
}
|
||||
} while (read >= 0)
|
||||
bis.close()
|
||||
bos.toByteArray
|
||||
}
|
||||
catch {
|
||||
case e: io.IOException =>
|
||||
OpenComputers.log.log(Level.WARNING, "Error loading auxiliary tile entity data.", e)
|
||||
Array.empty[Byte]
|
||||
}
|
||||
if (chunk == null) throw new IllegalArgumentException("chunk is null")
|
||||
val path = savePath
|
||||
val chunkPath = new io.File(path, s"${chunk.chunkXPos}.${chunk.chunkZPos}")
|
||||
val file = new io.File(chunkPath, name)
|
||||
try {
|
||||
// val bis = new io.BufferedInputStream(new GZIPInputStream(new io.FileInputStream(file)))
|
||||
val bis = new io.BufferedInputStream(new io.FileInputStream(file))
|
||||
val bos = new io.ByteArrayOutputStream
|
||||
val buffer = new Array[Byte](8 * 1024)
|
||||
var read = 0
|
||||
do {
|
||||
read = bis.read(buffer)
|
||||
if (read > 0) {
|
||||
bos.write(buffer, 0, read)
|
||||
}
|
||||
} while (read >= 0)
|
||||
bis.close()
|
||||
bos.toByteArray
|
||||
}
|
||||
catch {
|
||||
case e: io.IOException =>
|
||||
OpenComputers.log.log(Level.WARNING, "Error loading auxiliary tile entity data.", e)
|
||||
Array.empty[Byte]
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user