Added safety try-catch to machine loading code, to avoid crashes for unexpected states.

Added callback doc for experience upgrade and chunkloader upgrades.
This commit is contained in:
Florian Nücke 2014-07-11 14:10:54 +02:00
parent d24907b7ab
commit 5e268af84b
3 changed files with 15 additions and 5 deletions

View File

@ -28,10 +28,10 @@ class UpgradeChunkloader(val owner: Container) extends component.ManagedComponen
} }
} }
@Callback @Callback(doc = """function():boolean -- Gets whether the chunkloader is currently active.""")
def isActive(context: Context, args: Arguments): Array[AnyRef] = result(ticket.isDefined) def isActive(context: Context, args: Arguments): Array[AnyRef] = result(ticket.isDefined)
@Callback @Callback(doc = """function(enabled:boolean):boolean -- Enables or disables the chunkloader.""")
def setActive(context: Context, args: Arguments): Array[AnyRef] = { def setActive(context: Context, args: Arguments): Array[AnyRef] = {
val enabled = args.checkBoolean(0) val enabled = args.checkBoolean(0)
if (enabled && ticket.isEmpty) { if (enabled && ticket.isEmpty) {

View File

@ -39,7 +39,7 @@ class UpgradeExperience extends component.ManagedComponent {
} }
} }
@Callback(direct = true) @Callback(direct = true, doc = """function():number -- The current level of experience stored in this experience upgrade.""")
def level(context: Context, args: Arguments): Array[AnyRef] = { def level(context: Context, args: Arguments): Array[AnyRef] = {
val xpNeeded = xpForNextLevel - xpForLevel(level) val xpNeeded = xpForNextLevel - xpForLevel(level)
val xpProgress = math.max(0, experience - xpForLevel(level)) val xpProgress = math.max(0, experience - xpForLevel(level))

View File

@ -577,7 +577,7 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext
tmp.foreach(fs => fs.load(nbt.getCompoundTag("tmp"))) tmp.foreach(fs => fs.load(nbt.getCompoundTag("tmp")))
if (state.size > 0 && state.top != Machine.State.Stopped && init()) { if (state.size > 0 && state.top != Machine.State.Stopped && init()) try {
architecture.load(nbt) architecture.load(nbt)
signals ++= nbt.getTagList("signals").iterator[NBTTagCompound].map(signalNbt => { signals ++= nbt.getTagList("signals").iterator[NBTTagCompound].map(signalNbt => {
@ -612,6 +612,11 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext
pause(Settings.get.startupDelay) pause(Settings.get.startupDelay)
} }
} }
catch {
case t: Throwable =>
OpenComputers.log.log(Level.SEVERE, s"""Unexpected error loading a state of computer at (${owner.x}, ${owner.y}, ${owner.z}). """ +
s"""State: ${state.headOption.fold("no state")(_.toString)}. Unless you're upgrading/downgrading across a major version, please report this! Thank you.""", t)
}
else close() // Clean up in case we got a weird state stack. else close() // Clean up in case we got a weird state stack.
} }
@ -641,7 +646,7 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext
tmp.foreach(fs => nbt.setNewCompoundTag("tmp", fs.save)) tmp.foreach(fs => nbt.setNewCompoundTag("tmp", fs.save))
if (state.top != Machine.State.Stopped) { if (state.top != Machine.State.Stopped) try {
architecture.save(nbt) architecture.save(nbt)
val signalsNbt = new NBTTagList() val signalsNbt = new NBTTagList()
@ -674,6 +679,11 @@ class Machine(val owner: Owner, constructor: Constructor[_ <: Architecture]) ext
nbt.setLong("cpuTime", cpuTotal) nbt.setLong("cpuTime", cpuTotal)
nbt.setInteger("remainingPause", remainingPause) nbt.setInteger("remainingPause", remainingPause)
} }
catch {
case t: Throwable =>
OpenComputers.log.log(Level.SEVERE, s"""Unexpected error saving a state of computer at (${owner.x}, ${owner.y}, ${owner.z}). """ +
s"""State: ${state.headOption.fold("no state")(_.toString)}. Unless you're upgrading/downgrading across a major version, please report this! Thank you.""", t)
}
} }
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //