From 113dc0d44dc8efb328e6bdd95596b20c9c0644bd Mon Sep 17 00:00:00 2001 From: Jayden Sipe Date: Thu, 20 Jun 2024 09:28:50 -0400 Subject: [PATCH] Added ability to customize file save location path --- addons/gd_local_history/gd_local_history.gd | 57 +++++++++++++++++-- addons/gd_local_history/plugin.cfg | 6 +- .../local_history_panel.gd | 10 ++-- .../local_history_panel.tscn | 2 - project.godot | 4 ++ 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/addons/gd_local_history/gd_local_history.gd b/addons/gd_local_history/gd_local_history.gd index 360f1f4..6f20cc2 100644 --- a/addons/gd_local_history/gd_local_history.gd +++ b/addons/gd_local_history/gd_local_history.gd @@ -1,13 +1,58 @@ @tool extends EditorPlugin +class_name GDLocalHistory -var _previous_saved_files: Dictionary = {} var local_history_panel: LocalHistoryPanel = preload("res://addons/gd_local_history/ui/local_history_panel/local_history_panel.tscn").instantiate() +var _previous_saved_files: Dictionary = {} +static var save_file_path: String = "" +static var _allow_global_directory: StringName = "local_history/config/allow_global_directory" +static var _file_setting_name: StringName = "local_history/config/save_file_path" -# TODO: -# Add project setting for setting destination +func _disable_plugin() -> void: + # Gets rid of custom project settings + ProjectSettings.set_setting(_allow_global_directory, null) + ProjectSettings.set_setting(_file_setting_name, null) func _enter_tree() -> void: + # Init global directory project setting + if (!ProjectSettings.has_setting(_allow_global_directory)): + ProjectSettings.set_setting(_allow_global_directory, false) + ProjectSettings.set_initial_value(_allow_global_directory, false) + ProjectSettings.set_restart_if_changed(_allow_global_directory, true) + var _global_directory_property_info = { + "name": _allow_global_directory, + "type": TYPE_BOOL + } + ProjectSettings.add_property_info(_global_directory_property_info) + + # Init file setting name project setting + if (!ProjectSettings.has_setting(_file_setting_name)): + ProjectSettings.set_setting(_file_setting_name, "res://.gd_local_history") + ProjectSettings.set_initial_value(_file_setting_name, "res://.gd_local_history") + ProjectSettings.set_as_basic(_file_setting_name, true) + ProjectSettings.set_restart_if_changed(_file_setting_name, true) + + var _file_property_info: Dictionary = {} + if (ProjectSettings.get_setting(_allow_global_directory)): + _file_property_info = { + "name": _file_setting_name, + "type": TYPE_STRING, + "hint": PROPERTY_HINT_GLOBAL_DIR + } + else: + _file_property_info = { + "name": _file_setting_name, + "type": TYPE_STRING, + "hint": PROPERTY_HINT_DIR + } + ProjectSettings.add_property_info(_file_property_info) + ProjectSettings.save() + + # Init save file path + save_file_path = ProjectSettings.get_setting(_file_setting_name) + if (!DirAccess.dir_exists_absolute(save_file_path)): + DirAccess.make_dir_recursive_absolute(save_file_path) + # Add custom button to bottom panel add_control_to_bottom_panel(local_history_panel, "Local History") @@ -17,6 +62,7 @@ func _enter_tree() -> void: func _exit_tree() -> void: # Remove custom button from bottom panel remove_control_from_bottom_panel(local_history_panel) + local_history_panel.queue_free() # Remove signal connection resource_saved.disconnect(_local_history_save.bind()) @@ -27,14 +73,15 @@ func _local_history_save(_resource: Resource) -> void: var file_name: String = _resource.resource_path.get_file() var source_code: String = _resource.source_code + # Prevents having identical files when spamming save if (_previous_saved_files.get_or_add(file_name, "") == source_code): return _previous_saved_files[file_name] = source_code - var path: String = "res://.gd_local_history/%s/" % file_name + var path: String = "%s/%s/" % [save_file_path, file_name] if (!DirAccess.dir_exists_absolute(path)): DirAccess.make_dir_recursive_absolute(path) local_history_panel.create_file_tree_item(file_name) - var file: FileAccess = FileAccess.open(path + "%s-%s.txt" % [file_name, Time.get_unix_time_from_system()], FileAccess.WRITE) + var file: FileAccess = FileAccess.open("%s%s-%s.txt" % [path, file_name, Time.get_unix_time_from_system()], FileAccess.WRITE) file.store_string(_resource.source_code) file.close() diff --git a/addons/gd_local_history/plugin.cfg b/addons/gd_local_history/plugin.cfg index d3e6ffc..1a24c15 100644 --- a/addons/gd_local_history/plugin.cfg +++ b/addons/gd_local_history/plugin.cfg @@ -1,7 +1,7 @@ [plugin] name="GDLocalHistory" -description="" -author="" -version="" +description="An editor plugin for Godot that contains a local history panel. This shows recent changes to any files that have been saved recently, while also allowing you to refresh and delete entries." +author="Jayden" +version="1.1" script="gd_local_history.gd" diff --git a/addons/gd_local_history/ui/local_history_panel/local_history_panel.gd b/addons/gd_local_history/ui/local_history_panel/local_history_panel.gd index 0ad1b1c..9417b7e 100644 --- a/addons/gd_local_history/ui/local_history_panel/local_history_panel.gd +++ b/addons/gd_local_history/ui/local_history_panel/local_history_panel.gd @@ -10,7 +10,7 @@ var _tree_root: TreeItem func _ready() -> void: var _tree_root: TreeItem = tree.create_item() - var directories: PackedStringArray = DirAccess.get_directories_at("res://.gd_local_history") + var directories: PackedStringArray = DirAccess.get_directories_at(GDLocalHistory.save_file_path) for directory: String in directories: create_file_tree_item(directory) @@ -31,11 +31,11 @@ func _on_tree_item_selected() -> void: code_edit.text = metadata return - var folder_path: String = "res://.gd_local_history/%s" % selected_tree_item.get_text(0) + var folder_path: String = "%s/%s" % [GDLocalHistory.save_file_path, selected_tree_item.get_text(0)] _refresh_tree_item(selected_tree_item, folder_path) func _on_tree_button_clicked(item: TreeItem, column: int, id: int, mouse_button_index: int) -> void: - var folder_path: String = "res://.gd_local_history/%s" % item.get_text(0) + var folder_path: String = "%s/%s" % [GDLocalHistory.save_file_path, item.get_text(0)] match (id): 1: _clear_tree_item(item, folder_path, true) @@ -47,7 +47,7 @@ func _on_tree_button_clicked(item: TreeItem, column: int, id: int, mouse_button_ func _clear_tree_item(tree_item: TreeItem, folder_path: String = "", delete_files: bool = false) -> void: if (delete_files): for source_code_file_name: String in DirAccess.get_files_at(folder_path): - DirAccess.remove_absolute(folder_path + "/%s" % source_code_file_name) + DirAccess.remove_absolute("%s/%s" % [folder_path, source_code_file_name]) DirAccess.remove_absolute(folder_path) tree_item.free() else: @@ -59,4 +59,4 @@ func _refresh_tree_item(tree_item: TreeItem, folder_path: String) -> void: for source_code_file_name: String in DirAccess.get_files_at(folder_path): var txt_child: TreeItem = tree_item.create_child() txt_child.set_text(0, source_code_file_name) - txt_child.set_metadata(0, FileAccess.open(folder_path + "/%s" % source_code_file_name, FileAccess.READ).get_as_text()) + txt_child.set_metadata(0, FileAccess.open("%s/%s" % [folder_path, source_code_file_name], FileAccess.READ).get_as_text()) diff --git a/addons/gd_local_history/ui/local_history_panel/local_history_panel.tscn b/addons/gd_local_history/ui/local_history_panel/local_history_panel.tscn index 712fbb5..f9ed58e 100644 --- a/addons/gd_local_history/ui/local_history_panel/local_history_panel.tscn +++ b/addons/gd_local_history/ui/local_history_panel/local_history_panel.tscn @@ -29,8 +29,6 @@ layout_mode = 2 editable = false scroll_smooth = true minimap_draw = true -highlight_all_occurrences = true -highlight_current_line = true draw_tabs = true gutters_draw_line_numbers = true gutters_zero_pad_line_numbers = true diff --git a/project.godot b/project.godot index 3f4fe4f..0bd7c33 100644 --- a/project.godot +++ b/project.godot @@ -16,6 +16,10 @@ config/version="1.0" config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://icon.svg" +[debug] + +gdscript/warnings/untyped_declaration=2 + [editor_plugins] enabled=PackedStringArray("res://addons/gd_local_history/plugin.cfg")