Guest User

Untitled

a guest
Jan 15th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. package li.cil.oc.server.machine.luac
  2.  
  3. import li.cil.oc.Settings
  4. import li.cil.oc.api
  5. import li.cil.oc.api.driver.item.MutableProcessor
  6. import li.cil.oc.api.driver.item.Processor
  7. import li.cil.oc.api.network.Connector
  8. import li.cil.oc.util.ExtendedLuaState.extendLuaState
  9.  
  10. import scala.collection.convert.WrapAsScala._
  11.  
  12. class ComputerAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
  13. def initialize() {
  14. // Computer API, stuff that kinda belongs to os, but we don't want to
  15. // clutter it.
  16. lua.newTable()
  17.  
  18. // Allow getting the real world time for timeouts.
  19. lua.pushScalaFunction(lua => {
  20. lua.pushNumber(System.currentTimeMillis() / 1000.0)
  21. 1
  22. })
  23. lua.setField(-2, "realTime")
  24.  
  25. // The time the computer has been running, as opposed to the CPU time.
  26. lua.pushScalaFunction(lua => {
  27. lua.pushNumber(machine.upTime())
  28. 1
  29. })
  30. lua.setField(-2, "uptime")
  31.  
  32. // Allow the computer to figure out its own id in the component network.
  33. lua.pushScalaFunction(lua => {
  34. Option(node.address) match {
  35. case None => lua.pushNil()
  36. case Some(address) => lua.pushString(address)
  37. }
  38. 1
  39. })
  40. lua.setField(-2, "address")
  41.  
  42. lua.pushScalaFunction(lua => {
  43. // This is *very* unlikely, but still: avoid this getting larger than
  44. // what we report as the total memory.
  45. lua.pushInteger(((lua.getFreeMemory min (lua.getTotalMemory - owner.kernelMemory)) / owner.ramScale).toInt)
  46. 1
  47. })
  48. lua.setField(-2, "freeMemory")
  49.  
  50. // Allow the system to read how much memory it uses and has available.
  51. lua.pushScalaFunction(lua => {
  52. lua.pushInteger(((lua.getTotalMemory - owner.kernelMemory) / owner.ramScale).toInt)
  53. 1
  54. })
  55. lua.setField(-2, "totalMemory")
  56.  
  57. lua.pushScalaFunction(lua => {
  58. lua.pushInteger((lua.getCoroutineMemory / owner.ramScale).toInt)
  59. })
  60. lua.setField(-2, "coroutineMemory")
  61.  
  62. lua.pushScalaFunction(lua => {
  63. lua.pushBoolean(machine.signal(lua.checkString(1), lua.toSimpleJavaObjects(2): _*))
  64. 1
  65. })
  66. lua.setField(-2, "pushSignal")
  67.  
  68. // And it's /tmp address...
  69. lua.pushScalaFunction(lua => {
  70. val address = machine.tmpAddress
  71. if (address == null) lua.pushNil()
  72. else lua.pushString(address)
  73. 1
  74. })
  75. lua.setField(-2, "tmpAddress")
  76.  
  77. // User management.
  78. lua.pushScalaFunction(lua => {
  79. val users = machine.users
  80. users.foreach(lua.pushString)
  81. users.length
  82. })
  83. lua.setField(-2, "users")
  84.  
  85. lua.pushScalaFunction(lua => try {
  86. machine.addUser(lua.checkString(1))
  87. lua.pushBoolean(true)
  88. 1
  89. } catch {
  90. case e: Throwable =>
  91. lua.pushNil()
  92. lua.pushString(Option(e.getMessage).getOrElse(e.toString))
  93. 2
  94. })
  95. lua.setField(-2, "addUser")
  96.  
  97. lua.pushScalaFunction(lua => {
  98. lua.pushBoolean(machine.removeUser(lua.checkString(1)))
  99. 1
  100. })
  101. lua.setField(-2, "removeUser")
  102.  
  103. lua.pushScalaFunction(lua => {
  104. if (Settings.get.ignorePower)
  105. lua.pushNumber(Double.PositiveInfinity)
  106. else
  107. lua.pushNumber(node.asInstanceOf[Connector].globalBuffer)
  108. 1
  109. })
  110. lua.setField(-2, "energy")
  111.  
  112. lua.pushScalaFunction(lua => {
  113. lua.pushNumber(node.asInstanceOf[Connector].globalBufferSize)
  114. 1
  115. })
  116. lua.setField(-2, "maxEnergy")
  117.  
  118. lua.pushScalaFunction(lua => {
  119. machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
  120. case (stack, processor: MutableProcessor) => processor.allArchitectures.toSeq
  121. case (stack, processor: Processor) => Seq(processor.architecture(stack))
  122. } match {
  123. case Some(architectures) =>
  124. lua.pushValue(architectures.map(api.Machine.getArchitectureName))
  125. case _ =>
  126. lua.newTable()
  127. }
  128. 1
  129. })
  130. lua.setField(-2, "getArchitectures")
  131.  
  132. lua.pushScalaFunction(lua => {
  133. machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
  134. case (stack, processor: Processor) =>
  135. lua.pushString(api.Machine.getArchitectureName(processor.architecture(stack)))
  136. 1
  137. }.getOrElse(0)
  138. })
  139. lua.setField(-2, "getArchitecture")
  140.  
  141. lua.pushScalaFunction(lua => {
  142. val archName = lua.checkString(1)
  143. machine.host.internalComponents.map(stack => (stack, api.Driver.driverFor(stack))).collectFirst {
  144. case (stack, processor: MutableProcessor) => processor.allArchitectures.find(arch => api.Machine.getArchitectureName(arch) == archName) match {
  145. case Some(archClass) =>
  146. if (archClass != processor.architecture(stack)) {
  147. processor.setArchitecture(stack, archClass)
  148. lua.pushBoolean(true)
  149. }
  150. else {
  151. lua.pushBoolean(false)
  152. }
  153. 1
  154. case _ =>
  155. lua.pushNil()
  156. lua.pushString("unknown architecture")
  157. 2
  158. }
  159. }.getOrElse(0)
  160. })
  161. lua.setField(-2, "setArchitecture")
  162.  
  163. // Set the computer table.
  164. lua.setGlobal("computer")
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment