Updated name handling in LUA scripts

Added UUID support in text input
Fixed low tier screen reported as invalid GPU for OC
Improved live disconnection handling in LUA scripts
This commit is contained in:
Unknown 2018-04-26 22:25:15 +02:00 committed by unknown
parent 6dbd74fbb7
commit 0f6c5da42d
9 changed files with 153 additions and 98 deletions

View file

@ -4,7 +4,6 @@ w = {}
-- properties -- properties
local data = { } local data = { }
local data_shouldUpdateName = true
local data_name = nil local data_name = nil
local data_handlers = { } local data_handlers = { }
@ -23,8 +22,6 @@ local page_callbackKey
local event_refreshPeriod_s = 3.0 local event_refreshPeriod_s = 3.0
local event_refreshTimerId = -1 local event_refreshTimerId = -1
local ship = nil
local styles = { local styles = {
normal = { front = colors.black , back = colors.lightGray }, normal = { front = colors.black , back = colors.lightGray },
good = { front = colors.lime , back = colors.lightGray }, good = { front = colors.lime , back = colors.lightGray },
@ -386,8 +383,8 @@ local function format_string(value, nbchar)
str = "" .. value str = "" .. value
end end
if nbchar ~= nil then if nbchar ~= nil then
if #str > nbchar then if #str > math.abs(nbchar) then
str = string.sub(str, 1, nbchar - 1) .. "~" str = string.sub(str, 1, math.abs(nbchar) - 1) .. "~"
else else
str = string.sub(str .. " ", 1, nbchar) str = string.sub(str .. " ", 1, nbchar)
end end
@ -521,10 +518,12 @@ local function input_readText(currentValue)
term.setCursorBlink(true) term.setCursorBlink(true)
repeat repeat
w.status_tick() w.status_tick()
-- update display clearing extra characters
w.setColorNormal() w.setColorNormal()
w.setCursorPos(x, y) w.setCursorPos(x, y)
w.write(input .. " ") w.write(w.format_string(input, 37))
input = string.sub(input, -30) -- truncate input and set caret position
input = string.sub(input, -36)
w.setCursorPos(x + #input, y) w.setCursorPos(x + #input, y)
local params = { os.pullEventRaw() } local params = { os.pullEventRaw() }
@ -865,7 +864,7 @@ local function data_inspect(key, value)
end end
local function data_read() local function data_read()
w.data_updateName() w.data_shouldUpdateName()
data = { } data = { }
if fs.exists("shipdata.txt") then if fs.exists("shipdata.txt") then
@ -914,40 +913,59 @@ local function data_getName()
end end
local function data_setName() local function data_setName()
if ship ~= nil then -- check if any named component is connected
w.page_begin("<==== Set ship name ====>") local component = "computer"
w.writeLn("") for name, handlers in pairs(data_handlers) do
w.write("Enter ship name: ") if handlers.name ~= nil then
else component = name
w.page_begin("<==== Set name ====>") end
w.writeLn("")
w.write("Enter computer name: ")
end end
-- ask for a new name
w.page_begin("<==== Set " .. component .. " name ====>")
w.writeLn("")
w.write("Enter " .. component .. " name: ")
data_name = w.input_readText(data_name) data_name = w.input_readText(data_name)
-- update computer name
os.setComputerLabel(data_name) os.setComputerLabel(data_name)
if ship ~= nil then
ship.shipName(data_name) -- update connected components
for name, handlers in pairs(data_handlers) do
if handlers.name ~= nil then
handlers.name(data_name)
end end
end
-- w.reboot() -- not needed -- w.reboot() -- not needed
end end
local function data_updateName() local function data_shouldUpdateName()
data_shouldUpdateName = false local shouldUpdateName = false
-- check computer name
data_name = os.getComputerLabel() data_name = os.getComputerLabel()
if data_name == nil then if data_name == nil then
data_shouldUpdateName = true shouldUpdateName = true
data_name = "" .. os.getComputerID() data_name = "" .. os.getComputerID()
end end
if ship ~= nil then
local shipName = ship.shipName() -- check connected components names
if shipName == "default" then for name, handlers in pairs(data_handlers) do
data_shouldUpdateName = true if handlers.name ~= nil then
elseif data_name ~= shipName then local componentName = handlers.name()
data_shouldUpdateName = true if componentName == "default" or componentName == "" then
data_name = shipName shouldUpdateName = true
elseif shouldUpdateName then
data_name = componentName
elseif data_name ~= componentName then
shouldUpdateName = true
data_name = componentName
end end
end end
end
return shouldUpdateName
end end
local function data_splitString(source, sep) local function data_splitString(source, sep)
@ -958,14 +976,18 @@ local function data_splitString(source, sep)
return fields return fields
end end
local function data_register(name, callbackRead, callbackSave) local function data_register(name, callbackRead, callbackSave, callbackName)
-- read/save callbacks are always defined
if callbackRead == nil then if callbackRead == nil then
callbackRead = function() end callbackRead = function() end
end end
if callbackSave == nil then if callbackSave == nil then
callbackSave = function() end callbackSave = function() end
end end
data_handlers[name] = { read = callbackRead, save = callbackSave }
-- name callback is nill when not defined
data_handlers[name] = { read = callbackRead, save = callbackSave, name = callbackName }
end end
----------- Devices ----------- Devices
@ -1023,15 +1045,12 @@ local function boot()
end end
end end
if deviceType == "warpdriveShipController" then
ship = w.device_get(address)
end
w.writeLn("") w.writeLn("")
end end
-- update with ship name if available -- synchronize computer and connected components names
w.data_updateName() local shouldUpdateName = w.data_shouldUpdateName()
if data_shouldUpdateName then if shouldUpdateName then
w.data_setName() w.data_setName()
end end
@ -1229,7 +1248,7 @@ w = {
data_save = data_save, data_save = data_save,
data_getName = data_getName, data_getName = data_getName,
data_setName = data_setName, data_setName = data_setName,
data_updateName = data_updateName, data_shouldUpdateName = data_shouldUpdateName,
data_splitString = data_splitString, data_splitString = data_splitString,
data_register = data_register, data_register = data_register,
device_get = device_get, device_get = device_get,

View file

@ -358,7 +358,7 @@ function reactor_register()
w.event_register("reactorPulse" , function(eventName, param) reactor_pulse(param) return false end ) w.event_register("reactorPulse" , function(eventName, param) reactor_pulse(param) return false end )
w.event_register("reactorDeactivation", function( ) w.status_showWarning("Reactor deactivated") return false end ) w.event_register("reactorDeactivation", function( ) w.status_showWarning("Reactor deactivated") return false end )
w.event_register("reactorActivation" , function( ) w.status_showWarning("Reactor activated") return false end ) w.event_register("reactorActivation" , function( ) w.status_showWarning("Reactor activated") return false end )
w.data_register("reactor", reactor_read, nil) w.data_register("reactor", reactor_read, nil, nil)
end end
----------- connections status ----------- connections status

View file

@ -761,7 +761,7 @@ core_movement = { 0, 0, 0 }
core_rotationSteps = 0 core_rotationSteps = 0
function core_boot() function core_boot()
if ship == nil then if ship == nil or ship.interfaced() == nil then
return return
end end

View file

@ -31,8 +31,15 @@ function ship_read(parData)
if data.ship_summon == nil then data.ship_summon = false end if data.ship_summon == nil then data.ship_summon = false end
end end
function ship_name(parName)
if ship == nil or ship.interfaced() == nil then
return ''
end
return ship.shipName(parName)
end
function ship_boot() function ship_boot()
if ship == nil then if ship == nil or ship.interfaced() == nil then
return return
end end
@ -411,7 +418,7 @@ end
function ship_page_controls() function ship_page_controls()
w.page_begin(w.data_getName() .. " - Ship controls") w.page_begin(w.data_getName() .. " - Ship controls")
if ship == nil then if ship == nil or ship.interfaced() == nil then
w.status_showWarning("No ship controller detected") w.status_showWarning("No ship controller detected")
else else
local isValid, message = ship.isAssemblyValid() local isValid, message = ship.isAssemblyValid()
@ -522,7 +529,7 @@ end
function ship_page_crew() function ship_page_crew()
w.page_begin(w.data_getName() .. " - Ship crew") w.page_begin(w.data_getName() .. " - Ship crew")
if ship == nil then if ship == nil or ship.interfaced() == nil then
w.status_showWarning("No ship controller detected") w.status_showWarning("No ship controller detected")
else else
local isValid, message = ship.isAssemblyValid() local isValid, message = ship.isAssemblyValid()
@ -580,7 +587,7 @@ end
function ship_page_navigation() function ship_page_navigation()
w.page_begin(w.data_getName() .. " - Ship navigation") w.page_begin(w.data_getName() .. " - Ship navigation")
if ship == nil then if ship == nil or ship.interfaced() == nil then
w.status_showWarning("No ship controller detected") w.status_showWarning("No ship controller detected")
else else
local isValid, message = ship.isAssemblyValid() local isValid, message = ship.isAssemblyValid()
@ -635,7 +642,7 @@ function ship_register()
function(deviceType, address, wrap) ship = wrap end, function(deviceType, address, wrap) ship = wrap end,
function() end) function() end)
w.event_register("shipCoreCooldownDone" , function() w.status_showWarning("Ship core cooldown done") return false end ) w.event_register("shipCoreCooldownDone" , function() w.status_showWarning("Ship core cooldown done") return false end )
w.data_register("ship", ship_read, nil) w.data_register("ship", ship_read, nil, ship_name)
end end
----------- connections status ----------- connections status

View file

@ -527,7 +527,7 @@ function laser_station_page()
w.write("Laser batteries:") w.write("Laser batteries:")
for key, battery in pairs(laserstation.batteries) do for key, battery in pairs(laserstation.batteries) do
w.setCursorPos(1, 5 + key) w.setCursorPos(1, 5 + key)
laserbattery = laser_battery_getByName(battery.name) local laserbattery = laser_battery_getByName(battery.name)
if battery.enabled then if battery.enabled then
w.setColorSuccess() w.setColorSuccess()
else else
@ -610,7 +610,7 @@ function laser_station_config()
w.setCursorPos(1, 11) w.setCursorPos(1, 11)
w.write("Battery enabling: ") w.write("Battery enabling: ")
for key, battery in pairs(laserstation.batteries) do for key, battery in pairs(laserstation.batteries) do
laserbattery = laser_battery_getByName(battery.name) local laserbattery = laser_battery_getByName(battery.name)
local msg = laser_battery_getDescription(laserbattery) local msg = laser_battery_getDescription(laserbattery)
w.setCursorPos(1, 11 + (key % 5)) w.setCursorPos(1, 11 + (key % 5))
@ -824,7 +824,7 @@ function laser_register()
function() end) function() end)
w.event_register("laserScanning", function() laser_sendEvent() return false end ) w.event_register("laserScanning", function() laser_sendEvent() return false end )
w.event_register("laserSend" , function() return false end ) w.event_register("laserSend" , function() return false end )
w.data_register("laser", laser_read, laser_save) w.data_register("laser", laser_read, laser_save, nil)
end end
----------- connections status ----------- connections status

View file

@ -28,7 +28,6 @@ local colors = {-- loosely based on CC colors
-- properties -- properties
local data = { } local data = { }
local data_shouldUpdateName = true
local data_name = nil local data_name = nil
local data_handlers = { } local data_handlers = { }
@ -51,8 +50,6 @@ local page_callbackKey
local event_refreshPeriod_s = 5.0 local event_refreshPeriod_s = 5.0
local event_refreshTimerId = -1 local event_refreshTimerId = -1
local ship = nil
local styles = { local styles = {
normal = { front = colors.black , back = colors.lightGray }, normal = { front = colors.black , back = colors.lightGray },
good = { front = colors.lime , back = colors.lightGray }, good = { front = colors.lime , back = colors.lightGray },
@ -392,8 +389,8 @@ local function format_string(value, nbchar)
str = "" .. value str = "" .. value
end end
if nbchar ~= nil then if nbchar ~= nil then
if #str > nbchar then if #str > math.abs(nbchar) then
str = string.sub(str, 1, nbchar - 1) .. "~" str = string.sub(str, 1, math.abs(nbchar) - 1) .. "~"
else else
str = string.sub(str .. " ", 1, nbchar) str = string.sub(str .. " ", 1, nbchar)
end end
@ -528,10 +525,12 @@ local function input_readText(currentValue)
term.setCursorBlink(true) term.setCursorBlink(true)
repeat repeat
w.status_tick() w.status_tick()
-- update display clearing extra characters
w.setColorNormal() w.setColorNormal()
w.setCursorPos(x, y) w.setCursorPos(x, y)
w.write(input .. " ") w.write(w.format_string(input, 37))
input = string.sub(input, -30) -- truncate input and set caret position
input = string.sub(input, -36)
w.setCursorPos(x + #input, y) w.setCursorPos(x + #input, y)
local params = { event.pull() } local params = { event.pull() }
@ -801,6 +800,11 @@ local function event_handler(eventName, param)
elseif eventName == "component_removed" then elseif eventName == "component_removed" then
elseif eventName == "component_available" then elseif eventName == "component_available" then
elseif eventName == "component_unavailable" then elseif eventName == "component_unavailable" then
elseif eventName == "gpu_bound" then-- OpenOS internal event?
elseif eventName == "term_available" then
needRedraw = true
elseif eventName == "term_unavailable" then
needRedraw = true
-- not supported: task_complete, rednet_message, modem_message -- not supported: task_complete, rednet_message, modem_message
elseif event_handlers[eventName] ~= nil then elseif event_handlers[eventName] ~= nil then
needRedraw = event_handlers[eventName](eventName, param) needRedraw = event_handlers[eventName](eventName, param)
@ -841,7 +845,7 @@ local function data_inspect(key, value)
end end
local function data_read() local function data_read()
w.data_updateName() w.data_shouldUpdateName()
data = { } data = { }
if fs.exists("/etc/shipdata.txt") then if fs.exists("/etc/shipdata.txt") then
@ -890,39 +894,55 @@ local function data_getName()
end end
local function data_setName() local function data_setName()
if ship ~= nil then -- check if any named component is connected
w.page_begin("<==== Set ship name ====>") local component = "computer"
w.writeLn("") for name, handlers in pairs(data_handlers) do
w.write("Enter ship name: ") if handlers.name ~= nil then
else component = name
w.page_begin("<==== Set name ====>") end
w.writeLn("")
w.write("Enter computer name: ")
end end
-- ask for a new name
w.page_begin("<==== Set " .. component .. " name ====>")
w.writeLn("")
w.write("Enter " .. component .. " name: ")
data_name = w.input_readText(data_name) data_name = w.input_readText(data_name)
-- OpenComputers only allows to label filesystems => out -- OpenComputers only allows to label filesystems => out
if ship ~= nil then
ship.shipName(data_name) -- update connected components
for name, handlers in pairs(data_handlers) do
if handlers.name ~= nil then
handlers.name(data_name)
end end
end
-- w.reboot() -- not needed -- w.reboot() -- not needed
end end
local function data_updateName() local function data_shouldUpdateName()
data_shouldUpdateName = false local shouldUpdateName = false
-- check computer name
data_name = "" .. computer.address() data_name = "" .. computer.address()
if data_name == nil then local nameDefault = data_name
data_shouldUpdateName = true
data_name = "" .. computer.address() -- check connected components names
end for name, handlers in pairs(data_handlers) do
if ship ~= nil then if handlers.name ~= nil then
local shipName = ship.shipName() local componentName = handlers.name()
if shipName == "default" then if componentName == "default" or componentName == "" then
data_shouldUpdateName = true shouldUpdateName = true
else elseif shouldUpdateName then
data_name = shipName data_name = componentName
elseif data_name ~= componentName then
shouldUpdateName = data_name ~= nameDefault
data_name = componentName
end end
end end
end
return shouldUpdateName
end end
local function data_splitString(source, sep) local function data_splitString(source, sep)
@ -933,14 +953,18 @@ local function data_splitString(source, sep)
return fields return fields
end end
local function data_register(name, callbackRead, callbackSave) local function data_register(name, callbackRead, callbackSave, callbackName)
-- read/save callbacks are always defined
if callbackRead == nil then if callbackRead == nil then
callbackRead = function() end callbackRead = function() end
end end
if callbackSave == nil then if callbackSave == nil then
callbackSave = function() end callbackSave = function() end
end end
data_handlers[name] = { read = callbackRead, save = callbackSave }
-- name callback is nill when not defined
data_handlers[name] = { read = callbackRead, save = callbackSave, name = callbackName }
end end
----------- Devices ----------- Devices
@ -966,7 +990,8 @@ local function boot()
os.exit() os.exit()
end end
if component.gpu.getDepth() < 4 then if component.gpu.getDepth() < 4 then
print("Tier 2 GPU required") print("A tier 2 or higher GPU required")
print("A tier 2 or higher screen required")
os.exit() os.exit()
end end
print("loading...") print("loading...")
@ -993,15 +1018,12 @@ local function boot()
handlers.register(deviceType, address, w.device_get(address)) handlers.register(deviceType, address, w.device_get(address))
end end
if deviceType == "warpdriveShipController" then
ship = w.device_get(address)
end
w.writeLn("") w.writeLn("")
end end
-- update with ship name if available -- synchronize computer and connected components names
w.data_updateName() local shouldUpdateName = w.data_shouldUpdateName()
if data_shouldUpdateName then if shouldUpdateName then
w.data_setName() w.data_setName()
end end
@ -1197,7 +1219,7 @@ w = {
data_save = data_save, data_save = data_save,
data_getName = data_getName, data_getName = data_getName,
data_setName = data_setName, data_setName = data_setName,
data_updateName = data_updateName, data_shouldUpdateName = data_shouldUpdateName,
data_splitString = data_splitString, data_splitString = data_splitString,
data_register = data_register, data_register = data_register,
device_get = device_get, device_get = device_get,

View file

@ -363,7 +363,7 @@ function reactor_register()
w.event_register("reactorPulse" , function(eventName, param) reactor_pulse(param) return false end ) w.event_register("reactorPulse" , function(eventName, param) reactor_pulse(param) return false end )
w.event_register("reactorDeactivation", function( ) w.status_showWarning("Reactor deactivated") return false end ) w.event_register("reactorDeactivation", function( ) w.status_showWarning("Reactor deactivated") return false end )
w.event_register("reactorActivation" , function( ) w.status_showWarning("Reactor activated") return false end ) w.event_register("reactorActivation" , function( ) w.status_showWarning("Reactor activated") return false end )
w.data_register("reactor", reactor_read, nil) w.data_register("reactor", reactor_read, nil, nil)
end end
----------- connections status ----------- connections status

View file

@ -32,8 +32,15 @@ function ship_read(parData)
if data.ship_summon == nil then data.ship_summon = false end if data.ship_summon == nil then data.ship_summon = false end
end end
function ship_name(parName)
if ship == nil or ship.interfaced() == nil then
return ''
end
return ship.shipName(parName)
end
function ship_boot() function ship_boot()
if ship == nil then if ship == nil or ship.interfaced() == nil then
return return
end end
@ -415,7 +422,7 @@ end
function ship_page_controls() function ship_page_controls()
w.page_begin(w.data_getName() .. " - Ship controls") w.page_begin(w.data_getName() .. " - Ship controls")
if ship == nil then if ship == nil or ship.interfaced() == nil then
w.status_showWarning("No ship controller detected") w.status_showWarning("No ship controller detected")
else else
local isValid, message = ship.isAssemblyValid() local isValid, message = ship.isAssemblyValid()
@ -526,7 +533,7 @@ end
function ship_page_crew() function ship_page_crew()
w.page_begin(w.data_getName() .. " - Ship crew") w.page_begin(w.data_getName() .. " - Ship crew")
if ship == nil then if ship == nil or ship.interfaced() == nil then
w.status_showWarning("No ship controller detected") w.status_showWarning("No ship controller detected")
else else
local isValid, message = ship.isAssemblyValid() local isValid, message = ship.isAssemblyValid()
@ -584,7 +591,7 @@ end
function ship_page_navigation() function ship_page_navigation()
w.page_begin(w.data_getName() .. " - Ship navigation") w.page_begin(w.data_getName() .. " - Ship navigation")
if ship == nil then if ship == nil or ship.interfaced() == nil then
w.status_showWarning("No ship controller detected") w.status_showWarning("No ship controller detected")
else else
local isValid, message = ship.isAssemblyValid() local isValid, message = ship.isAssemblyValid()
@ -639,7 +646,7 @@ function ship_register()
function(deviceType, address, wrap) ship = wrap end, function(deviceType, address, wrap) ship = wrap end,
function() end) function() end)
w.event_register("shipCoreCooldownDone" , function() w.status_showWarning("Ship core cooldown done") return false end ) w.event_register("shipCoreCooldownDone" , function() w.status_showWarning("Ship core cooldown done") return false end )
w.data_register("ship", ship_read, nil) w.data_register("ship", ship_read, nil, ship_name)
end end
----------- connections status ----------- connections status

View file

@ -526,7 +526,7 @@ function laser_station_page()
w.write("Laser batteries:") w.write("Laser batteries:")
for key, battery in pairs(laserstation.batteries) do for key, battery in pairs(laserstation.batteries) do
w.setCursorPos(1, 5 + key) w.setCursorPos(1, 5 + key)
laserbattery = laser_battery_getByName(battery.name) local laserbattery = laser_battery_getByName(battery.name)
if battery.enabled then if battery.enabled then
w.setColorSuccess() w.setColorSuccess()
else else
@ -609,7 +609,7 @@ function laser_station_config()
w.setCursorPos(1, 11) w.setCursorPos(1, 11)
w.write("Battery enabling: ") w.write("Battery enabling: ")
for key, battery in pairs(laserstation.batteries) do for key, battery in pairs(laserstation.batteries) do
laserbattery = laser_battery_getByName(battery.name) local laserbattery = laser_battery_getByName(battery.name)
local msg = laser_battery_getDescription(laserbattery) local msg = laser_battery_getDescription(laserbattery)
w.setCursorPos(1, 11 + (key % 5)) w.setCursorPos(1, 11 + (key % 5))
@ -823,7 +823,7 @@ function laser_register()
function() end) function() end)
w.event_register("laserScanning", function() laser_sendEvent() return false end ) w.event_register("laserScanning", function() laser_sendEvent() return false end )
w.event_register("laserSend" , function() return false end ) w.event_register("laserSend" , function() return false end )
w.data_register("laser", laser_read, laser_save) w.data_register("laser", laser_read, laser_save, nil)
end end
----------- connections status ----------- connections status