mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-17 03:05:30 -04:00
parent
f981a05c0f
commit
4e33221e98
@ -1,5 +1,6 @@
|
|||||||
package li.cil.oc.server.component
|
package li.cil.oc.server.component
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets
|
||||||
import li.cil.oc.Settings
|
import li.cil.oc.Settings
|
||||||
import li.cil.oc.api
|
import li.cil.oc.api
|
||||||
import li.cil.oc.api.Network
|
import li.cil.oc.api.Network
|
||||||
@ -21,6 +22,8 @@ class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment {
|
|||||||
|
|
||||||
protected val openPorts = mutable.Set.empty[Int]
|
protected val openPorts = mutable.Set.empty[Int]
|
||||||
|
|
||||||
|
protected var wakeMessage: Option[String] = None
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
|
||||||
@Callback(doc = """function(port:number):boolean -- Opens the specified port. Returns true if the port was opened.""")
|
@Callback(doc = """function(port:number):boolean -- Opens the specified port. Returns true if the port was opened.""")
|
||||||
@ -75,6 +78,19 @@ class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment {
|
|||||||
@Callback(direct = true, doc = """function():number -- Gets the maximum packet size (config setting).""")
|
@Callback(direct = true, doc = """function():number -- Gets the maximum packet size (config setting).""")
|
||||||
def maxPacketSize(context: Context, args: Arguments): Array[AnyRef] = result(Settings.get.maxNetworkPacketSize)
|
def maxPacketSize(context: Context, args: Arguments): Array[AnyRef] = result(Settings.get.maxNetworkPacketSize)
|
||||||
|
|
||||||
|
@Callback(direct = true, doc = """function():string -- Get the current wake-up message.""")
|
||||||
|
def getWakeMessage(context: Context, args: Arguments): Array[AnyRef] = result(wakeMessage.orNull)
|
||||||
|
|
||||||
|
@Callback(doc = """function(message:string):string -- Set the wake-up message.""")
|
||||||
|
def setWakeMessage(context: Context, args: Arguments): Array[AnyRef] = {
|
||||||
|
val oldMessage = wakeMessage
|
||||||
|
if (args.optAny(0, null) == null)
|
||||||
|
wakeMessage = None
|
||||||
|
else
|
||||||
|
wakeMessage = Option(args.checkString(0))
|
||||||
|
result(oldMessage.orNull)
|
||||||
|
}
|
||||||
|
|
||||||
protected def doSend(packet: Packet) {
|
protected def doSend(packet: Packet) {
|
||||||
node.sendToReachable("network.message", packet)
|
node.sendToReachable("network.message", packet)
|
||||||
}
|
}
|
||||||
@ -109,8 +125,19 @@ class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected def receivePacket(packet: Packet, distance: Double) {
|
protected def receivePacket(packet: Packet, distance: Double) {
|
||||||
if (packet.source != node.address && Option(packet.destination).forall(_ == node.address) && openPorts.contains(packet.port)) {
|
if (packet.source != node.address && Option(packet.destination).forall(_ == node.address)) {
|
||||||
node.sendToReachable("computer.signal", Seq("modem_message", packet.source, Int.box(packet.port), Double.box(distance)) ++ packet.data: _*)
|
if (openPorts.contains(packet.port)) {
|
||||||
|
node.sendToReachable("computer.signal", Seq("modem_message", packet.source, Int.box(packet.port), Double.box(distance)) ++ packet.data: _*)
|
||||||
|
}
|
||||||
|
// Accept wake-up messages regardless of port because we close all ports
|
||||||
|
// when our computer shuts down.
|
||||||
|
packet.data match {
|
||||||
|
case Array(message: Array[Byte]) if wakeMessage.contains(new String(message, Charsets.UTF_8)) =>
|
||||||
|
node.sendToNeighbors("computer.start")
|
||||||
|
case Array(message: String) if wakeMessage.contains(message) =>
|
||||||
|
node.sendToNeighbors("computer.start")
|
||||||
|
case _ =>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,12 +148,16 @@ class NetworkCard(val host: EnvironmentHost) extends prefab.ManagedEnvironment {
|
|||||||
|
|
||||||
assert(openPorts.isEmpty)
|
assert(openPorts.isEmpty)
|
||||||
openPorts ++= nbt.getIntArray("openPorts")
|
openPorts ++= nbt.getIntArray("openPorts")
|
||||||
|
if (nbt.hasKey("wakeMessage")) {
|
||||||
|
wakeMessage = Option(nbt.getString("wakeMessage"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override def save(nbt: NBTTagCompound) {
|
override def save(nbt: NBTTagCompound) {
|
||||||
super.save(nbt)
|
super.save(nbt)
|
||||||
|
|
||||||
nbt.setIntArray("openPorts", openPorts.toArray)
|
nbt.setIntArray("openPorts", openPorts.toArray)
|
||||||
|
wakeMessage.foreach(nbt.setString("wakeMessage", _))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package li.cil.oc.server.component
|
package li.cil.oc.server.component
|
||||||
|
|
||||||
import li.cil.oc.Settings
|
|
||||||
import li.cil.oc.api.machine.Arguments
|
import li.cil.oc.api.machine.Arguments
|
||||||
import li.cil.oc.api.machine.Callback
|
import li.cil.oc.api.machine.Callback
|
||||||
import li.cil.oc.api.machine.Context
|
import li.cil.oc.api.machine.Context
|
||||||
@ -15,10 +14,11 @@ trait RedstoneSignaller extends prefab.ManagedEnvironment {
|
|||||||
@Callback(direct = true, doc = """function():number -- Get the current wake-up threshold.""")
|
@Callback(direct = true, doc = """function():number -- Get the current wake-up threshold.""")
|
||||||
def getWakeThreshold(context: Context, args: Arguments): Array[AnyRef] = result(wakeThreshold)
|
def getWakeThreshold(context: Context, args: Arguments): Array[AnyRef] = result(wakeThreshold)
|
||||||
|
|
||||||
@Callback(doc = """function(threshold:number) -- Set the wake-up threshold.""")
|
@Callback(doc = """function(threshold:number):number -- Set the wake-up threshold.""")
|
||||||
def setWakeThreshold(context: Context, args: Arguments): Array[AnyRef] = {
|
def setWakeThreshold(context: Context, args: Arguments): Array[AnyRef] = {
|
||||||
|
val oldThreshold = wakeThreshold
|
||||||
wakeThreshold = args.checkInteger(0)
|
wakeThreshold = args.checkInteger(0)
|
||||||
null
|
result(oldThreshold)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
@ -34,11 +34,11 @@ trait RedstoneSignaller extends prefab.ManagedEnvironment {
|
|||||||
|
|
||||||
override def load(nbt: NBTTagCompound): Unit = {
|
override def load(nbt: NBTTagCompound): Unit = {
|
||||||
super.load(nbt)
|
super.load(nbt)
|
||||||
wakeThreshold = nbt.getInteger(Settings.namespace + "wakeThreshold")
|
wakeThreshold = nbt.getInteger("wakeThreshold")
|
||||||
}
|
}
|
||||||
|
|
||||||
override def save(nbt: NBTTagCompound): Unit = {
|
override def save(nbt: NBTTagCompound): Unit = {
|
||||||
super.save(nbt)
|
super.save(nbt)
|
||||||
nbt.setInteger(Settings.namespace + "wakeThreshold", wakeThreshold)
|
nbt.setInteger("wakeThreshold", wakeThreshold)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user