Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace ApLib
- {
- //********************************************
- //********* ApLib - Xbdm Comm C# *************
- //********************************************
- //*************** Credits ********************
- //****** No reason for this, it's useless ****
- //****** Use IXboxConsole to do samething ****
- //*XeClutch - For how GROSS PhantomRTM Looks.*
- //********************************************
- public static class ApComm_Extras
- {
- public static string FormatXbdmData(this string Data)
- {
- return Data.Replace(" ", "").Replace("-", "");
- }
- }
- public class ApCommunicator
- {
- #region AppComm_Vars
- #region Known XBDM Responses
- private Dictionary<string, string> Resp = new Dictionary<string, string>()
- {
- {"XbCmd_Success", "200- "}, //Example: "200- <CPU KEY>"
- {"XbCmd2_Success", "200- OK"},
- {"SetMem_Success", "200- set "}, //Example: "200- set 4 bytes"
- {"GetPID_Success", "200- pid="},
- {"GetCID_Success", "200- consoleid="},
- {"XbCon_Success", "201- connected"},
- {"GetMem_Success", "202- memory data follows"},
- {"GetMemEx_Success", "203- binary response follows"},
- {"XbError_Syntax", "400- syntax error"},
- {"XbError_NoThread", "405- no such thread"},
- {"XbError_UnknownCMD", "407- unknown command"},
- };
- #endregion
- #region Known XBDM Commands
- private Dictionary<string, string> Cmds = new Dictionary<string, string>()
- {
- { "Get", "GetMem "},
- { "Set", "SetMem "},
- { "GetExt", "GetMemEx "},
- { "GetCpu", "GetCpuKey"},
- { "GetPID", "GetPID"},
- { "GetCID", "GetConsoleId"},
- { "Eject", "DvdEject"},
- { "Pause", "Stop"},
- { "Start", "Go" },
- { "Die", "Shutdown"},
- };
- #endregion
- public string ConsoleIp = "";
- public bool isConnected = false;
- private TcpClient xbCon = null;
- private string ResponseBuffer = "";
- private BinaryWriter InputStream = null;
- private StreamReader OutputStream = null;
- #endregion
- #region ApComm_Main
- public ApCommunicator()
- {
- if (xbCon == null)
- xbCon = new TcpClient(); // { ReceiveTimeout = 0, SendTimeout = 0 }; Causes Infinite Timeout
- }
- public bool Connect(string Ip)
- {
- try
- {
- if (isConnected)
- return true;
- xbCon.Connect(ConsoleIp = Ip, 730);
- OutputStream = new StreamReader(xbCon.GetStream());
- isConnected = OutputStream.ReadLine().Contains(Resp["XbCon_Success"]);
- }
- catch { }
- CommunicatorLog("Connection attempt to: " + ConsoleIp.ToString() + " was " + (isConnected ? "successfull!" : "unsuccessfull!"));
- return isConnected;
- }
- public void Disconnect()
- {
- try
- {
- if (xbCon == null)
- return;
- if (OutputStream != null)
- OutputStream.Close();
- if (InputStream != null)
- InputStream.Close();
- if (xbCon != null)
- xbCon.Close();
- CommunicatorLog("Successfully disconnected from " + ConsoleIp + "!");
- }
- catch { CommunicatorLog("Error disconnecting from " + ConsoleIp + "!"); }
- }
- public string SendXbdmCMD(string XbdmCommand)
- {
- try
- {
- if (!isConnected)
- return "Error";
- InputStream = new BinaryWriter(xbCon.GetStream());
- InputStream.Write(Encoding.ASCII.GetBytes(XbdmCommand + "\r\n"));
- OutputStream = new StreamReader(xbCon.GetStream());
- if (XbdmCommand.Contains(Cmds["Get"]))
- {
- if (OutputStream.ReadLine() == ".")
- OutputStream.ReadLine();
- }
- CommunicatorLog("Successfully Sent Command: " + XbdmCommand);
- return OutputStream.ReadLine();
- }
- catch
- {
- CommunicatorLog("Error sending command to Xbdm!");
- return "Error";
- }
- }
- #endregion
- #region ApComm_Read
- public byte ReadByte(uint Address)
- {
- return ReadBytes(Address, 1)[0];
- }
- public sbyte ReadSByte(uint Address)
- {
- return (sbyte)ReadBytes(Address, 1)[0];
- }
- public byte[] ReadBytes(uint Address, uint Length)
- {
- return ((ResponseBuffer = ReadHexString(Address, Length)) == "Error") ? new byte[Length] : ResponseBuffer.ToByteArray();
- }
- public string ReadString(uint Address, uint Length)
- {
- return ReadHexString(Address, Length).ToAsciiString(true);
- }
- public string ReadHexString(uint Address, uint Length)
- {
- return SendXbdmCMD(string.Format("{0}Addr=0x{1} Length=0x{2}", Cmds["Get"], Address.ToString("X"), Length.ToString("X2")));
- }
- public bool ReadBool(uint Address)
- {
- return (ReadByte(Address) == 0x01);
- }
- public float ReadFloat(uint Address)
- {
- return ReadBytes(Address, 4).ToSingle();
- }
- public short ReadShort(uint Address)
- {
- return ReadBytes(Address, 2).ToInt16();
- }
- public ushort ReadUShort(uint Address)
- {
- return ReadBytes(Address, 2).ToUInt16();
- }
- public int ReadInt(uint Address)
- {
- return ReadBytes(Address, 4).ToInt32();
- }
- public uint ReadUInt(uint Address)
- {
- return ReadBytes(Address, 4).ToUInt32();
- }
- public long ReadLong(uint Address)
- {
- return ReadBytes(Address, 8).ToInt64();
- }
- public ulong ReadULong(uint Address)
- {
- return ReadBytes(Address, 8).ToUInt64();
- }
- public double ReadDouble(uint Address)
- {
- return ReadBytes(Address, 8).ToDouble();
- }
- #endregion
- #region ApComm_Write
- public bool WriteByte(uint Address, byte Byte)
- {
- return WriteBytes(Address, new byte[] { Byte });
- }
- public bool WriteSByte(uint Address, sbyte SByte)
- {
- return WriteByte(Address, (byte)SByte);
- }
- public bool WriteBytes(uint Address, byte[] Bytes)
- {
- return WriteHexString(Address, Bytes.ToHexString());
- }
- public bool WriteString(uint Address, string String)
- {
- return WriteBytes(Address, String.ToByteArray());
- }
- public bool WriteHexString(uint Address, string HexString)
- {
- try
- {
- return (SendXbdmCMD(string.Format("{0}Addr={1} Data={2}", Cmds["Set"], Address, HexString.FormatXbdmData())) != "");
- }
- catch { return false; }
- }
- public bool WriteBool(uint Address, bool Bool)
- {
- return WriteBytes(Address, Bool.ToByteArray());
- }
- public bool WriteFloat(uint Address, float Float)
- {
- return WriteBytes(Address, Float.ToByteArray());
- }
- public bool WriteShort(uint Address, short Short)
- {
- return WriteBytes(Address, Short.ToByteArray());
- }
- public bool WriteUShort(uint Address, ushort UShort)
- {
- return WriteBytes(Address, UShort.ToByteArray());
- }
- public bool WriteInt(uint Address, int Int)
- {
- return WriteBytes(Address, Int.ToByteArray());
- }
- public bool WriteUInt(uint Address, uint UInt)
- {
- return WriteBytes(Address, UInt.ToByteArray());
- }
- public bool WriteLong(uint Address, long Long)
- {
- return WriteBytes(Address, Long.ToByteArray());
- }
- public bool WriteULong(uint Address, ulong ULong)
- {
- return WriteBytes(Address, ULong.ToByteArray());
- }
- public bool WriteDouble(uint Address, double Double)
- {
- return WriteBytes(Address, Double.ToByteArray());
- }
- #endregion
- #region ApComm_Misc
- public string CPUKey
- {
- get
- {
- return ((ResponseBuffer = SendXbdmCMD(Cmds["GetCpu"])) != "Error") ? ResponseBuffer.Replace(Resp["XbCmd_Success"], "") : ResponseBuffer;
- }
- }
- public string ConsoleID
- {
- get
- {
- return ((ResponseBuffer = SendXbdmCMD(Cmds["GetCID"])) != "Error") ? ResponseBuffer.Replace(Resp["GetCID_Success"], "") : ResponseBuffer;
- }
- }
- public string ProcessID
- {
- get
- {
- return ((ResponseBuffer = SendXbdmCMD(Cmds["GetPID"])) != "Error") ? ResponseBuffer.Replace(Resp["GetPID_Success"], "") : ResponseBuffer;
- }
- }
- public bool Pause()
- {
- return (SendXbdmCMD(Cmds["Pause"]) == Resp["XbCmd_Success"]);
- }
- public bool Unpause()
- {
- return (SendXbdmCMD(Cmds["Start"]) == Resp["XbCmd_Success"]);
- }
- public bool Shutdown()
- {
- return (SendXbdmCMD(Cmds["Die"]) == Resp["XbCmd_Success"]);
- }
- public bool EjectTray()
- {
- return (SendXbdmCMD(Cmds["Eject"]) == Resp["XbCmd_Success"]);
- }
- private void CommunicatorLog(string LogItem)
- {
- Console.WriteLine(string.Format("{0}{1}{2}{3}", "[", DateTime.Now.ToString("hh:mm"), "]", LogItem));
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement