Browse Source

made output file name checking more robust

Jonathan Higgins 2 months ago
parent
commit
65d21214be
5 changed files with 60 additions and 18 deletions
  1. 37 0
      Global/Global.gd
  2. 1 1
      project.godot
  3. 2 1
      scenes/Nodes/nodes.tscn
  4. 16 0
      scenes/Nodes/outputfile.gd
  5. 4 16
      scenes/main/scripts/control.gd

+ 37 - 0
Global/Global.gd

@@ -2,3 +2,40 @@ extends Node
 
 var outfile = "no_file" #bad name for the output directory
 var cdpoutput = "no_file" #output from running thread used for recycling output files
+
+func check_for_invalid_chars(file: String) -> Dictionary:
+	var output = {
+		"contains_invalid_characters" = false,
+		"invalid_characters_found" = [],
+		"string_without_invalid_characters" = ""
+	}
+	#check path and file name do not contain special characters
+	var check_characters = []
+	if file.contains("/"):
+		check_characters = file.get_basename().split("/")
+	else:
+		check_characters.append(file)
+		
+	var invalid_chars:= []
+	var regex = RegEx.new()
+	regex.compile("[^a-zA-Z0-9\\-_ :+]")
+	for string in check_characters:
+		if string != "":
+			var result = regex.search_all(string)
+			for matches in result:
+				var char = matches.get_string()
+				if invalid_chars.has(char) == false:
+					invalid_chars.append(char)
+
+	var invalid_string = "".join(invalid_chars)
+	
+	if invalid_chars.size() == 0:
+		output["contains_invalid_characters"] = false
+	else:
+		output["contains_invalid_characters"] = true
+		output["invalid_characters_found"] = invalid_chars
+		var cleaned_string = file
+		for char in invalid_chars:
+			cleaned_string = cleaned_string.replace(char, "")
+		output["string_without_invalid_characters"] = cleaned_string
+	return output

+ 1 - 1
project.godot

@@ -24,7 +24,7 @@ boot_splash/minimum_display_time=500
 [autoload]
 
 ConfigHandler="*res://global/config_handler.gd"
-Global="*res://global/Global.gd"
+Global="*res://global/global.gd"
 Nodes="*res://scenes/Nodes/nodes.tscn"
 
 [display]

+ 2 - 1
scenes/Nodes/nodes.tscn

@@ -246,7 +246,7 @@ theme_override_constants/margin_bottom = 2
 
 [node name="FileNameField" type="LineEdit" parent="outputfile" groups=["outputnode"]]
 layout_mode = 2
-tooltip_text = "Your output file name."
+tooltip_text = "Your output file name, cannot contain special characters"
 metadata/outputfunction = "filename"
 
 [node name="MarginContainer3" type="MarginContainer" parent="outputfile"]
@@ -827,6 +827,7 @@ size_flags_horizontal = 3
 text = "+"
 metadata/calc = "+"
 
+[connection signal="focus_exited" from="outputfile/FileNameField" to="outputfile" method="_on_file_name_field_focus_exited"]
 [connection signal="text_submitted" from="outputfile/FileNameField" to="outputfile" method="_on_file_name_field_text_submitted"]
 [connection signal="toggled" from="outputfile/DeleteIntermediateFilesToggle" to="outputfile" method="_on_delete_intermediate_files_toggle_toggled"]
 [connection signal="toggled" from="outputfile/ReuseFolderToggle" to="outputfile" method="_on_reuse_folder_toggle_toggled"]

+ 16 - 0
scenes/Nodes/outputfile.gd

@@ -38,3 +38,19 @@ func _on_reuse_folder_toggle_toggled(toggled_on: bool) -> void:
 
 func _on_position_offset_changed():
 	node_moved.emit(self, Rect2(position, size))
+
+
+func _on_file_name_field_text_submitted(new_text: String) -> void:
+	#check for slashes which can't be in a file name
+	if new_text.contains("/"):
+		new_text = new_text.replace("/", "")
+	if new_text.contains("\\"):
+		new_text = new_text.replace("\\", "")
+		
+	var check_characters = Global.check_for_invalid_chars(new_text)
+	if check_characters["contains_invalid_characters"] == true:
+		$FileNameField.text = check_characters["string_without_invalid_characters"]
+
+
+func _on_file_name_field_focus_exited() -> void:
+	_on_file_name_field_text_submitted($FileNameField.text)

+ 4 - 16
scenes/main/scripts/control.gd

@@ -398,21 +398,9 @@ func _on_file_dialog_dir_selected(dir: String) -> void:
 	Global.outfile = dir + "/" + outfilename.text.get_basename() + "_" + Time.get_date_string_from_system() + "_" + time_str
 	
 	#check path and file name do not contain special characters
-	var check_characters = Global.outfile.get_basename().split("/")
-	var invalid_chars:= []
-	var regex = RegEx.new()
-	regex.compile("[^a-zA-Z0-9\\-_ :+]")
-	for string in check_characters:
-		if string != "":
-			var result = regex.search_all(string)
-			for matches in result:
-				var char = matches.get_string()
-				if invalid_chars.has(char) == false:
-					invalid_chars.append(char)
-
-	var invalid_string = " ".join(invalid_chars)
-	
-	if invalid_chars.size() == 0:
+	var check_file_name = Global.check_for_invalid_chars(Global.outfile)
+	
+	if check_file_name["contains_invalid_characters"] == false:
 		run_thread.log_console("Output directory and file name(s):" + Global.outfile, true)
 		await get_tree().process_frame
 		
@@ -420,7 +408,7 @@ func _on_file_dialog_dir_selected(dir: String) -> void:
 	else:
 		run_thread.log_console("[color=#9c2828][b]Error:[/b][/color] Chosen file name or folder path " + Global.outfile.get_basename() + " contains invalid characters.", true)
 		run_thread.log_console("File names and paths can only contain A-Z a-z 0-9 - _ + and space.", true)
-		run_thread.log_console("Chosen file name/path contains the following invalid characters: " + invalid_string, true)
+		run_thread.log_console("Chosen file name/path contains the following invalid characters: " + " ".join(check_file_name["invalid_characters_found"]), true)
 		if $ProgressWindow.visible:
 			$ProgressWindow.hide()
 		if !$Console.visible: