Skip to content

Commit 017e463

Browse files
committed
feat(lua.endpoints): add screenshot endpoint
1 parent ade21ef commit 017e463

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

balatrobot.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ BB_ENDPOINTS = {
1313
-- Save/load endpoints
1414
"src/lua/endpoints/save.lua",
1515
"src/lua/endpoints/load.lua",
16+
-- Screenshot endpoint
17+
"src/lua/endpoints/screenshot.lua",
1618
-- Game control endpoints
1719
"src/lua/endpoints/set.lua",
1820
"src/lua/endpoints/add.lua",

src/lua/endpoints/screenshot.lua

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)