aboutsummaryrefslogtreecommitdiff
path: root/logic/stage.gd
diff options
context:
space:
mode:
Diffstat (limited to 'logic/stage.gd')
-rw-r--r--logic/stage.gd41
1 files changed, 25 insertions, 16 deletions
diff --git a/logic/stage.gd b/logic/stage.gd
index 892dee4..c0583b0 100644
--- a/logic/stage.gd
+++ b/logic/stage.gd
@@ -5,6 +5,7 @@ signal save # (database_entry: Dictionary)
signal discard # ()
const OPTION_SETS_FILE_PATH: String = "user://option_sets.json"
+const OPTION_SETS_FILE_VERSION: int = 1
const OPTION_SETS_NOT_AVAILABLE: String = "--"
const OPTION_SETS_TREE_STRUCTURE := {
"place": null,
@@ -93,7 +94,7 @@ func save_action():
self.visible = false
var staged_entry := get_stage()
gather_option_sets(staged_entry)
- store_option_sets()
+ save_option_sets()
emit_signal("save", staged_entry)
@@ -188,35 +189,43 @@ func gather_option_sets(entry: Dictionary, target: Dictionary = option_sets, blu
gather_option_sets(entry, target[key][value], blueprint[key])
-func import_option_sets(file_path: String = OPTION_SETS_FILE_PATH):
- load_option_sets(file_path)
- sanitize_option_sets(option_sets)
- store_option_sets()
-
-
func load_option_sets(file_path: String = OPTION_SETS_FILE_PATH):
var file := File.new()
file.open(file_path, File.READ_WRITE)
var file_content = file.get_as_text()
file.close()
var parse_result = JSON.parse(file_content)
- if parse_result.error == OK && typeof(parse_result.result) == TYPE_DICTIONARY:
- option_sets = parse_result.result
- else:
- option_sets = {}
+
+ if parse_result.error != OK || typeof(parse_result.result) != TYPE_DICTIONARY:
push_error("Failed to parse option sets file: '%s'.")
+ return
+
+ if parse_result.result["version"] != OPTION_SETS_FILE_VERSION:
+ push_error("Invalid option sets file version '%s', expected '%s'." % OPTION_SETS_FILE_VERSION)
+ return
+
+ if typeof(parse_result.result["option_sets"]) != TYPE_DICTIONARY:
+ push_error("Failed to load option sets file contents.")
+ return
+
+ option_sets = parse_result.result["option_sets"]
-func store_option_sets(file_path: String = OPTION_SETS_FILE_PATH):
+func save_option_sets(file_path: String = OPTION_SETS_FILE_PATH):
+ var option_sets_file := {
+ "version": OPTION_SETS_FILE_VERSION,
+ "option_sets": option_sets,
+ }
+ var indentation_char := "" if file_path == OPTION_SETS_FILE_PATH else "\t"
+ var file_content := JSON.print(option_sets_file, indentation_char)
+
var file := File.new()
file.open(file_path, File.WRITE)
- file.store_string(JSON.print(option_sets, "" if file_path == OPTION_SETS_FILE_PATH else "\t"))
+ file.store_string(file_content)
file.close()
-func clear_option_sets(save_changes: bool = false):
+func clear_option_sets():
option_sets = {}
- if save_changes:
- store_option_sets()