GetAss - A library to recursively parse a directory for game assets

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
rsheasby
Prole
Posts: 9
Joined: Wed Sep 26, 2018 9:08 pm

GetAss - A library to recursively parse a directory for game assets

Post by rsheasby » Mon Oct 08, 2018 5:17 pm

Hi there. So I'm pretty new to Love2D but I came up with this simple library to parse a directory for game asset files and create a table-tree for the assets based on the directory structure. This allows you to very easily access the assets just as they are organized in the filesystem. The usage is as follows:

Code: Select all

-- Assuming a directory structure like so:
-- assets
-- └── sprites
--     ├── sprite1.png
--     └── creatures
--         └── human
--             ├── sprite2.png
--             └── sprite3.png 

-- Simply invoke GetAss using the following prototype: GetAss(dir, pattern, func) where:
--     "dir" is the path from the root game directory to the assets directory you want to parse,
--     "pattern" is a string.match pattern that tells GetAss which files to add to the table tree, and what to cut out of the filename when adding it to the tree,
--     "func" is the function that GetAss will call for each matched file like so: func(filepath), the return value of which will be added to the tree.

GetAss = require 'getass'

assets = GetAss("assets", ".png$", function(filepath) return love.graphics.newImage(filepath) end)

-- You can now access the file structure as a table-tree like so:
love.graphics.draw(assets.sprites.sprite1)
love.graphics.draw(assets.sprites.creatures.human.sprite2)
love.graphics.draw(assets.sprites.creatures.human.sprite3)
EDIT: Whoops forgot link to library: https://gitlab.com/rsheasby/getass

pedrosgali
Party member
Posts: 101
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: GetAss - A library to recursively parse a directory for game assets

Post by pedrosgali » Mon Oct 08, 2018 6:02 pm

I like this, I've sent you a PM to discuss how you can expand this for any type of file. Didn't want to hijack your library thread. :awesome:

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end

rsheasby
Prole
Posts: 9
Joined: Wed Sep 26, 2018 9:08 pm

Re: GetAss - A library to recursively parse a directory for game assets

Post by rsheasby » Mon Oct 08, 2018 6:13 pm

pedrosgali wrote:
Mon Oct 08, 2018 6:02 pm
I like this, I've sent you a PM to discuss how you can expand this for any type of file. Didn't want to hijack your library thread. :awesome:
Haven't received PM but I really don't mind the thread getting hijacked. Feel free to comment right here. The thread is about the library in general, not about my personal use of it. Either way, I think it should work for other file types already. That's why I designed it to use any pattern and to take a function so you can do whatever you want with the path.

pedrosgali
Party member
Posts: 101
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: GetAss - A library to recursively parse a directory for game assets

Post by pedrosgali » Mon Oct 08, 2018 6:23 pm

Lol forgot to hit send.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end

User avatar
ivan
Party member
Posts: 1363
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: GetAss - A library to recursively parse a directory for game assets

Post by ivan » Tue Oct 09, 2018 5:30 am

The code looks fine.
I think returning a table of results is cleaner than having to provide a closure function.
My suggestion would be to change the API a little bit:

Code: Select all

function getass(dir, pattern, result)
  result = result or {}
  local filenames = love.filesystem.getDirectoryItems(dir)
  for index, filename in ipairs(filenames) do
    if filetype == "file" then
      ...
      table.insert(result, filepath)
    elseif filetype == "diectory" then
      getass(filepath, pattern, result)
    end
  end
  return result
end

rsheasby
Prole
Posts: 9
Joined: Wed Sep 26, 2018 9:08 pm

Re: GetAss - A library to recursively parse a directory for game assets

Post by rsheasby » Tue Oct 09, 2018 6:34 am

ivan wrote:
Tue Oct 09, 2018 5:30 am
The code looks fine.
I think returning a table of results is cleaner than having to provide a closure function.
My suggestion would be to change the API a little bit:

Code: Select all

function getass(dir, pattern, result)
  result = result or {}
  local filenames = love.filesystem.getDirectoryItems(dir)
  for index, filename in ipairs(filenames) do
    if filetype == "file" then
      ...
      table.insert(result, filepath)
    elseif filetype == "diectory" then
      getass(filepath, pattern, result)
    end
  end
  return result
end
So if I'm understanding correctly, you are suggesting something that recursively parses and just returns a table of all the filenames? I decided against that as there's already a pretty good example of that right there in the documentation minus the pattern matching: https://love2d.org/wiki/love.filesystem ... ctoryItems
My main reason for creating the library was that I wanted to be able to easily access the assets as an object tree just as they are in the filesystem. It makes organisation easy and automatic. The callback function was handy as it allows you to very easily adapt the library to whatever you want to use it for without needing to modify it. If you wanted to get a table-tree of filenames, you can also do it easily using the closure like so:

Code: Select all

files = GetAss("assets", ".png$", function(filepath) return filepath end)
And it can even be used easily to load code files at runtime like so:

Code: Select all

local function loadcode(filepath)
	local f = love.filesystem.load(filepath)
	return f()
end

modules = GetAss("src", ".lua$", loadcode)
I will add a parameter to disable the table-tree and just put the results into a single table though, as @pedrosgali mentioned wanting a similar option. After I add that, then the library should be able to do exactly what you mention although it will still need a simple closure. I will add a default closure too though that just returns the filename so you don't need to specify a closure for a simple default behaviour.

rsheasby
Prole
Posts: 9
Joined: Wed Sep 26, 2018 9:08 pm

Re: GetAss - A library to recursively parse a directory for game assets

Post by rsheasby » Tue Oct 09, 2018 8:44 am

Hey guys so thanks for the suggestions. I've added a bit more functionality. Firstly, there's sensible defaults now for the pattern and the function. The default pattern is "^"(find all files, don't remove anything from the filename) and the default function is

Code: Select all

function(filepath) return filepath end
which just puts the filepath in the table. Finally, There's now the ability to choose between a table-tree and a flat array-like table. If you don't specify a function then it will default to an array-table containing all the filenames. If you do specify a function then it will default to a table-tree containing the result of the function. Either way you can override the defaults using the "maketree" parameter. More details in the readme.

Post Reply

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests