diff options
| author | dam <dam@gudinoff> | 2022-04-06 22:52:14 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-04-06 22:52:14 +0000 |
| commit | 1fe674aec4bd6ce3fc666c8545dadb4f0614067d (patch) | |
| tree | 949a5ff1380bf82edaf387a5b4e04d0a4bf9d46a /logic/stage.gd | |
| parent | 0f1adc3bb1f41b5dd3490f176a5eee2c17007923 (diff) | |
| download | surgery-log-1fe674aec4bd6ce3fc666c8545dadb4f0614067d.tar.zst surgery-log-1fe674aec4bd6ce3fc666c8545dadb4f0614067d.zip | |
Improved file operations for database and option sets.
Added confirmation dialogs when importing database and option sets files.
Standardized the error print calls.
Fixed capitalization on menu entries.
Diffstat (limited to 'logic/stage.gd')
| -rw-r--r-- | logic/stage.gd | 85 |
1 files changed, 63 insertions, 22 deletions
diff --git a/logic/stage.gd b/logic/stage.gd index e2248fa..764ee52 100644 --- a/logic/stage.gd +++ b/logic/stage.gd @@ -105,7 +105,7 @@ func show_options(field: String): func save_action(): self.visible = false var staged_entry := get_stage() - gather_option_sets(staged_entry) + gather_option_sets(staged_entry, option_sets) save_option_sets() emit_signal("save", staged_entry) @@ -164,7 +164,7 @@ func get_stage() -> Dictionary: return entry -func sanitize_option_sets(entry: Dictionary, blueprint: Dictionary = OPTION_SETS_TREE_STRUCTURE): +static func sanitize_option_sets(entry: Dictionary, blueprint: Dictionary = OPTION_SETS_TREE_STRUCTURE): # Delete extra keys. var keys_to_delete: Array for key in entry: @@ -185,7 +185,7 @@ func sanitize_option_sets(entry: Dictionary, blueprint: Dictionary = OPTION_SETS sanitize_option_sets(entry[key][sub_key], blueprint[key]) -func gather_option_sets(entry: Dictionary, target: Dictionary = option_sets, blueprint: Dictionary = OPTION_SETS_TREE_STRUCTURE): +static func gather_option_sets(entry: Dictionary, target: Dictionary, blueprint: Dictionary = OPTION_SETS_TREE_STRUCTURE): for key in blueprint: if target.get(key) == null: target[key] = {} @@ -201,7 +201,55 @@ func gather_option_sets(entry: Dictionary, target: Dictionary = option_sets, blu gather_option_sets(entry, target[key][value], blueprint[key]) +func save_option_sets(file_path: String = OPTION_SETS_FILE_PATH): + match file_path.get_extension(): + "json": + export_json(file_path, option_sets) + +# "csv": +# export_csv(file_path, option_sets) + + _: + printerr("Invalid option sets file extension: '%s'." % file_path.get_file()) + return + + +static func export_json(file_path: String, data: Dictionary): + var option_sets_file := { + "version": OPTION_SETS_FILE_VERSION, + "option_sets": data, + } + 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(file_content) + file.close() + + +#static func export_csv(file_path: String, data: Dictionary): +# pass + + func load_option_sets(file_path: String = OPTION_SETS_FILE_PATH): + match file_path.get_extension(): + "json": + clear_option_sets() + option_sets = import_json(file_path) + if file_path != OPTION_SETS_FILE_PATH: + sanitize_option_sets(option_sets) + + "csv": + clear_option_sets() + option_sets = import_csv(file_path) + + _: + printerr("Invalid option sets file extension: '%s'." % file_path.get_file()) + return + + +static func import_json(file_path: String) -> Dictionary: var file := File.new() file.open(file_path, File.READ_WRITE) var file_content = file.get_as_text() @@ -209,32 +257,25 @@ func load_option_sets(file_path: String = OPTION_SETS_FILE_PATH): var parse_result = JSON.parse(file_content) if parse_result.error != OK || typeof(parse_result.result) != TYPE_DICTIONARY: - push_error("Failed to parse option sets file: '%s'.") - return + printerr("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 + printerr("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 + printerr("Failed to load option sets file contents.") + return {} - option_sets = parse_result.result["option_sets"] + return parse_result.result["option_sets"] -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(file_content) - file.close() +static func import_csv(file_path: String) -> Dictionary: + var result := {} + for it in Database.import_csv(file_path): + gather_option_sets(it, result) + return result func clear_option_sets(): |
