-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs.lua
98 lines (85 loc) · 4.13 KB
/
fs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
--[[
#########################################################################
# #
# fs.lua #
# #
# Love2D filesystem loading functions #
# #
# Copyright 2011 Josh Bothun #
# http://minornine.com #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License <http://www.gnu.org/licenses/> for #
# more details. #
# #
#########################################################################
--]]
local fs = {}
-- Generic loading function that does a deep search through `path`, calling
-- `file_callback` on each file and storing it in a map, and also calling
-- `interp_callback` with a percentage of completion arg, for easy hooking.
-- Finally, the function returns the constructed map
local function recursiveYieldingLoader(root, file_callback, interp_callback)
local map = {}
local count, total = 0, 0
function iter(path, docount)
for i, name in ipairs(love.filesystem.enumerate(path)) do
count = count + 1
fullpath = path .. '/' .. name
if not docount and love.filesystem.isFile(fullpath) then
-- Strip root dir and extension from key
local key = string.sub(fullpath, fullpath:find('/') + 1, fullpath:len())
key = key:gsub('%.[^.]*$', '')
map[key] = file_callback(fullpath)
if type(interp_callback) == 'function' then
interp_callback(count / total, fullpath)
end
elseif love.filesystem.isDirectory(fullpath) then
iter(fullpath, docount)
end
end
end
-- Count number of files in tree and store total
iter(root, true)
total = count
count = 0
-- Run actual function
iter(root)
-- Return resulting map
return map
end
-- Load directory tree into a map of lua chunks
function fs.loadChunks(path, callback)
return recursiveYieldingLoader(path, love.filesystem.load, callback)
end
-- Load directory tree into a map of love.graphics.Image
function fs.loadImages(path, callback)
return recursiveYieldingLoader(path, love.graphics.newImage, callback)
end
-- Load directory tree into a map of love.audio.Source
function fs.loadSounds(path, callback)
return recursiveYieldingLoader(path, love.audio.newSource, callback)
end
-- Load directory tree into a map of love.graphics.PixelEffect
function fs.loadShaders(path, callback)
-- Replace shaders keywords with the fake ones
function subshader(file)
local tmp = love.filesystem.read(file)
tmp = tmp:gsub('float', 'number')
tmp = tmp:gsub('sampler2D', 'Image')
tmp = tmp:gsub('uniform', 'extern')
tmp = tmp:gsub('texture2D', 'Texel')
return love.graphics.newPixelEffect(tmp)
end
return recursiveYieldingLoader(path, subshader, callback)
end
-- Namespace exports
leaf.fs = fs