summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ContentPack.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2018-12-31 13:54:06 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-14 16:56:53 -0400
commite55295385b592f422d43f0b525cf3bea7be19b52 (patch)
tree50888c8f22b05db91d24ff62460ee70121619325 /src/SMAPI/Framework/ContentPack.cs
parent7e9cb99acb143bc34f93c08086b95af0f95639ef (diff)
downloadSMAPI-e55295385b592f422d43f0b525cf3bea7be19b52.tar.gz
SMAPI-e55295385b592f422d43f0b525cf3bea7be19b52.tar.bz2
SMAPI-e55295385b592f422d43f0b525cf3bea7be19b52.zip
add HasFile content pack method
Diffstat (limited to 'src/SMAPI/Framework/ContentPack.cs')
-rw-r--r--src/SMAPI/Framework/ContentPack.cs27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/SMAPI/Framework/ContentPack.cs b/src/SMAPI/Framework/ContentPack.cs
index e39d03a1..5384d98f 100644
--- a/src/SMAPI/Framework/ContentPack.cs
+++ b/src/SMAPI/Framework/ContentPack.cs
@@ -47,6 +47,15 @@ namespace StardewModdingAPI.Framework
this.JsonHelper = jsonHelper;
}
+ /// <summary>Get whether a given file exists in the content pack.</summary>
+ /// <param name="path">The file path to check.</param>
+ public bool HasFile(string path)
+ {
+ this.AssertRelativePath(path, nameof(this.HasFile));
+
+ return File.Exists(Path.Combine(this.DirectoryPath, path));
+ }
+
/// <summary>Read a JSON file from the content pack folder.</summary>
/// <typeparam name="TModel">The model type.</typeparam>
/// <param name="path">The file path relative to the contnet directory.</param>
@@ -54,8 +63,7 @@ namespace StardewModdingAPI.Framework
/// <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.");
+ this.AssertRelativePath(path, nameof(this.ReadJsonFile));
path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path));
return this.JsonHelper.ReadJsonFileIfExists(path, out TModel model)
@@ -70,8 +78,7 @@ namespace StardewModdingAPI.Framework
/// <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.");
+ this.AssertRelativePath(path, nameof(this.WriteJsonFile));
path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path));
this.JsonHelper.WriteJsonFile(path, data);
@@ -95,5 +102,17 @@ namespace StardewModdingAPI.Framework
return this.Content.GetActualAssetKey(key, ContentSource.ModFolder);
}
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Assert that a relative path was passed it to a content pack method.</summary>
+ /// <param name="path">The path to check.</param>
+ /// <param name="methodName">The name of the method which was invoked.</param>
+ private void AssertRelativePath(string path, string methodName)
+ {
+ if (!PathUtilities.IsSafeRelativePath(path))
+ throw new InvalidOperationException($"You must call {nameof(IContentPack)}.{methodName} with a relative path.");
+ }
}
}