aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/FileSystem/ContentCollection.cs
blob: 1c19887befbcda5d3782dfbed7534d785b649372 (plain) (blame)
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
using LibHac.Common.Keys;
using LibHac.Fs.Fsa;
using LibHac.Ncm;
using LibHac.Tools.FsSystem.NcaUtils;
using LibHac.Tools.Ncm;
using Ryujinx.HLE.Loaders.Processes.Extensions;
using System;

namespace Ryujinx.HLE.FileSystem
{
    /// <summary>
    /// Thin wrapper around <see cref="Cnmt"/>
    /// </summary>
    public class ContentCollection
    {
        private readonly IFileSystem _pfs;
        private readonly Cnmt _cnmt;

        public ulong Id => _cnmt.TitleId;
        public TitleVersion Version => _cnmt.TitleVersion;
        public ContentMetaType Type => _cnmt.Type;
        public ulong ApplicationId => _cnmt.ApplicationTitleId;
        public ulong PatchId => _cnmt.PatchTitleId;
        public TitleVersion RequiredSystemVersion => _cnmt.MinimumSystemVersion;
        public TitleVersion RequiredApplicationVersion => _cnmt.MinimumApplicationVersion;
        public byte[] Digest => _cnmt.Hash;

        public ulong ProgramBaseId => Id & ~0x1FFFUL;
        public bool IsSystemTitle => _cnmt.Type < ContentMetaType.Application;

        public ContentCollection(IFileSystem pfs, Cnmt cnmt)
        {
            _pfs = pfs;
            _cnmt = cnmt;
        }

        public Nca GetNcaByType(KeySet keySet, ContentType type, int programIndex = 0)
        {
            // TODO: Replace this with a check for IdOffset as soon as LibHac supports it:
            // && entry.IdOffset == programIndex

            foreach (var entry in _cnmt.ContentEntries)
            {
                if (entry.Type != type)
                {
                    continue;
                }

                string ncaId = BitConverter.ToString(entry.NcaId).Replace("-", null).ToLower();
                Nca nca = _pfs.GetNca(keySet, $"/{ncaId}.nca");

                if (nca.GetProgramIndex() == programIndex)
                {
                    return nca;
                }
            }

            return null;
        }
    }
}