Skip to content

Commit 36092f2

Browse files
committed
Creating less tables.
Added changelog. More tests.
1 parent 21e6c13 commit 36092f2

File tree

6 files changed

+97
-29
lines changed

6 files changed

+97
-29
lines changed

Changelog.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Changelog
2+
LuaHotLoader
3+
4+
v1.3 (2022-07-19)
5+
- Added functions: monitor(), isAllowingExternalPaths().
6+
- Now normalizing paths and file extensions.
7+
- Module loading is more robust in very rare cases.
8+
- Fixed reloaded modules not getting loaded in protected mode like they should have.
9+
- Fixed required modules not getting their name as an argument.
10+
- Fixed %% in the log format resulting in %% getting printed too.
11+
- Fixed files innocently sometimes being treated as text files.
12+
- Fixed some incorrect documentation.
13+
- LÖVE: Using a more efficient way of monitoring files in Windows using WinAPI.
14+
- LÖVE: Updated list of automatically added loaders.
15+
16+
v1.2 (2019-09-06)
17+
- Added functions: resetCheckingState, setLogFormat/getLogFormat.
18+
- LÖVE: Fixed error when both love.graphics and love.audio weren't loaded.
19+
20+
v1.1 (2019-03-06)
21+
- Added functions: hasLoaded, hasRequired.
22+
23+
v1.0.1 (2018-10-09)
24+
- Fixed warning in LÖVE 11.
25+
26+
v1.0 (2018-10-08)
27+
- Initial release!

hotLoader.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,13 @@ local isLovePath
239239

240240

241241

242-
local love_getFileInfo = love and love.filesystem.getInfo
242+
local love_getFileInfo = love and love.filesystem.getInfo
243+
local love_getFileInfo_info = {}
243244

244245
local function fileExists(path)
245246
if isLovePath(path) then
246247
if love_getFileInfo then
247-
return (love_getFileInfo(path, "file") ~= nil)
248+
return (love_getFileInfo(path, "file", love_getFileInfo_info) ~= nil)
248249
else
249250
return love.filesystem.exists(path)
250251
end
@@ -347,7 +348,7 @@ local getLastModifiedTime
347348
if isLovePath(path) then
348349
if love_getFileInfo then
349350
-- LÖVE 11.0+
350-
local info = love_getFileInfo(path, "file")
351+
local info = love_getFileInfo(path, "file", love_getFileInfo_info)
351352
local time = info and info.modtime
352353
if time then return time end
353354

@@ -1025,7 +1026,7 @@ end
10251026

10261027
-- hotLoader.removeAllLoaders( )
10271028
function hotLoader.removeAllLoaders()
1028-
loaders = {}
1029+
if next(loaders) then loaders = {} end
10291030
end
10301031

10311032
-- loader|nil = hotLoader.getCustomLoader( path )
@@ -1052,7 +1053,7 @@ end
10521053

10531054
-- hotLoader.removeAllCustomLoaders( )
10541055
function hotLoader.removeAllCustomLoaders()
1055-
customLoaders = {}
1056+
if next(customLoaders) then customLoaders = {} end
10561057
end
10571058

10581059
-- loader|nil = hotLoader.getDefaultLoader( )
@@ -1192,7 +1193,7 @@ function hotLoader.monitor(path, cbData, cb)
11921193
if callWithData then
11931194
assertarg(3, "onFileModified", cb, "function")
11941195
else
1195-
cbData, cb = nil, cbData
1196+
cb = cbData
11961197
assertarg(2, "onFileModified", cb, "function")
11971198
end
11981199

tests/main.lua

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ io.stdout:setvbuf("no")
99
io.stderr:setvbuf("no")
1010

1111
-- Monkeypatch modules before loading library.
12-
local lfs = not love and require"lfs"
13-
local fakeModTime = 1
12+
local lfs = not love and require"lfs"
13+
local fakeModTimes = {}
1414

1515
local function pack(...)
1616
return {n=select("#", ...), ...}
@@ -24,26 +24,26 @@ if love then
2424
local info, err = _getInfo(path, ...)
2525
if not info then return nil, err end
2626

27-
info.modtime = fakeModTime
27+
info.modtime = fakeModTimes[path] or 1
2828
return info
2929
end
3030

3131
else
3232
local _getLastModified = love.filesystem.getLastModified
3333
local _exists = love.filesystem.exists
3434

35-
function love.filesystem.getLastModified(path) return fakeModTime end
36-
function love.filesystem.exists (path) return true end
35+
function love.filesystem.getLastModified(path) return fakeModTimes[path] or 1 end
36+
function love.filesystem.exists (path) return true end
3737
end
3838

3939
else
4040
local _attributes = lfs.attributes
4141

4242
function lfs.attributes(path, requestNameOrResultTable)
43-
if requestNameOrResultTable == "modification" then return fakeModTime end
43+
if requestNameOrResultTable == "modification" then return fakeModTimes[path] or 1 end
4444

