yagvm/Classes/GroupsInstallsManager.gd

60 lines
1.5 KiB
GDScript3
Raw Permalink Normal View History

2023-05-27 02:54:11 +00:00
extends Node
const GROUPS_BASE_FOLDER := "user://groups"
const METADATA_FILENAME := "meta.tres"
# key: GroupMetadata
2023-05-27 03:08:36 +00:00
# value: String = UUID
2023-05-27 02:54:11 +00:00
var groups: Dictionary = {}
func _ready() -> void:
DirAccess.make_dir_absolute(GROUPS_BASE_FOLDER)
2023-05-27 03:35:16 +00:00
var d := DirAccess.open(GROUPS_BASE_FOLDER)
d.list_dir_begin()
var current_folder := d.get_next()
while current_folder != "":
var res_path := GROUPS_BASE_FOLDER.path_join(current_folder).path_join(METADATA_FILENAME)
var gm: GroupMetadata = ResourceLoader.load(res_path, "GroupMetadata")
groups[gm] = current_folder
current_folder = d.get_next()
2023-05-27 02:54:11 +00:00
func create_group_folder(gm: GroupMetadata) -> void:
2023-05-27 03:08:36 +00:00
if gm in groups:
2023-05-27 02:54:11 +00:00
return
var folder_name := UUID.v4()
var d := DirAccess.open(GROUPS_BASE_FOLDER)
d.make_dir(folder_name)
groups[gm] = folder_name
gm.save_request.connect(group_metadata_should_save.bind(gm))
2023-05-27 03:08:36 +00:00
func delete_group_folder(gm: GroupMetadata) -> void:
if !groups.has(gm):
return
var path := ProjectSettings.globalize_path(GROUPS_BASE_FOLDER.path_join(groups[gm]))
OS.move_to_trash(path)
groups.erase(gm)
2023-05-27 02:54:11 +00:00
func group_metadata_should_save(gm: GroupMetadata) -> void:
var path := GROUPS_BASE_FOLDER.path_join(groups[gm]).path_join(METADATA_FILENAME)
var save_err := ResourceSaver.save(gm, path)
if save_err != OK:
print("couldn't save resource, error ", save_err)
2023-05-27 03:35:16 +00:00
func get_all_group_metadatas() -> Array[GroupMetadata]:
var res: Array[GroupMetadata]
res.assign(groups.keys())
res.sort_custom(
func(a: GroupMetadata, b: GroupMetadata):
return a.index < b.index
)
return res