Reputation: 423
In my app I have a structure similar to the following:
app (baseDirectory)
|
+-> config
|
+-> Levels
When using corona I am trying to autoload all files in the Levels directory. Below is how I am currently doing it. Note: This does work on Windows just not mac.
local dirPath = system.pathForFile('config/Levels')
for file in lfs.dir(dirPath) do
if (file ~= '.' and file ~= '..') then
-- require the file
end
end
Now if I use the following it works on Mac but not the 'config/Levels'.
local dirPath = system.pathForFile('config')
I'm not sure if this is a bug or if I am doing something wrong. I would assume since it works on Windows but not on Mac that it would be a bug.
So in conclusion how can I get the following to work with the above directory structure
local dirPath = system.pathForFile('config/Levels')
Upvotes: 0
Views: 629
Reputation: 423
After much debugging I have figured out the solution for Mac. Below is my function for getting files in a directory. Works for both Windows and Mac.
Must have a file named main.lua in the baseDirectory.
-- path is relative to the baseDirectory
function getFiles(path)
local filenames = { }
local baseDir = system.pathForFile('main.lua'):gsub("main.lua", "")
for file in lfs.dir(baseDir .. path) do
if (file ~= '.' and file ~= '..') then
table.insert(filenames, file)
end
end
if (#filenames == 0) then return false end
return filenames
end
The problem is system.pathForFile('directory/subdirectory') does not work on Mac as it is strictly looking for a file. As long as it can find some file in the baseDirectory you can append on the relative path to retrieve all files in the intended directory.
Upvotes: 1
Reputation: 36017
One solution is to change "Levels" in "levels". Maybe it is case sensitive. Second solution is to not use "levels" directory inside config folder. Here is how I suggest to organize your files:
app (baseDirectory)
|
+-> • configLevels
• configPlayer
• configOther
Upvotes: 0