mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-10 22:01:52 +09:00
OS library implementation
Former-commit-id: b4f7f283080ead5e92c273015bfe2559cbe99e47 Former-commit-id: adaa0af80068a916e534f677f6e5ffe1f3072241
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import li.cil.repack.org.luaj.vm2.*
|
||||
import li.cil.repack.org.luaj.vm2.lib.OneArgFunction
|
||||
import li.cil.repack.org.luaj.vm2.lib.TwoArgFunction
|
||||
import li.cil.repack.org.luaj.vm2.lib.ZeroArgFunction
|
||||
import org.luaj.vm2.*
|
||||
import org.luaj.vm2.lib.OneArgFunction
|
||||
import org.luaj.vm2.lib.TwoArgFunction
|
||||
import org.luaj.vm2.lib.ZeroArgFunction
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
|
||||
import net.torvald.terrarum.virtualcomputer.luaapi.Term.Companion.checkIBM437
|
||||
|
||||
@@ -1,36 +1,35 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import li.cil.repack.org.luaj.vm2.Globals
|
||||
import li.cil.repack.org.luaj.vm2.LuaFunction
|
||||
import li.cil.repack.org.luaj.vm2.LuaTable
|
||||
import li.cil.repack.org.luaj.vm2.LuaValue
|
||||
import li.cil.repack.org.luaj.vm2.lib.OneArgFunction
|
||||
import li.cil.repack.org.luaj.vm2.lib.ZeroArgFunction
|
||||
import org.luaj.vm2.lib.OneArgFunction
|
||||
import org.luaj.vm2.lib.ZeroArgFunction
|
||||
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
|
||||
import net.torvald.terrarum.virtualcomputer.luaapi.Term.Companion.checkIBM437
|
||||
import net.torvald.terrarum.virtualcomputer.terminal.Teletype
|
||||
import org.luaj.vm2.*
|
||||
|
||||
/**
|
||||
* Provide Lua an access to computer object that is in Java
|
||||
*
|
||||
* The "machine" refers to the computer fixture itself in the game world.
|
||||
*
|
||||
* Created by minjaesong on 16-09-19.
|
||||
*/
|
||||
internal class HostAccessProvider(globals: Globals, computer: BaseTerrarumComputer) {
|
||||
|
||||
init {
|
||||
globals["native"] = LuaTable()
|
||||
globals["native"]["println"] = PrintLn()
|
||||
globals["native"]["isHalted"] = IsHalted(computer)
|
||||
globals["machine"] = LuaTable()
|
||||
globals["machine"]["println"] = PrintLn()
|
||||
globals["machine"]["isHalted"] = IsHalted(computer)
|
||||
|
||||
globals["native"]["closeInputString"] = NativeCloseInputString(computer.term)
|
||||
globals["native"]["closeInputKey"] = NativeCloseInputKey(computer.term)
|
||||
globals["native"]["openInput"] = NativeOpenInput(computer.term)
|
||||
globals["native"]["getLastStreamInput"] = NativeGetLastStreamInput(computer.term)
|
||||
globals["native"]["getLastKeyPress"] = NativeGetLastKeyPress(computer.term)
|
||||
globals["machine"]["closeInputString"] = NativeCloseInputString(computer.term)
|
||||
globals["machine"]["closeInputKey"] = NativeCloseInputKey(computer.term)
|
||||
globals["machine"]["openInput"] = NativeOpenInput(computer.term)
|
||||
globals["machine"]["getLastStreamInput"] = NativeGetLastStreamInput(computer.term)
|
||||
globals["machine"]["getLastKeyPress"] = NativeGetLastKeyPress(computer.term)
|
||||
|
||||
// while lua's dofile/require is fixiated to fs, this command allows
|
||||
// libraries in JAR to be loaded.
|
||||
//globals["native"]["loadBuiltInLib"] = NativeLoadBuiltInLib()
|
||||
globals["machine"]["milliTime"] = NativeGetMilliTime(computer)
|
||||
|
||||
globals["machine"]["sleep"] = NativeBusySleep(computer)
|
||||
|
||||
globals["__haltsystemexplicit__"] = HaltComputer(computer)
|
||||
}
|
||||
@@ -98,4 +97,21 @@ internal class HostAccessProvider(globals: Globals, computer: BaseTerrarumComput
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
/** Time elapsed since the power is on. */
|
||||
class NativeGetMilliTime(val computer: BaseTerrarumComputer) : ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
return LuaValue.valueOf(computer.milliTime)
|
||||
}
|
||||
}
|
||||
|
||||
class NativeBusySleep(val computer: BaseTerrarumComputer) : OneArgFunction() {
|
||||
override fun call(mills: LuaValue): LuaValue {
|
||||
val starttime = computer.milliTime
|
||||
val sleeptime = mills.checkint()
|
||||
if (sleeptime > 1000) throw LuaError("Cannot busy-sleep more than a second.")
|
||||
while (computer.milliTime - starttime < sleeptime) { }
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import li.cil.repack.org.luaj.vm2.Globals
|
||||
import org.luaj.vm2.Globals
|
||||
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import li.cil.repack.org.luaj.vm2.Globals
|
||||
import li.cil.repack.org.luaj.vm2.LuaTable
|
||||
import li.cil.repack.org.luaj.vm2.LuaValue
|
||||
import li.cil.repack.org.luaj.vm2.lib.OneArgFunction
|
||||
import org.luaj.vm2.Globals
|
||||
import org.luaj.vm2.LuaTable
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.OneArgFunction
|
||||
import net.torvald.terrarum.gamecontroller.Key
|
||||
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import li.cil.repack.org.luaj.vm2.Globals
|
||||
import li.cil.repack.org.luaj.vm2.LuaTable
|
||||
import li.cil.repack.org.luaj.vm2.LuaValue
|
||||
import li.cil.repack.org.luaj.vm2.lib.TwoArgFunction
|
||||
import li.cil.repack.org.luaj.vm2.lib.ZeroArgFunction
|
||||
import org.luaj.vm2.Globals
|
||||
import org.luaj.vm2.LuaTable
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.TwoArgFunction
|
||||
import org.luaj.vm2.lib.ZeroArgFunction
|
||||
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import li.cil.repack.org.luaj.vm2.Globals
|
||||
import li.cil.repack.org.luaj.vm2.LuaValue
|
||||
import li.cil.repack.org.luaj.vm2.lib.OneArgFunction
|
||||
import org.luaj.vm2.Globals
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.OneArgFunction
|
||||
import net.torvald.terrarum.gameworld.toUint
|
||||
import org.apache.commons.codec.binary.Base64
|
||||
import org.apache.commons.codec.digest.DigestUtils
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import li.cil.repack.org.luaj.vm2.*
|
||||
import li.cil.repack.org.luaj.vm2.lib.*
|
||||
import org.luaj.vm2.*
|
||||
import org.luaj.vm2.lib.*
|
||||
import net.torvald.terrarum.virtualcomputer.terminal.Teletype
|
||||
import net.torvald.terrarum.virtualcomputer.terminal.Terminal
|
||||
import java.nio.charset.Charset
|
||||
|
||||
/**
|
||||
* Controls terminal as if it was a monitor
|
||||
* (not sending control sequences but just drives it, as if it was not a terminal @ 9600 baud)
|
||||
* (not sending control sequences but just drives it directly)
|
||||
*
|
||||
* Created by minjaesong on 16-09-12.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
package net.torvald.terrarum.virtualcomputer.luaapi
|
||||
|
||||
import org.luaj.vm2.Globals
|
||||
import org.luaj.vm2.LuaFunction
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.gameworld.WorldTime
|
||||
import org.luaj.vm2.LuaTable
|
||||
import org.luaj.vm2.LuaValue
|
||||
import org.luaj.vm2.lib.ZeroArgFunction
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Implementation of lua's os.date, to return world info of the game world.
|
||||
*
|
||||
* Created by minjaesong on 16-09-28.
|
||||
*/
|
||||
class WorldInformationProvider(globals: Globals) {
|
||||
|
||||
init {
|
||||
globals["os"]["time"] = LuaValue.NIL // history is LONG! Our 32-bit Lua's epoch is destined to break down...
|
||||
globals["os"]["date"] = OsDateImpl()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getWorldTimeInLuaFormat() : LuaTable {
|
||||
val t = LuaTable()
|
||||
if (Terrarum.gameStarted) {
|
||||
val time = Terrarum.ingame.world.time
|
||||
|
||||
// int Terrarum World Time format
|
||||
t["hour"] = time.hours
|
||||
t["min"] = time.minutes
|
||||
t["wday"] = time.dayOfWeek
|
||||
t["year"] = time.years
|
||||
t["yday"] = time.yearlyDays
|
||||
t["month"] = time.months
|
||||
t["sec"] = time.seconds
|
||||
t["day"] = time.days
|
||||
}
|
||||
else {
|
||||
t["hour"] = 0
|
||||
t["min"] = 0
|
||||
t["wday"] = 1
|
||||
t["year"] = 0
|
||||
t["yday"] = 1
|
||||
t["month"] = 1
|
||||
t["sec"] = 0
|
||||
t["day"] = 1
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
val defaultDateFormat = "%a %d %B %Y %X"
|
||||
|
||||
/** evaluate single C date format */
|
||||
fun String.evalAsDate(): String {
|
||||
if (Terrarum.gameStarted) {
|
||||
val time = Terrarum.ingame.world.time
|
||||
return when (this) {
|
||||
"%a" -> time.getDayNameShort()
|
||||
"%A" -> time.getDayNameFull()
|
||||
"%b" -> time.getMonthNameShort()
|
||||
"%B" -> time.getMonthNameFull()
|
||||
"%c" -> "%x".evalAsDate() + " " + "%X".evalAsDate()
|
||||
"%d" -> time.days.toString()
|
||||
"%H" -> time.hours.toString()
|
||||
"%I" -> throw IllegalArgumentException("%I: AM/PM concept does not exists.")
|
||||
"%M" -> time.minutes.toString()
|
||||
"%m" -> time.months.toString()
|
||||
"%p" -> throw IllegalArgumentException("%p: AM/PM concept does not exists.")
|
||||
"%S" -> time.seconds.toString()
|
||||
"%w" -> time.dayOfWeek.toString()
|
||||
"%x" -> "${String.format("%02d", time.years)}-${String.format("%02d", time.months)}-${String.format("%02d", time.days)}"
|
||||
"%X" -> "${String.format("%02d", time.hours)}:${String.format("%02d", time.minutes)}:${String.format("%02d", time.seconds)}"
|
||||
"%Y" -> time.years.toString()
|
||||
"%y" -> time.years.mod(100).toString()
|
||||
"%%" -> "%"
|
||||
else -> throw IllegalArgumentException("Unknown format string: $this")
|
||||
}
|
||||
}
|
||||
else {
|
||||
return when (this) {
|
||||
"%a" -> "---"
|
||||
"%A" -> "------"
|
||||
"%b" -> "----"
|
||||
"%B" -> "--------"
|
||||
"%c" -> "%x".evalAsDate() + " " + "%X".evalAsDate()
|
||||
"%d" -> "0"
|
||||
"%H" -> "0"
|
||||
"%I" -> throw IllegalArgumentException("%I: AM/PM concept does not exists.")
|
||||
"%M" -> "0"
|
||||
"%m" -> "0"
|
||||
"%p" -> throw IllegalArgumentException("%p: AM/PM concept does not exists.")
|
||||
"%S" -> "0"
|
||||
"%w" -> "0"
|
||||
"%x" -> "00-00-00"
|
||||
"%X" -> "00:00:00"
|
||||
"%Y" -> "0"
|
||||
"%y" -> "00"
|
||||
"%%" -> "%"
|
||||
else -> throw IllegalArgumentException("Unknown format string: $this")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val acceptedDateFormats = arrayOf("%a", "%A", "%b", "%B", "%c", "%d", "%H", "%I", "%M", "%m", "%p", "%S", "%w", "%x", "%X", "%Y", "%y", "%%" )
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes: cannot get a representation of arbitrary time.
|
||||
*/
|
||||
class OsDateImpl() : LuaFunction() {
|
||||
// no args
|
||||
override fun call(): LuaValue {
|
||||
return call(defaultDateFormat)
|
||||
}
|
||||
|
||||
override fun call(format: LuaValue): LuaValue {
|
||||
var arg = format.checkjstring()
|
||||
acceptedDateFormats.forEach {
|
||||
if (arg.contains(it))
|
||||
arg = arg.replace(it, it.evalAsDate(), ignoreCase = false)
|
||||
}
|
||||
return LuaValue.valueOf(arg)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user