Fixed another userdata related persistence issue (discrepancy in callback name after load leading to userdata failing to be saved again).

This commit is contained in:
Florian Nücke 2014-07-03 18:28:22 +02:00
parent ac25db2c0d
commit 0c96d38afb
8 changed files with 46 additions and 14 deletions

View File

@ -276,7 +276,7 @@ wrappedUserdataMeta = {
-- We need custom persist logic here to avoid ERIS trying to save the
-- userdata referenced in this table directly. It will be repopulated
-- in the load methods of the persisted userdata wrappers (see below).
[persistKey or "LuaJ"] = function()
[persistKey and persistKey() or "LuaJ"] = function()
return function()
-- When using special persistence we have to manually reassign the
-- metatable of the persisted value.
@ -343,7 +343,7 @@ local userdataWrapper = {
-- of the actual class when saving, so we can create a new instance via
-- reflection when loading again (and then immediately wrap it again).
-- Collect wrapped callback methods.
[persistKey or "LuaJ"] = function(self)
[persistKey and persistKey() or "LuaJ"] = function(self)
local className, nbt = userdata.save(wrappedUserdata[self])
-- The returned closure is what actually gets persisted, including the
-- upvalues, that being the classname and a byte array representing the

View File

@ -1,6 +1,7 @@
package li.cil.oc.server.component.machine
import li.cil.oc.api
import net.minecraft.nbt.NBTTagCompound
abstract class ArchitectureAPI(val machine: api.machine.Machine) {
protected def node = machine.node
@ -8,4 +9,8 @@ abstract class ArchitectureAPI(val machine: api.machine.Machine) {
protected def components = machine.components
def initialize()
def load(nbt: NBTTagCompound) {}
def save(nbt: NBTTagCompound) {}
}

View File

@ -181,7 +181,7 @@ class LuaJLuaArchitecture(val machine: api.machine.Machine) extends Architecture
}
catch {
case e: LuaError =>
OpenComputers.log.log(Level.WARNING, "Kernel crashed. This is a bug!" + e)
OpenComputers.log.log(Level.WARNING, "Kernel crashed. This is a bug!", e)
new ExecutionResult.Error("kernel panic: this is a bug, check your log file and report it")
case e: Throwable =>
OpenComputers.log.log(Level.WARNING, "Unexpected error in kernel. This is a bug!", e)

View File

@ -355,6 +355,10 @@ class NativeLuaArchitecture(val machine: api.machine.Machine) extends Architectu
}
kernelMemory = (nbt.getInteger("kernelMemory") * ramScale).toInt
for (api <- apis) {
api.load(nbt)
}
} catch {
case e: LuaRuntimeException =>
OpenComputers.log.warning("Could not unpersist computer.\n" + e.toString + (if (e.getLuaStackTrace.isEmpty) "" else "\tat " + e.getLuaStackTrace.mkString("\n\tat ")))
@ -397,6 +401,10 @@ class NativeLuaArchitecture(val machine: api.machine.Machine) extends Architectu
}
nbt.setInteger("kernelMemory", math.ceil(kernelMemory / ramScale).toInt)
for (api <- apis) {
api.save(nbt)
}
} catch {
case e: LuaRuntimeException =>
OpenComputers.log.warning("Could not persist computer.\n" + e.toString + (if (e.getLuaStackTrace.isEmpty) "" else "\tat " + e.getLuaStackTrace.mkString("\n\tat ")))

View File

@ -5,13 +5,20 @@ import java.util.UUID
import com.naef.jnlua.LuaState
import li.cil.oc.Settings
import li.cil.oc.server.component.machine.NativeLuaArchitecture
import li.cil.oc.util.ExtendedLuaState._
import net.minecraft.nbt.NBTTagCompound
import scala.collection.mutable
class PersistenceAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
private var persistKey = "__persist" + UUID.randomUUID().toString.replaceAll("-", "")
override def initialize() {
// Will be replaced by old value in load.
lua.pushString("__persist" + UUID.randomUUID().toString.replaceAll("-", ""))
lua.pushScalaFunction(lua => {
lua.pushString(persistKey)
1
})
lua.setGlobal("persistKey")
if (Settings.get.allowPersistence) {
@ -84,20 +91,30 @@ class PersistenceAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
}
}
override def load(nbt: NBTTagCompound) {
super.load(nbt)
if (nbt.hasKey("persistKey")) {
persistKey = nbt.getString("persistKey")
}
}
override def save(nbt: NBTTagCompound) {
super.save(nbt)
nbt.setString("persistKey", persistKey)
}
def configure() {
lua.getGlobal("eris")
lua.getField(-1, "settings")
lua.pushString("spkey")
lua.getGlobal("persistKey")
lua.pushString(persistKey)
lua.call(2, 0)
if (Settings.get.debugPersistence) {
lua.getField(-1, "settings")
lua.pushString("path")
lua.pushBoolean(true)
lua.call(2, 0)
}
lua.getField(-1, "settings")
lua.pushString("path")
lua.pushBoolean(Settings.get.debugPersistence)
lua.call(2, 0)
lua.pop(1)
}

View File

@ -8,7 +8,7 @@ import cpw.mods.fml.common.{ITickHandler, TickType}
import cpw.mods.fml.relauncher.Side
import net.minecraft.client.Minecraft
import org.lwjgl.BufferUtils
import org.lwjgl.openal.{AL10, Util}
import org.lwjgl.openal.{AL, AL10, Util}
import scala.collection.mutable
@ -29,7 +29,7 @@ object Audio extends ITickHandler {
def play(x: Float, y: Float, z: Float, frequencyInHz: Int, durationInMilliseconds: Int) {
val distanceBasedGain = math.max(0, 1 - Minecraft.getMinecraft.thePlayer.getDistance(x, y, z) / 12).toFloat
val gain = distanceBasedGain * volume
if (gain > 0) {
if (gain > 0 && AL.isCreated) {
val sampleCount = durationInMilliseconds * sampleRate / 1000
val data = BufferUtils.createByteBuffer(sampleCount)
val step = frequencyInHz / sampleRate.toFloat
@ -57,7 +57,9 @@ object Audio extends ITickHandler {
sources.synchronized(sources --= sources.filter(_.checkFinished))
// Clear error stack.
AL10.alGetError()
if (AL.isCreated) {
AL10.alGetError()
}
}
private class Source(val x: Float, y: Float, z: Float, val data: ByteBuffer, val gain: Float) {