From 8c5404f99cf413116bf637b82dcfcd466ab93d1f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 14 Nov 2016 23:32:38 -0500 Subject: fix error when json file doesn't exist (#170) --- src/StardewModdingAPI/ModHelper.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index bb753170..52cabff0 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -60,12 +60,24 @@ namespace StardewModdingAPI /// Read a JSON file. /// The model type. /// The file path relative to the mod directory. + /// Returns the deserialised model, or null if the file doesn't exist or is empty. public TModel ReadJsonFile(string path) where TModel : class { + // read file string fullPath = Path.Combine(this.DirectoryPath, path); - TModel model = JsonConvert.DeserializeObject(File.ReadAllText(fullPath)); + string json; + try + { + json = File.ReadAllText(fullPath); + } + catch (FileNotFoundException) + { + return null; + } + // deserialise model + TModel model = JsonConvert.DeserializeObject(json); if (model is IConfigFile) { var wrapper = (IConfigFile)model; -- cgit