From 87775317a525bcbdede9d9d6b48e458a9faff5ef Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 24 May 2023 12:41:02 -0700 Subject: [PATCH] don't throw if json is unreadable just act like the file didn't exist (unless strict is set) --- docs/changelog.txt | 1 + library/lua/json.lua | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index df00cf34a..42ce711eb 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -51,6 +51,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Misc Improvements - `autodump`: no longer checks for a keyboard cursor before executing, so ``autodump destroy`` (which doesn't require a cursor) can still function +- Settings: recover gracefully when settings files become corrupted (e.g. by CTD) - `orders`: update orders in orders library for prepared meals, bins, archer uniforms, and weapons - Terminal console no longer appears in front of the game window on startup - `gui/control-panel`: new preference for whether filters in lists search for substrings in the middle of words (e.g. if set to true, then "ee" will match "steel") diff --git a/library/lua/json.lua b/library/lua/json.lua index 2b4b3a66a..6ba8483dd 100644 --- a/library/lua/json.lua +++ b/library/lua/json.lua @@ -59,7 +59,13 @@ function _file:read(strict) end else self.exists = true - self.data = decode_file(self.path) + local ok, err = pcall(function() self.data = decode_file(self.path) end) + if not ok then + if strict then + error(('cannot decode file: %s: %s'):format(self.path, err)) + end + self.data = {} + end end return self.data end