mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 19:44:05 +09:00
Working line-of-text and single key input
Former-commit-id: 339650979ff2cb5ec18b52a9f3f38b281c7862c9 Former-commit-id: d5ebd860afc8d569ba9ab741b6ca7872380af949
This commit is contained in:
74
src/net/torvald/terrarum/virtualcomputer/assets/lua/BOOT.lua
Normal file
74
src/net/torvald/terrarum/virtualcomputer/assets/lua/BOOT.lua
Normal file
@@ -0,0 +1,74 @@
|
||||
--[[
|
||||
Must be loaded VERY FIRST!
|
||||
|
||||
Created by minjaesong on 16-09-13.
|
||||
--]]
|
||||
|
||||
-- path for any ingame libraries
|
||||
package.path = "/net/torvald/terrarum/virtualcomputer/assets/lua/?.lua;" .. package.path
|
||||
|
||||
-- global variables
|
||||
_G._VERSION = "Luaj-jse 5.2"
|
||||
_G.MONEYSYM = string.char(0x9D) -- currency sign
|
||||
_G.MIDDOT = string.char(0xFA) -- middle dot sign
|
||||
_G.DC1 = string.char(17) -- black
|
||||
_G.DC2 = string.char(18) -- white
|
||||
_G.DC3 = string.char(19) -- dim grey
|
||||
_G.DC4 = string.char(20) -- light grey
|
||||
_G.DLE = string.char(16) -- default error colour
|
||||
_G.getMem = function() collectgarbage() return collectgarbage("count") * 1024 end
|
||||
-- getTotalMem: implemented in Kotlin class
|
||||
_G.getFreeMem = function() return getTotalMem() - getMem() end
|
||||
_G.runscript = function(s, env)
|
||||
local code, reason = load(s, env)
|
||||
|
||||
if _G.getFreeMem() <= 0 then
|
||||
print("out of memory")
|
||||
__haltsystemexplicit__()
|
||||
return
|
||||
end
|
||||
|
||||
if code then
|
||||
xpcall(code, eprint)
|
||||
else
|
||||
print(DLE..tostring(reason))
|
||||
end
|
||||
end
|
||||
_G.__scanMode__ = "UNINIT" -- part of inputstream implementation
|
||||
|
||||
_COMPUTER = {} -- standard console colours
|
||||
_COMPUTER.prompt = DC3.."> "..DC4
|
||||
_COMPUTER.verbose = true -- print debug info
|
||||
_COMPUTER.loadedCLayer = {} -- list of loaded compatibility layers
|
||||
_COMPUTER.bootloader = "/boot/efi"
|
||||
_COMPUTER.OEM = ""
|
||||
|
||||
-- failsafe
|
||||
if getTotalMem() == 0 then print("no RAM installed") __haltsystemexplicit__() return end
|
||||
if _G.getFreeMem() <= 0 then print("out of memory") __haltsystemexplicit__() return end
|
||||
|
||||
-- load libraries that coded in Lua
|
||||
require("ROMLIB")
|
||||
|
||||
-- load bios, if any
|
||||
if fs.exists(_COMPUTER.bootloader) then shell.run(_COMPUTER.bootloader) end
|
||||
-- halt/run luaprompt upon the termination of bios.
|
||||
-- Valid BIOS should load OS and modify 'shell.status' to 'shell.halt' before terminating itself.
|
||||
if shell.status == shell.halt then
|
||||
__haltsystemexplicit__()
|
||||
end
|
||||
|
||||
-- load Lua prompt, if bios is not found
|
||||
if (#_COMPUTER.OEM > 0) then print(_COMPUTER.OEM) end
|
||||
print("Rom basic "..DC2.._VERSION..DC4)
|
||||
print(DC2..tostring(math.floor(getFreeMem()/1024+0.5))..DC4.." Kbytes free")
|
||||
print("Ok")
|
||||
|
||||
while not native.isHalted() do
|
||||
io.write(_COMPUTER.prompt)
|
||||
local s = io.read()
|
||||
runscript(s, "=stdin")
|
||||
end
|
||||
|
||||
native.closeInputString()
|
||||
return
|
||||
@@ -55,7 +55,9 @@ colors.yellow = 0x10
|
||||
colors.lime = 0x20
|
||||
colors.pink = 0x40
|
||||
colors.gray = 0x80
|
||||
colors.grey = 0x80
|
||||
colors.lightGray = 0x100
|
||||
colors.lightGrey = 0x100
|
||||
colors.cyan = 0x200
|
||||
colors.purple = 0x400
|
||||
colors.blue = 0x800
|
||||
@@ -64,28 +66,6 @@ colors.green = 0x2000
|
||||
colors.red = 0x4000
|
||||
colors.black = 0x8000
|
||||
|
||||
colors.combine = function(...)
|
||||
local ret = 0
|
||||
for _, c in ipairs(...) do
|
||||
ret = bor(ret, c)
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
local function containsCol(target, cccol)
|
||||
return bit32.band(target, cccol) > 0
|
||||
end
|
||||
|
||||
colors.subtract = function(cccol, ...)
|
||||
for _, c in ipairs(...) do
|
||||
if not containsCol(cccol, c) then
|
||||
cccol = bit32.bxor(cccol, c)
|
||||
end
|
||||
end
|
||||
return cccol
|
||||
end
|
||||
|
||||
|
||||
local function normaliseCCcol(cccol)
|
||||
if cccol >= 0x1 and cccol <= 0xFFFF then
|
||||
return intLog2(cccol)
|
||||
@@ -95,6 +75,9 @@ local function normaliseCCcol(cccol)
|
||||
end
|
||||
|
||||
|
||||
_G.colours = _G.colors
|
||||
|
||||
|
||||
--------------
|
||||
-- TERM API --
|
||||
--------------
|
||||
@@ -172,6 +155,7 @@ fs.copy = function(a, b) fs.cp(a, b) end
|
||||
fs.delete = function(p) fs.rm(p) end
|
||||
fs.combine = function(a, b) return fs.concat(a, b) end
|
||||
fs.getDir = function(p) return fs.parent(p) end
|
||||
fs.run = function(p) fs.dofile(p) end
|
||||
|
||||
|
||||
------------------
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
--[[
|
||||
Must be loaded VERY FIRST!
|
||||
|
||||
Created by minjaesong on 16-09-13.
|
||||
--]]
|
||||
|
||||
-- path for any ingame libraries
|
||||
package.path = "/net/torvald/terrarum/virtualcomputer/assets/lua/?.lua;" .. package.path
|
||||
|
||||
-- global variables
|
||||
_G.MONEYSYM = string.char(0x9D) -- currency sign
|
||||
_G.MIDDOT = string.char(0xFA) -- middle dot sign
|
||||
_COMPUTER = {} -- standard console colours
|
||||
_COMPUTER.DC1 = string.char(17) -- black
|
||||
_COMPUTER.DC2 = string.char(18) -- white
|
||||
_COMPUTER.DC3 = string.char(19) -- dim grey
|
||||
_COMPUTER.DC4 = string.char(20) -- light grey
|
||||
_COMPUTER.prompt = function()
|
||||
io.write(_COMPUTER.DC3.."> ".._COMPUTER.DC4)
|
||||
end
|
||||
_COMPUTER.verbose = true -- print debug info
|
||||
_COMPUTER.loadedCLayer = {} -- list of loaded compatibility layers
|
||||
_COMPUTER.bootloader = "/boot/efi"
|
||||
_COMPUTER.OEM = ""
|
||||
|
||||
-- load libraries that coded in Lua
|
||||
require("ROMLIB")
|
||||
|
||||
-- load bios, if any
|
||||
if fs.exists(_COMPUTER.bootloader) then shell.run(_COMPUTER.bootloader) end
|
||||
-- halt/run luaprompt upon the termination of bios.
|
||||
-- Valid BIOS should load OS and modify 'shell.status' to 'shell.halt' before terminated.
|
||||
if shell.status == shell.halt then
|
||||
__haltsystemexplicit__()
|
||||
else
|
||||
-- load Lua prompt, if bios is not found
|
||||
if (#_COMPUTER.OEM > 0) then print(_COMPUTER.OEM) end
|
||||
print("Rom basic ".._COMPUTER.DC2.._VERSION.._COMPUTER.DC4)
|
||||
-- print(_COMPUTER.DC2..freemem.._COMPUTER.DC4.." bytes free"
|
||||
print("Ok")
|
||||
end
|
||||
@@ -6,16 +6,108 @@
|
||||
-- ALIASES --
|
||||
-------------
|
||||
|
||||
fs.run = function(p)
|
||||
fs.dofile = function(p)
|
||||
local f = fs.open(p, "r")
|
||||
local s = f.readAll()
|
||||
load(s)()
|
||||
_G.runscript(s, "="..p)
|
||||
end
|
||||
|
||||
_G.loadstring = _G.load
|
||||
|
||||
_G.print = term.print
|
||||
|
||||
--_G.dofile = function(f) fs.dofile(f) end
|
||||
|
||||
|
||||
-----------------------------------------
|
||||
-- INPUTSTREAM AND SCANNER (java-like) --
|
||||
-----------------------------------------
|
||||
--[[
|
||||
In whatever code that actually runs everything (computer),
|
||||
there must be:
|
||||
|
||||
override fun keyPressed(key: Int, c: Char) {
|
||||
super.keyPressed(key, c)
|
||||
vt.keyPressed(key, c)
|
||||
|
||||
if (key == Key.RETURN) {
|
||||
val input = vt.closeInputString()
|
||||
}
|
||||
}
|
||||
|
||||
...it basically says to close the input if RETURN is hit,
|
||||
and THIS exact part will close the input for this function.
|
||||
]]
|
||||
_G.__scanForLine__ = function()
|
||||
native.closeInputString()
|
||||
native.openInput()
|
||||
_G.__scanMode__ = "line"
|
||||
local s
|
||||
repeat -- we can do this ONLY IF lua execution process is SEPARATE THREAD
|
||||
s = native.getLastStreamInput()
|
||||
until s
|
||||
-- input is closed when RETURN is hit. See above comments.
|
||||
return s
|
||||
end
|
||||
|
||||
-- use Keys API to identify the keycode
|
||||
_G.__scanForChar__ = function()
|
||||
native.closeInputString()
|
||||
native.openInput()
|
||||
_G.__scanMode__ = "a_key"
|
||||
local key
|
||||
repeat -- we can do this ONLY IF lua execution process is SEPARATE THREAD
|
||||
key = native.getLastKeyPress()
|
||||
until key
|
||||
-- input is closed when any key is hit. See above comments.
|
||||
return key
|
||||
end
|
||||
|
||||
io.read = _G.__scanForLine__
|
||||
|
||||
|
||||
-----------------
|
||||
-- PRINTSTREAM --
|
||||
-----------------
|
||||
|
||||
io.write = function(...)
|
||||
local args = {...}
|
||||
for _, v in ipairs(args) do
|
||||
local s = tostring(v)
|
||||
term.write(s)
|
||||
end
|
||||
end
|
||||
-- for some reason, inputstream above kills 'print' function.
|
||||
-- So we rewrite it.
|
||||
_G.print = function(...)
|
||||
local args = {...}
|
||||
|
||||
io.write(args[1])
|
||||
|
||||
if (#args > 1) then
|
||||
for i = 2, #args do
|
||||
io.write("\t")
|
||||
io.write(args[i])
|
||||
end
|
||||
end
|
||||
|
||||
io.write("\n")
|
||||
end
|
||||
|
||||
|
||||
---------------
|
||||
-- SHELL API --
|
||||
---------------
|
||||
|
||||
_G.shell = {}
|
||||
shell.status = shell.ok
|
||||
|
||||
shell.run = function(p) fs.dofile(p) end
|
||||
|
||||
|
||||
shell.ok = 0
|
||||
shell.halt = 127
|
||||
|
||||
|
||||
--------------
|
||||
-- HEXUTILS --
|
||||
@@ -62,20 +154,6 @@ _G.hexutils.toHexString = function(byteString)
|
||||
end
|
||||
|
||||
|
||||
---------------
|
||||
-- SHELL API --
|
||||
---------------
|
||||
|
||||
_G.shell = {}
|
||||
shell.status = shell.ok
|
||||
|
||||
shell.run = function(p) fs.run(p) end
|
||||
|
||||
|
||||
shell.ok = 0
|
||||
shell.halt = 127
|
||||
|
||||
|
||||
--------------
|
||||
-- KEYS API --
|
||||
--------------
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
// TODO Fill in from work_files/romapidoc/romapidoc.tex
|
||||
Reference in New Issue
Block a user