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:
2025-09-25 00:47:08 +04:00
parent bbf512b675
commit 742e4251fb
11 changed files with 914 additions and 442 deletions

View File

@@ -1,129 +1,36 @@
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
var match3_scene: Node2D
extends DebugMenuBase
func _ready():
DebugManager.log_debug("Match3DebugMenu _ready() called", "Match3")
DebugManager.debug_toggled.connect(_on_debug_toggled)
# Initialize with current debug state
# Set specific configuration for Match3DebugMenu
log_category = "Match3"
target_script_path = "res://scenes/game/gameplays/match3_gameplay.gd"
# Call parent's _ready
super._ready()
DebugManager.log_debug("Match3DebugMenu _ready() completed", log_category)
# Initialize with current debug state if enabled
var current_debug_state = DebugManager.is_debug_enabled()
DebugManager.log_debug("Match3DebugMenu current debug state is " + str(current_debug_state), "Match3")
visible = current_debug_state
DebugManager.log_debug("Match3DebugMenu initial visibility set to " + str(visible), "Match3")
if current_debug_state:
_on_debug_toggled(true)
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)
func _find_match3_scene():
func _find_target_scene():
# Debug menu is now: Match3 -> UILayer -> Match3DebugMenu
# So we need to go up two levels: get_parent() = UILayer, get_parent().get_parent() = Match3
var ui_layer = get_parent()
if ui_layer and ui_layer is CanvasLayer:
match3_scene = ui_layer.get_parent()
if match3_scene and match3_scene.get_script():
var script_path = match3_scene.get_script().resource_path
if script_path == "res://scenes/game/gameplays/match3_gameplay.gd":
DebugManager.log_debug("Found match3 scene: " + match3_scene.name + " at path: " + str(match3_scene.get_path()), "Match3")
var potential_match3 = ui_layer.get_parent()
if potential_match3 and potential_match3.get_script():
var script_path = potential_match3.get_script().resource_path
if script_path == target_script_path:
match3_scene = potential_match3
DebugManager.log_debug("Found match3 scene: " + match3_scene.name + " at path: " + str(match3_scene.get_path()), log_category)
_update_ui_from_scene()
_stop_search_timer()
return
# If we couldn't find it, clear the reference
# If we couldn't find it, clear the reference and continue searching
match3_scene = null
DebugManager.log_error("Could not find match3_gameplay scene", "Match3")
func _on_debug_toggled(enabled: bool):
DebugManager.log_debug("Match3DebugMenu debug toggled to " + str(enabled), "Match3")
visible = enabled
DebugManager.log_debug("Match3DebugMenu visibility set to " + str(visible), "Match3")
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", "Match3")
return
if match3_scene.has_method("regenerate_grid"):
DebugManager.log_debug("Calling regenerate_grid()", "Match3")
await match3_scene.regenerate_grid()
else:
DebugManager.log_error("match3_scene does not have regenerate_grid method", "Match3")
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", "Match3")
return
var new_value = int(value)
if match3_scene.has_method("set_tile_types"):
DebugManager.log_debug("Setting tile types to " + str(new_value), "Match3")
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", "Match3")
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", "Match3")
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), "Match3")
await match3_scene.set_grid_size(Vector2i(new_width, current_height))
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", "Match3")
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), "Match3")
await match3_scene.set_grid_size(Vector2i(current_width, new_height))
DebugManager.log_error("Could not find match3_gameplay scene", log_category)
_start_search_timer()