diff --git a/.gitignore b/.gitignore index 31db441..5d03950 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ old/ tracks/* !tracks/base.gd !tracks/example.gd +!tracks/exampleautoload.gd temp/ thumbnails/* !thumbnails/example.bmp diff --git a/tracks/base.gd b/tracks/base.gd index 53ed1e3..37eee4f 100644 --- a/tracks/base.gd +++ b/tracks/base.gd @@ -3,15 +3,50 @@ class_name BaseTrack var _default = null var _tracks = null +var _autoload = null func get_tracks(): - if not _tracks: + if not _tracks and not _autoload: return [] - var tracks = _tracks.duplicate() + var tracks = [] - if not _default: - return tracks + if _autoload: + var dir = DirAccess.open(_autoload.path) + + if dir: + dir.list_dir_begin() + var file_name = dir.get_next() + + while file_name != "": + if not dir.current_is_dir() and not file_name.ends_with(".import"): + var regex = RegEx.new() + regex.compile(_autoload.pattern) + var result = regex.search(file_name) + var track_name = file_name + if result: + track_name = result.get_string() + + tracks.push_back({ + "name": track_name, + "track": "%s/%s" % [_autoload.path, file_name] + }) + + file_name = dir.get_next() + + if _tracks: + for track in _tracks: + var filter = tracks.filter(func(a): return a.track == track.track) + + if filter.size() == 0: + # Does not exist as an autoload + tracks.push_back(track) + else: + # Exists as an overload, override all keys + var found_track = filter[0] + + for key in found_track: + track[key] = found_track[key] for track in tracks: for key in _default: diff --git a/tracks/exampleautoload.gd b/tracks/exampleautoload.gd new file mode 100644 index 0000000..9ef3725 --- /dev/null +++ b/tracks/exampleautoload.gd @@ -0,0 +1,20 @@ +extends BaseTrack + +# This is the same as example but uses the autoload system to automatically populate names from file names +# Gets the area that should become the name from regex +# You can use sites like this to help construct regex: https://regex101.com/ + +func _init(): + _default = { + "artist": "Kevin MacLeod", + "thumbnail": preload("res://thumbnails/example.bmp"), + "source": "https://incompetech.com/", + "disabled": false + } + + _autoload = { + "path": "res://music/example", + "pattern": "^[^.]+" + } + +# You can still override things using a _tracks variable and the track set to the path to the file for what you want to override