Skip to content

Commit

Permalink
Reuse make_custom code
Browse files Browse the repository at this point in the history
This cleans up the make_custom code and moves it to DialogicUtil so that it can be reused (and has to be fixed in one place only!)
  • Loading branch information
Jowan-Spooner committed Sep 9, 2024
1 parent df66abb commit 1b26261
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
53 changes: 52 additions & 1 deletion addons/dialogic/Core/DialogicUtil.gd
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,58 @@ static func get_scene_export_defaults(node:Node) -> Dictionary:

#endregion

#region MAKE CUSTOM

static func make_file_custom(original_file:String, target_folder:String, new_file_name := "", new_folder_name := "") -> String:
if not ResourceLoader.exists(original_file):
push_error("[Dialogic] Unable to make file with invalid path custom!")
return ""

if new_folder_name:
target_folder = target_folder.path_join(new_folder_name)
DirAccess.make_dir_absolute(target_folder)

if new_file_name.is_empty():
new_file_name = "custom_" + original_file.get_file()

if not new_file_name.ends_with(original_file.get_extension()):
new_file_name += "." + original_file.get_extension()

var target_file := target_folder.path_join(new_file_name)

DirAccess.copy_absolute(original_file, target_file)

var file := FileAccess.open(target_file, FileAccess.READ)
var file_text := file.get_as_text()
file.close()

# If we are customizing a scene, we check for any resources used in that scene that are in the same folder.
# Those will be copied as well and the scene will be modified to point to them.
if file_text.begins_with('[gd_scene'):
var base_path: String = original_file.get_base_dir()

var remove_uuid_regex := r'\[gd_scene .* (?<uid>uid="uid:[^"]*")'
var result := RegEx.create_from_string(remove_uuid_regex).search(file_text)
if result:
file_text = file_text.replace(result.get_string("uid"), "")

var file_regex := r'\Q"'+base_path+r'\E(?<file>[^"]*)"'
result = RegEx.create_from_string(file_regex).search(file_text)
while result:
DirAccess.copy_absolute(base_path.path_join(result.get_string('file')), target_folder.path_join(result.get_string('file')))
file_text = file_text.replace(base_path.path_join(result.get_string('file')), target_folder.path_join(result.get_string('file')))
result = RegEx.create_from_string(file_regex).search(file_text)

file = FileAccess.open(target_file, FileAccess.WRITE)
file.store_string(file_text)
file.close()

get_dialogic_plugin().get_editor_interface().get_resource_filesystem().scan_sources()

return target_file


#endregion

#region INSPECTOR FIELDS
################################################################################
Expand Down Expand Up @@ -606,4 +658,3 @@ static func get_portrait_position_suggestions(search_text := "") -> Dictionary:
suggestions.erase(search_text)

return suggestions

31 changes: 4 additions & 27 deletions addons/dialogic/Editor/CharacterEditor/char_edit_p_section_main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,10 @@ func create_new_portrait_scene(target_file: String, info: Dictionary) -> void:

func make_portrait_preset_custom(target_file:String, info: Dictionary) -> String:
var previous_file: String = info.path

var target_folder := target_file.get_base_dir()
target_file = target_file.get_file()

DirAccess.make_dir_absolute(target_folder)

DirAccess.copy_absolute(previous_file, target_folder.path_join(target_file))

var file := FileAccess.open(target_folder.path_join(target_file), FileAccess.READ)
var scene_text := file.get_as_text()
file.close()
if scene_text.begins_with('[gd_scene'):
var base_path: String = previous_file.get_base_dir()

var result := RegEx.create_from_string("\\Q\""+base_path+"\\E(?<file>[^\"]*)\"").search(scene_text)
while result:
DirAccess.copy_absolute(base_path.path_join(result.get_string('file')), target_folder.path_join(result.get_string('file')))
scene_text = scene_text.replace(base_path.path_join(result.get_string('file')), target_folder.path_join(result.get_string('file')))
result = RegEx.create_from_string("\\Q\""+base_path+"\\E(?<file>[^\"]*)\"").search(scene_text)

