2019-11-10 15:09:14 +10:30
|
|
|
#extends Object
|
|
|
|
extends Node
|
2021-11-27 22:20:37 +10:30
|
|
|
var FileHelpers := preload('res://scripts/FileHelpers.gd')
|
2019-11-10 15:09:14 +10:30
|
|
|
|
2021-11-27 22:20:37 +10:30
|
|
|
var RGT := preload('res://formats/RGT.gd')
|
|
|
|
var SM := preload('res://formats/SM.gd')
|
|
|
|
var SRT := preload('res://formats/SRT.gd')
|
|
|
|
|
|
|
|
const NOT_FOUND := ''
|
|
|
|
|
|
|
|
const default_difficulty_keys = ['Z', 'B', 'A', 'E', 'M', 'R']
|
2020-05-03 00:11:01 +09:30
|
|
|
|
2024-05-29 22:11:01 +09:30
|
|
|
var PATHS : PoolStringArray
|
|
|
|
func update_library_paths() -> void:
|
|
|
|
PATHS = Settings.get_library_paths()
|
|
|
|
|
2020-05-03 00:11:01 +09:30
|
|
|
func _ready() -> void:
|
2024-05-29 22:11:01 +09:30
|
|
|
update_library_paths()
|
2020-05-03 00:11:01 +09:30
|
|
|
print('Library paths: ', PATHS)
|
2024-05-29 22:11:01 +09:30
|
|
|
Settings.connect('config_loaded', self, 'update_library_paths')
|
2020-03-29 17:43:28 +10:30
|
|
|
|
2021-11-27 22:20:37 +10:30
|
|
|
|
|
|
|
func find_file(name: String, print_notfound:=false) -> String:
|
2020-12-21 22:52:15 +10:30
|
|
|
# Searches through all of the paths to find the file
|
|
|
|
var file := File.new()
|
|
|
|
for root in PATHS:
|
|
|
|
var filename: String = root + name
|
|
|
|
if file.file_exists(filename):
|
|
|
|
return filename
|
2021-11-27 22:20:37 +10:30
|
|
|
if print_notfound:
|
|
|
|
print('File not found in any libraries: ', name)
|
|
|
|
return NOT_FOUND
|
2020-12-21 22:52:15 +10:30
|
|
|
|
2020-03-29 17:43:28 +10:30
|
|
|
|
2023-03-18 14:34:42 +10:30
|
|
|
var fallback_audiostream: AudioStream = AudioStreamOGGVorbis.new()
|
|
|
|
var fallback_videostream: VideoStream = VideoStreamWebm.new()
|
2021-11-27 22:20:37 +10:30
|
|
|
var fallback_texture := ImageTexture.new()
|
|
|
|
|
2023-03-18 14:34:42 +10:30
|
|
|
func load_ogg(name: String) -> AudioStream: # Searches through all of the paths to find the file
|
2021-11-27 22:20:37 +10:30
|
|
|
match find_file(name):
|
|
|
|
NOT_FOUND: return fallback_audiostream
|
|
|
|
var filename: return FileHelpers.load_ogg(filename)
|
|
|
|
|
2023-03-18 14:34:42 +10:30
|
|
|
func load_video(name: String) -> VideoStream: # Searches through all of the paths to find the file
|
2021-11-27 22:20:37 +10:30
|
|
|
match find_file(name):
|
|
|
|
NOT_FOUND: return fallback_videostream
|
|
|
|
var filename: return FileHelpers.load_video(filename)
|
|
|
|
|
|
|
|
func load_image(name: String) -> ImageTexture: # Searches through all of the paths to find the file
|
|
|
|
match find_file(name, true):
|
|
|
|
NOT_FOUND: return fallback_texture
|
|
|
|
var filename: return FileHelpers.load_image(filename)
|
2020-03-30 23:21:20 +10:30
|
|
|
|
|
|
|
|
2021-11-27 22:20:37 +10:30
|
|
|
func scan_library() -> Dictionary:
|
2020-05-01 15:45:28 +09:30
|
|
|
print('Scanning library')
|
2020-03-29 17:43:28 +10:30
|
|
|
var song_defs = {}
|
2020-05-01 15:45:28 +09:30
|
|
|
var collections = {}
|
2020-03-29 17:43:28 +10:30
|
|
|
var genres = {}
|
2021-02-07 21:58:33 +10:30
|
|
|
|
|
|
|
for root in PATHS:
|
|
|
|
var rootdir = root + 'songs'
|
|
|
|
var dir = Directory.new()
|
|
|
|
var err = dir.make_dir_recursive(rootdir)
|
|
|
|
if err != OK:
|
|
|
|
print_debug('An error occurred while trying to create the songs directory: ', err)
|
|
|
|
return err
|
|
|
|
|
2021-11-27 22:20:37 +10:30
|
|
|
var songslist = FileHelpers.directory_list(rootdir, false)
|
2021-02-07 21:58:33 +10:30
|
|
|
if songslist.err != OK:
|
|
|
|
print('An error occurred when trying to access the songs directory: ', songslist.err)
|
|
|
|
return songslist.err
|
|
|
|
|
|
|
|
dir.open(rootdir)
|
|
|
|
for folder in songslist.folders:
|
|
|
|
var full_folder := '%s/%s' % [rootdir, folder]
|
|
|
|
|
|
|
|
if dir.file_exists(folder + '/song.json'):
|
|
|
|
# Our format
|
|
|
|
song_defs[folder] = FileLoader.load_folder(full_folder)
|
|
|
|
print('Loaded song directory: %s' % folder)
|
|
|
|
if song_defs[folder]['genre'] in genres:
|
|
|
|
genres[song_defs[folder]['genre']].append(folder)
|
2020-05-02 15:45:27 +09:30
|
|
|
else:
|
2021-02-07 21:58:33 +10:30
|
|
|
genres[song_defs[folder]['genre']] = [folder]
|
|
|
|
if typeof(song_defs[folder]['chart_difficulties']) == TYPE_ARRAY:
|
|
|
|
var diffs = song_defs[folder]['chart_difficulties']
|
|
|
|
var chart_difficulties = {}
|
|
|
|
for i in min(len(diffs), len(default_difficulty_keys)):
|
|
|
|
chart_difficulties[default_difficulty_keys[i]] = diffs[i]
|
|
|
|
song_defs[folder]['chart_difficulties'] = chart_difficulties
|
|
|
|
|
|
|
|
elif dir.file_exists(folder + '/collection.json'):
|
|
|
|
var collection = FileLoader.load_folder(full_folder, 'collection')
|
|
|
|
collections[folder] = collection
|
|
|
|
var base_dict = {'filepath': folder+'/'} # Top level of the collection dict contains defaults for every song in it
|
|
|
|
for key in collection.keys():
|
|
|
|
if key != 'songs':
|
|
|
|
base_dict[key] = collection[key]
|
|
|
|
for song_key in collection['songs'].keys():
|
|
|
|
var song_dict = collection['songs'][song_key]
|
|
|
|
var song_def = base_dict.duplicate()
|
|
|
|
for key in song_dict.keys():
|
|
|
|
song_def[key] = song_dict[key]
|
|
|
|
Library.add_song(song_key, song_def)
|
|
|
|
# Legacy compat stuff
|
|
|
|
song_defs[song_key] = song_def
|
|
|
|
if song_defs[song_key]['genre'] in genres:
|
|
|
|
genres[song_defs[song_key]['genre']].append(song_key)
|
|
|
|
else:
|
|
|
|
genres[song_defs[song_key]['genre']] = [song_key]
|
2020-05-03 00:11:01 +09:30
|
|
|
|
2020-03-30 23:21:20 +10:30
|
|
|
else:
|
2021-11-27 23:22:42 +10:30
|
|
|
var files_by_ext = FileHelpers.find_by_extensions(FileHelpers.directory_list(full_folder, false).files)
|
2021-02-07 21:58:33 +10:30
|
|
|
if 'sm' in files_by_ext:
|
|
|
|
var sm_filename = files_by_ext['sm'][0]
|
|
|
|
print(sm_filename)
|
|
|
|
var thing = SM.load_file(full_folder + '/' + sm_filename)
|
|
|
|
print(thing)
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print('Found non-song directory: ' + folder)
|
|
|
|
for file in songslist.files:
|
|
|
|
print('Found file: ' + file)
|
2020-03-29 17:43:28 +10:30
|
|
|
|
2020-08-22 14:33:40 +09:30
|
|
|
return {song_defs=song_defs, genres=genres}
|
2020-03-29 17:43:28 +10:30
|
|
|
|
|
|
|
|
2020-05-01 15:45:28 +09:30
|
|
|
func load_folder(folder, filename='song'):
|
2019-11-22 23:59:38 +10:30
|
|
|
var file = File.new()
|
2020-05-01 15:45:28 +09:30
|
|
|
var err = file.open('%s/%s.json' % [folder, filename], File.READ)
|
2019-11-22 23:59:38 +10:30
|
|
|
if err != OK:
|
|
|
|
print(err)
|
|
|
|
return err
|
|
|
|
var result_json = JSON.parse(file.get_as_text())
|
|
|
|
file.close()
|
|
|
|
if result_json.error != OK:
|
2020-05-01 15:45:28 +09:30
|
|
|
print('Error: ', result_json.error)
|
|
|
|
print('Error Line: ', result_json.error_line)
|
|
|
|
print('Error String: ', result_json.error_string)
|
2019-11-22 23:59:38 +10:30
|
|
|
return result_json.error
|
|
|
|
var result = result_json.result
|
2019-11-25 22:35:31 +10:30
|
|
|
result.directory = folder
|
2019-12-11 23:55:25 +10:30
|
|
|
return result
|
|
|
|
|
2021-11-27 22:20:37 +10:30
|
|
|
|
2020-05-03 00:11:01 +09:30
|
|
|
func load_filelist(filelist: Array, directory=''):
|
2020-05-02 15:45:27 +09:30
|
|
|
var charts = {}
|
2020-12-27 17:51:40 +10:30
|
|
|
var key := 0
|
2020-12-21 22:52:15 +10:30
|
|
|
for name in filelist:
|
|
|
|
var extension: String = name.rsplit('.', true, 1)[-1]
|
|
|
|
name = directory.rstrip('/') + '/' + name
|
|
|
|
var filename = find_file(name)
|
2021-11-27 22:20:37 +10:30
|
|
|
if filename != NOT_FOUND:
|
2020-12-21 22:52:15 +10:30
|
|
|
match extension:
|
|
|
|
'rgtm': # multiple charts
|
|
|
|
var res = RGT.load_file(filename)
|
|
|
|
for k in res:
|
2020-12-27 17:51:40 +10:30
|
|
|
charts[Library.difficulty_translations.get(k, k)] = res[k]
|
|
|
|
'rgts', 'rgtx': # single chart - The keys for this should be translated afterwards
|
2020-12-21 22:52:15 +10:30
|
|
|
charts[key] = RGT.load_file(filename)
|
|
|
|
key += 1
|
|
|
|
'srt': # maimai, single chart
|
2021-02-07 00:17:27 +10:30
|
|
|
var metadata_and_notes = SRT.load_file(filename)
|
|
|
|
Note.process_note_list(metadata_and_notes[1]) # SRT doesn't handle doubles
|
|
|
|
charts[key] = metadata_and_notes
|
2020-12-21 22:52:15 +10:30
|
|
|
key += 1
|
|
|
|
'sm': # Stepmania, multiple charts
|
|
|
|
var res = SM.load_file(filename)
|
2020-12-25 23:11:09 +10:30
|
|
|
for chart in res[1]:
|
2021-11-27 16:51:59 +10:30
|
|
|
var diff = chart.difficulty_str
|
|
|
|
charts[diff] = chart.notes
|
2020-12-21 22:52:15 +10:30
|
|
|
_:
|
|
|
|
pass
|
2020-05-02 15:45:27 +09:30
|
|
|
return charts
|
|
|
|
|
2020-05-03 00:11:01 +09:30
|
|
|
|
|
|
|
func save_json(filename: String, data: Dictionary):
|
2024-05-29 22:11:01 +09:30
|
|
|
filename = Settings.user_data_dir + filename
|
2020-05-03 00:11:01 +09:30
|
|
|
var dir = filename.rsplit('/', true, 1)[0]
|
2021-11-27 22:20:37 +10:30
|
|
|
match FileHelpers.init_directory(dir):
|
2020-12-25 23:11:09 +10:30
|
|
|
OK:
|
|
|
|
pass
|
|
|
|
var err:
|
2021-11-27 22:20:37 +10:30
|
|
|
print_debug('Error making directory for JSON file: ', err, FileHelpers.ERROR_CODES[err])
|
2020-12-25 23:11:09 +10:30
|
|
|
return err
|
2020-05-03 00:11:01 +09:30
|
|
|
var json = JSON.print(data)
|
|
|
|
var file = File.new()
|
2020-12-25 23:11:09 +10:30
|
|
|
match file.open(filename, File.WRITE):
|
|
|
|
OK:
|
|
|
|
file.store_string(json)
|
|
|
|
file.close()
|
|
|
|
return OK
|
|
|
|
var err:
|
2021-11-27 22:20:37 +10:30
|
|
|
print_debug('Error saving JSON file: ', err, FileHelpers.ERROR_CODES[err])
|
2020-12-25 23:11:09 +10:30
|
|
|
return err
|
2020-05-03 00:11:01 +09:30
|
|
|
|
2021-11-27 22:20:37 +10:30
|
|
|
|
2020-05-03 00:11:01 +09:30
|
|
|
func load_json(filename: String):
|
|
|
|
var file = File.new()
|
|
|
|
var err
|
|
|
|
for root in PATHS:
|
|
|
|
var filename1 = root + filename
|
|
|
|
if file.file_exists(filename1):
|
|
|
|
err = file.open(filename1, File.READ)
|
|
|
|
if err != OK:
|
2021-11-27 22:20:37 +10:30
|
|
|
print('An error occurred while trying to open file: ', filename1, err, FileHelpers.ERROR_CODES[err])
|
2020-05-03 00:11:01 +09:30
|
|
|
continue # return err
|
|
|
|
var result_json = JSON.parse(file.get_as_text())
|
|
|
|
file.close()
|
|
|
|
if result_json.error != OK:
|
|
|
|
print('Error: ', result_json.error)
|
|
|
|
print('Error Line: ', result_json.error_line)
|
|
|
|
print('Error String: ', result_json.error_string)
|
|
|
|
return result_json.error
|
|
|
|
return result_json.result
|
|
|
|
print('File not found in any libraries: ', filename)
|
|
|
|
return ERR_FILE_NOT_FOUND
|