aboutsummaryrefslogtreecommitdiff
path: root/logic
diff options
context:
space:
mode:
authordam <dam@gudinoff>2022-04-16 15:14:29 +0000
committerdam <dam@gudinoff>2022-04-16 15:14:29 +0000
commit7348ca93ccc1f4a55438d59430f7f870c08b56d6 (patch)
treeb26fb7e0b446cf7a828a29ad223e8d961e9d4af1 /logic
parent08b6f5a76c530b83acef39ec14f6d3b1e508964c (diff)
downloadsurgery-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')
-rw-r--r--logic/database.gd34
-rw-r--r--logic/stage.gd34
2 files changed, 44 insertions, 24 deletions
diff --git a/logic/database.gd b/logic/database.gd
index 04f617f..ed80ae7 100644
--- a/logic/database.gd
+++ b/logic/database.gd
@@ -2,7 +2,7 @@ extends TouchItemList
class_name Database
const DATABASE_FILE_PATH: String = "user://database.json"
-const DATABASE_FILE_VERSION: int = 1
+const DATABASE_FILE_VERSION: String = "SL_DB_V1"
var db: Array
var selected_idx: int
@@ -130,7 +130,7 @@ func save_database(file_path: String = DATABASE_FILE_PATH):
export_csv(file_path, db)
_:
- printerr("Invalid database file extension: '%s'." % file_path.get_file())
+ printerr("Invalid database file extension '%s', expected 'json' or 'csv'." % file_path.get_file())
return
@@ -171,7 +171,7 @@ func load_database(file_path: String = DATABASE_FILE_PATH):
db = import_csv(file_path)
_:
- printerr("Invalid database file extension: '%s'." % file_path.get_file())
+ printerr("Invalid database file extension '%s', expected 'json' or 'csv'." % file_path.get_file())
return
for it in db:
@@ -204,25 +204,35 @@ static func sanitize_database(database: Array):
static func import_json(file_path: String) -> Array:
+ 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 database 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 database file: '%s'.")
- return []
+ if parse_result.error != OK:
+ printerr("Failed to parse database file '%s' (error %d)." % [file_path, parse_result.error])
+ return result
+
+ if typeof(parse_result.result) != TYPE_DICTIONARY:
+ printerr("Invalid database file type '%s', expected '%s'." % [typeof(parse_result.result), TYPE_DICTIONARY])
+ return result
if parse_result.result["version"] != DATABASE_FILE_VERSION:
- printerr("Invalid database file version '%s', expected '%s'." % DATABASE_FILE_VERSION)
- return []
+ printerr("Invalid database file version '%s', expected '%s'." % [parse_result.result["version"], DATABASE_FILE_VERSION])
+ return result
if typeof(parse_result.result["database"]) != TYPE_ARRAY:
- printerr("Failed to load database file contents.")
- return []
+ printerr("Invalid database content type '%s', expected '%s'." % [typeof(parse_result.result["database"]), TYPE_ARRAY])
+ return result
- return parse_result.result["database"]
+ result = parse_result.result["database"]
+ return result
static func import_csv(file_path: String) -> Array:
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: