diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-14 23:32:38 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-14 23:32:38 -0500 |
commit | 8c5404f99cf413116bf637b82dcfcd466ab93d1f (patch) | |
tree | 35b2fa9e666fd93f3cfeb4bc4340a404ef78481c | |
parent | 33e6a6db23878d998a60757ae60789f21718673e (diff) | |
download | SMAPI-8c5404f99cf413116bf637b82dcfcd466ab93d1f.tar.gz SMAPI-8c5404f99cf413116bf637b82dcfcd466ab93d1f.tar.bz2 SMAPI-8c5404f99cf413116bf637b82dcfcd466ab93d1f.zip |
fix error when json file doesn't exist (#170)
-rw-r--r-- | src/StardewModdingAPI/ModHelper.cs | 14 |
1 files changed, 13 insertions, 1 deletions
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 /// <summary>Read a JSON file.</summary> /// <typeparam name="TModel">The model type.</typeparam> /// <param name="path">The file path relative to the mod directory.</param> + /// <returns>Returns the deserialised model, or <c>null</c> if the file doesn't exist or is empty.</returns> public TModel ReadJsonFile<TModel>(string path) where TModel : class { + // read file string fullPath = Path.Combine(this.DirectoryPath, path); - TModel model = JsonConvert.DeserializeObject<TModel>(File.ReadAllText(fullPath)); + string json; + try + { + json = File.ReadAllText(fullPath); + } + catch (FileNotFoundException) + { + return null; + } + // deserialise model + TModel model = JsonConvert.DeserializeObject<TModel>(json); if (model is IConfigFile) { var wrapper = (IConfigFile)model; |