lint fixes
Some checks failed
Continuous Integration / Code Formatting (pull_request) Successful in 27s
Continuous Integration / Code Quality Check (pull_request) Successful in 29s
Continuous Integration / Test Execution (pull_request) Failing after 33s
Continuous Integration / CI Summary (pull_request) Failing after 5s
Some checks failed
Continuous Integration / Code Formatting (pull_request) Successful in 27s
Continuous Integration / Code Quality Check (pull_request) Successful in 29s
Continuous Integration / Test Execution (pull_request) Failing after 33s
Continuous Integration / CI Summary (pull_request) Failing after 5s
This commit is contained in:
@@ -131,43 +131,64 @@ func set_setting(key: String, value) -> bool:
|
||||
func _validate_setting_value(key: String, value) -> bool:
|
||||
match key:
|
||||
"master_volume", "music_volume", "sfx_volume":
|
||||
# Enhanced numeric validation with NaN/Infinity checks
|
||||
if not (value is float or value is int):
|
||||
return false
|
||||
# Convert to float for validation
|
||||
var float_value = float(value)
|
||||
# Check for NaN and infinity
|
||||
if is_nan(float_value) or is_inf(float_value):
|
||||
DebugManager.log_warn(
|
||||
"Invalid float value for %s: %s" % [key, str(value)], "SettingsManager"
|
||||
)
|
||||
return false
|
||||
# Range validation
|
||||
return float_value >= 0.0 and float_value <= 1.0
|
||||
return _validate_volume_setting(key, value)
|
||||
"language":
|
||||
if not value is String:
|
||||
return false
|
||||
# Prevent extremely long strings
|
||||
if value.length() > MAX_SETTING_STRING_LENGTH:
|
||||
DebugManager.log_warn(
|
||||
"Language code too long: %d characters" % value.length(), "SettingsManager"
|
||||
)
|
||||
return false
|
||||
# Check for valid characters (alphanumeric and common separators only)
|
||||
var regex = RegEx.new()
|
||||
regex.compile("^[a-zA-Z0-9_-]+$")
|
||||
if not regex.search(value):
|
||||
DebugManager.log_warn(
|
||||
"Language code contains invalid characters: %s" % value, "SettingsManager"
|
||||
)
|
||||
return false
|
||||
# Check if language is supported
|
||||
if languages_data.has("languages") and languages_data.languages is Dictionary:
|
||||
return value in languages_data.languages
|
||||
else:
|
||||
# Fallback to basic validation if languages not loaded
|
||||
return value in ["en", "ru"]
|
||||
return _validate_language_setting(value)
|
||||
_:
|
||||
return _validate_default_setting(key, value)
|
||||
|
||||
|
||||
func _validate_volume_setting(key: String, value) -> bool:
|
||||
## Validate volume settings with numeric validation.
|
||||
##
|
||||
## Validates audio volume values are numbers within range (0.0 to 1.0).
|
||||
## Handles edge cases like NaN and infinity values.
|
||||
##
|
||||
## Args:
|
||||
## key: The setting key being validated (for error reporting)
|
||||
## value: The volume value to validate
|
||||
##
|
||||
## Returns:
|
||||
## bool: True if the value is a valid volume setting, False otherwise
|
||||
if not (value is float or value is int):
|
||||
return false
|
||||
# Convert to float for validation
|
||||
var float_value = float(value)
|
||||
# Check for NaN and infinity
|
||||
if is_nan(float_value) or is_inf(float_value):
|
||||
DebugManager.log_warn(
|
||||
"Invalid float value for %s: %s" % [key, str(value)], "SettingsManager"
|
||||
)
|
||||
return false
|
||||
# Range validation
|
||||
return float_value >= 0.0 and float_value <= 1.0
|
||||
|
||||
|
||||
func _validate_language_setting(value) -> bool:
|
||||
if not value is String:
|
||||
return false
|
||||
# Prevent extremely long strings
|
||||
if value.length() > MAX_SETTING_STRING_LENGTH:
|
||||
DebugManager.log_warn(
|
||||
"Language code too long: %d characters" % value.length(), "SettingsManager"
|
||||
)
|
||||
return false
|
||||
# Check for valid characters (alphanumeric and common separators only)
|
||||
var regex = RegEx.new()
|
||||
regex.compile("^[a-zA-Z0-9_-]+$")
|
||||
if not regex.search(value):
|
||||
DebugManager.log_warn(
|
||||
"Language code contains invalid characters: %s" % value, "SettingsManager"
|
||||
)
|
||||
return false
|
||||
# Check if language is supported
|
||||
if languages_data.has("languages") and languages_data.languages is Dictionary:
|
||||
return value in languages_data.languages
|
||||
# Fallback to basic validation if languages not loaded
|
||||
return value in ["en", "ru"]
|
||||
|
||||
|
||||
func _validate_default_setting(key: String, value) -> bool:
|
||||
# Default validation: accept if type matches default setting type
|
||||
var default_value = default_settings.get(key)
|
||||
if default_value == null:
|
||||
@@ -193,14 +214,34 @@ func _apply_setting_side_effect(key: String, value) -> void:
|
||||
|
||||
|
||||
func load_languages():
|
||||
var file_content = _load_languages_file()
|
||||
if file_content.is_empty():
|
||||
_load_default_languages_with_fallback("File loading failed")
|
||||
return
|
||||
|
||||
var parsed_data = _parse_languages_json(file_content)
|
||||
if not parsed_data:
|
||||
_load_default_languages_with_fallback("JSON parsing failed")
|
||||
return
|
||||
|
||||
if not _validate_languages_structure(parsed_data):
|
||||
_load_default_languages_with_fallback("Structure validation failed")
|
||||
return
|
||||
|
||||
languages_data = parsed_data
|
||||
DebugManager.log_info(
|
||||
"Languages loaded successfully: " + str(languages_data.languages.keys()), "SettingsManager"
|
||||
)
|
||||
|
||||
|
||||
func _load_languages_file() -> String:
|
||||
var file = FileAccess.open(LANGUAGES_JSON_PATH, FileAccess.READ)
|
||||
if not file:
|
||||
var error_code = FileAccess.get_open_error()
|
||||
DebugManager.log_error(
|
||||
"Could not open languages.json (Error code: %d)" % error_code, "SettingsManager"
|
||||
)
|
||||
_load_default_languages()
|
||||
return
|
||||
return ""
|
||||
|
||||
# Check file size to prevent memory exhaustion
|
||||
var file_size = file.get_length()
|
||||
@@ -210,14 +251,12 @@ func load_languages():
|
||||
"SettingsManager"
|
||||
)
|
||||
file.close()
|
||||
_load_default_languages()
|
||||
return
|
||||
return ""
|
||||
|
||||
if file_size == 0:
|
||||
DebugManager.log_error("Languages.json file is empty", "SettingsManager")
|
||||
file.close()
|
||||
_load_default_languages()
|
||||
return
|
||||
return ""
|
||||
|
||||
var json_string = file.get_as_text()
|
||||
var file_error = file.get_error()
|
||||
@@ -227,14 +266,16 @@ func load_languages():
|
||||
DebugManager.log_error(
|
||||
"Error reading languages.json (Error code: %d)" % file_error, "SettingsManager"
|
||||
)
|
||||
_load_default_languages()
|
||||
return
|
||||
return ""
|
||||
|
||||
return json_string
|
||||
|
||||
|
||||
func _parse_languages_json(json_string: String) -> Dictionary:
|
||||
# Validate the JSON string is not empty
|
||||
if json_string.is_empty():
|
||||
DebugManager.log_error("Languages.json contains empty content", "SettingsManager")
|
||||
_load_default_languages()
|
||||
return
|
||||
return {}
|
||||
|
||||
var json = JSON.new()
|
||||
var parse_result = json.parse(json_string)
|
||||
@@ -243,24 +284,18 @@ func load_languages():
|
||||
"JSON parsing failed at line %d: %s" % [json.error_line, json.error_string],
|
||||
"SettingsManager"
|
||||
)
|
||||
_load_default_languages()
|
||||
return
|
||||
return {}
|
||||
|
||||
if not json.data or not json.data is Dictionary:
|
||||
DebugManager.log_error("Invalid JSON data structure in languages.json", "SettingsManager")
|
||||
_load_default_languages()
|
||||
return
|
||||
return {}
|
||||
|
||||
# Validate the structure of the JSON data
|
||||
if not _validate_languages_structure(json.data):
|
||||
DebugManager.log_error("Languages.json structure validation failed", "SettingsManager")
|
||||
_load_default_languages()
|
||||
return
|
||||
return json.data
|
||||
|
||||
languages_data = json.data
|
||||
DebugManager.log_info(
|
||||
"Languages loaded successfully: " + str(languages_data.languages.keys()), "SettingsManager"
|
||||
)
|
||||
|
||||
func _load_default_languages_with_fallback(reason: String):
|
||||
DebugManager.log_warn("Loading default languages due to: " + reason, "SettingsManager")
|
||||
_load_default_languages()
|
||||
|
||||
|
||||
func _load_default_languages():
|
||||
@@ -289,7 +324,25 @@ func reset_settings_to_defaults() -> void:
|
||||
|
||||
|
||||
func _validate_languages_structure(data: Dictionary) -> bool:
|
||||
"""Validate the structure and content of languages.json data"""
|
||||
## Validate the structure and content of languages.json data.
|
||||
##
|
||||
## Validates language data loaded from the languages.json file.
|
||||
## Ensures the data structure is valid and contains required fields.
|
||||
##
|
||||
## Args:
|
||||
## data: Dictionary containing the parsed languages.json data
|
||||
##
|
||||
## Returns:
|
||||
## bool: True if data structure is valid, False if validation fails
|
||||
if not _validate_languages_root_structure(data):
|
||||
return false
|
||||
|
||||
var languages = data["languages"]
|
||||
return _validate_individual_languages(languages)
|
||||
|
||||
|
||||
func _validate_languages_root_structure(data: Dictionary) -> bool:
|
||||
"""Validate the root structure of languages data"""
|
||||
if not data.has("languages"):
|
||||
DebugManager.log_error("Languages.json missing 'languages' key", "SettingsManager")
|
||||
return false
|
||||
@@ -303,30 +356,40 @@ func _validate_languages_structure(data: Dictionary) -> bool:
|
||||
DebugManager.log_error("Languages dictionary is empty", "SettingsManager")
|
||||
return false
|
||||
|
||||
# Validate each language entry
|
||||
return true
|
||||
|
||||
|
||||
func _validate_individual_languages(languages: Dictionary) -> bool:
|
||||
"""Validate each individual language entry"""
|
||||
for lang_code in languages.keys():
|
||||
if not lang_code is String:
|
||||
DebugManager.log_error(
|
||||
"Language code is not a string: %s" % str(lang_code), "SettingsManager"
|
||||
)
|
||||
if not _validate_single_language_entry(lang_code, languages[lang_code]):
|
||||
return false
|
||||
return true
|
||||
|
||||
if lang_code.length() > MAX_SETTING_STRING_LENGTH:
|
||||
DebugManager.log_error("Language code too long: %s" % lang_code, "SettingsManager")
|
||||
return false
|
||||
|
||||
var lang_data = languages[lang_code]
|
||||
if not lang_data is Dictionary:
|
||||
DebugManager.log_error(
|
||||
"Language data for '%s' is not a dictionary" % lang_code, "SettingsManager"
|
||||
)
|
||||
return false
|
||||
func _validate_single_language_entry(lang_code: Variant, lang_data: Variant) -> bool:
|
||||
"""Validate a single language entry"""
|
||||
if not lang_code is String:
|
||||
DebugManager.log_error(
|
||||
"Language code is not a string: %s" % str(lang_code), "SettingsManager"
|
||||
)
|
||||
return false
|
||||
|
||||
# Validate required fields in language data
|
||||
if not lang_data.has("name") or not lang_data["name"] is String:
|
||||
DebugManager.log_error(
|
||||
"Language '%s' missing valid 'name' field" % lang_code, "SettingsManager"
|
||||
)
|
||||
return false
|
||||
if lang_code.length() > MAX_SETTING_STRING_LENGTH:
|
||||
DebugManager.log_error("Language code too long: %s" % lang_code, "SettingsManager")
|
||||
return false
|
||||
|
||||
if not lang_data is Dictionary:
|
||||
DebugManager.log_error(
|
||||
"Language data for '%s' is not a dictionary" % lang_code, "SettingsManager"
|
||||
)
|
||||
return false
|
||||
|
||||
# Validate required fields in language data
|
||||
if not lang_data.has("name") or not lang_data["name"] is String:
|
||||
DebugManager.log_error(
|
||||
"Language '%s' missing valid 'name' field" % lang_code, "SettingsManager"
|
||||
)
|
||||
return false
|
||||
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user