Skip to content

Commit

Permalink
Added ability to customize file save location path
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydensipe committed Jun 20, 2024
1 parent ec16bb8 commit 113dc0d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
57 changes: 52 additions & 5 deletions addons/gd_local_history/gd_local_history.gd
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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())
Expand All @@ -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()
6 changes: 3 additions & 3 deletions addons/gd_local_history/plugin.cfg
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 113dc0d

Please sign in to comment.