summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/ModContentHelper.cs
blob: 45899dd7a9c3e5409eec57a341580821920a6a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using StardewModdingAPI.Framework.Content;
using StardewModdingAPI.Framework.ContentManagers;
using StardewModdingAPI.Framework.Exceptions;

namespace StardewModdingAPI.Framework.ModHelpers
{
    /// <inheritdoc cref="IModContentHelper"/>
    internal class ModContentHelper : BaseHelper, IModContentHelper
    {
        /*********
        ** Fields
        *********/
        /// <summary>SMAPI's core content logic.</summary>
        private readonly ContentCoordinator ContentCore;

        /// <summary>A content manager for this mod which manages files from the mod's folder.</summary>
        private readonly ModContentManager ModContentManager;

        /// <summary>The friendly mod name for use in errors.</summary>
        private readonly string ModName;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="contentCore">SMAPI's core content logic.</param>
        /// <param name="modFolderPath">The absolute path to the mod folder.</param>
        /// <param name="modID">The unique ID of the relevant mod.</param>
        /// <param name="modName">The friendly mod name for use in errors.</param>
        /// <param name="gameContentManager">The game content manager used for map tilesheets not provided by the mod.</param>
        public ModContentHelper(ContentCoordinator contentCore, string modFolderPath, string modID, string modName, IContentManager gameContentManager)
            : base(modID)
        {
            string managedAssetPrefix = contentCore.GetManagedAssetPrefix(modID);

            this.ContentCore = contentCore;
            this.ModContentManager = contentCore.CreateModContentManager(managedAssetPrefix, modName, modFolderPath, gameContentManager);
            this.ModName = modName;
        }

        /// <inheritdoc />
        public T Load<T>(string relativePath)
        {
            IAssetName assetName = this.ContentCore.ParseAssetName(relativePath);

            try
            {
                return this.ModContentManager.LoadExact<T>(assetName, useCache: false);
            }
            catch (Exception ex) when (ex is not SContentLoadException)
            {
                throw new SContentLoadException($"{this.ModName} failed loading content asset '{relativePath}' from its mod folder.", ex);
            }
        }

        /// <inheritdoc />
        public IAssetName GetInternalAssetName(string relativePath)
        {
            return this.ModContentManager.GetInternalAssetKey(relativePath);
        }

        /// <inheritdoc />
        public IAssetData GetPatchHelper<T>(T data, string relativePath = null)
        {
            if (data == null)
                throw new ArgumentNullException(nameof(data), "Can't get a patch helper for a null value.");

            relativePath ??= $"temp/{Guid.NewGuid():N}";

            return new AssetDataForObject(this.ContentCore.GetLocale(), this.ContentCore.ParseAssetName(relativePath), data, key => this.ContentCore.ParseAssetName(key).Name);
        }
    }
}