nativefs = require 'nativefs'
local Keys_down = {}
function love.run()
Version, Major_version = App.love_version()
App.snapshot_love()
live.load()
for name in pairs(love.handlers) do
if App[name] then
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] = function(...) App[name](...) end
end
end
end
if App_for_tests == nil then
App_for_tests = {}
for k,v in pairs(App) do
App_for_tests[k] = v
end
App_for_tests.screen = {}
for k,v in pairs(App.screen) do
App_for_tests.screen[k] = v
end
end
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
App.screen.resize = function(width, height, flags)
local has_text_input = love.keyboard.hasTextInput()
love.window.setMode(width, height, flags)
if OS == 'iOS' then
love.keyboard.setTextInput(has_text_input) love.keyboard.setKeyRepeat(true)
end
end
App.screen.size = love.window.getMode
App.screen.move = love.window.setPosition
App.screen.position = love.window.getPosition
App.screen.print = love.graphics.print
App.open_for_reading =
function(filename)
local result = love.filesystem.newFile(filename)
local ok, err = result:open('r')
if ok then
return result
else
return ok, err
end
end
App.read_file =
function(path)
if not is_absolute_path(path) then
return false, 'Please use an unambiguous absolute path.'
end
local f, err = App.open_for_reading(path)
if err then
return false, err
end
local contents = f:read()
f:close()
return contents
end
App.open_for_writing =
function(filename)
local result = love.filesystem.newFile(filename)
local ok, err = result:open('w')
if ok then
return result
else
return ok, err
end
end
App.write_file =
function(path, contents)
if not is_absolute_path(path) then
return false, 'Please use an unambiguous absolute path.'
end
local f, err = App.open_for_writing(path)
if err then
return false, err
end
f:write(contents)
f:close()
return true
end
App.files = love.filesystem.getDirectoryItems
App.file_info = love.filesystem.getInfo
App.mkdir = love.filesystem.createDirectory
App.remove = love.filesystem.remove
App.source_dir = love.filesystem.getSource()..'/' 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
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
Test_errors = {}
App.run_tests(record_error)
if #Test_errors > 0 then
print('tests failed; mode error')
Mode = 'error'
Redo_initialization = true
Error_message = ''
if Warning_before_tests then
Error_message = Warning_before_tests..'\n\n'
end
Error_message = 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()
xpcall(function() App.initialize(love.arg.parseGameArguments(arg), arg) end, live.handle_initialization_error)
love.timer.step()
local dt = 0
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)
end
return function()
local status, result = xpcall(App.run_frame, live.handle_error)
return result
end
end
App = {}
function App.love_version()
local major_version, minor_version = love.getVersion()
local version = major_version..'.'..minor_version
return version, major_version
end
function App.snapshot_love()
if Love_snapshot then return end
Love_snapshot = {}
Love_snapshot.initial_font = love.graphics.getFont()
end
function App.undo_initialize()
love.graphics.setFont(Love_snapshot.initial_font)
end
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)
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()
xpcall(_G[name], function(err) record_error_fn(name, err) end)
end
love.graphics.setFont(saved_font)
local test_errors = Test_errors
App.restore_all_globals(globals)
Test_errors = test_errors
end
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)
local test_errors = Test_errors
App.restore_all_globals(globals)
Test_errors = test_errors
end
function App.initialize_for_test()
App.screen.init{width=100, height=50}
App.screen.contents = {} App.filesystem = {}
App.source_dir = ''
App.current_dir = ''
App.save_dir = ''
App.fake_keys_pressed = {}
App.fake_mouse_state = {x=-1, y=-1}
App.initialize_globals()
end
App.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
function App.screen.print(msg, x,y)
local screen_row = '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)
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)
end
App.time = 1
function App.get_time()
return App.time
end
function App.wait_fake_time(t)
App.time = App.time + t
end
function App.width(text)
return love.graphics.getFont():getWidth(text)
end
App.clipboard = ''
function App.get_clipboard()
return App.clipboard
end
function App.set_clipboard(s)
App.clipboard = s
end
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
App.fake_mouse_state = {x=-1, y=-1}
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
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
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 true
end
function App.mkdir(dirname)
end
function App.remove(filename)
App.filesystem[filename] = nil
end
function App.run_after_textinput(t)
App.keypressed(t)
App.textinput(t)
App.keyreleased(t)
App.screen.contents = {}
App.draw()
end
function App.run_after_keychord(chord, key)
App.keychord_press(chord, key)
App.keyreleased(key)
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)
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
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)
for k,v in pairs(_G) do
if x[k] == nil then
_G[k] = nil
end
end
for k,v in pairs(x) do
_G[k] = v
end
end
function record_error(test_name, err)
local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '')
table.insert(Test_errors, test_name..' -- '..err_without_line_number)
end
function record_error_by_test(test_name, err)
local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '')
Test_errors[test_name] = err_without_line_number
end