Searching ResourceDirectory for Lua files

I’m trying to write some code that will trawl a directory for a bunch of lua files and load them using the require function. Like so:

[lua]

  infodir=“autocode”

  infoPrefix=“gen”

  local path=system.pathForFile(nil,system.ResourceDirectory)

  assert(lfs.chdir(path…"/"…infodir))

  

  local pattern="^"…infoPrefix

  for file in lfs.dir(lfs.currentdir()) do

    if file:find(pattern) then

      local name=file:sub(1,-5)

      local m=require (infodir …"."…name)

      …

end

end

[/lua]

However I’ve noticed that Corona packs all the lua files away somewhere, while preserving the project directory structure. I’m assuming all the Lua gets packed into the resources.car file. Is it possible to interrogate the SDK some other way for a list of lua files? Being able to programmatically load them is useful for auto generated code.

Hm… interesting question. I had a look at the contents of resource.car and it does appear that all the lua files are compiled into it. All the lua files are actually referenced in plain text inside resource.car, so if you’re up for it, you could find them by parsing the file.

Perhaps there is a more convenient way  - after all, the package loader that’s called when loading modules with require() must be reading from that file, so it must already know how to parse it.

Thanks for the package loader idea. According to the docs, all the loading is done by loading functions:

http://docs.coronalabs.com/daily/api/library/package/require.html

I can’t think of a way of using them to traverse the underlying directory structure. I suspect I’ll either have to use JSON files instead, which don’t seem to wind up in the .car file or hardcode them

Out of interest what did you use to look inside the .car? I did a quick google and just got obviously wrong answers like: “Railroad Tycoon 3”. 

Corona SDK does not include .lua files in the app bundles.  They are compiled into byte code and put into a file that you should not try to dynamically access.  Corona SDK also does not permit dynamic loading of code modules.  You can load .json files to populate tables and have your Corona code behave based on those values.

Rob

In the end I’ve found that using pcall instead works better than lfs.

As I was already specifying the directory of the lua files in their prefix, I’ve found this works well on the device:

[lua]

local num=0

 local function load()

    local m=require (infodir …"."…infoPrefix…num)

    assert(m)

    num=num+1

    – do stuff

  end

  while pcall(load) do

  end

[/lua]

Obviously that only works if you’ve got a case where you know where all the files you want are and they’re named file0.lua, file1.lua etc. But if you’re using TexturePacker’s multipack mode, that’s what you get!

Here’s the working code I’m using:

https://gist.github.com/personalnadir/cfe6105837986291ba65

Is there a difference between how Corona SDK packs lua files for development and for release? The above will fail if Corona trims lua files not explicitly loaded through a require statement.

@Rob what are the restrictions on dynamically loaded lua files? I’ve read the guides and the doc, and not seen anything there. I know the dofile(), load(), loadfile(), and loadstring() are blocked. Although how does CoronaViewer work, as that loads files from a DropBox account?

Looking at that, it should work since as long as the files originated in the system.ResourcesDirectory.  We can’t compile any .lua files that are in system.DocuementsDirectory because that doesn’t exist when you deploy an app until it runs the first time.  Apple doesn’t permit apps that can program themselves.

Rob

Hm… interesting question. I had a look at the contents of resource.car and it does appear that all the lua files are compiled into it. All the lua files are actually referenced in plain text inside resource.car, so if you’re up for it, you could find them by parsing the file.

Perhaps there is a more convenient way  - after all, the package loader that’s called when loading modules with require() must be reading from that file, so it must already know how to parse it.

Thanks for the package loader idea. According to the docs, all the loading is done by loading functions:

http://docs.coronalabs.com/daily/api/library/package/require.html

I can’t think of a way of using them to traverse the underlying directory structure. I suspect I’ll either have to use JSON files instead, which don’t seem to wind up in the .car file or hardcode them

Out of interest what did you use to look inside the .car? I did a quick google and just got obviously wrong answers like: “Railroad Tycoon 3”. 

Corona SDK does not include .lua files in the app bundles.  They are compiled into byte code and put into a file that you should not try to dynamically access.  Corona SDK also does not permit dynamic loading of code modules.  You can load .json files to populate tables and have your Corona code behave based on those values.

Rob

In the end I’ve found that using pcall instead works better than lfs.

As I was already specifying the directory of the lua files in their prefix, I’ve found this works well on the device:

[lua]

local num=0

 local function load()

    local m=require (infodir …"."…infoPrefix…num)

    assert(m)

    num=num+1

    – do stuff

  end

  while pcall(load) do

  end

[/lua]

Obviously that only works if you’ve got a case where you know where all the files you want are and they’re named file0.lua, file1.lua etc. But if you’re using TexturePacker’s multipack mode, that’s what you get!

Here’s the working code I’m using:

https://gist.github.com/personalnadir/cfe6105837986291ba65

Is there a difference between how Corona SDK packs lua files for development and for release? The above will fail if Corona trims lua files not explicitly loaded through a require statement.

@Rob what are the restrictions on dynamically loaded lua files? I’ve read the guides and the doc, and not seen anything there. I know the dofile(), load(), loadfile(), and loadstring() are blocked. Although how does CoronaViewer work, as that loads files from a DropBox account?

Looking at that, it should work since as long as the files originated in the system.ResourcesDirectory.  We can’t compile any .lua files that are in system.DocuementsDirectory because that doesn’t exist when you deploy an app until it runs the first time.  Apple doesn’t permit apps that can program themselves.

Rob