Requiring Lua file from documents directory

Does anyone know if it’s possible(and if so how) do require a Lua file from the devices documents directory? My googling skills haven’t really turned up anything except this thread(http://developer.coronalabs.com/forum/2011/11/22/require-documents-directory) which doesn’t really work.

Below is what I tried:

[lua]–replaces the / with . as per require function uhh requires
local pathToDocuments = string.gsub(system.pathForFile( nil, system.DocumentsDirectory ), “/”, “.”)…"."
local myModule = require(pathToDocuments…moduleName)[/lua]

The error comes back that the file doesn’t exist. [import]uid: 147305 topic_id: 32447 reply_id: 332447[/import]

This is specifically prohibited by Apple. Allowing apps to download dynamic code leads to viruses, malware and other unexpected code for users.
[import]uid: 19626 topic_id: 32447 reply_id: 129069[/import]

Hello Rob,

Yea, I know. This project isn’t really meant for any app store so it wouldn’t be an issue. It’s just something to help speed up making maps for other projects I have going on.

Oh well, if there isn’t a way then I guess I’ll have to either add some bloat for a different saving/loading solution or edit the map files manually after creation.

[import]uid: 147305 topic_id: 32447 reply_id: 129072[/import]

Corona disabled loadString(and functions that handle .lua file interpretation), however you can probably get away with compiling your .lua files to byte-code (using luaC), and then requiring via a directory path from your .app (/…/Documents/[FileName]). I’ve not tried it personally, but Corona can only load byte-code lua files, and not fully interpreted versions once deployed.

If you type require(“fileThatDoesNotExit”) you can see which paths Corona uses to find .lu files. [import]uid: 134101 topic_id: 32447 reply_id: 129077[/import]

@budershank,

As I understand both your question and the way Corona works, I don’t think you can download an external uncompiled lua file and require it at run time. This would be tantamount to allowing run-time scripting which is not supported thanks to < certain vendors’ > policies.

In case you are tempted to ask, I know of know way to separately compile a script file either.

As an alternative (depending on the complexity of what you are trying to achieve) you would always set up some kind of state-machine of some sort in your main code and download a text file of instructions which get run/implemented by that state-machine. This wouldn’t be very efficient, but it would be flexible.

-Ed [import]uid: 110228 topic_id: 32447 reply_id: 129080[/import]

Thanks for the responses everyone. Given that there it is beyond some simple logic using require(actually looks like things were put in place to prevent this?) I’m just going to handle it differently…

Also, I don’t want to start a discussion on something that someone could use maliciously so no need to reply with other possible solutions :slight_smile:

Again, thanks! [import]uid: 147305 topic_id: 32447 reply_id: 129084[/import]

This is specifically prohibited by Apple. Allowing apps to download dynamic code leads to viruses, malware and other unexpected code for users.
[import]uid: 19626 topic_id: 32447 reply_id: 129069[/import]

Hello Rob,

Yea, I know. This project isn’t really meant for any app store so it wouldn’t be an issue. It’s just something to help speed up making maps for other projects I have going on.

Oh well, if there isn’t a way then I guess I’ll have to either add some bloat for a different saving/loading solution or edit the map files manually after creation.

[import]uid: 147305 topic_id: 32447 reply_id: 129072[/import]

Corona disabled loadString(and functions that handle .lua file interpretation), however you can probably get away with compiling your .lua files to byte-code (using luaC), and then requiring via a directory path from your .app (/…/Documents/[FileName]). I’ve not tried it personally, but Corona can only load byte-code lua files, and not fully interpreted versions once deployed.

If you type require(“fileThatDoesNotExit”) you can see which paths Corona uses to find .lu files. [import]uid: 134101 topic_id: 32447 reply_id: 129077[/import]

@budershank,

As I understand both your question and the way Corona works, I don’t think you can download an external uncompiled lua file and require it at run time. This would be tantamount to allowing run-time scripting which is not supported thanks to < certain vendors’ > policies.

In case you are tempted to ask, I know of know way to separately compile a script file either.

As an alternative (depending on the complexity of what you are trying to achieve) you would always set up some kind of state-machine of some sort in your main code and download a text file of instructions which get run/implemented by that state-machine. This wouldn’t be very efficient, but it would be flexible.

-Ed [import]uid: 110228 topic_id: 32447 reply_id: 129080[/import]

Thanks for the responses everyone. Given that there it is beyond some simple logic using require(actually looks like things were put in place to prevent this?) I’m just going to handle it differently…

Also, I don’t want to start a discussion on something that someone could use maliciously so no need to reply with other possible solutions :slight_smile:

Again, thanks! [import]uid: 147305 topic_id: 32447 reply_id: 129084[/import]