Skip to content

Commit

Permalink
Clean up livereload
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarity Flowers committed Oct 29, 2019
1 parent e03f0a0 commit 63cd661
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 185 deletions.
10 changes: 5 additions & 5 deletions draw.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local draw = {}
local livereload = require "livereload2"
-- local metropolis = livereload.init("metropolis_draw.lua")
-- local riverboat = livereload.init("riverboat_draw.lua")
-- local watermill = livereload.init("watermill_draw.lua")
local rack = livereload.init("rack_draw.lua")
local livereload = require "livereload"
-- local metropolis = livereload "metropolis_draw"
-- local riverboat = livereload "riverboat_draw"
-- local watermill = livereload "watermill_draw"
local rack = livereload "rack_draw"
local modes = require "modes"

local note_font = love.graphics.newFont("Bravura.otf", 30)
Expand Down
3 changes: 2 additions & 1 deletion draw_tools.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local tools = {}
local constants = require("livereload2").init("draw_constants.lua")
local livereload = require "livereload"
local constants = livereload "draw_constants"

function tools.drawStaffLines(x, flow_y, width, cleff, octave, play)
love.graphics.setLineWidth(1)
Expand Down
120 changes: 76 additions & 44 deletions livereload.lua
Original file line number Diff line number Diff line change
@@ -1,59 +1,91 @@
local lfs = require "lfs"

local livereload = {}
local RELOAD_CLOCK_TIME = 0.05

function livereload.init(file, reload_clock_time, module_args)
reload_clock_time = reload_clock_time or 0.05
local last_modified = 0
local reload_clock = reload_clock_time
local module = nil
local past_modules = {}
local last_state = nil
local function load_module(file, module_args, name)
local loaded_module, error = love.filesystem.load(file)
if error then
print("\nFailed when compiling " .. file, error)
return nil
end
if loaded_module then
local function fn() return loaded_module(module_args) end
local success, result = xpcall(fn, debug.traceback)
if success then
if result then
if name then
print("reloaded " .. file .. " (" .. name .. ")!")
else
print("loaded " .. file .. "!")
end
return result
end
else
print("\nFailed initing " .. file, result)
return nil
end
else
print("\nModule didn't load but had no error")
return nil
end
end

local function livereload(file, module_args)
file = file .. ".lua"
local last_modified = lfs.attributes(file, "modification")

local module = load_module(file, module_args)
local last_load = love.timer.getTime()
if module == nil then error("The module needs to exist!") end
local prev_module = nil
local broken = false

local live_module = {}

