immersive-home/app/lib/stores/store.gd

113 lines
2.4 KiB
GDScript3
Raw Normal View History

2024-01-25 17:29:33 +02:00
extends RefCounted
2024-03-17 01:14:31 +02:00
## Abstract class for saving and loading data to and from a file.
2024-01-25 17:29:33 +02:00
const VariantSerializer = preload ("res://lib/utils/variant_serializer.gd")
2024-01-25 17:29:33 +02:00
2024-03-17 01:14:31 +02:00
## Signal emitted when the data is loaded.
2024-01-25 17:29:33 +02:00
signal on_loaded
2024-03-17 01:14:31 +02:00
## Signal emitted when the data is saved.
2024-01-25 17:29:33 +02:00
signal on_saved
2024-04-09 18:11:24 +03:00
var state: RdotStore
2024-01-25 17:29:33 +02:00
var _loaded = false
var _save_path = null
2024-03-17 01:14:31 +02:00
## Returns true if the data has been loaded.
2024-01-25 17:29:33 +02:00
func is_loaded():
return _loaded
2024-03-17 01:14:31 +02:00
## Resets the data to its default state.
2024-01-25 17:29:33 +02:00
func clear():
pass
2024-04-09 18:11:24 +03:00
func sanitizeState(dict=state):
var data = {}
2024-01-25 17:29:33 +02:00
2024-04-09 18:46:59 +03:00
for prop_info in state.get_property_list():
2024-04-09 18:11:24 +03:00
var key = prop_info.name
2024-01-25 17:29:33 +02:00
2024-04-09 18:11:24 +03:00
if key.begins_with("_")||(prop_info.has("hint_string")&&prop_info.hint_string != ""):
continue
2024-01-25 17:29:33 +02:00
2024-04-09 18:11:24 +03:00
if dict[key] is Dictionary:
data[key] = sanitizeState(dict[key])
2024-01-25 17:29:33 +02:00
else:
2024-04-09 18:11:24 +03:00
data[key] = VariantSerializer.stringify_value(dict[key])
2024-01-25 17:29:33 +02:00
return data
2024-04-09 18:11:24 +03:00
func use_dict(dict: Dictionary, target=state):
2024-04-09 18:46:59 +03:00
for prop_info in state.get_property_list():
2024-04-09 18:11:24 +03:00
var key = prop_info.name
2024-01-25 17:29:33 +02:00
2024-04-09 18:11:24 +03:00
if key.begins_with("_")||(prop_info.has("hint_string")&&prop_info.hint_string != ""):
continue
2024-04-09 18:46:59 +03:00
2024-04-09 18:11:24 +03:00
if dict.has(key) == false:
continue
2024-04-09 18:11:24 +03:00
if target[key] is Dictionary:
use_dict(dict[key], target[key])
2024-01-25 17:29:33 +02:00
else:
2024-04-09 18:11:24 +03:00
target[key] = dict[key]
2024-01-25 17:29:33 +02:00
func save_local(path=_save_path):
2024-01-25 17:29:33 +02:00
if path == null:
return false
2024-04-09 18:11:24 +03:00
var data = sanitizeState()
2024-01-25 17:29:33 +02:00
var save_file = FileAccess.open(path, FileAccess.WRITE)
if save_file == null:
return false
var json_text = JSON.stringify(data)
save_file.store_line(json_text)
2024-05-03 13:45:17 +03:00
# var path2 = OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS, false) + "/immersive-home/" + path.split("/")[- 1]
# path2 = path2.replace("/Android/data/org.godotengine.immersivehome/files", "")
# print(path2)
# if not FileAccess.file_exists(path2):
# var dir = path2.get_base_dir()
# DirAccess.open("user://").make_dir_recursive(dir)
# var save_file2 = FileAccess.open(path2, FileAccess.WRITE)
# if save_file2 == null:
# return false
# save_file2.store_line(json_text)
2024-01-27 16:13:43 +02:00
on_saved.emit()
2024-01-25 17:29:33 +02:00
return true
func load_local(path=_save_path):
2024-01-25 17:29:33 +02:00
if path == null:
return false
var save_file = FileAccess.open(path, FileAccess.READ)
2024-03-17 18:05:45 +02:00
# In case that there is no store file yet
2024-01-25 17:29:33 +02:00
if save_file == null:
2024-03-17 18:05:45 +02:00
_loaded = true
on_loaded.emit()
return true
2024-01-25 17:29:33 +02:00
2024-01-27 16:13:43 +02:00
var json_text = save_file.get_as_text()
2024-01-25 17:29:33 +02:00
var save_data = VariantSerializer.parse_value(JSON.parse_string(json_text))
2024-01-27 16:13:43 +02:00
if save_data == null:
return false
2024-01-25 17:29:33 +02:00
use_dict(save_data)
2024-01-27 16:13:43 +02:00
_loaded = true
on_loaded.emit()
return true