neat-donk/runner-wrapper.lua

128 lines
3.5 KiB
Lua
Raw Normal View History

2021-04-09 15:06:55 -04:00
local base = string.gsub(@@LUA_SCRIPT_FILENAME@@, "(.*[/\\])(.*)", "%1")
2021-04-23 15:39:11 -04:00
local serpent = dofile(base.."/serpent.lua")
2021-04-09 15:06:55 -04:00
local tmpFileName = "/tmp/donk_runner_"..tostring(math.floor(random.integer(0, 0xffffffffffffffff))):hex()
local function message(_M, msg, color)
if color == nil then
color = 0x00009900
end
for i=#_M.onMessageHandler,1,-1 do
_M.onMessageHandler[i](msg, color)
end
end
2021-04-09 17:47:08 -04:00
local function save(_M, filename)
2021-04-09 15:06:55 -04:00
for i=#_M.onSaveHandler,1,-1 do
2021-04-09 17:47:08 -04:00
_M.onSaveHandler[i](filename)
2021-04-09 15:06:55 -04:00
end
end
local function onSave(_M, handler)
table.insert(_M.onSaveHandler, handler)
end
2021-04-09 17:47:08 -04:00
local function load(_M, filename)
2021-04-09 15:06:55 -04:00
for i=#_M.onLoadHandler,1,-1 do
2021-04-09 17:47:08 -04:00
_M.onLoadHandler[i](filename)
2021-04-09 15:06:55 -04:00
end
end
local function onLoad(_M, handler)
table.insert(_M.onLoadHandler, handler)
end
2021-04-09 17:47:08 -04:00
local function onMessage(_M, handler)
table.insert(_M.onMessageHandler, handler)
end
2021-04-09 15:06:55 -04:00
return function()
local _M = {
onMessageHandler = {},
2021-04-09 17:47:08 -04:00
onSaveHandler = {},
onLoadHandler = {},
2021-04-09 15:06:55 -04:00
}
_M.onRenderForm = function(handler)
end
_M.onMessage = function(handler)
onMessage(_M, handler)
end
2021-04-09 17:47:08 -04:00
_M.message = function(msg, color)
message(_M, msg, color)
end
_M.onSave = function(handler)
onSave(_M, handler)
end
_M.onLoad = function(handler)
onLoad(_M, handler)
end
2021-04-09 15:06:55 -04:00
_M.run = function(species, generationIdx, speciesIdx, genomeCallback, finishCallback)
local poppets = {}
for i=1,#species,1 do
2021-04-23 18:09:35 -04:00
local outputFileName = tmpFileName..'_output_'..i
local trunc = io.open(outputFileName, 'w')
trunc:close()
local inputFileName = tmpFileName.."_input_"..i
local inputFile = io.open(inputFileName, 'w')
inputFile:write(serpent.dump({species[i], generationIdx, speciesIdx + i - 1, outputFileName}))
inputFile:close()
local cmd = "RUNNER_DATA=\""..inputFileName.."\" lsnes \"--rom="..base.."/rom.sfc\" --unpause \"--lua="..base.."/runner-process.lua\""
message(_M, cmd)
local poppet = io.popen(cmd, 'r')
2021-04-09 15:06:55 -04:00
table.insert(poppets, poppet)
end
for i=1,#poppets,1 do
local poppet = poppets[i]
poppet:read('*a')
poppet:close()
end
2021-04-23 18:09:35 -04:00
for i=1,#species,1 do
local outputFileName = tmpFileName..'_output_'..i
local outputFile = io.open(outputFileName, "r")
local line = ""
repeat
local obj, err = loadstring(line)
if err ~= nil then
goto continue
end
obj = obj()
if obj == nil then
goto continue
end
if obj.type == 'onMessage' then
message(_M, obj.msg, obj.color)
elseif obj.type == 'onLoad' then
load(_M, obj.filename)
elseif obj.type == 'onSave' then
save(_M, obj.filename)
elseif obj.type == 'onGenome' then
species[obj.speciesIndex - speciesIdx + 1].genomes[obj.genomeIndex] = obj.genome
genomeCallback(obj.genome, obj.index)
elseif obj.type == 'onFinish' then
finishCallback()
end
::continue::
line = outputFile:read()
until(line == "" or line == nil)
outputFile:close()
2021-04-09 15:06:55 -04:00
end
end
return _M
end