142 lines
4.0 KiB
Luau
142 lines
4.0 KiB
Luau
--!strict
|
|
-- host/Love: LÖVE 11+. Files through love.filesystem (the game folder and the save directory), falling back to io for
|
|
-- paths outside them. ImageData is rgba8; SoundData is 16-bit.
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
|
|
local Host = {}
|
|
Host.name = "love"
|
|
Host.hasFiles = true
|
|
|
|
local function love(): any
|
|
return (_G :: any).love
|
|
end
|
|
local function io(): any
|
|
return (_G :: any).io
|
|
end
|
|
-- the FFI, when the build runs on LuaJIT (always, under LÖVE); called through _G so the build leaves it alone
|
|
local function ffi(): any
|
|
local req = (_G :: any).require
|
|
local ok, m = pcall(req, "ffi")
|
|
return if ok then m else nil
|
|
end
|
|
|
|
function Host.readFile(path: string): (buffer?, Err.Err?)
|
|
local fs = love().filesystem
|
|
if fs.getInfo(path, "file") then
|
|
local s, msg = fs.read(path)
|
|
if type(s) ~= "string" then
|
|
return nil, Err.new(Err.IO, "cannot read " .. path .. ": " .. tostring(msg))
|
|
end
|
|
return buffer.fromstring(s), nil
|
|
end
|
|
local f, msg = io().open(path, "rb")
|
|
if not f then
|
|
return nil, Err.new(Err.IO, "cannot open " .. path .. ": " .. tostring(msg))
|
|
end
|
|
local s = f:read("*a")
|
|
f:close()
|
|
return buffer.fromstring(s), nil
|
|
end
|
|
|
|
function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?)
|
|
local s = buffer.tostring(data)
|
|
if string.sub(path, 1, 1) ~= "/" and not string.find(path, "^%a:[/\\]") then
|
|
local ok, msg = love().filesystem.write(path, s)
|
|
if not ok then
|
|
return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(msg))
|
|
end
|
|
return true, nil
|
|
end
|
|
local f, msg = io().open(path, "wb")
|
|
if not f then
|
|
return false, Err.new(Err.IO, "cannot create " .. path .. ": " .. tostring(msg))
|
|
end
|
|
f:write(s)
|
|
f:close()
|
|
return true, nil
|
|
end
|
|
|
|
function Host.exists(path: string): boolean
|
|
if love().filesystem.getInfo(path, "file") then
|
|
return true
|
|
end
|
|
local f = io().open(path, "rb")
|
|
if f then
|
|
f:close()
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
function Host.toImageData(img: any): (any, Err.Err?)
|
|
if img.format ~= "rgba8" then
|
|
return nil, Err.new(Err.ARG, "toImageData: needs rgba8 pixels, got " .. tostring(img.format))
|
|
end
|
|
local n = img.width * img.height * 4
|
|
local F: any = ffi()
|
|
local B: { toffi: ((buffer) -> any)? } = buffer :: any
|
|
if F and B.toffi then
|
|
local id = love().image.newImageData(img.width, img.height, "rgba8")
|
|
F.copy(id:getFFIPointer(), B.toffi(img.pixels), n)
|
|
return id
|
|
end
|
|
local bytes = love().data.newByteData(buffer.readstring(img.pixels, 0, n))
|
|
return love().image.newImageData(img.width, img.height, "rgba8", bytes)
|
|
end
|
|
|
|
function Host.fromImageData(id: any): Types.Image
|
|
local w, h = id:getDimensions()
|
|
local img = Types.Image.new(w, h, "rgba8")
|
|
local F: any = ffi()
|
|
local B: { toffi: ((buffer) -> any)? } = buffer :: any
|
|
if F and B.toffi and id:getFormat() == "rgba8" then
|
|
F.copy(B.toffi(img.pixels), id:getFFIPointer(), w * h * 4)
|
|
return img
|
|
end
|
|
local s = id:getString()
|
|
buffer.writestring(img.pixels, 0, s, math.min(#s, w * h * 4))
|
|
return img
|
|
end
|
|
|
|
function Host.toSoundData(a: any): (any, Err.Err?)
|
|
local samples, frames, channels, rate = a.samples, a.frames, a.channels, a.sampleRate
|
|
if samples == nil then
|
|
local parts, total = {}, 0
|
|
while true do
|
|
local out, n = a:read(65536)
|
|
if n == 0 then
|
|
break
|
|
end
|
|
local copy = buffer.create(n * channels * 4)
|
|
buffer.copy(copy, 0, out, 0, n * channels * 4)
|
|
table.insert(parts, copy)
|
|
total += n
|
|
end
|
|
samples = buffer.create(total * channels * 4)
|
|
local o = 0
|
|
for _, p in parts do
|
|
buffer.copy(samples, o, p)
|
|
o += buffer.len(p)
|
|
end
|
|
frames = total
|
|
end
|
|
local sd = love().sound.newSoundData(math.max(frames, 1), rate, 16, channels)
|
|
local count = frames * channels
|
|
local F: any = ffi()
|
|
if F then
|
|
local p = F.cast("int16_t*", sd:getFFIPointer())
|
|
for i = 0, count - 1 do
|
|
local v = math.clamp(buffer.readf32(samples, i * 4), -1, 1)
|
|
p[i] = math.floor(v * 32767 + 0.5)
|
|
end
|
|
else
|
|
for i = 0, count - 1 do
|
|
sd:setSample(i, math.clamp(buffer.readf32(samples, i * 4), -1, 1))
|
|
end
|
|
end
|
|
return sd
|
|
end
|
|
|
|
return Host
|