mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-12 16:57:32 -04:00
Made argument parsing for CC modem emulation more forgiving, avoids crashing computers.
This commit is contained in:
parent
8c9f9749b1
commit
b60466c8b8
@ -26,11 +26,11 @@ class SwitchPeripheral(val switch: Switch) extends IPeripheral {
|
||||
result(switch.openPorts(computer).add(port))
|
||||
}),
|
||||
"isOpen" -> ((computer, context, arguments) => {
|
||||
val port = checkPort(arguments, 0)
|
||||
val port = optPort(arguments, 0)
|
||||
result(switch.openPorts(computer).contains(port))
|
||||
}),
|
||||
"close" -> ((computer, context, arguments) => {
|
||||
val port = checkPort(arguments, 0)
|
||||
val port = optPort(arguments, 0)
|
||||
result(switch.openPorts(computer).remove(port))
|
||||
}),
|
||||
"closeAll" -> ((computer, context, arguments) => {
|
||||
@ -119,6 +119,13 @@ class SwitchPeripheral(val switch: Switch) extends IPeripheral {
|
||||
port
|
||||
}
|
||||
|
||||
private def optPort(args: Array[AnyRef], index: Int): Int = {
|
||||
if (args.length < index - 1 || !args(index).isInstanceOf[Double]) return 0
|
||||
val port = args(index).asInstanceOf[Double].toInt
|
||||
if (port < 1 || port > 0xFFFF) return 0
|
||||
port
|
||||
}
|
||||
|
||||
private def checkString(args: Array[AnyRef], index: Int) = {
|
||||
if (args.length < index - 1 || !args(index).isInstanceOf[String])
|
||||
throw new IllegalArgumentException(s"bad argument #${index + 1} (string expected)")
|
||||
|
Loading…
x
Reference in New Issue
Block a user