4545
local values = pack(_attributes(path, requestNameOrResultTable))
46-
if type(values[1]) == "table" then values[1].modification = fakeModTime end
46+
if type(values[1]) == "table" then values[1].modification = fakeModTimes[path] or 1 end
4747

4848
return unpack(values, 1, values.n)
4949
end
@@ -87,37 +87,73 @@ do
8787
local hotLoader = newHotLoader()
8888

8989
-- Load file (with a custom loader).
90-
local path = (love and "" or thisDir) .. "test.txt"
91-
local loads = 0
90+
local path1 = (love and "" or thisDir) .. "test1.txt"
91+
local loads = 0
92+
fakeModTimes[path1] = 1
9293

93-
local text = hotLoader.load(path, function(path)
94+
local text = hotLoader.load(path1, function(path)
9495
loads = loads + 1
9596
return readFile(path)
9697
end)
97-
assert(hotLoader.hasLoaded(path))
98+
assert(hotLoader.hasLoaded(path1))
9899
assert(loads == 1) -- The custom loader should've been used.
99-
assert(text == "foobar")
100-
assert(hotLoader.load(path) == "foobar")
100+
assert(text == "foobar1")
101+
assert(hotLoader.load(path1) == "foobar1")
101102
assert(loads == 1) -- The file shouldn't have loaded again.
102103

103104
-- Reload file.
104-
hotLoader.unload(path)
105-
assert(not hotLoader.hasLoaded(path))
106-
hotLoader.unload(path) -- Should do nothing.
107-
hotLoader.load(path)
108-
assert(hotLoader.hasLoaded(path))
105+
hotLoader.unload(path1)
106+
assert(not hotLoader.hasLoaded(path1))
107+
hotLoader.unload(path1) -- Should do nothing.
108+
hotLoader.load(path1)
109+
assert(hotLoader.hasLoaded(path1))
109110
assert(loads == 2)
110111

111112
-- Run updates for a couple of seconds.
112113
for i = 1, 10 do hotLoader.update(.2) end
113114
assert(loads == 2) -- Nothing should've happened.
114115

115116
-- Update the file.
116-
fakeModTime = fakeModTime + 1
117+
fakeModTimes[path1] = fakeModTimes[path1] + 1
117118

118119
-- Run updates for a couple of seconds.
120+
print("Should reload.")
119121
for i = 1, 10 do hotLoader.update(.2) end
120122
assert(loads == 3) -- The file should've reloaded.
123+
124+
-- Monitor the file (which replaces the custom loader).
125+
local path2 = (love and "" or thisDir) .. "test2.txt"
126+
local monitors = 0
127+
fakeModTimes[path2] = 1
128+
hotLoader.monitor(path2, function(path, ...)
129+
monitors = monitors + 1
130+
assert(select("#", ...) == 0)
131+
end)
132+
assert(monitors == 0)
133+
134+
-- Run updates for a couple of seconds.
135+
for i = 1, 10 do hotLoader.update(.2) end
136+
assert(monitors == 0)
137+
138+
-- Update the file.
139+
fakeModTimes[path2] = fakeModTimes[path2] + 1
140+
141+
-- Run updates for a couple of seconds.
142+
print("Should reload.")
143+
for i = 1, 10 do hotLoader.update(.2) end
144+
assert(monitors == 1)
145+
assert(loads == 3) -- Make sure the previous file didn't reload.
146+
147+
-- Monitor with data.
148+
hotLoader.monitor(path2, "testdata", function(path, data)
149+
monitors = monitors + 1
150+
assert(data == "testdata")
151+
end)
152+
assert(monitors == 1)
153+
fakeModTimes[path2] = fakeModTimes[path2] + 1
154+
print("Should reload.")
155+
for i = 1, 10 do hotLoader.update(.2) end
156+
assert(monitors == 2)
121157
end
122158

123159
--
@@ -132,8 +168,10 @@ do
132168
end
133169

134170
-- Require file.
135-
local moduleName = "test"
136-
_G.requires = 0
171+
local moduleName = "test"
172+
local modulePath = (love and "" or thisDir) .. "test.lua"
173+
fakeModTimes[modulePath] = 1
174+
_G.requires = 0
137175
assert(not hotLoader.hasRequired(moduleName))
138176
assert(hotLoader.require(moduleName) == "foobar")
139177
assert(hotLoader.hasRequired(moduleName))
@@ -154,9 +192,10 @@ do
154192
assert(requires == 2) -- Nothing should've happened.
155193

156194
-- Update the file.
157-
fakeModTime = fakeModTime + 1
195+
fakeModTimes[modulePath] = fakeModTimes[modulePath] + 1
158196

159197
-- Run updates for a couple of seconds.
198+
print("Should reload.")
160199
for i = 1, 10 do hotLoader.update(.2) end
161200
assert(requires == 3) -- The file should've reloaded.
162201

tests/test.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/test1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foobar1

tests/test2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foobar2

0 commit comments

Comments
 (0)