aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs
blob: 439590e0c58a239c40092e60a8eb0815172884d9 (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
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.IO;

namespace Ryujinx.HLE.HOS.Ipc
{
    class IpcHandleDesc
    {
        public bool HasPId { get; private set; }

        public ulong PId { get; private set; }

        public int[] ToCopy { get; private set; }
        public int[] ToMove { get; private set; }

        public IpcHandleDesc(BinaryReader reader)
        {
            int word = reader.ReadInt32();

            HasPId = (word & 1) != 0;

            ToCopy = new int[(word >> 1) & 0xf];
            ToMove = new int[(word >> 5) & 0xf];

            PId = HasPId ? reader.ReadUInt64() : 0;

            for (int index = 0; index < ToCopy.Length; index++)
            {
                ToCopy[index] = reader.ReadInt32();
            }

            for (int index = 0; index < ToMove.Length; index++)
            {
                ToMove[index] = reader.ReadInt32();
            }
        }

        public IpcHandleDesc(int[] copy, int[] move)
        {
            ToCopy = copy ?? throw new ArgumentNullException(nameof(copy));
            ToMove = move ?? throw new ArgumentNullException(nameof(move));
        }

        public IpcHandleDesc(int[] copy, int[] move, ulong pId) : this(copy, move)
        {
            PId = pId;

            HasPId = true;
        }

        public static IpcHandleDesc MakeCopy(params int[] handles)
        {
            return new IpcHandleDesc(handles, new int[0]);
        }

        public static IpcHandleDesc MakeMove(params int[] handles)
        {
            return new IpcHandleDesc(new int[0], handles);
        }

        public byte[] GetBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryWriter writer = new BinaryWriter(ms);

                int word = HasPId ? 1 : 0;

                word |= (ToCopy.Length & 0xf) << 1;
                word |= (ToMove.Length & 0xf) << 5;

                writer.Write(word);

                if (HasPId)
                {
                    writer.Write(PId);
                }

                foreach (int handle in ToCopy)
                {
                    writer.Write(handle);
                }

                foreach (int handle in ToMove)
                {
                    writer.Write(handle);
                }

                return ms.ToArray();
            }
        }
    }
}