aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/Loaders/Npdm/Npdm.cs
blob: 4a99de98c2583e3455b46bae0072016219749707 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Ryujinx.HLE.Exceptions;
using System.IO;
using System.Text;

namespace Ryujinx.HLE.Loaders.Npdm
{
    // https://github.com/SciresM/hactool/blob/master/npdm.c
    // https://github.com/SciresM/hactool/blob/master/npdm.h
    // http://switchbrew.org/index.php?title=NPDM
    public class Npdm
    {
        private const int MetaMagic = 'M' << 0 | 'E' << 8 | 'T' << 16 | 'A' << 24;

        public byte ProcessFlags { get; private set; }
        public bool Is64Bit { get; private set; }
        public byte MainThreadPriority { get; private set; }
        public byte DefaultCpuId { get; private set; }
        public int PersonalMmHeapSize { get; private set; }
        public int Version { get; private set; }
        public int MainThreadStackSize { get; private set; }
        public string TitleName { get; set; }
        public byte[] ProductCode { get; private set; }

        public Aci0 Aci0 { get; private set; }
        public Acid Acid { get; private set; }

        /// <exception cref="InvalidNpdmException">The stream doesn't contain valid NPDM data.</exception>
        /// <exception cref="System.NotImplementedException">The FsAccessHeader.ContentOwnerId section is not implemented.</exception>
        /// <exception cref="System.ArgumentException">The stream does not support reading, is <see langword="null"/>, or is already closed.</exception>
        /// <exception cref="System.ArgumentException">An error occured while reading bytes from the stream.</exception>
        /// <exception cref="EndOfStreamException">The end of the stream is reached.</exception>
        /// <exception cref="System.ObjectDisposedException">The stream is closed.</exception>
        /// <exception cref="IOException">An I/O error occurred.</exception>
        public Npdm(Stream stream)
        {
            BinaryReader reader = new(stream);

            if (reader.ReadInt32() != MetaMagic)
            {
                throw new InvalidNpdmException("NPDM Stream doesn't contain NPDM file!");
            }

            reader.ReadInt64();

            ProcessFlags = reader.ReadByte();

            Is64Bit = (ProcessFlags & 1) != 0;

            reader.ReadByte();

            MainThreadPriority = reader.ReadByte();
            DefaultCpuId = reader.ReadByte();

            reader.ReadInt32();

            PersonalMmHeapSize = reader.ReadInt32();

            Version = reader.ReadInt32();

            MainThreadStackSize = reader.ReadInt32();

            byte[] tempTitleName = reader.ReadBytes(0x10);

            TitleName = Encoding.UTF8.GetString(tempTitleName, 0, tempTitleName.Length).Trim('\0');

            ProductCode = reader.ReadBytes(0x10);

            stream.Seek(0x30, SeekOrigin.Current);

            int aci0Offset = reader.ReadInt32();
#pragma warning disable IDE0059 // Remove unnecessary value assignment
            int aci0Size = reader.ReadInt32();
            int acidOffset = reader.ReadInt32();
            int acidSize = reader.ReadInt32();
#pragma warning restore IDE0059

            Aci0 = new Aci0(stream, aci0Offset);
            Acid = new Acid(stream, acidOffset);
        }
    }
}