Skip to content

Commit

Permalink
Styles/Portraits: Fix UID issues on scenes created with Make Custom (#…
Browse files Browse the repository at this point in the history
…2387)

Fixes a VERY BAD bug where scenes created using "make custom" would keep the old UID. This resulted in the wrong scenes being loaded in-game. This has haunted me for a while now. Glad I found a fix finally.

Also unifies the make_custom code into a function in DialogicUtil so the style editor and custom portrait system can reuse the same code!
  • Loading branch information
Jowan-Spooner authored Sep 12, 2024
1 parent 975e1eb commit df9d178
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 67 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()

10 changes: 7 additions & 3 deletions addons/dialogic/Modules/StyleEditor/style_editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ func load_style_list() -> void:
var idx := 0
for style in styles:
%StyleList.add_item(style.name, get_theme_icon("PopupMenu", "EditorIcons"))
%StyleList.set_item_tooltip(idx, style.resource_path)
%StyleList.set_item_metadata(idx, style)

if style.resource_path == default_style:
%StyleList.set_item_icon_modulate(idx, get_theme_color("warning_color", "Editor"))
if style.resource_path.begins_with("res://addons/dialogic"):
%StyleList.set_item_icon_modulate(idx, get_theme_color("property_color_z", "Editor"))
%StyleList.set_item_tooltip(idx, "This is a default style. Only edit it if you know what you are doing!")
%StyleList.set_item_custom_bg_color(idx, get_theme_color("property_color_z", "Editor").lerp(get_theme_color("dark_color_3", "Editor"), 0.8))
if style.name == latest:
%StyleList.select(idx)
load_style(style)
%StyleList.set_item_tooltip(idx, style.resource_path)
%StyleList.set_item_metadata(idx, style)
idx += 1

if len(styles) == 0:
Expand Down Expand Up @@ -388,4 +393,3 @@ func _get_new_name(base_name:String) -> String:
return new_name

#endregion

51 changes: 15 additions & 36 deletions addons/dialogic/Modules/StyleEditor/style_layer_editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -250,41 +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 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()

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 @@ -293,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 df9d178

Please sign in to comment.