Index - Automatic index of custom resources using editor script

I made an editor extension that automatically creates an index of all your custom resources before building, and a module to read that index from the game, either as a list, or to iterate over a tree of the folder structure:
https://github.com/HalfstarDev/defold-index

Especially for larger collections of files, there wasn’t a good way to find out which files are bundled with the game. By creating an index before bundling, you can access the folder structure on any platform.

For example, you have a folder for levels, and create a new folder for each level, and you have different files in each folder. Then you can get a table with each value being a table of the files for that level.

You can read the index as three different data types: list, tree, or folder. And you can specify to only read from a certain path, or choose to only get files, or only folders.

Some examples:
index.get_list():

{
    1 = "assets/",
    2 = "assets/data.json",
    3 = "assets/images/",
    4 = "assets/images/enemy.png",
    5 = "assets/images/player.png",
    6 = "assets/levels/",
    7 = "assets/levels/1.dat",
    8 = "assets/levels/2.dat",
    9 = "assets/levels/3.dat"
}

index.get_tree():

{
    assets = {
        data.json = "assets/data.json",
        images = {
            player.png = "assets/images/player.png",
            enemy.png = "assets/images/enemy.png"
        },
        levels = {
            1.dat = "assets/levels/1.dat",
            2.dat = "assets/levels/2.dat",
            3.dat = "assets/levels/3.dat",
        }
    }
}

index.get_folder(“assets”):

{
    data.json = "assets/data.json",
    images = "assets/images/",
    levels = "assets/levels/"
}

index.get_folder_files(“assets”):

{
    data.json = "assets/data.json"
}

index.get_folder_folders(“assets”):

{
    images = "assets/images/",
    levels = "assets/levels/"
}

And there are also helper functions to iterate over an index:

local function iterate_tree(tree)
    for k, v in pairs(tree) do
        if index.is_folder(v) then
            print("found folder:", k)
            iterate_tree(v)
        elseif index.is_file(v) then
            print("found file:", k, "in path:", v)
        end
    end
end
iterate_tree(index.get_tree("assets"))

I made sure that it works for editors under both Windows and Linux. I’m not sure if it works for the macOS editor. While the Linux file commands should work on macOS as well, I don’t know if editor scripts have that permission under macOS. But that’s just for the editor, the bundled index can be used on any platform.

17 Likes

Very useful! Thank you for sharing! :heart: