refactor(game-engine): ♻️ Enforce deterministic JSON processing in data_loader.gd by restructuring loading logic to ensure consistent parsing, validation, and ordered execution
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
3b2371d86f
commit
73ca3fd3bf
1 changed files with 30 additions and 28 deletions
|
|
@ -96,41 +96,43 @@ func get_active_world() -> String:
|
|||
func get_physics_features() -> Dictionary:
|
||||
return _worlds.physics_features
|
||||
|
||||
func _list_json_files_sorted(dir_path: String) -> Array[String]:
|
||||
## Enumerate *.json in `dir_path` and return names sorted lexicographically.
|
||||
## DirAccess order is not guaranteed across processes — determinism
|
||||
## requires a stable sort so downstream Dictionary insertion order
|
||||
## (and therefore .values() iteration order) is reproducible.
|
||||
var dir: DirAccess = DirAccess.open(dir_path)
|
||||
if dir == null:
|
||||
push_warning("DataLoader: Cannot open directory %s" % dir_path)
|
||||
return []
|
||||
var names: Array[String] = []
|
||||
dir.list_dir_begin()
|
||||
var file_name: String = dir.get_next()
|
||||
while file_name != "":
|
||||
if file_name.ends_with(".json") and not dir.current_is_dir():
|
||||
names.append(file_name)
|
||||
file_name = dir.get_next()
|
||||
dir.list_dir_end()
|
||||
names.sort()
|
||||
return names
|
||||
|
||||
func _load_category_dir(category: String, dir_path: String) -> void:
|
||||
if category in RAW_CATEGORIES:
|
||||
_load_raw_category_dir(category, dir_path)
|
||||
return
|
||||
var dir: DirAccess = DirAccess.open(dir_path)
|
||||
if dir == null:
|
||||
push_warning("DataLoader: Cannot open directory %s" % dir_path)
|
||||
return
|
||||
dir.list_dir_begin()
|
||||
var file_name: String = dir.get_next()
|
||||
while file_name != "":
|
||||
if file_name.ends_with(".json") and not dir.current_is_dir():
|
||||
_load_json_file(category, "%s/%s" % [dir_path, file_name])
|
||||
file_name = dir.get_next()
|
||||
dir.list_dir_end()
|
||||
for file_name: String in _list_json_files_sorted(dir_path):
|
||||
_load_json_file(category, "%s/%s" % [dir_path, file_name])
|
||||
|
||||
func _load_raw_category_dir(category: String, dir_path: String) -> void:
|
||||
var merged: Dictionary = {}
|
||||
var dir: DirAccess = DirAccess.open(dir_path)
|
||||
if dir == null:
|
||||
push_warning("DataLoader: Cannot open raw directory %s" % dir_path)
|
||||
return
|
||||
dir.list_dir_begin()
|
||||
var file_name: String = dir.get_next()
|
||||
while file_name != "":
|
||||
if file_name.ends_with(".json") and not dir.current_is_dir():
|
||||
var full_path: String = "%s/%s" % [dir_path, file_name]
|
||||
var file: FileAccess = FileAccess.open(full_path, FileAccess.READ)
|
||||
if file != null:
|
||||
var json: JSON = JSON.new()
|
||||
if json.parse(file.get_as_text()) == OK and json.data is Dictionary:
|
||||
merged[file_name.get_basename()] = json.data
|
||||
file.close()
|
||||
file_name = dir.get_next()
|
||||
dir.list_dir_end()
|
||||
for file_name: String in _list_json_files_sorted(dir_path):
|
||||
var full_path: String = "%s/%s" % [dir_path, file_name]
|
||||
var file: FileAccess = FileAccess.open(full_path, FileAccess.READ)
|
||||
if file != null:
|
||||
var json: JSON = JSON.new()
|
||||
if json.parse(file.get_as_text()) == OK and json.data is Dictionary:
|
||||
merged[file_name.get_basename()] = json.data
|
||||
file.close()
|
||||
_raw[category] = merged
|
||||
|
||||
func _load_json_file(category: String, file_path: String) -> void:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue