aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs
blob: 081b569520dfa50ae9159d16a6fc36c617121561 (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 long 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.ReadInt64() : 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, long PId) : this(Copy, Move)
        {
            this.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((long)PId);
                }

                foreach (int Handle in ToCopy)
                {
                    Writer.Write(Handle);
                }

                foreach (int Handle in ToMove)
                {
                    Writer.Write(Handle);
                }

                return MS.ToArray();
            }
        }
    }
}