Page 1 of 2

File dialogs in Love2D?

Posted: Sun May 15, 2022 11:14 pm
by sarako
I'm working on a little modding tool in Love2D, and I'd like to have a way for the user to open/save files with a system file dialog. Is there a library or anything to support that? Thanks!

Re: File dialogs in Love2D?

Posted: Mon May 16, 2022 1:10 am
by pgimeno
There are two approaches that I know of. One is https://github.com/linux-man/lovefs which has libraries to access the filesystem, and interfaces for different Löve GUI libraries to show the dialogs.

Another one is this: viewtopic.php?f=4&t=82442 which uses FFI to call the native file dialog of each operating system, but I think it only supports Linux (GTK+) and Windows.

It's likely that either of those will need some updates, as they were originally designed for much older versions of Löve.

Re: File dialogs in Love2D?

Posted: Mon May 16, 2022 3:38 am
by togFox
SLAB does this but might be a bit bloated for what you need.

Re: File dialogs in Love2D?

Posted: Mon May 16, 2022 6:14 am
by zorg
You can also use NativeFS (https://github.com/EngineerSmith/nativefs) and code your own file dialog system on top of that.

Re: File dialogs in Love2D?

Posted: Tue May 17, 2022 5:03 pm
by milon
You can use https://love2d.org/wiki/love.window.showMessageBox for user interaction, and build the file/folder list however you like. Perhaps NativeFS that zorg linked, or you can roll your own solution if you're feeling brave.

Here's a thing that I had sitting around; use it if you like with no attribution required.

Code: Select all


local system = {}
system.path = love.filesystem.getWorkingDirectory().."/test.txt"
system.list_files = "ls "
system.list_dirs_prefix = "-d -N "
system.list_dirs_suffix = "*/"
if jit.os == "Windows" then
    system.list_files = "dir /b "
    system.list_dirs_prefix = "/ad "
    system.list_dirs_suffix = ""
end

function listFiles(path, dirOnly)
    local command = system.list_files
    if dirOnly then
        command = command..system.list_dirs_prefix..path..system.list_dirs_suffix
    else
        command = command..path
    end
    print("> "..command, (dirOnly and "(dirOnly)" or "(everything)")) -- testing feedback
    local handle = io.popen(command)
    local output = handle:read("*a")
    handle:close()
    return output
end

function strFindLast(str, phrase)
    local len = phrase:len()
    local i = str:len() - len + 1
    while i > 0 do
        if str:sub(i, i + len - 1) == phrase then return i end
        i = i - 1
    end
end

function isPath(path)
    local parentDir = path:sub(1, strFindLast(path, "/"))
    parentDir = listFiles(parentDir, true)
    return parentDir:find(path)
end

---- exists() and isdir() functions ----
-- modified from
-- https://stackoverflow.com/questions/1340230/check-if-directory-exists-in-lua

--- Check if a file or directory exists in this path
function exists(file)
   local ok, err, code = os.rename(file, file)
   if not ok then
      if code == 13 then
         -- Permission denied, but it exists
         return true
      end
   end
   return ok, err
end

--- Check if path is a directory
function isDir(path)
   -- "/" works on Linux and Windows, but fails on Wine (everything succeeds if the file/path exists)
   --~ if path == cmd.root then return true end
   return exists(path.."/") --or path == cmd.root
end
--------

local isPathStr = tostring(isPath(system.path))
local isDirStr  = tostring(isDir(system.path))
local listFilesStr = listFiles(system.path)

function love.draw()
    love.graphics.print("Checking: " .. system.path, 10, 10)

    love.graphics.print("Is that a directory?  (isPath)   "..isPathStr, 10, 30)
    love.graphics.print("Is that a directory?  (isDir)     "..isDirStr, 10, 45)
end

Be advised that it works on Linux & Windows, fails on Wine, and is untested on Mac. Mac & other 'nix platforms should work, but I have no way to test that.

Re: File dialogs in Love2D?

Posted: Wed May 18, 2022 6:08 pm
by sarako
LoveFS looks perfect! It's even using LuaFrames, which is the GUI framework I went with. Thanks so much!

Re: File dialogs in Love2D?

Posted: Thu May 19, 2022 5:38 pm
by sarako
So does Love2D prevent you from accessing files at all outside the folder the program is in? Trying to call getInfo on files based on an absolute filepath ("C:\...") return nil.

Re: File dialogs in Love2D?

Posted: Thu May 19, 2022 5:49 pm
by GVovkiv
sarako wrote: Thu May 19, 2022 5:38 pm So does Love2D prevent you from accessing files at all outside the folder the program is in? Trying to call getInfo on files based on an absolute filepath ("C:\...") return nil.
Love uses sandboxed filesystem.
You can only read/write data in AppData folder (or other OS's AppData equivalent)
And you can only read data from source folder (or from fused archive).
I don't know if lua's io library also sandboxed, but love's filesystem works in that way.
So as zorg mentioned, you need use https://github.com/EngineerSmith/nativefs library to get data outside of sandbox.

Re: File dialogs in Love2D?

Posted: Thu May 19, 2022 5:54 pm
by sarako
Thanks for explaining!

Re: File dialogs in Love2D?

Posted: Thu May 19, 2022 10:04 pm
by sarako
Sadly nativefs and lovefs don't play well together. I think it's due to conflicting ffi.cdef's. Poking around to see if I can get them to work together, otherwise I'll use them as inspiration for my own file load/save dialog.