function live_module.run(state, dt, run_args)
reload_clock = reload_clock + dt
if reload_clock > reload_clock_time then
reload_clock = reload_clock - reload_clock_time
local modified = lfs.attributes(file, "modification")
if modified > last_modified then
local loaded_module, error = loadfile(file)
if error then
last_modified = modified
print("\nFailed when compiling " .. file, error)
for name, value in pairs(module) do
if type(value) == "function" then
local function fn(...)
local args = {...}
local time = love.timer.getTime()
local modified = lfs.attributes(file, "modification")
if modified > last_modified and time - last_load >= RELOAD_CLOCK_TIME then
local result = load_module(file, module_args, name)
if result ~= nil then
prev_module = module
module = result
last_modified = modified
last_load = time
broken = false
elseif prev_modules ~= nil then
module = prev_module
prev_module = nil
else
print("The module didn't load idk")
end
end
if loaded_module then
local function fn() return loaded_module(module_args) end
local success, result = xpcall(fn, debug.traceback)
if success then
if result then
print "reloaded!"
table.insert(past_modules, {module, last_state})
module = result
last_modified = modified
if module == nil then error("No module") end
while module and not broken do
local function fn2() return module[name](unpack(args)) end
local xpcall_result = {xpcall(fn2, debug.traceback)}
if not xpcall_result[1] then
print("\nFailed when running " .. file)
print(xpcall_result[2])
if prev_module then
module = prev_module
prev_module = nil
else
broken = true
print("No fallbacks for broken module")
end
else
print("\nFailed initing " .. file, result)
last_modified = modified
return unpack(xpcall_result, 2, #xpcall_result)
end
end
return nil
end
end

if module then
local function fn() return module.run(state, dt, run_args) end
local success, result = xpcall(fn, debug.traceback)
if success then
last_state = result
return result
else
print("\nFailed when running " ..file, result)
if #past_modules then
module, last_state = unpack(table.remove(past_modules))
end
return last_state
end
live_module[name] = fn
else
live_module[name] = value
end
end

Expand Down
96 changes: 0 additions & 96 deletions livereload2.lua

This file was deleted.

4 changes: 2 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local livereload = require "livereload2"
local livereload = require "livereload"


local state
Expand Down Expand Up @@ -53,7 +53,7 @@ function love.load()
midithread = love.thread.newThread("midiloader.lua")
midithread:start(midiout, midiinput)

draw = livereload.init("draw.lua")
draw = livereload "draw"

font = love.graphics.newFont("leaguespartan-bold.ttf", 10)
love.graphics.setFont(font)
Expand Down
4 changes: 2 additions & 2 deletions metropolis_draw.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local livereload = require "livereload2"
local livereload = require "livereload"
local constants = require("draw_constants")
local tools = livereload.init("draw_tools.lua")
local tools = livereload "draw_tools"

local metropolis = {}

Expand Down
8 changes: 4 additions & 4 deletions midi.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local luamidi = require "luamidi"
local livereload = require "livereload2"
local metropolis = livereload.init("metropolis_midi.lua")
local riverboat = livereload.init("riverboat.lua")
local watermill = livereload.init("watermill_midi.lua")
local livereload = require "livereload"
local metropolis = livereload "metropolis_midi"
local riverboat = livereload "riverboat"
local watermill = livereload "watermill_midi"
local tools = require "miditools"
local modes = require "modes"

Expand Down
4 changes: 2 additions & 2 deletions midiloader.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local timer = require "love.timer"
-- local lfs = require "lfs"
local livereload = require "livereload2"
local livereload = require "livereload"


-- Receive values sent via thread:start
Expand All @@ -15,7 +15,7 @@ end

local time = nil

local module = livereload.init("rack.lua")
local module = livereload "rack"
local running = true

while running do
Expand Down
6 changes: 3 additions & 3 deletions modules.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local livereload = require "livereload2"
local utils = livereload.init('utils.lua')
local notes = livereload.init "notes.lua"
local livereload = require "livereload"
local utils = livereload "utils"
local notes = livereload "notes"

local modules = {}

Expand Down
12 changes: 6 additions & 6 deletions music_draw.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local livereload = require "livereload2"
local tools = livereload.init("draw_tools.lua")
local constants = livereload.init("draw_constants.lua")
local notes = livereload.init("notes.lua")
local utils = livereload.init("utils.lua")
local midiconst = livereload.init 'midi_constants.lua'
local livereload = require "livereload"
local tools = livereload "draw_tools"
local constants = livereload "draw_constants"
local notes = livereload "notes"
local utils = livereload "utils"
local midiconst = livereload "midi_constants"

local idefaults = utils.idefaults

Expand Down
2 changes: 1 addition & 1 deletion notes.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local livereload = require "livereload"
local utils = livereload.init "utils.lua"
local utils = livereload "utils"
local notes = {}

--[[
Expand Down
14 changes: 7 additions & 7 deletions rack.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local livereload = require "livereload2"
local utils = livereload.init('utils.lua')
local modules = livereload.init('modules.lua')
local notes = livereload.init('notes.lua')
local constants = livereload.init 'midi_constants.lua'
local tools = livereload.init 'miditools.lua'
local livereload = require "livereload"
local utils = livereload "utils"
local modules = livereload "modules"
local notes = livereload "notes"
local constants = livereload "midi_constants"
local tools = livereload "miditools"

local rack = {}

Expand Down Expand Up @@ -34,7 +34,7 @@ step:
}
]]

local notes = livereload.init("notes.lua")
local notes = livereload "notes"

local function events_equal(a, b)
if a.time == b.time then
Expand Down
14 changes: 7 additions & 7 deletions rack_draw.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local livereload = require "livereload2"
local tools = livereload.init("draw_tools.lua")
local constants = livereload.init("draw_constants.lua")
local notes = livereload.init("notes.lua")
local draw = livereload.init("music_draw.lua")
local utils = livereload.init('utils.lua')
local midiconst = livereload.init 'midi_constants.lua'
local livereload = require "livereload"
local tools = livereload "draw_tools"
local constants = livereload "draw_constants"
local notes = livereload "notes"
local draw = livereload "music_draw"
local utils = livereload "utils"
local midiconst = livereload "midi_constants"

local rack = {}

Expand Down
6 changes: 3 additions & 3 deletions riverboat_draw.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local riverboat = {}
local livereload = require "livereload2"
local livereload = require "livereload"
local constants = require "draw_constants"
local miditools = livereload.init("miditools.lua")
local tools = livereload.init("draw_tools.lua")
local miditools = livereload "miditools"
local tools = livereload "draw_tools"


function riverboat.draw(state, time, fonts, selected, x, section_y, width)
Expand Down
Loading

0 comments on commit 63cd661

Please sign in to comment.