file = FileAccess.open(target_folder.path_join(target_file), FileAccess.WRITE)
file.store_string(scene_text)
file.close()

find_parent('EditorView').plugin_reference.get_editor_interface().get_resource_filesystem().scan_sources()
return target_folder.path_join(target_file)

var result_path := DialogicUtil.make_file_custom(previous_file, target_file.get_base_dir(), target_file.get_file())

return result_path


func set_scene_path(path:String) -> void:
Expand Down Expand Up @@ -121,4 +99,3 @@ func reload_ui(data: Dictionary) -> void:
%SceneLabel.tooltip_text = path
%SceneLabel.add_theme_color_override("font_color", get_theme_color("property_color_x", "Editor"))
%OpenSceneButton.show()

56 changes: 15 additions & 41 deletions addons/dialogic/Modules/StyleEditor/style_layer_editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -250,46 +250,22 @@ func _on_make_custom_layout_file_selected(file:String) -> void:


func make_layer_custom(target_folder:String, custom_name := "") -> void:
if not ResourceLoader.exists(current_style.get_layer_info(current_layer_idx).path):
printerr("[Dialogic] Unable to copy layer that has no scene path specified!")
return

var target_file := ""
var previous_file: String = current_style.get_layer_info(current_layer_idx).path

var original_file: String = current_style.get_layer_info(current_layer_idx).path
var custom_new_folder := ""

if custom_name.is_empty():
target_file = 'custom_' + previous_file.get_file()
target_folder = target_folder.path_join(%StyleBrowser.premade_scenes_reference[previous_file].name.to_pascal_case())
else:
if not custom_name.ends_with('.tscn'):
custom_name += ".tscn"
target_file = custom_name

DirAccess.make_dir_absolute(target_folder)

DirAccess.copy_absolute(previous_file, target_folder.path_join(target_file))

var file := FileAccess.open(target_folder.path_join(target_file), FileAccess.READ)
var scene_text := file.get_as_text()
file.close()
if scene_text.begins_with('[gd_scene'):
var base_path: String = previous_file.get_base_dir()

var remove_uuid_regex := r'\[gd_scene .* (?<uid>uid="uid:[^"]*")'
var result := RegEx.create_from_string(remove_uuid_regex).search(scene_text)
scene_text = scene_text.replace(result.get_string("uid"), "")

var file_regex := r'\Q"'+base_path+r'\E(?<file>[^"]*)"'
result = RegEx.create_from_string(file_regex).search(scene_text)
while result:
DirAccess.copy_absolute(base_path.path_join(result.get_string('file')), target_folder.path_join(result.get_string('file')))
scene_text = scene_text.replace(base_path.path_join(result.get_string('file')), target_folder.path_join(result.get_string('file')))
result = RegEx.create_from_string(file_regex).search(scene_text)

file = FileAccess.open(target_folder.path_join(target_file), FileAccess.WRITE)
file.store_string(scene_text)
file.close()

current_style.set_layer_scene(current_layer_idx, target_folder.path_join(target_file))
custom_name = "custom_"+%StyleBrowser.premade_scenes_reference[original_file].name.to_snake_case()
custom_new_folder = %StyleBrowser.premade_scenes_reference[original_file].name.to_pascal_case()

var result_path := DialogicUtil.make_file_custom(
original_file,
target_folder,
custom_name,
custom_new_folder,
)

current_style.set_layer_scene(current_layer_idx, result_path)

load_style_layer_list()

Expand All @@ -298,8 +274,6 @@ func make_layer_custom(target_folder:String, custom_name := "") -> void:
else:
%LayerTree.get_root().get_child(%LayerTree.get_selected().get_index()).select(0)

find_parent('EditorView').plugin_reference.get_editor_interface().get_resource_filesystem().scan_sources()


func make_layout_custom(target_folder:String) -> void:
target_folder = target_folder.path_join("Custom" + current_style.name.to_pascal_case())
Expand Down

0 comments on commit 1b26261

Please sign in to comment.