|
1 | | -# defold-index |
2 | | -Creates an index for custom resources in Defold |
| 1 | +# index |
| 2 | +This extension for the Defold game engine can automatically create an index of all your custom resources before building a game using an editor script, and provides a module for easy access to that index. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +## Installation |
| 7 | +* Copy the folder `index` and the files `.index.txt` and `hooks.editor_script` into your project folder root. |
| 8 | +* Add `.index.txt` to your custom resources in `game.project`, separated by a comma from other resources. |
| 9 | +* (If you already use a file called `hooks.editor_script`, copy the content from both into one, as you can only use one at the same time.) |
| 10 | +* (If you added the extension while the project is already opened, load it first by pressing `Project → Reload Editor Scripts`.) |
| 11 | + |
| 12 | +## Create index |
| 13 | +The index will be created automatically by the editor script, and saved in `.index.txt`. All files in your custom resources will be indexed, besides hidden files, indicated by starting with `.`, like `.hidden_file.txt`. |
| 14 | + |
| 15 | +Once the index is created from the editor, it will be bundled with the game, and can be used to access the folder structure, even by target platforms which do not have the file operation access needed to create the index in the first place, like HTML5. |
| 16 | + |
| 17 | +## Read index |
| 18 | +While you are free to just open the file `.index.txt` and use it as you wish, this extension provides some functions to make reading the index easier. You can get the index in form of a list, tree, or folder, and also filter the content to only read from a certain folder, or to only get files, or only folders. |
| 19 | + |
| 20 | +To access the index from another script, import the module with |
| 21 | +```lua |
| 22 | +local index = require "index.index" |
| 23 | +``` |
| 24 | + |
| 25 | +### Lists |
| 26 | +To read the index in form of a list, use `index.get_list([path])`. If no `path` is provided, it will be considered the root, and give you the complete index, which is also the case for the other functions. The list includes paths to every file and every folder included in your custom resources, including subfolders. The list will be a lua table, with integers as keys, and the full paths as values, and sorted alphabetically. You can also get a list of only files, or only folders, by using `index.get_list_files()` or `index.get_list_folders()`. |
| 27 | + |
| 28 | +Example: `index.get_list()` |
| 29 | +```lua |
| 30 | +{ |
| 31 | + 1 = "assets/", |
| 32 | + 2 = "assets/data.json", |
| 33 | + 3 = "assets/images/", |
| 34 | + 4 = "assets/images/enemy.png", |
| 35 | + 5 = "assets/images/player.png", |
| 36 | + 6 = "assets/levels/", |
| 37 | + 7 = "assets/levels/1.dat", |
| 38 | + 8 = "assets/levels/2.dat", |
| 39 | + 9 = "assets/levels/3.dat" |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +### Trees |
| 44 | +To read the index in form of a tree, use `index.get_tree([path])`. The result is a nested table with the file names as keys, and for files with the full paths as values, and for folders with a tree of that folder as value. |
| 45 | + |
| 46 | +Example: `index.get_tree()` |
| 47 | +```lua |
| 48 | +{ |
| 49 | + assets = { |
| 50 | + data.json = "assets/data.json", |
| 51 | + images = { |
| 52 | + player.png = "assets/images/player.png", |
| 53 | + enemy.png = "assets/images/enemy.png" |
| 54 | + }, |
| 55 | + levels = { |
| 56 | + 1.dat = "assets/levels/1.dat", |
| 57 | + 2.dat = "assets/levels/2.dat", |
| 58 | + 3.dat = "assets/levels/3.dat", |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### Folders |
| 65 | +To read the index in form of a folder, use `index.get_folder([path])`. The result is a table with the file names as keys, and the full paths as values for both files and folders. Like with lists, you can use `index.get_folder_files()` and `index.get_folder_folders()` to get only files, or only folders. Unlike `get_list()`, the table only contains the content of this folder, and not any deeper levels. |
| 66 | + |
| 67 | +Example: `index.get_folder("assets")` |
| 68 | +```lua |
| 69 | +{ |
| 70 | + data.json = "assets/data.json", |
| 71 | + images = "assets/images/", |
| 72 | + levels = "assets/levels/" |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Use index |
| 77 | +There are also some helper functions to use the index. |
| 78 | + |
| 79 | +To find out if a value from the index is a file or a folder, you can use `index.is_file(path_or_tree)` and `index.is_folder(path_or_tree)`, which returns a boolean. This works for values from lists, trees, and folders. This can be used for example to iterate over the whole index. |
| 80 | + |
| 81 | +Examples: |
| 82 | +```lua |
| 83 | +local function iterate_list(list) |
| 84 | + for _, v in pairs(list) do |
| 85 | + if index.is_folder(v) then |
| 86 | + print("found folder: ", v) |
| 87 | + elseif index.is_file(v) then |
| 88 | + print(" found file:", v) |
| 89 | + end |
| 90 | + end |
| 91 | +end |
| 92 | +iterate_list(index.get_list()) |
| 93 | +``` |
| 94 | +```lua |
| 95 | +local function iterate_tree(tree) |
| 96 | + for k, v in pairs(tree) do |
| 97 | + if index.is_folder(v) then |
| 98 | + print("found folder: ", k) |
| 99 | + iterate_tree(v) |
| 100 | + elseif index.is_file(v) then |
| 101 | + print(" found file:", k, "in path:", v) |
| 102 | + end |
| 103 | + end |
| 104 | +end |
| 105 | +iterate_tree(index.get_tree()) |
| 106 | +``` |
| 107 | +```lua |
| 108 | +local function iterate_folder(folder) |
| 109 | + for key, value in pairs(folder) do |
| 110 | + if index.is_folder(value) then |
| 111 | + print("character " .. key) |
| 112 | + for name, path in pairs(index.get_folder_files(value)) do |
| 113 | + print(" " .. name .. " = " .. path) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | +end |
| 118 | +iterate_folder(index.get_folder_folders("assets/images/characters")) |
| 119 | +``` |
| 120 | + |
| 121 | +To find out if there is a file indexed on a given path, use `index.is_file_indexed(path)`. |
| 122 | + |
| 123 | +You can also do a deep search for a file name, using `index.find_file(name, [path])`. |
| 124 | + |
| 125 | +## Functions |
| 126 | +| Name | Description | |
| 127 | +| ----------------------------------- | ------------------------------------------------------------- | |
| 128 | +| `index.get_list([path])` | get index in form of a list | |
| 129 | +| `index.get_list_files([path])` | get index in form of a list, containing only files | |
| 130 | +| `index.get_list_folders([path])` | get index in form of a list, containing only folders | |
| 131 | +| `index.get_tree([path])` | get index in form of a tree | |
| 132 | +| `index.get_folder([path])` | get index in form of a folder | |
| 133 | +| `index.get_folder_files([path])` | get index in form of a folder, containing only files | |
| 134 | +| `index.get_folder_folders([path])` | get index in form of a folder, containing only folders | |
| 135 | +| `index.is_file(path_or_tree)` | returns true if given path or tree is a file | |
| 136 | +| `index.is_folder(path_or_tree)` | returns true if given path or tree is a folder | |
| 137 | +| `index.find_file(name, [path])` | searches for file with given name, and returns path if found | |
| 138 | +| `index.create()` | creates index file (not necessary to call manually) | |
0 commit comments