diff options
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Framework/ContentPack.cs | 27 | ||||
-rw-r--r-- | src/SMAPI/IContentPack.cs | 4 |
2 files changed, 27 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."); + } } } diff --git a/src/SMAPI/IContentPack.cs b/src/SMAPI/IContentPack.cs index 9ba32394..32cbc6fc 100644 --- a/src/SMAPI/IContentPack.cs +++ b/src/SMAPI/IContentPack.cs @@ -21,6 +21,10 @@ namespace StardewModdingAPI /********* ** Public methods *********/ + /// <summary>Get whether a given file exists in the content pack.</summary> + /// <param name="path">The file path to check.</param> + bool HasFile(string path); + /// <summary>Read a JSON file from the content pack 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 content pack directory.</param> |