Properly migrate CPUs that were manually configured to a Lua 5.2 architecture to the new class name and properly handle errors when instantiating architectures. Closes #1213.

This commit is contained in:
Florian Nücke 2015-06-11 20:36:36 +02:00
parent 76d7a6afc2
commit 18c901c80b
2 changed files with 12 additions and 2 deletions

View File

@ -12,6 +12,7 @@ import li.cil.oc.common.Slot
import li.cil.oc.common.Tier
import li.cil.oc.common.item
import li.cil.oc.common.item.Delegator
import li.cil.oc.server.machine.luac.NativeLuaArchitecture
import net.minecraft.item.ItemStack
import scala.collection.convert.WrapAsScala._
@ -40,7 +41,13 @@ abstract class DriverCPU extends Item with Processor {
override def architecture(stack: ItemStack): Class[_ <: Architecture] = {
if (stack.hasTagCompound) {
val archClass = stack.getTagCompound.getString(Settings.namespace + "archClass")
val archClass = stack.getTagCompound.getString(Settings.namespace + "archClass") match {
case clazz if clazz == classOf[NativeLuaArchitecture].getName =>
// Migrate old saved CPUs to new versions (since the class they refer still
// exists, but is abstract, which would lead to issues).
api.Machine.LuaArchitecture.getName
case clazz => clazz
}
if (!archClass.isEmpty) try return Class.forName(archClass).asSubclass(classOf[Architecture]) catch {
case t: Throwable =>
OpenComputers.log.warn("Failed getting class for CPU architecture. Resetting CPU to use the default.", t)

View File

@ -124,9 +124,12 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach
case Some(driver: Processor) if driver.slot(stack) == Slot.CPU =>
Option(driver.architecture(stack)) match {
case Some(clazz) =>
if (architecture == null || architecture.getClass != clazz) {
if (architecture == null || architecture.getClass != clazz) try {
newArchitecture = clazz.getConstructor(classOf[machine.Machine]).newInstance(this)
}
catch {
case t: Throwable => OpenComputers.log.warn("Failed instantiating a CPU architecture.", t)
}
else {
newArchitecture = architecture
}