public static void WriteMemory(this MiraConnection p_Connection, int p_ProcessId, ulong p_Address, byte[] p_Data)
        {
            var s_HeaderData = p_Connection.SerializeObject(new RpcMessageHeader
            {
                Category    = (int)RPC_CATEGORY.RPCCAT_DBG,
                Magic       = MiraConnection.c_Magic,
                ErrorType   = (int)DebuggerCmds.Debugger_WriteMem,
                Request     = true,
                PayloadSize = (ushort)Marshal.SizeOf <ReadWriteMem>()
            }.ToUInt64());

            var s_Payload = new ReadWriteMem
            {
                ProcessId  = p_ProcessId,
                Address    = p_Address,
                DataLength = (ulong)p_Data.LongLength,
                Data       = new byte[0x1000]
            };

            Array.Copy(p_Data, 0, s_Payload.Data, 0, p_Data.Length);

            var s_PayloadData = p_Connection.SerializeObject(s_Payload);

            using (var s_Writer = new BinaryWriter(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                s_Writer.Write(s_HeaderData);
                s_Writer.Write(s_PayloadData);
            }
        }
        public static bool GetHDDKeys(this MiraConnection p_Connection, out DumpHddKeys p_Output)
        {
            p_Output = new DumpHddKeys
            {
                Encrypted = new byte[0x60],
                Key       = new byte[0x20]
            };

            if (!p_Connection.Connected)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Category    = (int)RPC_CATEGORY.RPCCAT_SYSTEM,
                Magic       = MiraConnection.c_Magic,
                ErrorType   = unchecked ((int)UtilityCmds.OrbisUtils_DumpHddKeys),
                Request     = true,
                PayloadSize = (ushort)Marshal.SizeOf <DumpHddKeys>()
            }, p_Connection.SerializeObject(p_Output));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType < 0)
            {
                return(false);
            }

            new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());
            p_Output = p_Connection.ReceiveObject <DumpHddKeys>();

            return(true);
        }
        public static DebuggerPtrace Ptrace(this MiraConnection p_Connection, int p_Request, int p_ProcessId, ulong p_Address, int p_Data, bool p_UseInternalBuffer = false, byte[] p_BufferData = null)
        {
            var s_Data = new byte[0x800];

            if (p_BufferData != null && p_BufferData.Length <= s_Data.Length)
            {
                Buffer.BlockCopy(p_BufferData, 0, s_Data, 0, p_BufferData.Length);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Category    = (int)RPC_CATEGORY.RPCCAT_DBG,
                Magic       = MiraConnection.c_Magic,
                ErrorType   = unchecked ((int)DebuggerCmds.Debugger_Ptrace),
                Request     = true,
                PayloadSize = (ushort)Marshal.SizeOf <DebuggerPtrace>()
            }, p_Connection.SerializeObject(new DebuggerPtrace
            {
                Address            = p_Address,
                Buffer             = s_Data,
                Data               = p_Data,
                ProcessId          = p_ProcessId,
                Request            = p_Request,
                SetAddressToBuffer = p_UseInternalBuffer ? 1 : 0
            }));

            var s_Response = p_Connection.ReceiveObject <DebuggerPtrace>();

            return(s_Response);
        }
        public static byte[] Read(this MiraConnection p_Connection, int p_Descriptor, uint p_Offset, ulong p_Size)
        {
            if (!p_Connection.Connected)
            {
                return(null);
            }

            if (p_Descriptor < 0)
            {
                return(null);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_Read,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferRead>(),
            }, p_Connection.SerializeObject(new FileTransferRead
            {
                Handle = p_Descriptor,
                Offset = p_Offset,
                Size   = p_Size
            }));

            var s_Response = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Response.ErrorType != 0)
            {
                return(null);
            }

            var s_Payload = p_Connection.ReceiveObject <FileTransferRead>();

            var s_BlockCount = s_Payload.Size / 0x4000;
            var s_Leftover   = s_Payload.Size % 0x4000;

            byte[] s_Data;
            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                using (var s_Writer = new BinaryWriter(new MemoryStream()))
                {
                    for (ulong i = 0; i < s_BlockCount; ++i)
                    {
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }

                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));
                    s_Data = ((MemoryStream)s_Writer.BaseStream).ToArray();
                }
            }

            return(s_Data);
        }
        public static void ReadFile(this MiraConnection p_Connection, string p_SourcePath, string p_LocalPath)
        {
            if (!p_Connection.Connected)
            {
                return;
            }

            int s_Descriptor = p_Connection.Open(p_SourcePath, 0, 0);

            if (s_Descriptor < 0)
            {
                return;
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_ReadFile,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferReadFile>()
            }, p_Connection.SerializeObject(new FileTransferReadFile
            {
                Handle = s_Descriptor,
                Size   = 0
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return;
            }

            var s_Response = p_Connection.ReceiveObject <FileTransferReadFile>();

            var s_BlockCount = s_Response.Size / 0x4000;
            var s_Leftover   = s_Response.Size % 0x4000;

            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                using (var s_Writer = new BinaryWriter(new FileStream(p_LocalPath, FileMode.Create, FileAccess.ReadWrite)))
                {
                    for (ulong i = 0; i < s_BlockCount; ++i)
                    {
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }

                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));
                }
            }

            // Close the handle
            p_Connection.Close(s_Descriptor);
        }
        public static bool WriteFile(this MiraConnection p_Connection, string p_Path, byte[] p_Data)
        {
            if (!p_Connection.Connected)
            {
                return(false);
            }

            var s_Descriptor = p_Connection.Open(p_Path, 0x0002 | 0x0200 /* O_RDWR | O_CREAT*/, 0777);

            if (s_Descriptor < 0)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_WriteFile,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferWriteFile>()
            }, p_Connection.SerializeObject(new FileTransferWriteFile
            {
                Handle = s_Descriptor,
                Size   = (ulong)p_Data.Length
            }));

            var s_BlockCount = p_Data.Length / 0x4000;
            var s_Leftover   = p_Data.Length % 0x4000;

            using (var s_Reader = new BinaryReader(new MemoryStream(p_Data)))
            {
                using (var s_Writer = new BinaryWriter(p_Connection.GetStream(), Encoding.ASCII, true))
                {
                    for (int i = 0; i < s_BlockCount; ++i)
                    {
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }

                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));
                }
            }

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            p_Connection.Close(s_Descriptor);

            return(s_Result.ErrorType == 0);
        }
        public static IEnumerable <FileTransferGetdents> GetDents(this MiraConnection p_Connection, string p_Path)
        {
            if (!p_Connection.Connected)
            {
                return(null);
            }

            // Send the request
            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_GetDents,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferGetdents>(),
            }, p_Connection.SerializeObject(new FileTransferGetdents
            {
                Path   = p_Path,
                Handle = -1,
                Type   = 0
            }));

            // Check the result
            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return(Enumerable.Empty <FileTransferGetdents>());
            }

            // Read the amount of directory entries
            var   s_List      = new List <FileTransferGetdents>();
            ulong s_DentCount = 0;

            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
                s_DentCount = s_Reader.ReadUInt64();

            Debug.WriteLine($"Dent Count: {s_DentCount}");

            for (ulong i = 0; i < s_DentCount; ++i)
            {
                s_List.Add(p_Connection.ReceiveObject <FileTransferGetdents>());
            }

            return(s_List);
        }
        public static bool Delete(this MiraConnection p_Connection, string p_Path)
        {
            if (!p_Connection.Connected)
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(p_Path))
            {
                return(false);
            }

            if (p_Path.Length > 255)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = unchecked ((int)FileTransferCmds.FileTransfer_Delete),
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferDelete>(),
            }, p_Connection.SerializeObject(new FileTransferDelete
            {
                Path = p_Path
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return(false);
            }

            return(true);
        }
        public static int Open(this MiraConnection p_Connection, string p_Path, int p_Flags, int p_Mode)
        {
            if (!p_Connection.Connected)
            {
                return(-1);
            }

            if (p_Path.Length > 255)
            {
                return(-1);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_Open,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferOpen>()
            }, p_Connection.SerializeObject(new FileTransferOpen
            {
                Flags  = p_Flags,
                Handle = -1,
                Mode   = p_Mode,
                Path   = p_Path
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Result.ErrorType != 0)
            {
                return(-1);
            }

            var s_Response = p_Connection.ReceiveObject <FileTransferOpen>();

            return(s_Response.Handle);
        }
        public static bool Stat(this MiraConnection p_Connection, string p_Path, out FileTransferStat p_Stat)
        {
            p_Stat = new FileTransferStat();

            if (!p_Connection.Connected)
            {
                return(false);
            }

            if (p_Path.Length > 255)
            {
                return(false);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = unchecked ((int)FileTransferCmds.FileTransfer_Stat),
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferStat>()
            }, p_Connection.SerializeObject(new FileTransferStat
            {
                Path = p_Path
            }));

            var s_Response = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            if (s_Response.ErrorType != 0)
            {
                return(false);
            }

            p_Stat = p_Connection.ReceiveObject <FileTransferStat>();

            return(true);
        }
        public static int Kill(this MiraConnection p_Connection, int p_Pid, int p_Signal)
        {
            if (!p_Connection.Connected)
            {
                return(-1);
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Category    = (int)RPC_CATEGORY.RPCCAT_DBG,
                Magic       = MiraConnection.c_Magic,
                ErrorType   = unchecked ((int)DebuggerCmds.Debugger_Kill),
                Request     = true,
                PayloadSize = (ushort)Marshal.SizeOf <DebuggerKill>()
            }, p_Connection.SerializeObject(new DebuggerKill
            {
                Pid    = p_Pid,
                Signal = p_Signal
            }));

            var s_Result = new RpcMessageHeader(p_Connection.ReceiveObject <ulong>());

            return((int)s_Result.ErrorType);
        }
        public static void Close(this MiraConnection p_Connection, int p_Descriptor)
        {
            if (!p_Connection.Connected)
            {
                return;
            }

            if (p_Descriptor < 0)
            {
                return;
            }

            p_Connection.SendMessage(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_Close,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferClose>()
            }, p_Connection.SerializeObject(new FileTransferClose
            {
                Handle = p_Descriptor
            }));
        }
        public static byte[] ReadFile(this MiraConnection p_Connection, string p_Path)
        {
            if (!p_Connection.Connected)
            {
                return(null);
            }

            int s_Descriptor = p_Connection.Open(p_Path, 0, 0);

            if (s_Descriptor < 0)
            {
                return(null);
            }

            var s_HeaderData = p_Connection.SerializeObject(new RpcMessageHeader
            {
                Magic       = MiraConnection.c_Magic,
                Category    = (int)RPC_CATEGORY.RPCCAT_FILE,
                Request     = true,
                ErrorType   = (int)FileTransferCmds.FileTransfer_ReadFile,
                PayloadSize = (ushort)Marshal.SizeOf <FileTransferReadFile>()
            });

            var s_RequestData = p_Connection.SerializeObject(new FileTransferReadFile
            {
                Handle = s_Descriptor,
                Size   = 0
            });

            using (var s_Writer = new BinaryWriter(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                s_Writer.Write(s_HeaderData);
                s_Writer.Write(s_RequestData);
            }

            var s_Response = p_Connection.ReceiveObject <FileTransferReadFile>();

            var s_BlockCount  = s_Response.Size / 0x4000;
            var s_Leftover    = s_Response.Size % 0x4000;
            var s_CurrentSize = 0;

            byte[] s_Data;
            using (var s_Reader = new BinaryReader(p_Connection.GetStream(), Encoding.ASCII, true))
            {
                using (var s_Writer = new BinaryWriter(new MemoryStream()))
                {
                    for (ulong i = 0; i < s_BlockCount; ++i)
                    {
                        s_CurrentSize += 0x4000;
                        s_Writer.Write(s_Reader.ReadBytes(0x4000));
                    }


                    s_Writer.Write(s_Reader.ReadBytes((int)s_Leftover));

                    s_Data = ((MemoryStream)s_Writer.BaseStream).ToArray();
                }
            }

            // Close the handle
            p_Connection.Close(s_Descriptor);

            return(s_Data);
        }