From 97b1aad886f49172498b5a5e6237b5c427d17b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sat, 27 May 2023 05:54:11 +0300 Subject: [PATCH] save groups meta --- Classes/GroupMetadata.gd | 2 ++ Classes/GroupsInstallsManager.gd | 31 +++++++++++++++++++++++++++++++ Classes/UUID.gd | 10 ++++++---- UI/Components/GroupMenu.gd | 2 ++ UI/Dialogs/GroupEditDialog.gd | 2 ++ UI/Main/GroupsView.gd | 2 ++ project.godot | 1 + 7 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 Classes/GroupsInstallsManager.gd diff --git a/Classes/GroupMetadata.gd b/Classes/GroupMetadata.gd index 3b85058..0ff55be 100644 --- a/Classes/GroupMetadata.gd +++ b/Classes/GroupMetadata.gd @@ -6,3 +6,5 @@ class_name GroupMetadata @export var description: String @export var installs: Array[InstallMetadata] @export var settings_overrides: Dictionary + +signal save_request diff --git a/Classes/GroupsInstallsManager.gd b/Classes/GroupsInstallsManager.gd new file mode 100644 index 0000000..4671570 --- /dev/null +++ b/Classes/GroupsInstallsManager.gd @@ -0,0 +1,31 @@ +extends Node + +const GROUPS_BASE_FOLDER := "user://groups" +const METADATA_FILENAME := "meta.tres" + +# key: GroupMetadata +# values: String = UUID +var groups: Dictionary = {} + + +func _ready() -> void: + DirAccess.make_dir_absolute(GROUPS_BASE_FOLDER) + + +func create_group_folder(gm: GroupMetadata) -> void: + if gm in groups.values(): + 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)) + + +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) diff --git a/Classes/UUID.gd b/Classes/UUID.gd index 69e59bc..c03281a 100644 --- a/Classes/UUID.gd +++ b/Classes/UUID.gd @@ -22,9 +22,11 @@ # Note: The code might not be as pretty it could be, since it's written # in a way that maximizes performance. Methods are inlined and loops are avoided. +class_name UUID const BYTE_MASK: int = 0b11111111 -static func uuidbin(): + +static func uuidbin() -> Array: randomize() # 16 random bytes with the bytes on index 6 and 8 modified return [ @@ -34,7 +36,7 @@ static func uuidbin(): randi() & BYTE_MASK, randi() & BYTE_MASK, randi() & BYTE_MASK, randi() & BYTE_MASK, ] -static func uuidbinrng(rng: RandomNumberGenerator): +static func uuidbinrng(rng: RandomNumberGenerator) -> Array: rng.randomize() return [ rng.randi() & BYTE_MASK, rng.randi() & BYTE_MASK, rng.randi() & BYTE_MASK, rng.randi() & BYTE_MASK, @@ -43,7 +45,7 @@ static func uuidbinrng(rng: RandomNumberGenerator): rng.randi() & BYTE_MASK, rng.randi() & BYTE_MASK, rng.randi() & BYTE_MASK, rng.randi() & BYTE_MASK, ] -static func v4(): +static func v4() -> String: # 16 random bytes with the bytes on index 6 and 8 modified var b = uuidbin() @@ -64,7 +66,7 @@ static func v4(): b[10], b[11], b[12], b[13], b[14], b[15] ] -static func v4_rng(rng: RandomNumberGenerator): +static func v4_rng(rng: RandomNumberGenerator) -> String: # 16 random bytes with the bytes on index 6 and 8 modified var b = uuidbinrng(rng) diff --git a/UI/Components/GroupMenu.gd b/UI/Components/GroupMenu.gd index 8208d08..2ff8238 100644 --- a/UI/Components/GroupMenu.gd +++ b/UI/Components/GroupMenu.gd @@ -56,6 +56,7 @@ func add_new_install() -> void: install_item.setup_from_metadata(im) install_item.edit_item_button_pressed.connect(edit_install.bind(install_item)) group_metadata.installs.append(im) + group_metadata.save_request.emit() , CONNECT_ONE_SHOT) install_edit_dialog.show() @@ -144,6 +145,7 @@ func setup_rename() -> void: func(new_text: String): header_text_label.text = new_text group_metadata.name = new_text + group_metadata.save_request.emit() reenable.call() ) diff --git a/UI/Dialogs/GroupEditDialog.gd b/UI/Dialogs/GroupEditDialog.gd index 286b0fa..f9af2f4 100644 --- a/UI/Dialogs/GroupEditDialog.gd +++ b/UI/Dialogs/GroupEditDialog.gd @@ -23,6 +23,8 @@ func get_group_metadata() -> GroupMetadata: group_metadata.settings_overrides = override_editor_settings_menu.get_overrides() group_metadata.name = name_line_edit.text + group_metadata.save_request.emit() + return group_metadata diff --git a/UI/Main/GroupsView.gd b/UI/Main/GroupsView.gd index 14d24f9..48bf065 100644 --- a/UI/Main/GroupsView.gd +++ b/UI/Main/GroupsView.gd @@ -30,6 +30,8 @@ func add_new_group() -> void: group_menu.setup_from_metadata(gm) group_menu.edit_group_button_pressed.connect(edit_group.bind(group_menu)) + GroupsInstallsManager.create_group_folder(gm) + GroupsInstallsManager.group_metadata_should_save(gm) , CONNECT_ONE_SHOT) diff --git a/project.godot b/project.godot index ad6a782..aebd622 100644 --- a/project.godot +++ b/project.godot @@ -21,6 +21,7 @@ config/icon="res://icon.svg" DotEnv="*res://Classes/DotEnv.gd" ReleasesManager="*res://Classes/ReleasesManager.gd" +GroupsInstallsManager="*res://Classes/GroupsInstallsManager.gd" [display]