Read files outside of love or project directory

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
deb75
Prole
Posts: 38
Joined: Sun Apr 18, 2021 1:11 pm

Read files outside of love or project directory

Post by deb75 »

Hello,

I am trying to run this :

Code: Select all

    img = love.graphics.newImage("d:/xxxx/Documents/tiles/12/2061/1436.png")
The path is outside of the love directory (in which is love.exe) and outside of the project directory (in which is the main.lua)

Love fails to find the file, although I did check the file exists and is readable.

If I perform a hard link to the tiles directory within the project directory and change the code into :

Code: Select all

    img = love.graphics.newImage("tiles/12/2061/1436.png")
it works.

However, this has the disavantage of putting a lot of data into the project directory, several Go in my case,
it will not be possible to distribute the game like this.

I have two questions :
- during the development stage, I would like to be able to access data from anywhere on the disk, how can I do ?
- when distributing the game, some data can be embedded, but most of it not. Is it possible to distribute the executable
game with some command line options, one of it being the path where external data is ?

Regards
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Read files outside of love or project directory

Post by GVovkiv »

https://www.love2d.org/wiki/love.filesystem

Basically, filesystem function give you access to AppData, Folder where game is and .love archive content (if you run game from that one)
(https://icculus.org/physfs/, you can read about physfs which love uses here)
I guess it's more than enough
But if you really want to load data from any place you may find usefull https://github.com/megagrump/nativefs which should allow you take data anywhere, besides AppData and game's folder, but i can't confirm if it works on OSs like android etc
(Since, i believe, this library just use vanilla lua file i/o functons, which may be very harmful if you make cross-platform games or even worse ffi)
Or you can take look at this https://www.love2d.org/wiki/love.filesystem.mount which allow you to mount .zip files
deb75
Prole
Posts: 38
Joined: Sun Apr 18, 2021 1:11 pm

Re: Read files outside of love or project directory

Post by deb75 »

Thanks for your answeer.

I tried to put some data in %appData\LOVE (windows 10) but still trying to load an image from there :

Code: Select all

   img = love.graphics.newImage("tiles/12/2061/1436.png")
fails although I can access %appData\LOVE\tiles\12\2061\1436.png

How can I use an image from the game directory ?

Regards
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Read files outside of love or project directory

Post by GVovkiv »

By default, love read/write data to AppData(or OS equivalent)/Love/*game identity*/ (https://www.love2d.org/wiki/love.filesystem.setIdentity), and (for read operations) it then try to read from game/source folder (but it can de changed in conf.lua t.appendidentity = true)
So, if you wish to load, for example, images from AppData/Love/*game folder* you can try
In your conf.lua (https://www.love2d.org/wiki/Config_Files) you can write

Code: Select all

function love.conf(t)
    t.identity = "ok"
end
and then put your data in that folder and than simply load any data as you normally do

Code: Select all

 image = love.graphics.newImage("image.png")--in that case, game will try to load image from AppData/Love/ok/image.png, it there is no file, it will try to find file in source directory
but if you want to distrubute game in this way...
well, i don't why you want do it in that way unless your game will have some sort of gamefiles check system (like in Steam) and some sort of content download functions, which may not be something with what you want to deal with

i believe, if your game doesn't need mod support/custom content support/ etc just put all your game file inside 1 .love file
deb75
Prole
Posts: 38
Joined: Sun Apr 18, 2021 1:11 pm

Re: Read files outside of love or project directory

Post by deb75 »

Thanks for letting me know all this.

My issue is that my game uses some slippy maps, like google map.

Slippy maps are made of hundreds of tiles for each zoom level. The whole of them weights approximately 5Go.

So, I hardly can distribute my game with the slippy map. This one has to stay outside. This all the more true
as slippy maps can be updated quickly, faster than the game itself.

Settiing identity and appendidentity lets me put the slippy map outside

Thanks again for your help.

Regards
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: Read files outside of love or project directory

Post by Nelvin »

I'd use nativefs so you can load your images/data from wherever you want to.

You actually can even use the regular Lua io module as long as you know the files you want to load and don't need any directory operations (like get the list of files in a directory) as Lua io has no support or even concept for directories.

Code: Select all

function load_image( filename )
    local nfs = require( "nativefs" ) -- require from wherever you've put the nativefs.lua
    local file_data = nfs.newFileData( filename )
    if file_data then
        local image_data = love.image.newImageData( file_data )
        if image_data then
            return love.graphics.newImage( image_data )
        end
    end
end
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Read files outside of love or project directory

Post by GVovkiv »

Nelvin wrote: Sun Jun 06, 2021 9:09 am I'd use nativefs so you can load your images/data from wherever you want to.

You actually can even use the regular Lua io module as long as you know the files you want to load and don't need any directory operations (like get the list of files in a directory) as Lua io has no support or even concept for directories.

Code: Select all

function load_image( filename )
    local nfs = require( "nativefs" ) -- require from wherever you've put the nativefs.lua
    local file_data = nfs.newFileData( filename )
    if file_data then
        local image_data = love.image.newImageData( file_data )
        if image_data then
            return love.graphics.newImage( image_data )
        end
    end
end
since nativefs works only under windows and under ffi and since you can download data in your AppData i really don't recommend this method, as i say before
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Read files outside of love or project directory

Post by grump »

GVovkiv wrote: Sat Jun 05, 2021 1:53 pm (Since, i believe, this library just use vanilla lua file i/o functons, which may be very harmful if you make cross-platform games or even worse ffi)
GVovkiv wrote: Sun Jun 06, 2021 10:20 am since nativefs works only under windows
Wrong, wrong, and wrong. Please don't spread misinformation.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Read files outside of love or project directory

Post by GVovkiv »

grump wrote: Sun Jun 06, 2021 10:41 am
GVovkiv wrote: Sat Jun 05, 2021 1:53 pm (Since, i believe, this library just use vanilla lua file i/o functons, which may be very harmful if you make cross-platform games or even worse ffi)
GVovkiv wrote: Sun Jun 06, 2021 10:20 am since nativefs works only under windows
Wrong, wrong, and wrong. Please don't spread misinformation.
ok
deb75
Prole
Posts: 38
Joined: Sun Apr 18, 2021 1:11 pm

Re: Read files outside of love or project directory

Post by deb75 »

Thanks very much for all theses clarifications

I think I will give nativefs a try

Newbie question : what is "ffi" ?

Regards
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests