Made power support more stable (checking if methods are implemented before injecting the interfaces, to prevent crashes if APIs change, e.g.)

This commit is contained in:
Florian Nücke 2014-08-08 22:51:38 +02:00
parent 7fb44f578a
commit 9f304bda4f
2 changed files with 19 additions and 1 deletions

View File

@ -64,8 +64,20 @@ class ClassTransformer extends IClassTransformer {
// Inject available power interfaces into power acceptors. // Inject available power interfaces into power acceptors.
if (classNode.interfaces.contains("li/cil/oc/common/tileentity/traits/PowerAcceptor")) { if (classNode.interfaces.contains("li/cil/oc/common/tileentity/traits/PowerAcceptor")) {
def missingImplementations(interfaceName: String) = {
val node = classNodeFor(interfaceName)
if (node == null) Seq(s"Interface ${interfaceName.replaceAll("/", ".")} not found.")
else node.methods.filterNot(im => classNode.methods.exists(cm => im.name == cm.name && im.desc == cm.desc)).map(method => s"Missing implementation of ${method.name + method.desc}")
}
for ((mod, interfaces) <- powerTypes if mod.isAvailable) { for ((mod, interfaces) <- powerTypes if mod.isAvailable) {
interfaces.foreach(classNode.interfaces.add) val missing = interfaces.flatMap(missingImplementations)
if (missing.isEmpty) {
interfaces.foreach(classNode.interfaces.add)
}
else {
log.warning(s"Skipping power support for mod ${mod.id}.")
missing.foreach(log.warning)
}
} }
} }

View File

@ -49,6 +49,8 @@ object Mods {
}) })
} }
val ComputerCraft = new Mod { val ComputerCraft = new Mod {
def id = IDs.ComputerCraft
override def isAvailable = ComputerCraft15.isAvailable || ComputerCraft16.isAvailable override def isAvailable = ComputerCraft15.isAvailable || ComputerCraft16.isAvailable
} }
val Factorization = new SimpleMod(IDs.Factorization, providesPower = true) val Factorization = new SimpleMod(IDs.Factorization, providesPower = true)
@ -63,6 +65,8 @@ object Mods {
val ProjectRedTransmission = new SimpleMod(IDs.ProjectRedTransmission) val ProjectRedTransmission = new SimpleMod(IDs.ProjectRedTransmission)
val RedLogic = new SimpleMod(IDs.RedLogic) val RedLogic = new SimpleMod(IDs.RedLogic)
val StargateTech2 = new Mod { val StargateTech2 = new Mod {
def id = IDs.StargateTech2
val isAvailable = Loader.isModLoaded(IDs.StargateTech2) && { val isAvailable = Loader.isModLoaded(IDs.StargateTech2) && {
val mod = Loader.instance.getIndexedModList.get(IDs.StargateTech2) val mod = Loader.instance.getIndexedModList.get(IDs.StargateTech2)
mod.getVersion.startsWith("0.7.") mod.getVersion.startsWith("0.7.")
@ -78,6 +82,8 @@ object Mods {
trait Mod { trait Mod {
knownMods += this knownMods += this
def id: String
def isAvailable: Boolean def isAvailable: Boolean
def providesPower: Boolean = false def providesPower: Boolean = false