Files
mortal-maze-the-game/tests/runtime_test_profile.gd

32 lines
1.4 KiB
GDScript

extends RefCounted
## Aisla todas las suites del perfil del jugador. Nunca escribe ni borra su guardado.
## Funciona tanto si el runner cargo autoloads como si usa un SceneTree independiente.
static func prepare(tree: SceneTree) -> PlayerProgressStore:
var path: String = "user://__test_progress_%d_%d.json" % [OS.get_process_id(), Time.get_ticks_usec()]
var profile: PlayerProgressStore = tree.root.get_node_or_null("PlayerProgress") as PlayerProgressStore
if profile == null:
# La carga diferida evita precargar el mismo PackedScene antes de que Godot
# inicialice su autoload cuando la suite arranca mediante --script.
var profile_scene: PackedScene = load("res://game/player_progress.tscn") as PackedScene
profile = profile_scene.instantiate() as PlayerProgressStore
profile.name = "PlayerProgress"
profile.save_path = path
tree.root.add_child(profile)
else:
profile.save_path = path
profile.set("_owned_ids", {})
profile.set("_save_blocked", false)
profile.set("_primary_is_valid", false)
profile.last_error = ""
profile.tree_exiting.connect(_remove_test_files.bind(path))
return profile
static func _remove_test_files(path: String) -> void:
if not path.begins_with("user://__test_progress_"):
return
for suffix: String in ["", ".tmp", ".bak"]:
if FileAccess.file_exists(path + suffix):
DirAccess.remove_absolute(ProjectSettings.globalize_path(path + suffix))