Advertisement
Apathy420

[.NET]ApLib - Simple XBDM Communicator

Oct 25th, 2014
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace ApLib
  11. {
  12.     //********************************************
  13.     //********* ApLib - Xbdm Comm C# *************
  14.     //********************************************
  15.     //*************** Credits ********************
  16.     //****** No reason for this, it's useless ****
  17.     //****** Use IXboxConsole to do samething ****
  18.     //*XeClutch - For how GROSS PhantomRTM Looks.*
  19.     //********************************************
  20.  
  21.     public static class ApComm_Extras
  22.     {
  23.         public static string FormatXbdmData(this string Data)
  24.         {
  25.             return Data.Replace(" ", "").Replace("-", "");
  26.         }
  27.     }
  28.  
  29.     public class ApCommunicator
  30.     {
  31.        
  32.         #region AppComm_Vars
  33.  
  34.         #region Known XBDM Responses
  35.  
  36.         private Dictionary<string, string> Resp = new Dictionary<string, string>()
  37.         {
  38.             {"XbCmd_Success",      "200- "},         //Example: "200- <CPU KEY>"
  39.             {"XbCmd2_Success",     "200- OK"},
  40.             {"SetMem_Success",     "200- set "},     //Example: "200- set 4 bytes"
  41.             {"GetPID_Success",     "200- pid="},
  42.             {"GetCID_Success",     "200- consoleid="},  
  43.             {"XbCon_Success",      "201- connected"},
  44.             {"GetMem_Success",     "202- memory data follows"},
  45.             {"GetMemEx_Success",   "203- binary response follows"},
  46.            
  47.             {"XbError_Syntax",     "400- syntax error"},
  48.             {"XbError_NoThread",     "405- no such thread"},
  49.             {"XbError_UnknownCMD", "407- unknown command"},
  50.         };
  51.  
  52.         #endregion
  53.  
  54.         #region Known XBDM Commands
  55.  
  56.         private Dictionary<string, string> Cmds = new Dictionary<string, string>()
  57.         {
  58.             { "Get",     "GetMem "},
  59.             { "Set",     "SetMem "},
  60.             { "GetExt",  "GetMemEx "},
  61.             { "GetCpu",  "GetCpuKey"},
  62.             { "GetPID",  "GetPID"},
  63.             { "GetCID",  "GetConsoleId"},
  64.             { "Eject",   "DvdEject"},
  65.             { "Pause",   "Stop"},
  66.             { "Start",   "Go" },
  67.             { "Die",     "Shutdown"},
  68.         };
  69.  
  70.         #endregion
  71.  
  72.         public string ConsoleIp = "";
  73.         public bool isConnected = false;
  74.         private TcpClient xbCon = null;
  75.         private string ResponseBuffer = "";
  76.         private BinaryWriter InputStream = null;
  77.         private StreamReader OutputStream = null;
  78.  
  79.         #endregion
  80.  
  81.         #region ApComm_Main
  82.  
  83.         public ApCommunicator()
  84.         {
  85.             if (xbCon == null)
  86.                 xbCon = new TcpClient(); // { ReceiveTimeout = 0, SendTimeout = 0 }; Causes Infinite Timeout
  87.         }
  88.        
  89.         public bool Connect(string Ip)
  90.         {
  91.             try
  92.             {
  93.                 if (isConnected)
  94.                     return true;
  95.  
  96.                 xbCon.Connect(ConsoleIp = Ip, 730);
  97.                
  98.                 OutputStream = new StreamReader(xbCon.GetStream());
  99.  
  100.                 isConnected = OutputStream.ReadLine().Contains(Resp["XbCon_Success"]);
  101.             }
  102.             catch { }
  103.  
  104.             CommunicatorLog("Connection attempt to: " + ConsoleIp.ToString() + " was " + (isConnected ? "successfull!" : "unsuccessfull!"));
  105.  
  106.             return isConnected;
  107.         }
  108.        
  109.         public void Disconnect()
  110.         {
  111.             try
  112.             {
  113.                 if (xbCon == null)
  114.                     return;
  115.  
  116.                 if (OutputStream != null)
  117.                     OutputStream.Close();
  118.  
  119.                 if (InputStream != null)
  120.                     InputStream.Close();
  121.  
  122.                 if (xbCon != null)
  123.                     xbCon.Close();
  124.  
  125.                 CommunicatorLog("Successfully disconnected from " + ConsoleIp + "!");
  126.             }
  127.             catch { CommunicatorLog("Error disconnecting from " + ConsoleIp + "!"); }
  128.         }
  129.  
  130.         public string SendXbdmCMD(string XbdmCommand)
  131.         {
  132.             try
  133.             {
  134.                 if (!isConnected)
  135.                     return "Error";
  136.  
  137.                 InputStream = new BinaryWriter(xbCon.GetStream());
  138.  
  139.                 InputStream.Write(Encoding.ASCII.GetBytes(XbdmCommand + "\r\n"));
  140.  
  141.                 OutputStream = new StreamReader(xbCon.GetStream());
  142.  
  143.                 if (XbdmCommand.Contains(Cmds["Get"]))
  144.                 {
  145.                      if (OutputStream.ReadLine() == ".")
  146.                         OutputStream.ReadLine();
  147.                 }
  148.  
  149.                 CommunicatorLog("Successfully Sent Command: " + XbdmCommand);
  150.                
  151.                 return OutputStream.ReadLine();
  152.             }
  153.             catch
  154.             {
  155.                 CommunicatorLog("Error sending command to Xbdm!");
  156.                 return "Error";
  157.             }
  158.         }
  159.  
  160.         #endregion
  161.  
  162.         #region ApComm_Read
  163.  
  164.         public byte ReadByte(uint Address)
  165.         {
  166.             return ReadBytes(Address, 1)[0];
  167.         }
  168.         public sbyte ReadSByte(uint Address)
  169.         {
  170.             return (sbyte)ReadBytes(Address, 1)[0];
  171.         }
  172.         public byte[] ReadBytes(uint Address, uint Length)
  173.         {
  174.             return ((ResponseBuffer = ReadHexString(Address, Length)) == "Error") ? new byte[Length] : ResponseBuffer.ToByteArray();
  175.         }
  176.         public string ReadString(uint Address, uint Length)
  177.         {
  178.             return ReadHexString(Address, Length).ToAsciiString(true);
  179.         }
  180.         public string ReadHexString(uint Address, uint Length)
  181.         {
  182.             return SendXbdmCMD(string.Format("{0}Addr=0x{1} Length=0x{2}", Cmds["Get"], Address.ToString("X"), Length.ToString("X2")));
  183.         }
  184.         public bool ReadBool(uint Address)
  185.         {
  186.             return (ReadByte(Address) == 0x01);
  187.         }
  188.         public float ReadFloat(uint Address)
  189.         {
  190.             return ReadBytes(Address, 4).ToSingle();
  191.         }
  192.         public short ReadShort(uint Address)
  193.         {
  194.             return ReadBytes(Address, 2).ToInt16();
  195.         }
  196.         public ushort ReadUShort(uint Address)
  197.         {
  198.             return ReadBytes(Address, 2).ToUInt16();
  199.         }
  200.         public int ReadInt(uint Address)
  201.         {
  202.             return ReadBytes(Address, 4).ToInt32();
  203.         }
  204.         public uint ReadUInt(uint Address)
  205.         {
  206.             return ReadBytes(Address, 4).ToUInt32();
  207.         }
  208.         public long ReadLong(uint Address)
  209.         {
  210.             return ReadBytes(Address, 8).ToInt64();
  211.         }
  212.         public ulong ReadULong(uint Address)
  213.         {
  214.             return ReadBytes(Address, 8).ToUInt64();
  215.         }
  216.         public double ReadDouble(uint Address)
  217.         {
  218.             return ReadBytes(Address, 8).ToDouble();
  219.         }
  220.  
  221.         #endregion
  222.  
  223.         #region ApComm_Write
  224.  
  225.         public bool WriteByte(uint Address, byte Byte)
  226.         {
  227.             return WriteBytes(Address, new byte[] { Byte });
  228.         }
  229.         public bool WriteSByte(uint Address, sbyte SByte)
  230.         {
  231.             return WriteByte(Address, (byte)SByte);
  232.         }
  233.         public bool WriteBytes(uint Address, byte[] Bytes)
  234.         {
  235.             return WriteHexString(Address, Bytes.ToHexString());
  236.         }
  237.         public bool WriteString(uint Address, string String)
  238.         {
  239.             return WriteBytes(Address, String.ToByteArray());
  240.         }
  241.         public bool WriteHexString(uint Address, string HexString)
  242.         {
  243.             try
  244.             {
  245.                 return (SendXbdmCMD(string.Format("{0}Addr={1} Data={2}", Cmds["Set"], Address, HexString.FormatXbdmData())) != "");
  246.             }
  247.             catch { return false; }
  248.         }
  249.         public bool WriteBool(uint Address, bool Bool)
  250.         {
  251.             return WriteBytes(Address, Bool.ToByteArray());
  252.         }
  253.         public bool WriteFloat(uint Address, float Float)
  254.         {
  255.             return WriteBytes(Address, Float.ToByteArray());
  256.         }
  257.         public bool WriteShort(uint Address, short Short)
  258.         {
  259.             return WriteBytes(Address, Short.ToByteArray());
  260.         }
  261.         public bool WriteUShort(uint Address, ushort UShort)
  262.         {
  263.             return WriteBytes(Address, UShort.ToByteArray());
  264.         }
  265.         public bool WriteInt(uint Address, int Int)
  266.         {
  267.             return WriteBytes(Address, Int.ToByteArray());
  268.         }
  269.         public bool WriteUInt(uint Address, uint UInt)
  270.         {
  271.             return WriteBytes(Address, UInt.ToByteArray());
  272.         }
  273.         public bool WriteLong(uint Address, long Long)
  274.         {
  275.             return WriteBytes(Address, Long.ToByteArray());
  276.         }
  277.         public bool WriteULong(uint Address, ulong ULong)
  278.         {
  279.             return WriteBytes(Address, ULong.ToByteArray());
  280.         }
  281.         public bool WriteDouble(uint Address, double Double)
  282.         {
  283.             return WriteBytes(Address, Double.ToByteArray());
  284.         }
  285.  
  286.         #endregion
  287.  
  288.         #region ApComm_Misc
  289.  
  290.         public string CPUKey
  291.         {
  292.             get
  293.             {
  294.                 return ((ResponseBuffer = SendXbdmCMD(Cmds["GetCpu"])) != "Error") ? ResponseBuffer.Replace(Resp["XbCmd_Success"], "") : ResponseBuffer;
  295.             }
  296.         }
  297.         public string ConsoleID
  298.         {
  299.             get
  300.             {
  301.                 return ((ResponseBuffer = SendXbdmCMD(Cmds["GetCID"])) != "Error") ? ResponseBuffer.Replace(Resp["GetCID_Success"], "") : ResponseBuffer;
  302.             }
  303.         }
  304.         public string ProcessID
  305.         {
  306.             get
  307.             {
  308.                 return ((ResponseBuffer = SendXbdmCMD(Cmds["GetPID"])) != "Error") ? ResponseBuffer.Replace(Resp["GetPID_Success"], "") : ResponseBuffer;
  309.             }
  310.         }
  311.         public bool Pause()
  312.         {
  313.             return (SendXbdmCMD(Cmds["Pause"]) == Resp["XbCmd_Success"]);
  314.         }
  315.         public bool Unpause()
  316.         {
  317.             return (SendXbdmCMD(Cmds["Start"]) == Resp["XbCmd_Success"]);
  318.         }
  319.         public bool Shutdown()
  320.         {
  321.             return (SendXbdmCMD(Cmds["Die"]) == Resp["XbCmd_Success"]);
  322.         }
  323.         public bool EjectTray()
  324.         {
  325.             return (SendXbdmCMD(Cmds["Eject"]) == Resp["XbCmd_Success"]);
  326.         }
  327.  
  328.         private void CommunicatorLog(string LogItem)
  329.         {
  330.             Console.WriteLine(string.Format("{0}{1}{2}{3}", "[", DateTime.Now.ToString("hh:mm"), "]", LogItem));
  331.         }
  332.  
  333.         #endregion
  334.  
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement