Add autoloading tracks from folder

This commit is contained in:
Ategon 2024-08-27 18:35:42 -04:00
parent 5a6dfa94b5
commit 19ea7b18d2
3 changed files with 60 additions and 4 deletions

1
.gitignore vendored
View file

@ -6,6 +6,7 @@ old/
tracks/* tracks/*
!tracks/base.gd !tracks/base.gd
!tracks/example.gd !tracks/example.gd
!tracks/exampleautoload.gd
temp/ temp/
thumbnails/* thumbnails/*
!thumbnails/example.bmp !thumbnails/example.bmp

View file

@ -3,15 +3,50 @@ class_name BaseTrack
var _default = null var _default = null
var _tracks = null var _tracks = null
var _autoload = null
func get_tracks(): func get_tracks():
if not _tracks: if not _tracks and not _autoload:
return [] return []
var tracks = _tracks.duplicate() var tracks = []
if not _default: if _autoload:
return tracks 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 track in tracks:
for key in _default: for key in _default:

20
tracks/exampleautoload.gd Normal file
View file

@ -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