diff options
| author | dam <dam@gudinoff> | 2022-04-16 15:14:29 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-04-16 15:14:29 +0000 |
| commit | 7348ca93ccc1f4a55438d59430f7f870c08b56d6 (patch) | |
| tree | b26fb7e0b446cf7a828a29ad223e8d961e9d4af1 /logic/stage.gd | |
| parent | 08b6f5a76c530b83acef39ec14f6d3b1e508964c (diff) | |
| download | surgery-log-7348ca93ccc1f4a55438d59430f7f870c08b56d6.tar.zst surgery-log-7348ca93ccc1f4a55438d59430f7f870c08b56d6.zip | |
Added details to error messages when loading JSON files for database and option sets.
Diffstat (limited to 'logic/stage.gd')
| -rw-r--r-- | logic/stage.gd | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/logic/stage.gd b/logic/stage.gd index 747cccf..e6d9474 100644 --- a/logic/stage.gd +++ b/logic/stage.gd @@ -5,7 +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_FILE_VERSION: String = "SL_OS_V1" const OPTION_SETS_NOT_AVAILABLE: String = "--" const OPTION_SETS_TREE_STRUCTURE := { "place": null, @@ -210,7 +210,7 @@ func save_option_sets(file_path: String = OPTION_SETS_FILE_PATH): # export_csv(file_path, option_sets) _: - printerr("Invalid option sets file extension: '%s'." % file_path.get_file()) + printerr("Invalid option sets file extension '%s', expected 'json'." % file_path.get_file()) return @@ -245,30 +245,40 @@ func load_option_sets(file_path: String = OPTION_SETS_FILE_PATH): option_sets = import_csv(file_path) _: - printerr("Invalid option sets file extension: '%s'." % file_path.get_file()) + printerr("Invalid option sets file extension '%s', expected 'json' or 'csv'." % file_path.get_file()) return static func import_json(file_path: String) -> Dictionary: + var result := {} + var file := File.new() - file.open(file_path, File.READ_WRITE) + var error := file.open(file_path, File.READ_WRITE) + if error != OK: + printerr("Failed to open option sets file '%s' (error %d)." % [file_path, error]) + return result 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: - printerr("Failed to parse option sets file: '%s'.") - return {} + if parse_result.error != OK: + printerr("Failed to parse option sets file '%s' (error %d)." % [file_path, parse_result.error]) + return result + + if typeof(parse_result.result) != TYPE_DICTIONARY: + printerr("Invalid option sets file type '%s', expected '%s'." % [typeof(parse_result.result), TYPE_DICTIONARY]) + return result if parse_result.result["version"] != OPTION_SETS_FILE_VERSION: - printerr("Invalid option sets file version '%s', expected '%s'." % OPTION_SETS_FILE_VERSION) - return {} + printerr("Invalid option sets file version '%s', expected '%s'." % [parse_result.result["version"], OPTION_SETS_FILE_VERSION]) + return result if typeof(parse_result.result["option_sets"]) != TYPE_DICTIONARY: - printerr("Failed to load option sets file contents.") - return {} + printerr("Invalid option sets content type '%s', expected '%s'." % [typeof(parse_result.result["option_sets"]), TYPE_DICTIONARY]) + return result - return parse_result.result["option_sets"] + result = parse_result.result["option_sets"] + return result static func import_csv(file_path: String) -> Dictionary: |
