Fix critical memory leaks, race conditions, and improve code quality
- Fix memory leaks in match3_gameplay.gd with proper queue_free() usage - Add comprehensive error handling and fallback mechanisms to SettingsManager - Resolve scene loading race conditions in GameManager with state protection - Remove problematic static variables from tile.gd, replace with instance-based approach - Consolidate duplicate debug menu classes into shared DebugMenuBase - Add input validation across all user input paths for security and stability
This commit is contained in:
@@ -1,71 +1,17 @@
|
||||
extends Control
|
||||
extends DebugMenuBase
|
||||
|
||||
@onready var regenerate_button: Button = $VBoxContainer/RegenerateButton
|
||||
@onready var gem_types_spinbox: SpinBox = $VBoxContainer/GemTypesContainer/GemTypesSpinBox
|
||||
@onready var gem_types_label: Label = $VBoxContainer/GemTypesContainer/GemTypesLabel
|
||||
@onready var grid_width_spinbox: SpinBox = $VBoxContainer/GridSizeContainer/GridWidthContainer/GridWidthSpinBox
|
||||
@onready var grid_height_spinbox: SpinBox = $VBoxContainer/GridSizeContainer/GridHeightContainer/GridHeightSpinBox
|
||||
@onready var grid_width_label: Label = $VBoxContainer/GridSizeContainer/GridWidthContainer/GridWidthLabel
|
||||
@onready var grid_height_label: Label = $VBoxContainer/GridSizeContainer/GridHeightContainer/GridHeightLabel
|
||||
|
||||
var match3_scene: Node2D
|
||||
var search_timer: Timer
|
||||
|
||||
# Fixed: Add cleanup function
|
||||
func _exit_tree():
|
||||
if search_timer:
|
||||
search_timer.queue_free()
|
||||
|
||||
func _ready():
|
||||
DebugManager.debug_toggled.connect(_on_debug_toggled)
|
||||
|
||||
# Initialize with current debug state
|
||||
var current_debug_state = DebugManager.is_debug_enabled()
|
||||
visible = current_debug_state
|
||||
regenerate_button.pressed.connect(_on_regenerate_pressed)
|
||||
gem_types_spinbox.value_changed.connect(_on_gem_types_changed)
|
||||
grid_width_spinbox.value_changed.connect(_on_grid_width_changed)
|
||||
grid_height_spinbox.value_changed.connect(_on_grid_height_changed)
|
||||
|
||||
# Initialize gem types spinbox
|
||||
gem_types_spinbox.min_value = 3
|
||||
gem_types_spinbox.max_value = 8
|
||||
gem_types_spinbox.step = 1
|
||||
gem_types_spinbox.value = 5 # Default value
|
||||
|
||||
# Initialize grid size spinboxes
|
||||
grid_width_spinbox.min_value = 4
|
||||
grid_width_spinbox.max_value = 12
|
||||
grid_width_spinbox.step = 1
|
||||
grid_width_spinbox.value = 8 # Default value
|
||||
|
||||
grid_height_spinbox.min_value = 4
|
||||
grid_height_spinbox.max_value = 12
|
||||
grid_height_spinbox.step = 1
|
||||
grid_height_spinbox.value = 8 # Default value
|
||||
|
||||
# Fixed: Create timer for periodic match3 scene search
|
||||
search_timer = Timer.new()
|
||||
search_timer.wait_time = 0.1
|
||||
search_timer.timeout.connect(_find_match3_scene)
|
||||
add_child(search_timer)
|
||||
|
||||
# Start searching immediately and continue until found
|
||||
_find_match3_scene()
|
||||
|
||||
func _find_match3_scene():
|
||||
func _find_target_scene():
|
||||
# Fixed: Search more thoroughly for match3 scene
|
||||
if match3_scene:
|
||||
# Already found, stop searching
|
||||
if search_timer and search_timer.timeout.is_connected(_find_match3_scene):
|
||||
search_timer.stop()
|
||||
_stop_search_timer()
|
||||
return
|
||||
|
||||
# Search in current scene tree
|
||||
var current_scene = get_tree().current_scene
|
||||
if current_scene:
|
||||
# Try to find match3 by class name first
|
||||
match3_scene = _find_node_by_script(current_scene, "res://scenes/game/gameplays/match3_gameplay.gd")
|
||||
# Try to find match3 by script path first
|
||||
match3_scene = _find_node_by_script(current_scene, target_script_path)
|
||||
|
||||
# Fallback: search by common node names
|
||||
if not match3_scene:
|
||||
@@ -75,132 +21,9 @@ func _find_match3_scene():
|
||||
break
|
||||
|
||||
if match3_scene:
|
||||
DebugManager.log_debug("Found match3 scene: " + match3_scene.name, "DebugMenu")
|
||||
# Update UI with current values
|
||||
if match3_scene.has_method("get") and "TILE_TYPES" in match3_scene:
|
||||
gem_types_spinbox.value = match3_scene.TILE_TYPES
|
||||
gem_types_label.text = "Gem Types: " + str(match3_scene.TILE_TYPES)
|
||||
|
||||
# Update grid size values
|
||||
if "GRID_SIZE" in match3_scene:
|
||||
var grid_size = match3_scene.GRID_SIZE
|
||||
grid_width_spinbox.value = grid_size.x
|
||||
grid_height_spinbox.value = grid_size.y
|
||||
grid_width_label.text = "Width: " + str(grid_size.x)
|
||||
grid_height_label.text = "Height: " + str(grid_size.y)
|
||||
|
||||
# Stop the search timer
|
||||
if search_timer and search_timer.timeout.is_connected(_find_match3_scene):
|
||||
search_timer.stop()
|
||||
DebugManager.log_debug("Found match3 scene: " + match3_scene.name, log_category)
|
||||
_update_ui_from_scene()
|
||||
_stop_search_timer()
|
||||
else:
|
||||
# Continue searching if not found
|
||||
if search_timer and not search_timer.timeout.is_connected(_find_match3_scene):
|
||||
search_timer.timeout.connect(_find_match3_scene)
|
||||
search_timer.start()
|
||||
|
||||
func _find_node_by_script(node: Node, script_path: String) -> Node:
|
||||
# Helper function to find node by its script path
|
||||
if node.get_script():
|
||||
var node_script = node.get_script()
|
||||
if node_script.resource_path == script_path:
|
||||
return node
|
||||
|
||||
for child in node.get_children():
|
||||
var result = _find_node_by_script(child, script_path)
|
||||
if result:
|
||||
return result
|
||||
|
||||
return null
|
||||
|
||||
func _on_debug_toggled(enabled: bool):
|
||||
visible = enabled
|
||||
if enabled:
|
||||
# Always refresh match3 scene reference when debug menu opens
|
||||
if not match3_scene:
|
||||
_find_match3_scene()
|
||||
# Update display values
|
||||
if match3_scene and match3_scene.has_method("get") and "TILE_TYPES" in match3_scene:
|
||||
gem_types_spinbox.value = match3_scene.TILE_TYPES
|
||||
gem_types_label.text = "Gem Types: " + str(match3_scene.TILE_TYPES)
|
||||
|
||||
# Update grid size display values
|
||||
if match3_scene and "GRID_SIZE" in match3_scene:
|
||||
var grid_size = match3_scene.GRID_SIZE
|
||||
grid_width_spinbox.value = grid_size.x
|
||||
grid_height_spinbox.value = grid_size.y
|
||||
grid_width_label.text = "Width: " + str(grid_size.x)
|
||||
grid_height_label.text = "Height: " + str(grid_size.y)
|
||||
|
||||
func _on_regenerate_pressed():
|
||||
if not match3_scene:
|
||||
_find_match3_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find match3 scene for regeneration", "DebugMenu")
|
||||
return
|
||||
|
||||
if match3_scene.has_method("regenerate_grid"):
|
||||
DebugManager.log_debug("Calling regenerate_grid()", "DebugMenu")
|
||||
await match3_scene.regenerate_grid()
|
||||
else:
|
||||
DebugManager.log_error("match3_scene does not have regenerate_grid method", "DebugMenu")
|
||||
|
||||
func _on_gem_types_changed(value: float):
|
||||
if not match3_scene:
|
||||
_find_match3_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find match3 scene for gem types change", "DebugMenu")
|
||||
return
|
||||
|
||||
var new_value = int(value)
|
||||
if match3_scene.has_method("set_tile_types"):
|
||||
DebugManager.log_debug("Setting tile types to " + str(new_value), "DebugMenu")
|
||||
await match3_scene.set_tile_types(new_value)
|
||||
gem_types_label.text = "Gem Types: " + str(new_value)
|
||||
else:
|
||||
DebugManager.log_error("match3_scene does not have set_tile_types method", "DebugMenu")
|
||||
# Fallback: try to set TILE_TYPES directly
|
||||
if "TILE_TYPES" in match3_scene:
|
||||
match3_scene.TILE_TYPES = new_value
|
||||
gem_types_label.text = "Gem Types: " + str(new_value)
|
||||
|
||||
func _on_grid_width_changed(value: float):
|
||||
if not match3_scene:
|
||||
_find_match3_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find match3 scene for grid width change", "DebugMenu")
|
||||
return
|
||||
|
||||
var new_width = int(value)
|
||||
grid_width_label.text = "Width: " + str(new_width)
|
||||
|
||||
# Get current height
|
||||
var current_height = int(grid_height_spinbox.value)
|
||||
|
||||
if match3_scene.has_method("set_grid_size"):
|
||||
DebugManager.log_debug("Setting grid size to " + str(new_width) + "x" + str(current_height), "DebugMenu")
|
||||
await match3_scene.set_grid_size(Vector2i(new_width, current_height))
|
||||
else:
|
||||
DebugManager.log_error("match3_scene does not have set_grid_size method", "DebugMenu")
|
||||
|
||||
func _on_grid_height_changed(value: float):
|
||||
if not match3_scene:
|
||||
_find_match3_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find match3 scene for grid height change", "DebugMenu")
|
||||
return
|
||||
|
||||
var new_height = int(value)
|
||||
grid_height_label.text = "Height: " + str(new_height)
|
||||
|
||||
# Get current width
|
||||
var current_width = int(grid_width_spinbox.value)
|
||||
|
||||
if match3_scene.has_method("set_grid_size"):
|
||||
DebugManager.log_debug("Setting grid size to " + str(current_width) + "x" + str(new_height), "DebugMenu")
|
||||
await match3_scene.set_grid_size(Vector2i(current_width, new_height))
|
||||
else:
|
||||
DebugManager.log_error("match3_scene does not have set_grid_size method", "DebugMenu")
|
||||
_start_search_timer()
|
||||
|
||||
213
scenes/ui/DebugMenuBase.gd
Normal file
213
scenes/ui/DebugMenuBase.gd
Normal file
@@ -0,0 +1,213 @@
|
||||
class_name DebugMenuBase
|
||||
extends Control
|
||||
|
||||
@onready var regenerate_button: Button = $VBoxContainer/RegenerateButton
|
||||
@onready var gem_types_spinbox: SpinBox = $VBoxContainer/GemTypesContainer/GemTypesSpinBox
|
||||
@onready var gem_types_label: Label = $VBoxContainer/GemTypesContainer/GemTypesLabel
|
||||
@onready var grid_width_spinbox: SpinBox = $VBoxContainer/GridSizeContainer/GridWidthContainer/GridWidthSpinBox
|
||||
@onready var grid_height_spinbox: SpinBox = $VBoxContainer/GridSizeContainer/GridHeightContainer/GridHeightSpinBox
|
||||
@onready var grid_width_label: Label = $VBoxContainer/GridSizeContainer/GridWidthContainer/GridWidthLabel
|
||||
@onready var grid_height_label: Label = $VBoxContainer/GridSizeContainer/GridHeightContainer/GridHeightLabel
|
||||
|
||||
@export var target_script_path: String = "res://scenes/game/gameplays/match3_gameplay.gd"
|
||||
@export var log_category: String = "DebugMenu"
|
||||
|
||||
var match3_scene: Node2D
|
||||
var search_timer: Timer
|
||||
|
||||
func _exit_tree():
|
||||
if search_timer:
|
||||
search_timer.queue_free()
|
||||
|
||||
func _ready():
|
||||
DebugManager.log_debug("DebugMenuBase _ready() called", log_category)
|
||||
DebugManager.debug_toggled.connect(_on_debug_toggled)
|
||||
|
||||
# Initialize with current debug state
|
||||
var current_debug_state = DebugManager.is_debug_enabled()
|
||||
visible = current_debug_state
|
||||
|
||||
# Connect signals
|
||||
regenerate_button.pressed.connect(_on_regenerate_pressed)
|
||||
gem_types_spinbox.value_changed.connect(_on_gem_types_changed)
|
||||
grid_width_spinbox.value_changed.connect(_on_grid_width_changed)
|
||||
grid_height_spinbox.value_changed.connect(_on_grid_height_changed)
|
||||
|
||||
# Initialize spinbox values with validation
|
||||
_initialize_spinboxes()
|
||||
|
||||
# Set up scene finding
|
||||
_setup_scene_finding()
|
||||
|
||||
# Start searching for target scene
|
||||
_find_target_scene()
|
||||
|
||||
func _initialize_spinboxes():
|
||||
# Initialize gem types spinbox
|
||||
gem_types_spinbox.min_value = 3
|
||||
gem_types_spinbox.max_value = 8
|
||||
gem_types_spinbox.step = 1
|
||||
gem_types_spinbox.value = 5 # Default value
|
||||
|
||||
# Initialize grid size spinboxes
|
||||
grid_width_spinbox.min_value = 4
|
||||
grid_width_spinbox.max_value = 12
|
||||
grid_width_spinbox.step = 1
|
||||
grid_width_spinbox.value = 8 # Default value
|
||||
|
||||
grid_height_spinbox.min_value = 4
|
||||
grid_height_spinbox.max_value = 12
|
||||
grid_height_spinbox.step = 1
|
||||
grid_height_spinbox.value = 8 # Default value
|
||||
|
||||
func _setup_scene_finding():
|
||||
# Create timer for periodic scene search
|
||||
search_timer = Timer.new()
|
||||
search_timer.wait_time = 0.1
|
||||
search_timer.timeout.connect(_find_target_scene)
|
||||
add_child(search_timer)
|
||||
|
||||
# Virtual method - override in derived classes for specific finding logic
|
||||
func _find_target_scene():
|
||||
DebugManager.log_error("_find_target_scene() not implemented in derived class", log_category)
|
||||
|
||||
func _find_node_by_script(node: Node, script_path: String) -> Node:
|
||||
# Helper function to find node by its script path
|
||||
if not node:
|
||||
return null
|
||||
|
||||
if node.get_script():
|
||||
var node_script = node.get_script()
|
||||
if node_script.resource_path == script_path:
|
||||
return node
|
||||
|
||||
for child in node.get_children():
|
||||
var result = _find_node_by_script(child, script_path)
|
||||
if result:
|
||||
return result
|
||||
|
||||
return null
|
||||
|
||||
func _update_ui_from_scene():
|
||||
if not match3_scene:
|
||||
return
|
||||
|
||||
# Update gem types display
|
||||
if match3_scene.has_method("get") and "TILE_TYPES" in match3_scene:
|
||||
gem_types_spinbox.value = match3_scene.TILE_TYPES
|
||||
gem_types_label.text = "Gem Types: " + str(match3_scene.TILE_TYPES)
|
||||
|
||||
# Update grid size display
|
||||
if "GRID_SIZE" in match3_scene:
|
||||
var grid_size = match3_scene.GRID_SIZE
|
||||
grid_width_spinbox.value = grid_size.x
|
||||
grid_height_spinbox.value = grid_size.y
|
||||
grid_width_label.text = "Width: " + str(grid_size.x)
|
||||
grid_height_label.text = "Height: " + str(grid_size.y)
|
||||
|
||||
func _stop_search_timer():
|
||||
if search_timer and search_timer.timeout.is_connected(_find_target_scene):
|
||||
search_timer.stop()
|
||||
|
||||
func _start_search_timer():
|
||||
if search_timer and not search_timer.timeout.is_connected(_find_target_scene):
|
||||
search_timer.timeout.connect(_find_target_scene)
|
||||
search_timer.start()
|
||||
|
||||
func _on_debug_toggled(enabled: bool):
|
||||
DebugManager.log_debug("Debug toggled to " + str(enabled), log_category)
|
||||
visible = enabled
|
||||
if enabled:
|
||||
# Always refresh scene reference when debug menu opens
|
||||
if not match3_scene:
|
||||
_find_target_scene()
|
||||
_update_ui_from_scene()
|
||||
|
||||
func _on_regenerate_pressed():
|
||||
if not match3_scene:
|
||||
_find_target_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find target scene for regeneration", log_category)
|
||||
return
|
||||
|
||||
if match3_scene.has_method("regenerate_grid"):
|
||||
DebugManager.log_debug("Calling regenerate_grid()", log_category)
|
||||
await match3_scene.regenerate_grid()
|
||||
else:
|
||||
DebugManager.log_error("Target scene does not have regenerate_grid method", log_category)
|
||||
|
||||
func _on_gem_types_changed(value: float):
|
||||
if not match3_scene:
|
||||
_find_target_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find target scene for gem types change", log_category)
|
||||
return
|
||||
|
||||
var new_value = int(value)
|
||||
# Input validation
|
||||
if new_value < gem_types_spinbox.min_value or new_value > gem_types_spinbox.max_value:
|
||||
DebugManager.log_error("Invalid gem types value: %d (range: %d-%d)" % [new_value, gem_types_spinbox.min_value, gem_types_spinbox.max_value], log_category)
|
||||
return
|
||||
|
||||
if match3_scene.has_method("set_tile_types"):
|
||||
DebugManager.log_debug("Setting tile types to " + str(new_value), log_category)
|
||||
await match3_scene.set_tile_types(new_value)
|
||||
gem_types_label.text = "Gem Types: " + str(new_value)
|
||||
else:
|
||||
DebugManager.log_error("Target scene does not have set_tile_types method", log_category)
|
||||
# Fallback: try to set TILE_TYPES directly
|
||||
if "TILE_TYPES" in match3_scene:
|
||||
match3_scene.TILE_TYPES = new_value
|
||||
gem_types_label.text = "Gem Types: " + str(new_value)
|
||||
|
||||
func _on_grid_width_changed(value: float):
|
||||
if not match3_scene:
|
||||
_find_target_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find target scene for grid width change", log_category)
|
||||
return
|
||||
|
||||
var new_width = int(value)
|
||||
# Input validation
|
||||
if new_width < grid_width_spinbox.min_value or new_width > grid_width_spinbox.max_value:
|
||||
DebugManager.log_error("Invalid grid width value: %d (range: %d-%d)" % [new_width, grid_width_spinbox.min_value, grid_width_spinbox.max_value], log_category)
|
||||
return
|
||||
|
||||
grid_width_label.text = "Width: " + str(new_width)
|
||||
|
||||
# Get current height
|
||||
var current_height = int(grid_height_spinbox.value)
|
||||
|
||||
if match3_scene.has_method("set_grid_size"):
|
||||
DebugManager.log_debug("Setting grid size to " + str(new_width) + "x" + str(current_height), log_category)
|
||||
await match3_scene.set_grid_size(Vector2i(new_width, current_height))
|
||||
else:
|
||||
DebugManager.log_error("Target scene does not have set_grid_size method", log_category)
|
||||
|
||||
func _on_grid_height_changed(value: float):
|
||||
if not match3_scene:
|
||||
_find_target_scene()
|
||||
|
||||
if not match3_scene:
|
||||
DebugManager.log_error("Could not find target scene for grid height change", log_category)
|
||||
return
|
||||
|
||||
var new_height = int(value)
|
||||
# Input validation
|
||||
if new_height < grid_height_spinbox.min_value or new_height > grid_height_spinbox.max_value:
|
||||
DebugManager.log_error("Invalid grid height value: %d (range: %d-%d)" % [new_height, grid_height_spinbox.min_value, grid_height_spinbox.max_value], log_category)
|
||||
return
|
||||
|
||||
grid_height_label.text = "Height: " + str(new_height)
|
||||
|
||||
# Get current width
|
||||
var current_width = int(grid_width_spinbox.value)
|
||||
|
||||
if match3_scene.has_method("set_grid_size"):
|
||||
DebugManager.log_debug("Setting grid size to " + str(current_width) + "x" + str(new_height), log_category)
|
||||
await match3_scene.set_grid_size(Vector2i(current_width, new_height))
|
||||
else:
|
||||
DebugManager.log_error("Target scene does not have set_grid_size method", log_category)
|
||||
@@ -46,7 +46,22 @@ func _update_controls_from_settings():
|
||||
language_selector.selected = lang_index
|
||||
|
||||
func _on_volume_slider_changed(value, setting_key):
|
||||
settings_manager.set_setting(setting_key, value)
|
||||
# Input validation for volume settings
|
||||
if not setting_key in ["master_volume", "music_volume", "sfx_volume"]:
|
||||
DebugManager.log_error("Invalid volume setting key: " + str(setting_key), "Settings")
|
||||
return
|
||||
|
||||
if not (value is float or value is int):
|
||||
DebugManager.log_error("Invalid volume value type: " + str(typeof(value)), "Settings")
|
||||
return
|
||||
|
||||
# Clamp value to valid range
|
||||
var clamped_value = clamp(float(value), 0.0, 1.0)
|
||||
if clamped_value != value:
|
||||
DebugManager.log_warn("Volume value %f clamped to %f" % [value, clamped_value], "Settings")
|
||||
|
||||
if not settings_manager.set_setting(setting_key, clamped_value):
|
||||
DebugManager.log_error("Failed to set volume setting: " + setting_key, "Settings")
|
||||
|
||||
func _exit_settings():
|
||||
DebugManager.log_info("Exiting settings", "Settings")
|
||||
@@ -81,11 +96,26 @@ func setup_language_selector():
|
||||
language_selector.selected = lang_index
|
||||
|
||||
func _on_language_selector_item_selected(index: int):
|
||||
if index < language_codes.size():
|
||||
var selected_lang = language_codes[index]
|
||||
settings_manager.set_setting("language", selected_lang)
|
||||
DebugManager.log_info("Language changed to: " + selected_lang, "Settings")
|
||||
localization_manager.change_language(selected_lang)
|
||||
# Input validation for language selection
|
||||
if index < 0:
|
||||
DebugManager.log_error("Invalid language index (negative): " + str(index), "Settings")
|
||||
return
|
||||
|
||||
if index >= language_codes.size():
|
||||
DebugManager.log_error("Language index out of bounds: %d (max: %d)" % [index, language_codes.size() - 1], "Settings")
|
||||
return
|
||||
|
||||
var selected_lang = language_codes[index]
|
||||
if not selected_lang or selected_lang.is_empty():
|
||||
DebugManager.log_error("Empty or null language code at index " + str(index), "Settings")
|
||||
return
|
||||
|
||||
if not settings_manager.set_setting("language", selected_lang):
|
||||
DebugManager.log_error("Failed to set language setting: " + selected_lang, "Settings")
|
||||
return
|
||||
|
||||
DebugManager.log_info("Language changed to: " + selected_lang, "Settings")
|
||||
localization_manager.change_language(selected_lang)
|
||||
|
||||
func update_text():
|
||||
$SettingsContainer/SettingsTitle.text = tr("settings_title")
|
||||
|
||||
Reference in New Issue
Block a user