sokoban.love/app.lua

539 lines
16 KiB
Lua
Raw Normal View History

2023-08-31 06:58:21 +01:00
nativefs = require 'nativefs'
2023-09-04 22:17:04 +01:00
local Keys_down = {}
-- main entrypoint for LÖVE
--
-- Most apps can just use the default shown in https://love2d.org/wiki/love.run,
-- but we need to override it to:
2023-09-04 22:17:04 +01:00
-- * recover from errors (by sending them to the driver and waiting for a command)
-- * run all tests (functions starting with 'test_') on startup, and
-- * save some state that makes it possible to switch between the main app
-- and a source editor, while giving each the illusion of complete
-- control.
function love.run()
2023-04-20 03:45:17 +01:00
App.version_check()
App.snapshot_love()
2023-11-17 06:10:03 +00:00
live.load()
-- have LÖVE delegate all handlers to App if they exist
for name in pairs(love.handlers) do
if App[name] then
2023-09-04 22:17:04 +01:00
-- love.keyboard.isDown doesn't work on Android, so emulate it using
-- keypressed and keyreleased events
if name == 'keypressed' then
love.handlers[name] = function(key, scancode, isrepeat)
Keys_down[key] = true
return App.keypressed(key, scancode, isrepeat)
end
elseif name == 'keyreleased' then
love.handlers[name] = function(key, scancode)
Keys_down[key] = nil
return App.keyreleased(key, scancode)
end
else
love.handlers[name] = App[name]
end
end
end
fix initialization errors using driver.love Changes inside on.initialize are minefields. Until now, if you made a mistake when modifying on.initialize, you could end up in a situation where the app would fail irrecoverably on the next startup. You'd have to go dig up a text editor to fix it. After this commit, errors in on.initialize wait for commands from driver.love just like any other error. Recovering from errors during initialization is a little different than normal. I don't know how much of initialization completed successfully, so I redo all of it. I think this should be safe; the sorts of things we want to do on startup tend to be idempotent just like the sorts of things we do within an event loop with our existing error handling. Things are still not ideal. Initialization by definition happens only when the app starts up. When you make changes to it, you won't find out about errors until you restart the app[1], which can be much later and a big context switch. But at least you'll be able to fix it in the usual way. Slightly more seamless[2]. One glitch to note: at least on Linux, an app with an initialization error feels "sticky". I can't seem to switch focus away from it using Alt-tab. Hitting F4 on the driver also jarringly brings the client app back in focus when there was an initialization error. But the mouse does work consistently. This feels similar to the issues I find when an app goes unresponsive sometimes. The window manager really wants me to respond to the dialog that it's unresponsive. Still, feels like an improvement. [1] I really need to provide that driver command to restart the app! But there's no room in the menus! I really need a first-class command palette like pensieve.love has! [2] https://lobste.rs/s/idi1wt/open_source_vs_ux
2023-11-17 16:31:41 +00:00
-- Stash initial state of App (right after loading files) for tests
if App_for_tests == nil then
App_for_tests = {}
for k,v in pairs(App) do
App_for_tests[k] = v
end
-- there's one nested table
App_for_tests.screen = {}
for k,v in pairs(App.screen) do
App_for_tests.screen[k] = v
end
end
-- Mutate App for the real app
-- disable test methods
App.screen.init = nil
App.filesystem = nil
App.time = nil
App.run_after_textinput = nil
App.run_after_keychord = nil
App.keypress = nil
App.keyrelease = nil
App.run_after_mouse_click = nil
App.run_after_mouse_press = nil
App.run_after_mouse_release = nil
App.fake_keys_pressed = nil
App.fake_key_press = nil
App.fake_key_release = nil
App.fake_mouse_state = nil
App.fake_mouse_press = nil
App.fake_mouse_release = nil
2023-09-04 22:17:04 +01:00
-- other methods dispatch to real hardware
App.screen.resize = love.window.setMode
App.screen.size = love.window.getMode
App.screen.move = love.window.setPosition
App.screen.position = love.window.getPosition
App.screen.print = love.graphics.print
2023-08-31 06:58:21 +01:00
App.open_for_reading =
function(filename)
local result = nativefs.newFile(filename)
local ok, err = result:open('r')
if ok then
return result
else
return ok, err
end
end
2023-09-09 17:53:37 +01:00
App.read_file =
function(path)
if not is_absolute_path(path) then
return --[[status]] false, 'Please use an unambiguous absolute path.'
end
local f, err = App.open_for_reading(path)
if err then
return --[[status]] false, err
end
local contents = f:read()
f:close()
return contents
end
2023-08-31 06:58:21 +01:00
App.open_for_writing =
function(filename)
local result = nativefs.newFile(filename)
local ok, err = result:open('w')
if ok then
return result
else
return ok, err
end
end
2023-09-09 17:53:37 +01:00
App.write_file =
function(path, contents)
if not is_absolute_path(path) then
return --[[status]] false, 'Please use an unambiguous absolute path.'
end
local f, err = App.open_for_writing(path)
if err then
return --[[status]] false, err
end
f:write(contents)
f:close()
return --[[status]] true
end
2023-08-31 06:58:21 +01:00
App.files = nativefs.getDirectoryItems
2023-09-08 22:58:54 +01:00
App.mkdir = nativefs.createDirectory
App.remove = nativefs.remove
2023-10-28 09:05:35 +01:00
App.source_dir = love.filesystem.getSource()..'/' -- '/' should work even on Windows
2023-08-31 06:58:21 +01:00
App.current_dir = nativefs.getWorkingDirectory()..'/'
App.save_dir = love.filesystem.getSaveDirectory()..'/'
App.get_time = love.timer.getTime
App.get_clipboard = love.system.getClipboardText
App.set_clipboard = love.system.setClipboardText
2023-09-04 22:17:04 +01:00
App.key_down = function(key) return Keys_down[key] end
App.mouse_move = love.mouse.setPosition
App.mouse_down = love.mouse.isDown
App.mouse_x = love.mouse.getX
App.mouse_y = love.mouse.getY
-- Tests always run at the start.
2023-01-21 07:21:59 +00:00
Test_errors = {}
App.run_tests(record_error)
-- example handler
if #Test_errors > 0 then
Mode = 'error'
Redo_initialization = true
Error_message = 'There were test failures:\n\n'..table.concat(Test_errors, '\n')
live.send_run_time_error_to_driver(Error_message)
end
App.initialize_globals()
fix initialization errors using driver.love Changes inside on.initialize are minefields. Until now, if you made a mistake when modifying on.initialize, you could end up in a situation where the app would fail irrecoverably on the next startup. You'd have to go dig up a text editor to fix it. After this commit, errors in on.initialize wait for commands from driver.love just like any other error. Recovering from errors during initialization is a little different than normal. I don't know how much of initialization completed successfully, so I redo all of it. I think this should be safe; the sorts of things we want to do on startup tend to be idempotent just like the sorts of things we do within an event loop with our existing error handling. Things are still not ideal. Initialization by definition happens only when the app starts up. When you make changes to it, you won't find out about errors until you restart the app[1], which can be much later and a big context switch. But at least you'll be able to fix it in the usual way. Slightly more seamless[2]. One glitch to note: at least on Linux, an app with an initialization error feels "sticky". I can't seem to switch focus away from it using Alt-tab. Hitting F4 on the driver also jarringly brings the client app back in focus when there was an initialization error. But the mouse does work consistently. This feels similar to the issues I find when an app goes unresponsive sometimes. The window manager really wants me to respond to the dialog that it's unresponsive. Still, feels like an improvement. [1] I really need to provide that driver command to restart the app! But there's no room in the menus! I really need a first-class command palette like pensieve.love has! [2] https://lobste.rs/s/idi1wt/open_source_vs_ux
2023-11-17 16:31:41 +00:00
xpcall(function() App.initialize(love.arg.parseGameArguments(arg), arg) end, live.handle_initialization_error)
love.timer.step()
local dt = 0
-- one iteration of the event loop
-- return nil to continue the event loop, non-nil to quit
App.run_frame = function()
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
dt = love.timer.step()
App.update(dt)
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
App.draw()
love.graphics.present()
love.timer.sleep(0.001)
-- returning nil continues the loop
end
-- protect against runtime errors
return function()
local status, result = xpcall(App.run_frame, live.handle_error)
return result
end
end
-- The rest of this file wraps around various LÖVE primitives to support
-- automated tests. Often tests will run with a fake version of a primitive
-- that redirects to the real love.* version once we're done with tests.
--
-- Not everything is so wrapped yet. Sometimes you still have to use love.*
-- primitives directly.
App = {}
-- save/restore various framework globals we care about -- only on very first load
function App.snapshot_love()
if Love_snapshot then return end
Love_snapshot = {}
-- save the entire initial font; it doesn't seem reliably recreated using newFont
Love_snapshot.initial_font = love.graphics.getFont()
end
function App.undo_initialize()
love.graphics.setFont(Love_snapshot.initial_font)
end
2023-08-27 00:16:21 +01:00
function App.run_tests(record_error_fn)
local sorted_names = {}
for name,binding in pairs(_G) do
if name:find('test_') == 1 then
table.insert(sorted_names, name)
end
end
table.sort(sorted_names)
2023-08-27 00:16:21 +01:00
local globals = App.shallow_copy_all_globals()
App = App_for_tests
local saved_font = love.graphics.getFont()
love.graphics.setFont(Love_snapshot.initial_font)
for _,name in ipairs(sorted_names) do
App.initialize_for_test()
--? print('=== '..name)
2023-08-27 00:16:21 +01:00
xpcall(_G[name], function(err) record_error_fn(name, err) end)
end
2023-08-27 00:16:21 +01:00
love.graphics.setFont(saved_font)
-- restore all global state except Test_errors
local test_errors = Test_errors
App.restore_all_globals(globals)
Test_errors = test_errors
end
2023-08-27 00:16:21 +01:00
function App.run_test(test, record_error_fn)
local globals = App.shallow_copy_all_globals()
App = App_for_tests
local saved_font = love.graphics.getFont()
love.graphics.setFont(Love_snapshot.initial_font)
App.initialize_for_test()
xpcall(test, function(err) record_error_fn('', err) end)
love.graphics.setFont(saved_font)
-- restore all global state except Test_errors
local test_errors = Test_errors
App.restore_all_globals(globals)
Test_errors = test_errors
end
2023-08-27 00:04:20 +01:00
function App.initialize_for_test()
2023-04-12 05:33:33 +01:00
App.screen.init{width=100, height=50}
App.screen.contents = {} -- clear screen
App.filesystem = {}
App.source_dir = ''
App.current_dir = ''
App.save_dir = ''
App.fake_keys_pressed = {}
App.fake_mouse_state = {x=-1, y=-1}
2023-01-23 06:12:58 +00:00
App.initialize_globals()
end
-- App.screen.resize and App.screen.move seem like better names than
-- love.window.setMode and love.window.setPosition respectively. They'll
-- be side-effect-free during tests, and they'll save their results in
-- attributes of App.screen for easy access.
App.screen={}
-- Use App.screen.init in tests to initialize the fake screen.
function App.screen.init(dims)
App.screen.width = dims.width
App.screen.height = dims.height
end
function App.screen.resize(width, height, flags)
App.screen.width = width
App.screen.height = height
App.screen.flags = flags
end
function App.screen.size()
return App.screen.width, App.screen.height, App.screen.flags
end
function App.screen.move(x,y, displayindex)
App.screen.x = x
App.screen.y = y
App.screen.displayindex = displayindex
end
function App.screen.position()
return App.screen.x, App.screen.y, App.screen.displayindex
end
-- If you use App.screen.print instead of love.graphics.print,
-- tests will be able to check what was printed using App.screen.check below.
--
-- One drawback of this approach: the y coordinate used depends on font size,
-- which feels brittle.
function App.screen.print(msg, x,y)
local screen_row = 'y'..tostring(y)
2022-05-23 23:38:42 +01:00
--? print('drawing "'..msg..'" at y '..tostring(y))
local screen = App.screen
if screen.contents[screen_row] == nil then
screen.contents[screen_row] = {}
for i=0,screen.width-1 do
screen.contents[screen_row][i] = ''
end
end
if x < screen.width then
screen.contents[screen_row][x] = msg
end
end
function App.screen.check(y, expected_contents, msg)
--? print('checking for "'..expected_contents..'" at y '..tostring(y))
local screen_row = 'y'..tostring(y)
local contents = ''
if App.screen.contents[screen_row] == nil then
error('no text at y '..tostring(y))
end
for i,s in ipairs(App.screen.contents[screen_row]) do
contents = contents..s
end
check_eq(contents, expected_contents, msg)
2022-07-12 07:03:27 +01:00
end
-- If you access the time using App.get_time instead of love.timer.getTime,
-- tests will be able to move the time back and forwards as needed using
-- App.wait_fake_time below.
App.time = 1
function App.get_time()
return App.time
end
function App.wait_fake_time(t)
App.time = App.time + t
end
2022-05-30 00:37:31 +01:00
function App.width(text)
return love.graphics.getFont():getWidth(text)
2022-05-30 00:37:31 +01:00
end
-- If you access the clipboard using App.get_clipboard and App.set_clipboard
-- instead of love.system.getClipboardText and love.system.setClipboardText
-- respectively, tests will be able to manipulate the clipboard by
-- reading/writing App.clipboard.
2022-06-03 02:52:49 +01:00
App.clipboard = ''
function App.get_clipboard()
2022-06-03 02:52:49 +01:00
return App.clipboard
end
function App.set_clipboard(s)
2022-06-03 02:52:49 +01:00
App.clipboard = s
end
-- In tests I mostly send chords all at once to the keyboard handlers.
-- However, you'll occasionally need to check if a key is down outside a handler.
-- If you use App.key_down instead of love.keyboard.isDown, tests will be able to
-- simulate keypresses using App.fake_key_press and App.fake_key_release
-- below. This isn't very realistic, though, and it's up to tests to
-- orchestrate key presses that correspond to the handlers they invoke.
App.fake_keys_pressed = {}
function App.key_down(key)
return App.fake_keys_pressed[key]
end
function App.fake_key_press(key)
App.fake_keys_pressed[key] = true
end
function App.fake_key_release(key)
App.fake_keys_pressed[key] = nil
end
-- Tests mostly will invoke mouse handlers directly. However, you'll
-- occasionally need to check if a mouse button is down outside a handler.
-- If you use App.mouse_down instead of love.mouse.isDown, tests will be able to
-- simulate mouse clicks using App.fake_mouse_press and App.fake_mouse_release
-- below. This isn't very realistic, though, and it's up to tests to
-- orchestrate presses that correspond to the handlers they invoke.
App.fake_mouse_state = {x=-1, y=-1} -- x,y always set
function App.mouse_move(x,y)
App.fake_mouse_state.x = x
App.fake_mouse_state.y = y
end
function App.mouse_down(mouse_button)
return App.fake_mouse_state[mouse_button]
end
function App.mouse_x()
return App.fake_mouse_state.x
end
function App.mouse_y()
return App.fake_mouse_state.y
end
function App.fake_mouse_press(x,y, mouse_button)
App.fake_mouse_state.x = x
App.fake_mouse_state.y = y
App.fake_mouse_state[mouse_button] = true
end
function App.fake_mouse_release(x,y, mouse_button)
App.fake_mouse_state.x = x
App.fake_mouse_state.y = y
App.fake_mouse_state[mouse_button] = nil
end
-- If you use App.open_for_reading and App.open_for_writing instead of other
-- various Lua and LÖVE helpers, tests will be able to check the results of
-- file operations inside the App.filesystem table.
function App.open_for_reading(filename)
if App.filesystem[filename] then
return {
lines = function(self)
return App.filesystem[filename]:gmatch('[^\n]+')
end,
read = function(self)
return App.filesystem[filename]
end,
close = function(self)
end,
}
end
end
function App.read_file(filename)
return App.filesystem[filename]
end
2023-09-09 16:27:27 +01:00
function App.open_for_writing(filename)
App.filesystem[filename] = ''
return {
write = function(self, s)
App.filesystem[filename] = App.filesystem[filename]..s
end,
close = function(self)
end,
}
end
function App.write_file(filename, contents)
App.filesystem[filename] = contents
return --[[status]] true
end
function App.mkdir(dirname)
-- nothing in test mode
end
function App.remove(filename)
App.filesystem[filename] = nil
end
-- Some helpers to trigger an event and then refresh the screen. Akin to one
-- iteration of the event loop.
-- all textinput events are also keypresses
2022-07-13 06:13:06 +01:00
-- TODO: handle chords of multiple keys
function App.run_after_textinput(t)
App.keypressed(t)
App.textinput(t)
App.keyreleased(t)
App.screen.contents = {}
App.draw()
end
-- not all keys are textinput
2022-07-13 06:13:06 +01:00
-- TODO: handle chords of multiple keys
function App.run_after_keychord(chord)
App.keychord_press(chord)
App.keyreleased(chord)
App.screen.contents = {}
App.draw()
end
function App.run_after_mouse_click(x,y, mouse_button)
App.fake_mouse_press(x,y, mouse_button)
App.mousepressed(x,y, mouse_button)
App.fake_mouse_release(x,y, mouse_button)
App.mousereleased(x,y, mouse_button)
App.screen.contents = {}
App.draw()
end
function App.run_after_mouse_press(x,y, mouse_button)
App.fake_mouse_press(x,y, mouse_button)
App.mousepressed(x,y, mouse_button)
2022-05-25 21:38:33 +01:00
App.screen.contents = {}
App.draw()
end
function App.run_after_mouse_release(x,y, mouse_button)
App.fake_mouse_release(x,y, mouse_button)
App.mousereleased(x,y, mouse_button)
App.screen.contents = {}
App.draw()
end
-- miscellaneous internal helpers
function App.color(color)
love.graphics.setColor(color.r, color.g, color.b, color.a)
end
function App.shallow_copy_all_globals()
local result = {}
for k,v in pairs(_G) do
result[k] = v
end
return result
end
function App.restore_all_globals(x)
-- delete extra bindings
for k,v in pairs(_G) do
if x[k] == nil then
_G[k] = nil
end
end
-- restore previous bindings
for k,v in pairs(x) do
_G[k] = v
end
2023-01-22 05:47:21 +00:00
end
-- Test_errors will be an array
function record_error(test_name, err)
local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '')
table.insert(Test_errors, test_name..' -- '..err_without_line_number)
end
-- Test_errors will be a table by test name
2023-01-24 04:12:23 +00:00
function record_error_by_test(test_name, err)
local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '')
Test_errors[test_name] = err_without_line_number
--? Test_errors[test_name] = debug.traceback(err_without_line_number)
end