|
| 1 | +-- src/lua/endpoints/screenshot.lua |
| 2 | + |
| 3 | +-- ========================================================================== |
| 4 | +-- Screenshot Endpoint Params |
| 5 | +-- ========================================================================== |
| 6 | + |
| 7 | +---@class Request.Endpoint.Screenshot.Params |
| 8 | +---@field path string File path for the screenshot file |
| 9 | + |
| 10 | +-- ========================================================================== |
| 11 | +-- Screenshot Endpoint Utils |
| 12 | +-- ========================================================================== |
| 13 | + |
| 14 | +local nativefs = require("nativefs") |
| 15 | + |
| 16 | +-- ========================================================================== |
| 17 | +-- Screenshot Endpoint |
| 18 | +-- ========================================================================== |
| 19 | + |
| 20 | +---@type Endpoint |
| 21 | +return { |
| 22 | + |
| 23 | + name = "screenshot", |
| 24 | + |
| 25 | + description = "Take a screenshot of the current game state", |
| 26 | + |
| 27 | + schema = { |
| 28 | + path = { |
| 29 | + type = "string", |
| 30 | + required = true, |
| 31 | + description = "File path for the screenshot file", |
| 32 | + }, |
| 33 | + }, |
| 34 | + |
| 35 | + requires_state = nil, |
| 36 | + |
| 37 | + ---@param args Request.Endpoint.Screenshot.Params |
| 38 | + ---@param send_response fun(response: Response.Endpoint) |
| 39 | + execute = function(args, send_response) |
| 40 | + local path = args.path |
| 41 | + |
| 42 | + love.graphics.captureScreenshot(function(imagedata) |
| 43 | + -- Encode ImageData to PNG format |
| 44 | + local filedata = imagedata:encode("png") |
| 45 | + |
| 46 | + if not filedata then |
| 47 | + send_response({ |
| 48 | + message = "Failed to encode screenshot", |
| 49 | + name = BB_ERROR_NAMES.INTERNAL_ERROR, |
| 50 | + }) |
| 51 | + return |
| 52 | + end |
| 53 | + |
| 54 | + -- Get PNG data as string |
| 55 | + local png_data = filedata:getString() |
| 56 | + |
| 57 | + -- Write to target path using nativefs |
| 58 | + local write_success = nativefs.write(path, png_data) |
| 59 | + if not write_success then |
| 60 | + send_response({ |
| 61 | + message = "Failed to write screenshot file to '" .. path .. "'", |
| 62 | + name = BB_ERROR_NAMES.INTERNAL_ERROR, |
| 63 | + }) |
| 64 | + return |
| 65 | + end |
| 66 | + |
| 67 | + send_response({ |
| 68 | + success = true, |
| 69 | + path = path, |
| 70 | + }) |
| 71 | + end) |
| 72 | + end, |
| 73 | +} |
0 commit comments