summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ContentPack.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2018-11-19 13:48:19 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2018-11-19 13:48:19 -0500
commit593723b7940ba72a786fc4c7366c56f9813d977b (patch)
tree4d23fbef5bc5a20115f10ca04ae3379df78cc8e1 /src/SMAPI/Framework/ContentPack.cs
parent4f28ea33bd7cc65485402c5e85259083e86b49e1 (diff)
parent3dc27a5681dcfc4ae30e95570d9966f2e14a4dd7 (diff)
downloadSMAPI-593723b7940ba72a786fc4c7366c56f9813d977b.tar.gz
SMAPI-593723b7940ba72a786fc4c7366c56f9813d977b.tar.bz2
SMAPI-593723b7940ba72a786fc4c7366c56f9813d977b.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/ContentPack.cs')
-rw-r--r--src/SMAPI/Framework/ContentPack.cs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/ContentPack.cs b/src/SMAPI/Framework/ContentPack.cs
index 62d8b80d..49285388 100644
--- a/src/SMAPI/Framework/ContentPack.cs
+++ b/src/SMAPI/Framework/ContentPack.cs
@@ -51,14 +51,32 @@ namespace StardewModdingAPI.Framework
/// <typeparam name="TModel">The model type.</typeparam>
/// <param name="path">The file path relative to the contnet directory.</param>
/// <returns>Returns the deserialised model, or <c>null</c> if the file doesn't exist or is empty.</returns>
+ /// <exception cref="InvalidOperationException">The <paramref name="path"/> is not relative or contains directory climbing (../).</exception>
public TModel ReadJsonFile<TModel>(string path) where TModel : class
{
+ if (!PathUtilities.IsSafeRelativePath(path))
+ throw new InvalidOperationException($"You must call {nameof(IContentPack)}.{nameof(this.ReadJsonFile)} with a relative path.");
+
path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path));
return this.JsonHelper.ReadJsonFileIfExists(path, out TModel model)
? model
: null;
}
+ /// <summary>Save data to a JSON file in the content pack's folder.</summary>
+ /// <typeparam name="TModel">The model type. This should be a plain class that has public properties for the data you want. The properties can be complex types.</typeparam>
+ /// <param name="path">The file path relative to the mod folder.</param>
+ /// <param name="data">The arbitrary data to save.</param>
+ /// <exception cref="InvalidOperationException">The <paramref name="path"/> is not relative or contains directory climbing (../).</exception>
+ public void WriteJsonFile<TModel>(string path, TModel data) where TModel : class
+ {
+ if (!PathUtilities.IsSafeRelativePath(path))
+ throw new InvalidOperationException($"You must call {nameof(IContentPack)}.{nameof(this.WriteJsonFile)} with a relative path.");
+
+ path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path));
+ this.JsonHelper.WriteJsonFile(path, data);
+ }
+
/// <summary>Load content from the content pack folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
/// <typeparam name="T">The expected data type. The main supported types are <see cref="Map"/>, <see cref="Texture2D"/>, and dictionaries; other types may be supported by the game's content pipeline.</typeparam>
/// <param name="key">The local path to a content file relative to the content pack folder.</param>