From 08e203a36f8140425c21f13e8b5f229c9c83fac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 23 Aug 2014 22:46:19 +0200 Subject: [PATCH] Externalized saved tmpfs contents. --- .../scala/li/cil/oc/common/SaveHandler.scala | 20 +++++++++++++------ .../oc/server/component/machine/Machine.scala | 9 ++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/SaveHandler.scala b/src/main/scala/li/cil/oc/common/SaveHandler.scala index ddf3426bd..464671897 100644 --- a/src/main/scala/li/cil/oc/common/SaveHandler.scala +++ b/src/main/scala/li/cil/oc/common/SaveHandler.scala @@ -31,13 +31,12 @@ object SaveHandler { scheduleSave(owner.world, owner.x, owner.z, nbt, name, data) } + def scheduleSave(owner: Owner, nbt: NBTTagCompound, name: String, save: NBTTagCompound => Unit) { + scheduleSave(owner, nbt, name, writeNBT(save)) + } + def scheduleSave(container: Container, nbt: NBTTagCompound, name: String, save: NBTTagCompound => Unit) { - val tmpNbt = new NBTTagCompound() - save(tmpNbt) - val baos = new ByteArrayOutputStream() - val dos = new DataOutputStream(baos) - CompressedStreamTools.write(tmpNbt, dos) - scheduleSave(container.world, math.round(container.xPosition - 0.5).toInt, math.round(container.zPosition - 0.5).toInt, nbt, name, baos.toByteArray) + scheduleSave(container.world, math.round(container.xPosition - 0.5).toInt, math.round(container.zPosition - 0.5).toInt, nbt, name, writeNBT(save)) } def scheduleSave(world: World, x: Int, z: Int, nbt: NBTTagCompound, name: String, data: Array[Byte]) { @@ -53,6 +52,15 @@ object SaveHandler { scheduleSave(dimension, chunk, name, data) } + private def writeNBT(save: NBTTagCompound => Unit) = { + val tmpNbt = new NBTTagCompound() + save(tmpNbt) + val baos = new ByteArrayOutputStream() + val dos = new DataOutputStream(baos) + CompressedStreamTools.write(tmpNbt, dos) + baos.toByteArray + } + def loadNBT(nbt: NBTTagCompound, name: String): NBTTagCompound = { val data = load(nbt, name) val bais = new ByteArrayInputStream(data) diff --git a/src/main/scala/li/cil/oc/server/component/machine/Machine.scala b/src/main/scala/li/cil/oc/server/component/machine/Machine.scala index 61a925dc8..ea6a85cb8 100644 --- a/src/main/scala/li/cil/oc/server/component/machine/Machine.scala +++ b/src/main/scala/li/cil/oc/server/component/machine/Machine.scala @@ -9,7 +9,7 @@ import li.cil.oc.api.machine._ import li.cil.oc.api.network._ import li.cil.oc.api.{FileSystem, Network, machine} import li.cil.oc.common.component.ManagedComponent -import li.cil.oc.common.tileentity +import li.cil.oc.common.{SaveHandler, tileentity} import li.cil.oc.server.PacketSender import li.cil.oc.server.driver.Registry import li.cil.oc.server.network.{ArgumentsImpl, Callbacks} @@ -574,7 +574,10 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext _components ++= nbt.getTagList("components").iterator[NBTTagCompound].map(c => c.getString("address") -> c.getString("name")) - tmp.foreach(fs => fs.load(nbt.getCompoundTag("tmp"))) + tmp.foreach(fs => { + if (nbt.hasKey("tmp")) fs.load(nbt.getCompoundTag("tmp")) + else fs.load(SaveHandler.loadNBT(nbt, node.address + "_tmp")) + }) if (state.size > 0 && state.top != Machine.State.Stopped && init()) try { architecture.load(nbt) @@ -645,7 +648,7 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext } nbt.setTag("components", componentsNbt) - tmp.foreach(fs => nbt.setNewCompoundTag("tmp", fs.save)) + tmp.foreach(fs => SaveHandler.scheduleSave(owner, nbt, node.address + "_tmp", fs.save _)) if (state.top != Machine.State.Stopped) try { architecture.save(nbt)