love.filesystem.mount question about loading content from save 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.
Post Reply
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

love.filesystem.mount question about loading content from save directory

Post by Pangit »

Hi

I have a few questions about loading files from the zip folder, I understand that I need to have a copy of the zip file in the save folder of the love project directory.

So the structure i have is

loveproject/assets/save/data.zip

I found the following code on the forum and this appears to unzip the contents of the zip folder to the save directory.

I keep getting assertion fails when I tried to mount the zip file in the source directory (upon reading the forums it appears this is not possible and needs to be done from the projects save directory.

I have some question,

1. I would like to unzip the zip file in the save directory, read the files I want into memory then delete the files. Is this possible or advisable? or should I just leave the files be in the save directory and delete them upon program exit?

2. I am not to clear about what this code is doing - ok I understand that its unpacking the zip file into some directory, but how should i edit it so it works in my project folder?

Code: Select all

local lfs = love.filesystem

local function enu(folder, saveDir)
   local filesTable = lfs.getDirectoryItems(folder)
   if saveDir ~= "" and not lfs.isDirectory(saveDir) then lfs.createDirectory(saveDir) end
   
   for i,v in ipairs(filesTable) do
      local file = folder.."/"..v
      local saveFile = saveDir.."/"..v
      if saveDir == "" then saveFile = v end
     
      if lfs.isDirectory(file) then
         lfs.createDirectory(saveFile)
         enu(file, saveFile)
      else
         lfs.write(saveFile, tostring(lfs.read(file)))
      end
   end
end

function extractZIP(file, dir, delete)
   local dir = dir or ""
   local temp = tostring(math.random(1000, 2000))
   success = lfs.mount(file, temp)
      if success then enu(temp, dir) end
   lfs.unmount(file)
   if delete then lfs.remove(file) end
end

Code: Select all

function love.load()
    love.filesystem.mount("data.zip", "assets/save")
 
    assert(love.filesystem.exists("assets/save/225_img224.png"))

    background = love.graphics.newImage("assets/save/225_img224.png")
end

function love.quit()
    print("***************************\n*      Have a nice day.     *\n***************************")
    love.event.quit()
end

function love.keypressed(key)   
    if key == 'b' then
      print("The B key was pressed.")
    end
    if key == 'a' then
        file = love.filesystem.newFile("data.txt")
        file:open("r")
        data = file:read()
        print(data)
file:close()
    end
    if key == 'escape' then
      love.event.push("quit") 
   end
end

function love.update(dt)
end

function love.draw()
    love.graphics.draw(background,0,0)
end
3. This is the main.lua file... where should I put the extractZIP snippet, in its own file that i load like the library or in the main.lua program file?

My system is linux.

4. so I was wondering is there a way in love I can read in the image data without having to extract the png's from the archive. Like with text files in linux I can do somthing like this...

unzip -c archive.zip file1.txt file2.txt | less

I was thinking there might be a way to do this with the image data in the archive in love2d?
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.filesystem.mount question about loading content from save directory

Post by zorg »

Answer to your 1st point:
- If you mount a zip archive, you don't really extract it anywhere on disk; it just allows löve's filesystem access to the files inside that archive.
- When you're done with it, you use [wiki]love.filesystem.unmount[/wiki].

The zip needs to be in the game's save folder since... well, my guess is that the reasoning was, if one creates a "standalone" .love file, it being an archive already, it may not be possible to mount a zip from another... but i'm not that sure about whether that's the main reason or not.

Answer to your 2nd point:
Also, the snippet you posted indeed does "dump" out the zip's contents into a folder of your choice, but if you don't need to do that, then i'd suggest you don't do it. Again, that will not work with the source directory.

Then again, any reason you would want to have archives inside your source dir?
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: love.filesystem.mount question about loading content from save directory

Post by Pangit »

Hi thank you for your answers, I was just trying to understand the thinking behind the framework. Ideally I want to keep everything within a file structure that I can define. If it has to be in the save directory, its ok. I can just set something up like
projectfile/save/assets/images/character/
projectfile/save/assets/images/background/
projectfile/save/assets/images/ui/

And mount the zips from there. Can poke around the mounted directores and access whatever I want from there.

Ok so the mount is not unpacking the zip file, thats great. so I should be able to just access the mounted directories.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.filesystem.mount question about loading content from save directory

Post by zorg »

But again, my question is, why can't you put your assets into the project folder? Without archiving them?
The only limitations on löve's "internal" filesystem's structure is that both project and save folder are defined as the 'root' folder, so a path like "log/abc.lua", if it exists in both places, will make the source folder's version unreachable, since i believe the save folder has more priority (for a good reason).
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: love.filesystem.mount question about loading content from save directory

Post by Pangit »

Hi what I want to do is keep the program content data separate from the program code. So I could use the same code and just drop a different content zip (or possibly an ace file.) have the data. Just so I could do updates ect easily. And have an easy way to distribute updates for content to certain users and not others.

Re-reading the docs, it seems that I have to have the file structure /project/save/file.zip I can create what directories ect whin /project/save/ that I like but the zip file needs to live in project/save/?

One of the things I was curious about was if its possible to really protect the assets and program code with lua, could it be locked down like say with a C project? Or is that kinda thing frowned upon with lua.

I guess I could just do something cheesy like security through obscurity but that's not going to stop a motivated person taking the program assets and code ect... But it would stop casual problems.

There were a few things I read about like luac but then that's helps protect the code but its not a great solution, for a motivated person they could still access the code. I guess a checksum check on the data and the code would also go a long way to stop casual interference. What I want to try and prevent is someone casually just taking all of the game assets, editing the program to cheat ect...
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.filesystem.mount question about loading content from save directory

Post by zorg »

No to ACE, yes to ZIP, but still in the save folder. Let me give you a suggestion:

Have a structure in your project folder like this:

Code: Select all

- code
- assets
- main.lua
- conf.lua
Have your assets be in the assets folder of your projects folder, code naturally in that code folder.

If you want to give updated assets, have people download them as zip files, and have them put that into the save folder (or, more advanced, let the app check for updates, and if it finds one, download that through löve itself, into the save folder). The zip should contain either an assets folder, and you'd mount it into the root (with l.fs.mount(pathtozip,'/')) or just the assets, and you'd mount it like this (l.fs.mount(pathtozip,'/assets/'). The program will always prefer the zip in the save folder, so it will use the updated one, rather than the "default", if you even want to include that with your .love file.

You don't need to have any specific file structure. You have one project folder somewhere on your harddrive, and you have one save folder in a very specific location, that's OS dependent (wiki), everything else is your decision.

Your best protection will be a good license, really. Not even a C or some other project is truly "locked down" or has its assets unrecoverable from the outside, question is, how tough did they make it for people to get to them. Sooner or later, if someone thinks it's worth their time to try, they will try.

I just don't see this being a casual problem, and besides, only the most potatoe of potatoes try and steal a game outright, most will copy the mechanic and/or make similar looking assets. There were tons of threads past about this same issue as well, most concluded the same way.

Cheating is a perogative of the user; if the game is singleplayer, that is. And you should have a secure server and serverside verification if you want to code a multiplayer game, anyway.

Well, at least you see how pointless the struggle really is :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: love.filesystem.mount question about loading content from save directory

Post by Pangit »

Thanks for your help! I got the program to mount and read the files in the zip. Just a case of me reading the documentation more closely again with your pointers :awesome:

For the gentle reader who is facing the same problem as I did but perhaps is want to dig through the docs...

first thing, if you can't find the save directory where you think it should live, use the love.filesystem.getSaveDirectory() to find out where that save file is really hiding on the system. Thinking about this further its a nice way also to identify what the system the program is running on as each platform is going to have different save file locations.. :cool:
  • Windows XP: C:\Documents and Settings\user\Application Data\LOVE\ or %appdata%\LOVE\
    Windows Vista and 7: C:\Users\user\AppData\Roaming\LOVE or %appdata%\LOVE\
    Linux: $XDG_DATA_HOME/love/ or ~/.local/share/love/
    Mac: /Users/user/Library/Application Support/LOVE/
    Android: /data/user/0/org.love2d.android/files/save/ or /data/data/org.love2d.android/files/save/ (On Android there are various save locations. If these don't work then you can use love.filesystem.getSaveDirectory() to check.)
For me this was the big turning point in realising that my understanding of where the save file actually was was totally wrong. It was my own fault I should have read the relevant documents more closely but eh.. pain is the best teacher lol. No wonder I couldn't get the program to mount the zip file because I had placed it in the wrong location.. the program was expecting it to be in one place but I had put it in another :ehem:

Anyway. After this shock I quickly find the real location, copied the zip across and altered the program. Boom! its all working fine.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.filesystem.mount question about loading content from save directory

Post by zorg »

One more thing just to alleviate further pain, in fused mode, the LOVE directory in the save path gets dropped, so for example, on windows vista/7, the save directory will be at

Code: Select all

C:\Users\user\AppData\Roaming\[identity] or %appdata%\[identity]
instead of

Code: Select all

C:\Users\user\AppData\Roaming\LOVE\[identity] or %appdata%\LOVE\[identity]
See here for more info.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Pangit
Party member
Posts: 148
Joined: Thu Jun 16, 2016 9:20 am

Re: love.filesystem.mount question about loading content from save directory

Post by Pangit »

Thank you! You predicted a headache.
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests