neat-donk/runner-process.lua

148 lines
3.7 KiB
Lua
Raw Normal View History

2021-05-01 19:39:35 -04:00
local gui, utime, callback, set_timer_timeout = gui, utime, callback, set_timer_timeout
2021-04-09 15:06:55 -04:00
local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
2021-04-28 18:54:39 -04:00
local Promise = dofile(base.."/promise.lua")
2021-05-01 19:39:35 -04:00
-- Only the parent should manage ticks!
callback.register('timer', function()
Promise.update()
set_timer_timeout(1)
2021-05-01 19:39:35 -04:00
end)
set_timer_timeout(1)
2021-04-09 15:06:55 -04:00
local Runner = dofile(base.."/runner.lua")
2021-04-23 15:39:11 -04:00
local serpent = dofile(base.."/serpent.lua")
2021-05-01 19:39:35 -04:00
local util = dofile(base.."/util.lua")(Promise)
2021-04-23 18:09:35 -04:00
2021-04-09 15:06:55 -04:00
local statusLine = nil
local statusColor = 0x0000ff00
2021-04-28 18:54:39 -04:00
local species = nil
local speciesId = -1
2021-04-28 18:54:39 -04:00
local generationIndex = nil
local inputPipeName = os.getenv("RUNNER_INPUT_PIPE")
2021-05-04 20:30:52 -04:00
local outputPipeName = os.getenv("RUNNER_OUTPUT_PIPE")
print('Opening input pipe '..inputPipeName)
local inputPipe = util.openReadPipe(inputPipeName)
if inputPipe == nil then
error('Error opening input file')
end
print('Opened input pipe '..inputPipeName)
2021-05-04 20:30:52 -04:00
print('Opening output file '..outputPipeName)
local outputPipe = util.openReadPipeWriter(outputPipeName)
print('Opened output file '..outputPipeName)
local function writeResponse(object)
2021-05-04 20:30:52 -04:00
outputPipe:write(serpent.dump(object).."\n")
outputPipe:flush()
end
2021-05-01 19:39:35 -04:00
local runner = Runner(Promise)
2021-04-09 15:06:55 -04:00
runner.onMessage(function(msg, color)
statusLine = msg
statusColor = color
2021-04-28 18:54:39 -04:00
print(msg)
writeResponse({
type = 'onMessage',
speciesId = speciesId,
msg = msg,
color = color,
})
2021-04-09 15:06:55 -04:00
end)
runner.onRenderForm(function(form)
2021-05-01 20:50:13 -04:00
local guiWidth, guiHeight = gui.resolution()
2021-04-24 06:00:28 -04:00
gui.left_gap(0)
2021-04-09 15:06:55 -04:00
gui.top_gap(0)
gui.bottom_gap(0)
gui.right_gap(0)
2021-04-24 06:00:28 -04:00
form:draw(0, 0)
2021-04-09 15:06:55 -04:00
if statusLine ~= nil then
2021-05-01 20:50:13 -04:00
gui.rectangle(0, guiHeight - 20, guiWidth, 20, 1, 0x00000000, statusColor)
2021-04-24 06:00:28 -04:00
gui.text(0, guiHeight - 20, statusLine, 0x00000000)
2021-04-09 15:06:55 -04:00
end
-- This isn't passed up to the parent since we're handling the GUI.
end)
runner.onSave(function(filename)
writeResponse({
type = 'onSave',
filename = filename,
speciesId = speciesId,
})
2021-04-09 15:06:55 -04:00
end)
runner.onLoad(function(filename)
writeResponse({
type = 'onLoad',
filename = filename,
speciesId = speciesId,
})
2021-04-09 15:06:55 -04:00
end)
local function waitLoop(inputLine)
return util.promiseWrap(function()
local ok, inputData = serpent.load(inputLine)
2021-04-28 18:54:39 -04:00
if not ok or inputData == nil or speciesId == inputData[1].id then
2021-05-05 00:16:41 -04:00
io.stderr:write("Deserialization error\n")
io.stderr:write(inputLine.."\n")
return
end
print('Received input from master process')
2021-04-28 18:54:39 -04:00
species = inputData[1]
2021-04-28 18:54:39 -04:00
speciesId = species.id
2021-04-28 18:54:39 -04:00
generationIndex = inputData[2]
2021-04-28 18:54:39 -04:00
print('Running')
2021-04-28 18:54:39 -04:00
return runner.run(
species,
generationIndex,
function(genome, index)
writeResponse({
2021-04-28 18:54:39 -04:00
type = 'onGenome',
genome = genome,
genomeIndex = index,
speciesId = speciesId,
})
end
):next(function()
writeResponse({
2021-05-01 19:39:35 -04:00
type = 'onFinish',
speciesId = speciesId,
})
end)
end):next(function()
return inputPipe:read("*l")
2021-05-01 19:39:35 -04:00
end):next(waitLoop)
end
2021-04-28 18:54:39 -04:00
2021-05-01 19:39:35 -04:00
local sec, usec = utime()
local ts = sec * 1000000 + usec
local waiter = util.promiseWrap(function()
return inputPipe:read("*l")
end)
writeResponse({ type = 'onInit', ts = ts })
2021-05-01 19:39:35 -04:00
print(string.format('Wrote init to output at %d', ts))
waiter:next(waitLoop):catch(function(error)
if type(error) == "table" then
error = "\n"..table.concat(error, "\n")
end
print('Runner process error: '..error)
io.stderr:write('Runner process error: '..error..'\n')
end)