Interface: love.filesystem
Posted: Wed Mar 05, 2008 4:06 am
The purpose of this thread is to work out an interface for the forthcoming "safe" filesystem via PhysFS. The point is that the standard Lua io library is removed, and game creators only are allowed write access to a single folder.
The folders could perhaps be:
Windows: C:\Documents and Settings\<user>\Application Data\love\<game>\save.lua
Linux: /home/<user>/.love/<game>/save.lua
Mac (if ever): /Users/<username>/love/<game>/save.lua
I first suggested that <game> could be the name of the file being run. Mike suggested that it could be the name of the file being run if there was no id attribute in the config file, in which case that would be used instead.
This way game creators can be extra sure that there's no folder conflict with their game. There might be better ways to solve this, so by all means; speak up if you can think of anything.
Ok, on to the interface:
I understand that some people would want file:write( "hello") etc instead, but in that case we would also have to change love.graphics:draw(image, x, y) to image:draw(x, y) etc. We have discussed this forever, and have so far gone with the former. If you prefer one strongly over the other, please start a separate thread for that discussion as it has nothing do with love.filesystem .
Example use, saving the highscore as a string, but loading it as a Lua table.
Of course, it should be possible to get the string as well, without parsing:
My only major problem is love.filesystem:enumerate, as I am not very keen on creating a Lua table containing all the files internally. A newline separated string could also work. (dir1\ndir2\nfile1\nfile2\nfile3\netc).
I haven't thought of everything, so please speak up if you see something you don't like. (Or if there's something you would like to see).
The folders could perhaps be:
Windows: C:\Documents and Settings\<user>\Application Data\love\<game>\save.lua
Linux: /home/<user>/.love/<game>/save.lua
Mac (if ever): /Users/<username>/love/<game>/save.lua
I first suggested that <game> could be the name of the file being run. Mike suggested that it could be the name of the file being run if there was no id attribute in the config file, in which case that would be used instead.
Code: Select all
# Config file
author = "Bob"
title = "Awesome game II"
id = "awesome-3e76e8d"
Ok, on to the interface:
Code: Select all
love.objects:newFile( file )
love.filesystem:open( file )
love.filesystem:close( file )
love.filesystem:write( file, string )
love.filesystem:read( file )
love.filesystem:append( file, string )
love.fileystem:include( file )
love.filesystem:enumerate( directory )
file:getSize()
Example use, saving the highscore as a string, but loading it as a Lua table.
Code: Select all
-- When saving the highscore:
file = love.objects:newFile( "highscore.lua" )
love.filesystem:open(file)
love.filesystem:write(file, "highscore = { bob = 2000, jane = 1000, harry = 500 }")
love.filesystem:close(file)
-- Loading later:
file = love.objects:newFile( "highscore.lua" )
love.filesystem:include(file) -- Parsed as Lua source.
-- highscore can be accessed:
local bobscore = highscore.bob -- 2000
Code: Select all
str = love.filesystem:read(file)
I haven't thought of everything, so please speak up if you see something you don't like. (Or if there's something you would like to see).