Page 1 of 2

Is there a way of saving and loading files from anywhere?

Posted: Sat May 04, 2019 10:29 pm
by Darlex
Is there a way of saving and loading files from anywhere? Like "C:\users\darlex\Desktop\project1" or "\home\darlex\desktop\project1"?
Im trying to do a game editor thing but i dont know how to do that :/

Re: Is there a way of saving and loading files from anywhere?

Posted: Sat May 04, 2019 11:13 pm
by Rehtinor
I'd suggest the IO library: https://www.lua.org/pil/21.2.2.html

Re: Is there a way of saving and loading files from anywhere?

Posted: Sat May 04, 2019 11:14 pm
by zorg
Not easily; löve by default doesn't support that (with the exception of dragging folders and files), but there are a few solutions;
You can either use an external lib like luafilesystem and sacrifice it working with any constructors löve gives you (since they still depend on PhysFS that love.filesystem uses), or you can use my library that for now, allows loading from anywhere: love-fml on github. (and yes, my lib can take care of OS-specific differences in a neat way, so you don't have to worry about that)

Re: Is there a way of saving and loading files from anywhere?

Posted: Sun May 05, 2019 12:34 am
by Ref
As posted before, you could always do the following.

Code: Select all

-- ***** Load/Save from Anywhere on Computer *****
-- To use, wrap 'filename' with 'mount'.
-- 				for  filename =  'D:/Scripts/Love/test.png'
-- Load Example:			love.graphics.newImage( mount( filename ) )
-- Save Example:			id:encode('png', mount( filename ) )
-- 				for dir = 'D:/Scripts/Love'
-- Files in Directory:	files = love.filesystem.getDirectoryItems( mount( dir ) )
--	Create Directory:	love.filesystem.createDirectory( mount( savedir ) )

mount = function( name )
	local ffi = require( 'ffi' )
	local lv		= ffi.load( 'love' )
	ffi.cdef[[ int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath); ]];
	ffi.cdef[[ int PHYSFS_setWriteDir(const char *newDir); ]]
	local file	= name:match( "^.+/(.+)$" )			-- filename
	local path	= string.sub( name, 1, -(#file+1) )	-- path without filename
	lv.PHYSFS_setWriteDir( path )
	lv.PHYSFS_mount( path, nil, 0 )
	return file
	end

Re: Is there a way of saving and loading files from anywhere?

Posted: Sun May 05, 2019 4:22 am
by ivan
Darlex wrote: Sat May 04, 2019 10:29 pm Is there a way of saving and loading files from anywhere? Like "C:\users\darlex\Desktop\project1" or "\home\darlex\desktop\project1"?
"C:\" is a Windows thing and it won't work across platforms.
You want your game to work on any device, right?
Darlex wrote: Sat May 04, 2019 10:29 pm Im trying to do a game editor thing but i dont know how to do that :/
Just save the maps to the appData folder and you should be fine.

Re: Is there a way of saving and loading files from anywhere?

Posted: Sun May 05, 2019 4:47 pm
by Darlex
ivan wrote: Sun May 05, 2019 4:22 am
Darlex wrote: Sat May 04, 2019 10:29 pm Is there a way of saving and loading files from anywhere? Like "C:\users\darlex\Desktop\project1" or "\home\darlex\desktop\project1"?
"C:\" is a Windows thing and it won't work across platforms.
You want your game to work on any device, right?
Darlex wrote: Sat May 04, 2019 10:29 pm Im trying to do a game editor thing but i dont know how to do that :/
Just save the maps to the appData folder and you should be fine.
Im trying to do a game/map editor and the fact of saving everything in the appdata thing is tedious.
I want to give that sensation of comfort of saving and loading files from anywhere to anywhere.
Like tilesheets, lua files and etcetera

Re: Is there a way of saving and loading files from anywhere?

Posted: Sun May 05, 2019 4:48 pm
by Darlex
Ref wrote: Sun May 05, 2019 12:34 am As posted before, you could always do the following.

Code: Select all

-- ***** Load/Save from Anywhere on Computer *****
-- To use, wrap 'filename' with 'mount'.
-- 				for  filename =  'D:/Scripts/Love/test.png'
-- Load Example:			love.graphics.newImage( mount( filename ) )
-- Save Example:			id:encode('png', mount( filename ) )
-- 				for dir = 'D:/Scripts/Love'
-- Files in Directory:	files = love.filesystem.getDirectoryItems( mount( dir ) )
--	Create Directory:	love.filesystem.createDirectory( mount( savedir ) )

mount = function( name )
	local ffi = require( 'ffi' )
	local lv		= ffi.load( 'love' )
	ffi.cdef[[ int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath); ]];
	ffi.cdef[[ int PHYSFS_setWriteDir(const char *newDir); ]]
	local file	= name:match( "^.+/(.+)$" )			-- filename
	local path	= string.sub( name, 1, -(#file+1) )	-- path without filename
	lv.PHYSFS_setWriteDir( path )
	lv.PHYSFS_mount( path, nil, 0 )
	return file
	end
Wow, that's useful! Thanks!
But... Does work with unix-like systems? ("/home/darlex/desktop/project1/")

Re: Is there a way of saving and loading files from anywhere?

Posted: Sun May 05, 2019 4:54 pm
by Darlex
zorg wrote: Sat May 04, 2019 11:14 pm Not easily; löve by default doesn't support that (with the exception of dragging folders and files), but there are a few solutions;
You can either use an external lib like luafilesystem and sacrifice it working with any constructors löve gives you (since they still depend on PhysFS that love.filesystem uses), or you can use my library that for now, allows loading from anywhere: love-fml on github. (and yes, my lib can take care of OS-specific differences in a neat way, so you don't have to worry about that)
Thanks! but is a little complex to me. (yes. im retarded)

Re: Is there a way of saving and loading files from anywhere?

Posted: Sun May 05, 2019 5:12 pm
by ivan
Darlex wrote: Sun May 05, 2019 4:47 pm I want to give that sensation of comfort of saving and loading files from anywhere to anywhere.
Frankly speaking, I don't see anything "comfortable" in dealing with file dialogs, etc.
In fact, most modern programs never ask you to browse around the device's filesystem.
A good example would be apps on Android and iOS.

Re: Is there a way of saving and loading files from anywhere?

Posted: Mon May 06, 2019 5:16 am
by zorg
ivan wrote: Sun May 05, 2019 5:12 pm
Darlex wrote: Sun May 05, 2019 4:47 pm I want to give that sensation of comfort of saving and loading files from anywhere to anywhere.
Frankly speaking, I don't see anything "comfortable" in dealing with file dialogs, etc.
In fact, most modern programs never ask you to browse around the device's filesystem.
A good example would be apps on Android and iOS.
Beauty in the eye of the beholder, i guess; frankly, i'd rather know how to navigate the cesspool of a filesystem that modern OS-es have than just blindly rely on apps telling me where they put my files. :o:
Darlex wrote: Sun May 05, 2019 4:54 pm Thanks! but is a little complex to me.
As the "Usage" part says on github, you really only need to do require('fml')() if you put the files next to your main.lua; after that, you can use syntax like "drv/c/Program Files/..." for windows or "drv/usr/..." for Linux and similar for OSX to refer to any file on your filesystem... and you can still use "src/..." or "usr/..." for the source or save directories, respectively.