Advertisement
BaSs_HaXoR

PS3MAPI.cs [C#][Source]

Mar 11th, 2015
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 62.59 KB | None | 0 0
  1. /*PS3 MANAGER API
  2.  * Copyright (c) 2014 _NzV_.
  3.  *
  4.  * This code is write by _NzV_ <donm7v@gmail.com>.
  5.  * It may be used for any purpose as long as this notice remains intact on all
  6.  * source code distributions.
  7.  */
  8. using System;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Windows.Forms;
  14. namespace PS3ManagerAPI
  15. {
  16.     public class PS3MAPI
  17.     {
  18.  
  19.         public int PS3M_API_PC_LIB_VERSION = 0x0112;
  20.  
  21.         public CORE_CMD Core = new CORE_CMD();
  22.         public SERVER_CMD Server = new SERVER_CMD();
  23.         public PS3_CMD PS3 = new PS3_CMD();
  24.         public PROCESS_CMD Process = new PROCESS_CMD();
  25.  
  26.         public PS3MAPI()
  27.         {
  28.             Core = new CORE_CMD();
  29.             Server = new SERVER_CMD();
  30.             PS3 = new PS3_CMD();
  31.             Process = new PROCESS_CMD();
  32.         }
  33.  
  34.         #region PS3MAPI_CLient
  35.  
  36.         /// <summary>Get PS3 Manager PC Lib Version.</summary>
  37.         public string GetLibVersion_Str()
  38.         {
  39.             string ver = PS3M_API_PC_LIB_VERSION.ToString("X4");
  40.             string char1 = ver.Substring(1, 1) + ".";
  41.             string char2 = ver.Substring(2, 1) + ".";
  42.             string char3 = ver.Substring(3, 1);
  43.             return char1 + char2 + char3;
  44.         }
  45.  
  46.         /// <summary>
  47.         /// Indicates if PS3MAPI is connected
  48.         /// </summary>
  49.         public bool IsConnected
  50.         {
  51.             get { return PS3MAPI_Client_Server.IsConnected; }
  52.         }
  53.         /// <summary>
  54.         /// Indicates if PS3MAPI is attached
  55.         /// </summary>
  56.         public bool IsAttached
  57.         {
  58.             get { return PS3MAPI_Client_Server.IsAttached; }
  59.         }        
  60.  
  61.         /// <summary>Connect the target with "ConnectDialog".</summary>
  62.         /// <param name="ip">Ip</param>
  63.         /// <param name="port">Port</param>
  64.         public bool ConnectTarget(string ip, int port = 7887)
  65.         {
  66.             try
  67.             {
  68.                 PS3MAPI_Client_Server.Connect(ip, port);
  69.                 return true;
  70.             }
  71.             catch (Exception ex)
  72.             {
  73.                 throw new Exception(ex.Message, ex);
  74.             }
  75.         }
  76.  
  77.         /// <summary>Connect the target with "ConnectDialog".</summary>
  78.         public bool ConnectTarget()
  79.         {
  80.             ConnectDialog input = new ConnectDialog();
  81.             try
  82.             {
  83.                 bool result = false;
  84.                 if (input.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  85.                 {
  86.                     result = ConnectTarget(input.txtIp.Text, int.Parse(input.txtPort.Text));
  87.                 }
  88.                 if (input != null)
  89.                 {
  90.                     input.Dispose();
  91.                     input = null;
  92.                 }
  93.                 return result;
  94.             }
  95.             catch (Exception ex)
  96.             {
  97.                 if (input != null)
  98.                 {
  99.                     input.Dispose();
  100.                     input = null;
  101.                 }
  102.                 throw new Exception(ex.Message, ex);
  103.             }
  104.         }
  105.  
  106.         /// <summary>Attach to process by pid.</summary>
  107.         /// <param name="pid">Process PID</param>
  108.         public bool AttachProcess(uint pid)
  109.         {
  110.             try
  111.             {
  112.                 Process.Process_Pid = pid;
  113.                 return true;
  114.             }
  115.             catch (Exception ex)
  116.             {
  117.                 throw new Exception(ex.Message, ex);
  118.             }
  119.         }
  120.  
  121.        /// <summary>Attach to process with "AttachDialog".</summary>
  122.         public bool AttachProcess()
  123.         {
  124.             AttachDialog input = null;
  125.             try
  126.             {
  127.             retry:
  128.                 bool result = false;
  129.                 if (input != null)
  130.                 {
  131.                     input.Dispose();
  132.                     input = null;
  133.                 }
  134.                 input = new AttachDialog(this);
  135.                 System.Windows.Forms.DialogResult dialog_result = input.ShowDialog();
  136.                 if (dialog_result == System.Windows.Forms.DialogResult.OK)
  137.                 {
  138.                     string[] split = input.comboBox1.Text.Split(new char[] { '_' });
  139.                     result = AttachProcess(System.Convert.ToUInt32(split[0], 16));
  140.                 }
  141.                 else if (dialog_result == System.Windows.Forms.DialogResult.Retry)
  142.                 {
  143.                     goto retry;
  144.                 }
  145.                 if (input != null)
  146.                 {
  147.                     input.Dispose();
  148.                 }
  149.                 return result;
  150.             }
  151.             catch (Exception ex)
  152.             {
  153.                 if (input != null)
  154.                 {
  155.                     input.Dispose();
  156.                     input = null;
  157.                 }
  158.                 throw new Exception(ex.Message, ex);
  159.             }
  160.         }
  161.  
  162.         /// <summary>Disconnect the target.</summary>
  163.         public void DisconnectTarget()
  164.         {
  165.             try
  166.             {
  167.                 PS3MAPI_Client_Server.Disconnect();
  168.             }
  169.             catch// (Exception ex)
  170.             {
  171.                // throw new Exception(ex.Message, ex);
  172.             }
  173.         }
  174.  
  175.         LogDialog LogDialog = null;
  176.         /// <summary>Show log with "LogDialog".</summary>
  177.         public void ShowLog()
  178.         {
  179.             if (LogDialog == null) LogDialog = new LogDialog(this);
  180.             LogDialog.Show();
  181.         }
  182.         /// <summary>Show log with "LogDialog".</summary>
  183.         public string Log
  184.         {
  185.             get { return PS3MAPI_Client_Server.Log; }
  186.         }
  187.  
  188.         public class SERVER_CMD
  189.         {
  190.             /// <summary>
  191.             /// Specified Timeout: Defaults to 5000 (5 seconds)
  192.             /// </summary>
  193.             public int Timeout
  194.             {
  195.                 get { return PS3MAPI_Client_Server.Timeout; }
  196.                 set { PS3MAPI_Client_Server.Timeout = value; }
  197.             }
  198.  
  199.             /// <summary>Get PS3 Manager API Server Version.</summary>
  200.             public uint GetVersion()
  201.             {
  202.                 try
  203.                 {
  204.                     return PS3MAPI_Client_Server.Server_Get_Version();
  205.                 }
  206.                 catch (Exception ex)
  207.                 {
  208.                     throw new Exception(ex.Message, ex);
  209.                 }
  210.             }
  211.             /// <summary>Get PS3 Manager API Server Version.</summary>
  212.             public string GetVersion_Str()
  213.             {
  214.                 string ver = PS3MAPI_Client_Server.Server_Get_Version().ToString("X4");
  215.                 string char1 = ver.Substring(1, 1) + ".";
  216.                 string char2 = ver.Substring(2, 1) + ".";
  217.                 string char3 = ver.Substring(3, 1);
  218.                 return char1 + char2 + char3;
  219.             }
  220.         }
  221.  
  222.         public class CORE_CMD
  223.         {
  224.             /// <summary>Get PS3 Manager API Core Version.</summary>
  225.             public uint GetVersion()
  226.             {
  227.                 try
  228.                 {
  229.                     return PS3MAPI_Client_Server.Core_Get_Version();
  230.                 }
  231.                 catch (Exception ex)
  232.                 {
  233.                     throw new Exception(ex.Message, ex);
  234.                 }
  235.             }
  236.             /// <summary>Get PS3 Manager API Core Version.</summary>
  237.             public string GetVersion_Str()
  238.             {
  239.                 string ver = PS3MAPI_Client_Server.Core_Get_Version().ToString("X4");
  240.                 string char1 = ver.Substring(1, 1) + ".";
  241.                 string char2 = ver.Substring(2, 1) + ".";
  242.                 string char3 = ver.Substring(3, 1);
  243.                 return char1 + char2 + char3;
  244.             }
  245.           }
  246.  
  247.         public class PS3_CMD
  248.         {
  249.             /// <summary>Get PS3 Firmware Version.</summary>
  250.             public uint GetFirmwareVersion()
  251.             {
  252.                 try
  253.                 {
  254.                     return PS3MAPI_Client_Server.PS3_GetFwVersion();
  255.                 }
  256.                 catch (Exception ex)
  257.                 {
  258.                     throw new Exception(ex.Message, ex);
  259.                 }
  260.             }
  261.             /// <summary>Get PS3 Firmware Version.</summary>
  262.             public string GetFirmwareVersion_Str()
  263.             {
  264.                 string ver = PS3MAPI_Client_Server.PS3_GetFwVersion().ToString("X4");
  265.                 string char1 = ver.Substring(1, 1) + ".";
  266.                 string char2 = ver.Substring(2, 1);
  267.                 string char3 = ver.Substring(3, 1);
  268.                 return char1 + char2 + char3;
  269.             }
  270.  
  271.             /// <summary>Get PS3 Firmware Type.</summary>
  272.             public string GetFirmwareType()
  273.             {
  274.                 try
  275.                 {
  276.                     return PS3MAPI_Client_Server.PS3_GetFirmwareType();
  277.                 }
  278.                 catch (Exception ex)
  279.                 {
  280.                     throw new Exception(ex.Message, ex);
  281.                 }
  282.             }
  283.             public enum PowerFlags
  284.             {
  285.                 ShutDown,
  286.                 QuickReboot,
  287.                 SoftReboot,
  288.                 HardReboot
  289.             }
  290.             /// <summary>Shutdown Or Reboot PS3.</summary>
  291.             /// <param name="flag">Shutdown, QuickReboot, SoftReboot, HardReboot</param>
  292.             public void Power(PowerFlags flag)
  293.             {
  294.                 try
  295.                 {
  296.                     if (flag == PowerFlags.ShutDown) PS3MAPI_Client_Server.PS3_Shutdown();
  297.                     else if (flag == PowerFlags.QuickReboot) PS3MAPI_Client_Server.PS3_Reboot();
  298.                     else if (flag == PowerFlags.SoftReboot) PS3MAPI_Client_Server.PS3_SoftReboot();
  299.                     else if (flag == PowerFlags.HardReboot) PS3MAPI_Client_Server.PS3_HardReboot();
  300.                 }
  301.                 catch (Exception ex)
  302.                 {
  303.                     throw new Exception(ex.Message, ex);
  304.                 }
  305.             }
  306.             /// <summary>PS3 VSH Notify.</summary>
  307.             /// <param name="msg)">Your message</param>
  308.             public void Notify(string msg)
  309.             {
  310.                 try
  311.                 {
  312.                     PS3MAPI_Client_Server.PS3_Notify(msg);
  313.                 }
  314.                 catch (Exception ex)
  315.                 {
  316.                     throw new Exception(ex.Message, ex);
  317.                 }
  318.             }
  319.             public enum BuzzerMode
  320.             {
  321.                 Single,
  322.                 Double,
  323.                 Triple
  324.             }
  325.             /// <summary>Ring PS3 Buzzer.</summary>
  326.             /// <param name="mode">Simple, Double, Continuous</param>
  327.             public void RingBuzzer(BuzzerMode mode)
  328.             {
  329.                 try
  330.                 {
  331.                     if (mode == BuzzerMode.Single) PS3MAPI_Client_Server.PS3_Buzzer(1);
  332.                     else if (mode == BuzzerMode.Double) PS3MAPI_Client_Server.PS3_Buzzer(2);
  333.                     else if (mode == BuzzerMode.Triple) PS3MAPI_Client_Server.PS3_Buzzer(3);
  334.                 }
  335.                 catch (Exception ex)
  336.                 {
  337.                     throw new Exception(ex.Message, ex);
  338.                 }
  339.             }
  340.             public enum LedColor
  341.             {
  342.                 Red = 0,
  343.                 Green = 1,
  344.                 Yellow = 2
  345.             }
  346.             public enum LedMode
  347.             {
  348.                 Off = 0,
  349.                 On = 1,
  350.                 BlinkFast = 2,
  351.                 BlinkSlow = 3
  352.             }
  353.             /// <summary>PS3 Led.</summary>
  354.             /// <param name="color">Red, Green, Yellow</param>
  355.             /// <param name="mode">Off, On, BlinkFast, BlinkSlow</param>
  356.             public void Led(LedColor color, LedMode mode)
  357.             {
  358.                 try
  359.                 {
  360.                     PS3MAPI_Client_Server.PS3_Led(Convert.ToInt32(color), Convert.ToInt32(mode));
  361.                 }
  362.                 catch (Exception ex)
  363.                 {
  364.                     throw new Exception(ex.Message, ex);
  365.                 }
  366.             }
  367.             /// <summary>Get PS3 Temperature.</summary>
  368.             /// <param name="cpu">Return value for the cpu temperature in Celsius.</param>
  369.             /// <param name="rsx">Return value for the rsx temperature in Celsius.</param>
  370.             public void GetTemperature( out uint cpu, out uint rsx)
  371.             {
  372.                 cpu = 0; rsx = 0;
  373.                 try
  374.                 {
  375.                     PS3MAPI_Client_Server.PS3_GetTemp(out cpu, out rsx);
  376.                 }
  377.                 catch (Exception ex)
  378.                 {
  379.                     throw new Exception(ex.Message, ex);
  380.                 }
  381.             }
  382.             /// <summary>Disable PS3 Syscall.</summary>
  383.             /// <param name="num">Syscall number</param>
  384.             public void DisableSyscall(int num)
  385.             {
  386.                 try
  387.                 {
  388.                     PS3MAPI_Client_Server.PS3_DisableSyscall(num);
  389.                 }
  390.                 catch (Exception ex)
  391.                 {
  392.                     throw new Exception(ex.Message, ex);
  393.                 }
  394.             }
  395.             /// <summary>Return true if this syscall is enbaled</summary>
  396.             /// <param name="num">Syscall number</param>
  397.             public bool CheckSyscall(int num)
  398.             {
  399.                 try
  400.                 {
  401.                     return PS3MAPI_Client_Server.PS3_CheckSyscall(num);
  402.                 }
  403.                 catch (Exception ex)
  404.                 {
  405.                     throw new Exception(ex.Message, ex);
  406.                 }
  407.             }
  408.             public enum Syscall8Mode
  409.             {
  410.                 Enabled = 0,
  411.                 Only_CobraMambaAndPS3MAPI_Enabled = 1,
  412.                 Only_PS3MAPI_Enabled = 2,
  413.                 FakeDisabled = 3,
  414.                 Disabled = 4
  415.             }
  416.             /// <summary>Partial Disable PS3 Syscall 8.</summary>
  417.             /// <param name="mode">Enabled, Only Cobra and PS3M_API features enabled, Only PS3M_API features enabled, Fake Disable</param>
  418.             public void PartialDisableSyscall8(Syscall8Mode mode)
  419.             {
  420.                 try
  421.                 {
  422.                     if (mode == Syscall8Mode.Enabled) PS3MAPI_Client_Server.PS3_PartialDisableSyscall8(0);
  423.                     else if (mode == Syscall8Mode.Only_CobraMambaAndPS3MAPI_Enabled) PS3MAPI_Client_Server.PS3_PartialDisableSyscall8(1);
  424.                     else if (mode == Syscall8Mode.Only_PS3MAPI_Enabled) PS3MAPI_Client_Server.PS3_PartialDisableSyscall8(2);
  425.                     else if (mode == Syscall8Mode.FakeDisabled) PS3MAPI_Client_Server.PS3_PartialDisableSyscall8(3);
  426.                 }
  427.                 catch (Exception ex)
  428.                 {
  429.                     throw new Exception(ex.Message, ex);
  430.                 }
  431.             }
  432.             /// <summary>Remove COBRA/MAMBA Hook.</summary>
  433.             public Syscall8Mode PartialCheckSyscall8()
  434.             {
  435.                 try
  436.                 {
  437.                     if (PS3MAPI_Client_Server.PS3_PartialCheckSyscall8() == 0) return Syscall8Mode.Enabled;
  438.                     else if (PS3MAPI_Client_Server.PS3_PartialCheckSyscall8() == 1) return Syscall8Mode.Only_CobraMambaAndPS3MAPI_Enabled;
  439.                     else if (PS3MAPI_Client_Server.PS3_PartialCheckSyscall8() == 2) return Syscall8Mode.Only_PS3MAPI_Enabled;
  440.                     else return Syscall8Mode.FakeDisabled;
  441.                 }
  442.                 catch (Exception ex)
  443.                 {
  444.                     throw new Exception(ex.Message, ex);
  445.                 }
  446.             }
  447.         public void RemoveHook()
  448.             {
  449.                 try
  450.                 {
  451.                     PS3MAPI_Client_Server.PS3_RemoveHook();
  452.                 }
  453.                 catch (Exception ex)
  454.                 {
  455.                     throw new Exception(ex.Message, ex);
  456.                 }
  457.             }
  458.             /// <summary>Clear PS3 History.</summary>
  459.             /// <param name="include_directory">If set to true, "unsafe" directory will be also deleted.</param>
  460.             public void ClearHistory(bool include_directory = true)
  461.             {
  462.                 try
  463.                 {
  464.                     PS3MAPI_Client_Server.PS3_ClearHistory(include_directory);
  465.                 }
  466.                 catch (Exception ex)
  467.                 {
  468.                     throw new Exception(ex.Message, ex);
  469.                 }
  470.             }
  471.             /// <summary>Check Partial Syscall8 disable</summary>
  472.             }
  473.  
  474.         public class PROCESS_CMD
  475.         {
  476.  
  477.             public MEMORY_CMD Memory = new MEMORY_CMD();
  478.             public MODULES_CMD Modules = new MODULES_CMD();
  479.  
  480.             public PROCESS_CMD()
  481.             {
  482.                 Memory = new MEMORY_CMD();
  483.                 Modules = new MODULES_CMD();
  484.             }
  485.  
  486.             /// <summary>
  487.             /// Return all process_pid
  488.             /// </summary>
  489.             public uint[] Processes_Pid
  490.             {
  491.                 get { return PS3MAPI_Client_Server.Processes_Pid; }
  492.             }
  493.  
  494.             /// <summary>
  495.             /// Return current attached process_pid
  496.             /// </summary>)
  497.             public uint Process_Pid
  498.             {
  499.                 get { return PS3MAPI_Client_Server.Process_Pid; }
  500.                 set { PS3MAPI_Client_Server.Process_Pid = value; }
  501.             }
  502.  
  503.            /// <summary>Get a pid list of all runing processes.</summary>
  504.             public uint[] GetPidProcesses()
  505.             {
  506.                 try
  507.                 {
  508.                     return PS3MAPI_Client_Server.Process_GetPidList();
  509.                 }
  510.                 catch (Exception ex)
  511.                 {
  512.                     throw new Exception(ex.Message, ex);
  513.                 }
  514.             }
  515.  
  516.             /// <summary>Get name of this process.</summary>
  517.             /// <param name="pid">Process Pid</param>
  518.             public string GetName(uint pid)
  519.                 {
  520.                     try
  521.                     {
  522.                         return PS3MAPI_Client_Server.Process_GetName(pid);
  523.                     }
  524.                     catch (Exception ex)
  525.                     {
  526.                         throw new Exception(ex.Message, ex);
  527.                     }
  528.                 }
  529.  
  530.             public class MEMORY_CMD
  531.             {
  532.              /// <summary>Set memory to the attached process.</summary>
  533.             /// <param name="pid">Process Pid</param>
  534.             /// <param name="Address">Address</param>
  535.             /// <param name="Bytes">Bytes</param>
  536.             public void Set(uint Pid, uint Address, byte[] Bytes)
  537.             {
  538.                 try
  539.                 {
  540.                     PS3MAPI_Client_Server.Memory_Set(Pid, Address, Bytes);
  541.                 }
  542.                 catch (Exception ex)
  543.                 {
  544.                     throw new Exception(ex.Message, ex);
  545.                 }
  546.             }
  547.             /// <summary>Get memory from the attached process.</summary>
  548.             /// <param name="pid">Process Pid</param>
  549.             /// <param name="Address">Address</param>
  550.             /// <param name="Bytes">Bytes</param>
  551.             public void Get(uint Pid, uint Address, byte[] Bytes)
  552.             {
  553.                 try
  554.                 {
  555.                     PS3MAPI_Client_Server.Memory_Get(Pid, Address, Bytes);
  556.                 }
  557.                 catch (Exception ex)
  558.                 {
  559.                     throw new Exception(ex.Message, ex);
  560.                 }
  561.             }
  562.             /// <summary>Get memory from the attached process.</summary>
  563.             /// <param name="pid">Process Pid</param>
  564.             /// <param name="Address">Address</param>
  565.             /// <param name="Length">Length</param>
  566.             public byte[] Get(uint Pid, uint Address, uint Length)
  567.             {
  568.                 try
  569.                 {
  570.                     byte[] buffer = new byte[Length];
  571.                     PS3MAPI_Client_Server.Memory_Get(Pid, Address, buffer);
  572.                     return buffer;
  573.                 }
  574.                 catch (Exception ex)
  575.                 {
  576.                     throw new Exception(ex.Message, ex);
  577.                 }
  578.             }
  579.         }
  580.  
  581.             public class MODULES_CMD
  582.             {
  583.                 /// <summary>
  584.                 /// Return all modules_prx_id
  585.                 /// </summary>
  586.             public int[] Modules_Prx_Id
  587.              {
  588.                     get { return PS3MAPI_Client_Server.Modules_Prx_Id; }
  589.              }
  590.  
  591.  
  592.             /// <summary>Get a prx_id list of all modules for this process.</summary>
  593.             /// <param name="pid">Process Pid</param>
  594.             public int[] GetPrxIdModules(uint pid)
  595.             {
  596.                 try
  597.                 {
  598.                     return PS3MAPI_Client_Server.Module_GetPrxIdList(pid);
  599.                 }
  600.                 catch (Exception ex)
  601.                 {
  602.                     throw new Exception(ex.Message, ex);
  603.                 }
  604.             }
  605.             /// <summary>Get name of this module</summary>
  606.             /// <param name="pid">Process Pid</param>
  607.             /// <param name="prxid">Module Prx_id</param>
  608.             public string GetName(uint pid, int prxid)
  609.             {
  610.                 try
  611.                 {
  612.                     return PS3MAPI_Client_Server.Module_GetName(pid, prxid);
  613.                 }
  614.                 catch (Exception ex)
  615.                 {
  616.                     throw new Exception(ex.Message, ex);
  617.                 }
  618.             }
  619.             /// <summary>Get filename of this module</summary>
  620.             /// <param name="pid">Process Pid</param>
  621.             /// <param name="prxid">Module Prx_id</param>
  622.             public string GetFilename(uint pid, int prxid)
  623.             {
  624.                 try
  625.                 {
  626.                     return PS3MAPI_Client_Server.Module_GetFilename(pid, prxid);
  627.                 }
  628.                 catch (Exception ex)
  629.                 {
  630.                     throw new Exception(ex.Message, ex);
  631.                 }
  632.             }
  633.             /// <summary>Load a module.</summary>
  634.             /// <param name="pid">Process Pid</param>
  635.             /// <param name="path">Path of the plugin to load.</param>
  636.             public void Load(uint pid, string path)
  637.             {
  638.                 try
  639.                 {
  640.                     PS3MAPI_Client_Server.Module_Load(pid, path);
  641.                 }
  642.                 catch (Exception ex)
  643.                 {
  644.                     throw new Exception(ex.Message, ex);
  645.                 }
  646.             }
  647.             /// <summary>Unload a module.</summary>
  648.             /// <param name="pid">Process Pid</param>
  649.             /// <param name="prxid">Module Prx_id</param>
  650.             public void Unload(uint pid, int prxid)
  651.             {
  652.                 try
  653.                 {
  654.                     PS3MAPI_Client_Server.Module_Unload(pid, prxid);
  655.                 }
  656.                 catch (Exception ex)
  657.                 {
  658.                     throw new Exception(ex.Message, ex);
  659.                 }
  660.             }
  661.         }
  662.    
  663.        }
  664.  
  665.          #endregion PS3MAPI_CLient
  666.  
  667.         #region PS3MAPI_Client_Web
  668.         internal class PS3MAPI_Client_Web
  669.         {
  670.             //Not Yet
  671.         }
  672.         #endregion PS3MAPI_Client_Web
  673.  
  674.         #region PS3MAPI_Client_Server
  675.         internal class PS3MAPI_Client_Server
  676.         {
  677.             #region Private Members
  678.  
  679.             static private int ps3m_api_server_minversion = 0x0111;
  680.             static private PS3MAPI_ResponseCode eResponseCode;
  681.             static private string sResponse;
  682.             static private string sMessages = "";
  683.             static private string sServerIP = "";
  684.             static private int iPort = 7887;
  685.             static private string sBucket = "";
  686.             static private int iTimeout = 5000; // 5 Second
  687.             static private uint iPid = 0;
  688.             static private uint[] iprocesses_pid = new uint[16];
  689.             static private int[] imodules_prx_id = new int[64];
  690.             static private string sLog = "";
  691.             #endregion Private Members
  692.  
  693.             #region Internal Members
  694.  
  695.             static internal Socket main_sock;
  696.             static internal Socket listening_sock;
  697.             static internal Socket data_sock;
  698.             static internal IPEndPoint main_ipEndPoint;
  699.             static internal IPEndPoint data_ipEndPoint;
  700.              internal enum PS3MAPI_ResponseCode
  701.             {
  702.                 DataConnectionAlreadyOpen = 125,
  703.                 MemoryStatusOK = 150,
  704.                 CommandOK = 200,
  705.                 RequestSuccessful = 226,
  706.                 EnteringPassiveMode = 227,
  707.                 PS3MAPIConnected = 220,
  708.                 PS3MAPIConnectedOK = 230,
  709.                 MemoryActionCompleted = 250,
  710.                 MemoryActionPended = 350
  711.             }
  712.  
  713.             #endregion Internal Members
  714.  
  715.              #region Public Properties
  716.  
  717.              /// <summary>
  718.              /// Return all process_pid
  719.              /// </summary>
  720.              static public string Log
  721.              {
  722.                  get { return sLog; }
  723.              }
  724.  
  725.             /// <summary>
  726.             /// Return all process_pid
  727.             /// </summary>
  728.             static public uint[] Processes_Pid
  729.             {
  730.                 get { return iprocesses_pid; }
  731.             }
  732.  
  733.             /// <summary>
  734.             /// Attached process_pid
  735.             /// </summary>
  736.             static public uint Process_Pid
  737.             {
  738.                 get { return iPid; }
  739.                 set { iPid = value; }
  740.             }
  741.  
  742.             /// <summary>
  743.             /// Return all modules_prx_id
  744.             /// </summary>
  745.             static public int[] Modules_Prx_Id
  746.             {
  747.                 get { return imodules_prx_id; }
  748.             }
  749.  
  750.             /// <summary>
  751.             /// User Specified Timeout: Defaults to 5000 (5 seconds)
  752.             /// </summary>
  753.             static public int Timeout
  754.             {
  755.                 get { return iTimeout; }
  756.                 set { iTimeout = value; }
  757.             }
  758.  
  759.             /// <summary>
  760.             /// Indicates if PS3MAPI is connected
  761.             /// </summary>
  762.             static public bool IsConnected
  763.             {
  764.                 get { return ((main_sock != null) ? main_sock.Connected : false); }
  765.             }
  766.  
  767.             /// <summary>
  768.             /// Indicates if PS3MAPI is attached
  769.             /// </summary>
  770.             static public bool IsAttached
  771.             {
  772.                 get { return ((iPid != 0) ? true : false); }
  773.             }
  774.            
  775.             #endregion Public Properties
  776.  
  777.             //SERVER---------------------------------------------------------------------------------
  778.             internal static void Connect()
  779.             {
  780.                 Connect(sServerIP, iPort);
  781.             }
  782.             internal static void Connect(string sServer, int Port)
  783.             {
  784.                 sServerIP = sServer;
  785.                 iPort = Port;
  786.                 if (Port.ToString().Length == 0)
  787.                 {
  788.                     throw new Exception("Unable to Connect - No Port Specified.");
  789.                 }
  790.                 if (sServerIP.Length == 0)
  791.                 {
  792.                     throw new Exception("Unable to Connect - No Server Specified.");
  793.                 }
  794.                 if (main_sock != null)
  795.                 {
  796.                     if (main_sock.Connected)
  797.                     {
  798.                         return;
  799.                     }
  800.                 }
  801.                 main_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  802.                 main_ipEndPoint = new IPEndPoint(Dns.GetHostByName(sServerIP).AddressList[0], Port);
  803.                 try
  804.                 {
  805.                     main_sock.Connect(main_ipEndPoint);
  806.                 }
  807.                 catch (Exception ex)
  808.                 {
  809.                     throw new Exception(ex.Message, ex);
  810.                 }
  811.                 ReadResponse();
  812.                 if (eResponseCode != PS3MAPI_ResponseCode.PS3MAPIConnected)
  813.                 {
  814.                     Fail();
  815.                 }
  816.                 ReadResponse();
  817.                 if (eResponseCode != PS3MAPI_ResponseCode.PS3MAPIConnectedOK)
  818.                 {
  819.                     Fail();
  820.                 }
  821.                 if (Server_GetMinVersion() != ps3m_api_server_minversion)
  822.                 {
  823.                     Disconnect();
  824.                     throw new Exception("PS3M_API_PC_LIB OUTDATED! PLEASE UPDATE.");
  825.                 }
  826.                 return;
  827.             }
  828.             internal static void Disconnect()
  829.             {
  830.                 CloseDataSocket();
  831.                 if (main_sock != null)
  832.                 {
  833.                     if (main_sock.Connected)
  834.                     {
  835.                         SendCommand("DISCONNECT");
  836.                         iPid = 0;
  837.                         main_sock.Close();
  838.                     }
  839.                     main_sock = null;
  840.                 }
  841.                 main_ipEndPoint = null;
  842.             }
  843.             internal static uint Server_Get_Version()
  844.             {
  845.                 if (IsConnected)
  846.                 {
  847.                     SendCommand("SERVER GETVERSION");
  848.                     switch (eResponseCode)
  849.                     {
  850.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  851.                         case PS3MAPI_ResponseCode.CommandOK:
  852.                             break;
  853.                         default:
  854.                             Fail();
  855.                             break;
  856.                     }
  857.                     return Convert.ToUInt32(sResponse);
  858.                 }
  859.                 else
  860.                 {
  861.                     throw new Exception("PS3MAPI not connected!");
  862.                 }
  863.             }
  864.             internal static uint Server_GetMinVersion()
  865.             {
  866.                 if (IsConnected)
  867.                 {
  868.                     SendCommand("SERVER GETMINVERSION");
  869.                     switch (eResponseCode)
  870.                     {
  871.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  872.                         case PS3MAPI_ResponseCode.CommandOK:
  873.                             break;
  874.                         default:
  875.                             Fail();
  876.                             break;
  877.                     }
  878.                     return Convert.ToUInt32(sResponse);
  879.                 }
  880.                 else
  881.                 {
  882.                     throw new Exception("PS3MAPI not connected!");
  883.                 }
  884.             }
  885.             //CORE-----------------------------------------------------------------------------------
  886.             internal static uint Core_Get_Version()
  887.             {
  888.                 if (IsConnected)
  889.                 {
  890.                     SendCommand("CORE GETVERSION");
  891.                     switch (eResponseCode)
  892.                     {
  893.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  894.                         case PS3MAPI_ResponseCode.CommandOK:
  895.                             break;
  896.                         default:
  897.                             Fail();
  898.                             break;
  899.                     }
  900.                     return Convert.ToUInt32(sResponse);
  901.                 }
  902.                 else
  903.                 {
  904.                     throw new Exception("PS3MAPI not connected!");
  905.                 }
  906.             }
  907.             internal static uint Core_GetMinVersion()
  908.             {
  909.                 if (IsConnected)
  910.                 {
  911.                     SendCommand("CORE GETMINVERSION");
  912.                     switch (eResponseCode)
  913.                     {
  914.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  915.                         case PS3MAPI_ResponseCode.CommandOK:
  916.                             break;
  917.                         default:
  918.                             Fail();
  919.                             break;
  920.                     }
  921.                     return Convert.ToUInt32(sResponse);
  922.                 }
  923.                 else
  924.                 {
  925.                     throw new Exception("PS3MAPI not connected!");
  926.                 }
  927.             }
  928.             //PS3------------------------------------------------------------------------------------
  929.             internal static uint PS3_GetFwVersion()
  930.             {
  931.                 if (IsConnected)
  932.                 {
  933.                     SendCommand("PS3 GETFWVERSION");
  934.                     switch (eResponseCode)
  935.                     {
  936.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  937.                         case PS3MAPI_ResponseCode.CommandOK:
  938.                             break;
  939.                         default:
  940.                             Fail();
  941.                             break;
  942.                     }
  943.                     return Convert.ToUInt32(sResponse);
  944.                 }
  945.                 else
  946.                 {
  947.                     throw new Exception("PS3MAPI not connected!");
  948.                 }
  949.             }
  950.             internal static string PS3_GetFirmwareType()
  951.             {
  952.                 if (IsConnected)
  953.                 {
  954.                     SendCommand("PS3 GETFWTYPE");
  955.                     switch (eResponseCode)
  956.                     {
  957.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  958.                         case PS3MAPI_ResponseCode.CommandOK:
  959.                             break;
  960.                         default:
  961.                             Fail();
  962.                             break;
  963.                     }
  964.                     return sResponse;
  965.                 }
  966.                 else
  967.                 {
  968.                     throw new Exception("PS3MAPI not connected!");
  969.                 }
  970.             }
  971.             internal static void PS3_Shutdown()
  972.             {
  973.                 if (IsConnected)
  974.                 {
  975.                     SendCommand("PS3 SHUTDOWN");
  976.                     switch (eResponseCode)
  977.                     {
  978.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  979.                         case PS3MAPI_ResponseCode.CommandOK:
  980.                             Disconnect();
  981.                             break;
  982.                         default:
  983.                             Fail();
  984.                             break;
  985.                     }
  986.  
  987.                 }
  988.                 else
  989.                 {
  990.                     throw new Exception("PS3MAPI not connected!");
  991.                 }
  992.             }
  993.             internal static void PS3_Reboot()
  994.             {
  995.                 if (IsConnected)
  996.                 {
  997.                     SendCommand("PS3 REBOOT");
  998.                     switch (eResponseCode)
  999.                     {
  1000.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1001.                         case PS3MAPI_ResponseCode.CommandOK:
  1002.                             Disconnect();
  1003.                             break;
  1004.                         default:
  1005.                             Fail();
  1006.                             break;
  1007.                     }
  1008.                 }
  1009.                 else
  1010.                 {
  1011.                     throw new Exception("PS3MAPI not connected!");
  1012.                 }
  1013.             }
  1014.             internal static void PS3_SoftReboot()
  1015.             {
  1016.                 if (IsConnected)
  1017.                 {
  1018.                     SendCommand("PS3 SOFTREBOOT");
  1019.                     switch (eResponseCode)
  1020.                     {
  1021.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1022.                         case PS3MAPI_ResponseCode.CommandOK:
  1023.                             Disconnect();
  1024.                             break;
  1025.                         default:
  1026.                             Fail();
  1027.                             break;
  1028.                     }
  1029.                 }
  1030.                 else
  1031.                 {
  1032.                     throw new Exception("PS3MAPI not connected!");
  1033.                 }
  1034.             }
  1035.             internal static void PS3_HardReboot()
  1036.             {
  1037.                 if (IsConnected)
  1038.                 {
  1039.                     SendCommand("PS3 HARDREBOOT");
  1040.                     switch (eResponseCode)
  1041.                     {
  1042.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1043.                         case PS3MAPI_ResponseCode.CommandOK:
  1044.                             Disconnect();
  1045.                             break;
  1046.                         default:
  1047.                             Fail();
  1048.                             break;
  1049.                     }
  1050.                 }
  1051.                 else
  1052.                 {
  1053.                     throw new Exception("PS3MAPI not connected!");
  1054.                 }
  1055.             }
  1056.             internal static void PS3_Notify(string msg)
  1057.             {
  1058.                 if (IsConnected)
  1059.                 {
  1060.                     SendCommand("PS3 NOTIFY  " + msg);
  1061.                     switch (eResponseCode)
  1062.                     {
  1063.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1064.                         case PS3MAPI_ResponseCode.CommandOK:
  1065.                             break;
  1066.                         default:
  1067.                             Fail();
  1068.                             break;
  1069.                     }
  1070.                 }
  1071.                 else
  1072.                 {
  1073.                     throw new Exception("PS3MAPI not connected!");
  1074.                 }
  1075.             }
  1076.             internal static void PS3_Buzzer(int mode)
  1077.             {
  1078.                 if (IsConnected)
  1079.                 {
  1080.                     SendCommand("PS3 BUZZER" + mode .ToString());
  1081.                     switch (eResponseCode)
  1082.                     {
  1083.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1084.                         case PS3MAPI_ResponseCode.CommandOK:
  1085.                             break;
  1086.                         default:
  1087.                             Fail();
  1088.                             break;
  1089.                     }
  1090.                 }
  1091.                 else
  1092.                 {
  1093.                     throw new Exception("PS3MAPI not connected!");
  1094.                 }
  1095.             }
  1096.             internal static void PS3_Led(int color, int mode)
  1097.             {
  1098.                 if (IsConnected)
  1099.                 {
  1100.                     SendCommand("PS3 LED " + color.ToString() + " " + mode.ToString());
  1101.                     switch (eResponseCode)
  1102.                     {
  1103.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1104.                         case PS3MAPI_ResponseCode.CommandOK:
  1105.                             break;
  1106.                         default:
  1107.                             Fail();
  1108.                             break;
  1109.                     }
  1110.                 }
  1111.                 else
  1112.                 {
  1113.                     throw new Exception("PS3MAPI not connected!");
  1114.                 }
  1115.             }
  1116.             internal static void PS3_GetTemp(out uint cpu, out uint rsx)
  1117.             {
  1118.                 cpu = 0; rsx = 0;
  1119.                 if (IsConnected)
  1120.                 {
  1121.                     SendCommand("PS3 GETTEMP");
  1122.                     switch (eResponseCode)
  1123.                     {
  1124.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1125.                         case PS3MAPI_ResponseCode.CommandOK:
  1126.                             break;
  1127.                         default:
  1128.                             Fail();
  1129.                             break;
  1130.                     }
  1131.                     string[] tmp = sResponse.Split(new char[] { '|' });
  1132.                     cpu = System.Convert.ToUInt32(tmp[0], 10);
  1133.                     rsx = System.Convert.ToUInt32(tmp[1], 10);
  1134.                 }
  1135.                 else
  1136.                 {
  1137.                     throw new Exception("PS3MAPI not connected!");
  1138.                 }
  1139.             }
  1140.             internal static void PS3_DisableSyscall(int num)
  1141.             {
  1142.                 if (IsConnected)
  1143.                 {
  1144.                     SendCommand("PS3 DISABLESYSCALL " + num.ToString());
  1145.                     switch (eResponseCode)
  1146.                     {
  1147.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1148.                         case PS3MAPI_ResponseCode.CommandOK:
  1149.                             break;
  1150.                         default:
  1151.                             Fail();
  1152.                             break;
  1153.                     }
  1154.                 }
  1155.                 else
  1156.                 {
  1157.                     throw new Exception("PS3MAPI not connected!");
  1158.                 }
  1159.             }
  1160.             internal static void PS3_ClearHistory(bool include_directory)
  1161.             {
  1162.                 if (IsConnected)
  1163.                 {
  1164.                    if (include_directory) SendCommand("PS3 DELHISTORY+D");
  1165.                    else SendCommand("PS3 DELHISTORY");
  1166.                     switch (eResponseCode)
  1167.                     {
  1168.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1169.                         case PS3MAPI_ResponseCode.CommandOK:
  1170.                             break;
  1171.                         default:
  1172.                             Fail();
  1173.                             break;
  1174.                     }
  1175.                 }
  1176.                 else
  1177.                 {
  1178.                     throw new Exception("PS3MAPI not connected!");
  1179.                 }
  1180.             }
  1181.             internal static bool PS3_CheckSyscall(int num)
  1182.             {
  1183.                 if (IsConnected)
  1184.                 {
  1185.                     SendCommand("PS3 CHECKSYSCALL " + num.ToString());
  1186.                     switch (eResponseCode)
  1187.                     {
  1188.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1189.                         case PS3MAPI_ResponseCode.CommandOK:
  1190.                             break;
  1191.                         default:
  1192.                             Fail();
  1193.                             break;
  1194.                     }
  1195.                     if (Convert.ToInt32(sResponse) == 0) return true;
  1196.                     else return false;
  1197.                 }
  1198.                 else
  1199.                 {
  1200.                     throw new Exception("PS3MAPI not connected!");
  1201.                 }
  1202.             }
  1203.             internal static void PS3_PartialDisableSyscall8(int mode)
  1204.             {
  1205.                 if (IsConnected)
  1206.                 {
  1207.                     SendCommand("PS3 PDISABLESYSCALL8 " + mode.ToString());
  1208.                     switch (eResponseCode)
  1209.                     {
  1210.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1211.                         case PS3MAPI_ResponseCode.CommandOK:
  1212.                             break;
  1213.                         default:
  1214.                             Fail();
  1215.                             break;
  1216.                     }
  1217.                 }
  1218.                 else
  1219.                 {
  1220.                     throw new Exception("PS3MAPI not connected!");
  1221.                 }
  1222.             }
  1223.             internal static int PS3_PartialCheckSyscall8()
  1224.             {
  1225.                 if (IsConnected)
  1226.                 {
  1227.                     SendCommand("PS3 PCHECKSYSCALL8");
  1228.                     switch (eResponseCode)
  1229.                     {
  1230.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1231.                         case PS3MAPI_ResponseCode.CommandOK:
  1232.                             break;
  1233.                         default:
  1234.                             Fail();
  1235.                             break;
  1236.                     }
  1237.                     return Convert.ToInt32(sResponse);
  1238.                 }
  1239.                 else
  1240.                 {
  1241.                     throw new Exception("PS3MAPI not connected!");
  1242.                 }
  1243.             }
  1244.             internal static void PS3_RemoveHook()
  1245.             {
  1246.                 if (IsConnected)
  1247.                 {
  1248.                     SendCommand("PS3 REMOVEHOOK");
  1249.                     switch (eResponseCode)
  1250.                     {
  1251.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1252.                         case PS3MAPI_ResponseCode.CommandOK:
  1253.                             break;
  1254.                         default:
  1255.                             Fail();
  1256.                             break;
  1257.                     }
  1258.                 }
  1259.                 else
  1260.                 {
  1261.                     throw new Exception("PS3MAPI not connected!");
  1262.                 }
  1263.             }
  1264.             //PROCESS--------------------------------------------------------------------------------
  1265.             internal static string Process_GetName(uint pid)
  1266.             {
  1267.                 if (IsConnected)
  1268.                 {
  1269.                     SendCommand("PROCESS GETNAME " + pid.ToString());
  1270.                     switch (eResponseCode)
  1271.                     {
  1272.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1273.                         case PS3MAPI_ResponseCode.CommandOK:
  1274.                             break;
  1275.                         default:
  1276.                             Fail();
  1277.                             break;
  1278.                     }
  1279.                     return sResponse;
  1280.                 }
  1281.                 else
  1282.                 {
  1283.                     throw new Exception("PS3MAPI not connected!");
  1284.                 }
  1285.             }
  1286.             internal static uint[] Process_GetPidList()
  1287.             {
  1288.                 if (IsConnected)
  1289.                 {
  1290.                     SendCommand("PROCESS GETALLPID");
  1291.                     switch (eResponseCode)
  1292.                     {
  1293.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1294.                         case PS3MAPI_ResponseCode.CommandOK:
  1295.                             break;
  1296.                         default:
  1297.                             Fail();
  1298.                             break;
  1299.                     }
  1300.                     int i = 0;
  1301.                     iprocesses_pid = new uint[16];
  1302.                     foreach (string s in sResponse.Split(new char[] { '|' }))
  1303.                     {
  1304.                         if (s.Length != 0 && s != null && s != ""  && s != " " && s!= "0") { iprocesses_pid[i] = Convert.ToUInt32(s, 10); i++; }
  1305.                     }
  1306.                     return iprocesses_pid;
  1307.                 }
  1308.                 else
  1309.                 {
  1310.                     throw new Exception("PS3MAPI not connected!");
  1311.                 }
  1312.             }
  1313.             //MEMORY--------------------------------------------------------------------------------
  1314.             internal static void Memory_Get(uint Pid, uint Address, byte[] Bytes)
  1315.             {
  1316.                 if (IsConnected)
  1317.                 {
  1318.                     SetBinaryMode(true);
  1319.                     int BytesLength = Bytes.Length;
  1320.                     long TotalBytes = 0;
  1321.                     long lBytesReceived = 0;
  1322.                     bool bComplete = false;
  1323.                     OpenDataSocket();
  1324.                     SendCommand("MEMORY GET " + string.Format("{0}", Pid) + " " + string.Format("{0}", Address) + " " + string.Format("{0}", Bytes.Length));
  1325.                     switch (eResponseCode)
  1326.                     {
  1327.                         case PS3MAPI_ResponseCode.DataConnectionAlreadyOpen:
  1328.                         case PS3MAPI_ResponseCode.MemoryStatusOK:
  1329.                             break;
  1330.                         default:
  1331.                             throw new Exception(sResponse);
  1332.                     }
  1333.                     ConnectDataSocket();
  1334.                     while (bComplete != true)
  1335.                     {
  1336.                         try
  1337.                         {
  1338.                             lBytesReceived = data_sock.Receive(Bytes, BytesLength, 0);
  1339.                             if (lBytesReceived > 0)
  1340.                             {
  1341.                                 TotalBytes += lBytesReceived;
  1342.                                 if ((int)(((TotalBytes) * 100) / BytesLength) >= 100) bComplete = true;
  1343.                             }
  1344.                             else
  1345.                             {
  1346.                                 bComplete = true;
  1347.                             }
  1348.                             if (bComplete)
  1349.                             {
  1350.                                 CloseDataSocket();
  1351.                                 ReadResponse();
  1352.                                 switch (eResponseCode)
  1353.                                 {
  1354.                                     case PS3MAPI_ResponseCode.RequestSuccessful:
  1355.                                     case PS3MAPI_ResponseCode.MemoryActionCompleted:
  1356.                                         break;
  1357.                                     default:
  1358.                                         throw new Exception(sResponse);
  1359.                                 }
  1360.                                 SetBinaryMode(false);
  1361.                             }
  1362.                         }
  1363.                         catch (Exception e)
  1364.                         {
  1365.                             CloseDataSocket();
  1366.                             ReadResponse();
  1367.                             SetBinaryMode(false);
  1368.                             throw e;
  1369.                         }
  1370.                     }
  1371.                 }
  1372.                 else
  1373.                 {
  1374.                     throw new Exception("PS3MAPI not connected!");
  1375.                 }
  1376.             }
  1377.             internal static void Memory_Set(uint Pid, uint Address, byte[] Bytes)
  1378.             {
  1379.                 if (IsConnected)
  1380.                 {
  1381.                     SetBinaryMode(true);
  1382.                     int BytesLength = Bytes.Length;
  1383.                     long TotalBytes = 0;
  1384.                     long lBytesSended = 0;
  1385.                     bool bComplete = false;
  1386.                     OpenDataSocket();
  1387.                     SendCommand("MEMORY SET " + string.Format("{0}", Pid) + " " + string.Format("{0}", Address));
  1388.                     switch (eResponseCode)
  1389.                     {
  1390.                         case PS3MAPI_ResponseCode.DataConnectionAlreadyOpen:
  1391.                         case PS3MAPI_ResponseCode.MemoryStatusOK:
  1392.                             break;
  1393.                         default:
  1394.                             throw new Exception(sResponse);
  1395.                     }
  1396.                     ConnectDataSocket();
  1397.                     while (bComplete != true)
  1398.                     {
  1399.                         try
  1400.                         {
  1401.                             lBytesSended = data_sock.Send(Bytes, BytesLength, 0);
  1402.                             bComplete = false;
  1403.                             if (lBytesSended > 0)
  1404.                             {
  1405.                                 TotalBytes += lBytesSended;
  1406.                                 if ((int)(((TotalBytes) * 100) / BytesLength) >= 100) bComplete = true;
  1407.                             }
  1408.                             else
  1409.                             {
  1410.                                 bComplete = true;
  1411.                             }
  1412.                             if (bComplete)
  1413.                             {
  1414.                                 CloseDataSocket();
  1415.                                 ReadResponse();
  1416.                                 switch (eResponseCode)
  1417.                                 {
  1418.                                     case PS3MAPI_ResponseCode.RequestSuccessful:
  1419.                                     case PS3MAPI_ResponseCode.MemoryActionCompleted:
  1420.                                         break;
  1421.                                     default:
  1422.                                         throw new Exception(sResponse);
  1423.                                 }
  1424.                                 SetBinaryMode(false);
  1425.                             }
  1426.                         }
  1427.                         catch (Exception e)
  1428.                         {
  1429.                             CloseDataSocket();
  1430.                             ReadResponse();
  1431.                             SetBinaryMode(false);
  1432.                             throw e;
  1433.                         }
  1434.                     }
  1435.                 }
  1436.                 else
  1437.                 {
  1438.                     throw new Exception("PS3MAPI not connected!");
  1439.                 }
  1440.             }
  1441.             //MODULES--------------------------------------------------------------------------------
  1442.             internal static int[] Module_GetPrxIdList(uint pid)
  1443.             {
  1444.                 if (IsConnected)
  1445.                 {
  1446.                     SendCommand("MODULE GETALLPRXID " + pid.ToString());
  1447.                     switch (eResponseCode)
  1448.                     {
  1449.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1450.                         case PS3MAPI_ResponseCode.CommandOK:
  1451.                             break;
  1452.                         default:
  1453.                             Fail();
  1454.                             break;
  1455.                     }
  1456.                     int i = 0;
  1457.                     imodules_prx_id = new int[128];
  1458.                     foreach (string s in sResponse.Split(new char[] { '|' }))
  1459.                     {
  1460.                         if (s.Length != 0 && s != null && s != "" && s != " " && s != "0") { imodules_prx_id[i] = Convert.ToInt32(s, 10); i++; }
  1461.                     }
  1462.                     return imodules_prx_id;
  1463.                 }
  1464.                 else
  1465.                 {
  1466.                     throw new Exception("PS3MAPI not connected!");
  1467.                 }
  1468.             }
  1469.             internal static string Module_GetName(uint pid, int prxid)
  1470.             {
  1471.                 if (IsConnected)
  1472.                 {
  1473.                     SendCommand("MODULE GETNAME " + pid.ToString() + " " + prxid.ToString());
  1474.                     switch (eResponseCode)
  1475.                     {
  1476.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1477.                         case PS3MAPI_ResponseCode.CommandOK:
  1478.                             break;
  1479.                         default:
  1480.                             Fail();
  1481.                             break;
  1482.                     }
  1483.                     return sResponse;
  1484.                 }
  1485.                 else
  1486.                 {
  1487.                     throw new Exception("PS3MAPI not connected!");
  1488.                 }
  1489.             }
  1490.             internal static string Module_GetFilename(uint pid, int prxid)
  1491.             {
  1492.                 if (IsConnected)
  1493.                 {
  1494.                     SendCommand("MODULE GETFILENAME " + pid.ToString() + " " + prxid.ToString());
  1495.                     switch (eResponseCode)
  1496.                     {
  1497.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1498.                         case PS3MAPI_ResponseCode.CommandOK:
  1499.                             break;
  1500.                         default:
  1501.                             Fail();
  1502.                             break;
  1503.                     }
  1504.                     return sResponse;
  1505.                 }
  1506.                 else
  1507.                 {
  1508.                     throw new Exception("PS3MAPI not connected!");
  1509.                 }
  1510.             }
  1511.             internal static void Module_Load(uint pid, string path)
  1512.             {
  1513.                 if (IsConnected)
  1514.                 {
  1515.                     SendCommand("MODULE LOAD " + pid.ToString() + " " + path);
  1516.                     switch (eResponseCode)
  1517.                     {
  1518.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1519.                         case PS3MAPI_ResponseCode.CommandOK:
  1520.                             break;
  1521.                         default:
  1522.                             Fail();
  1523.                             break;
  1524.                     }
  1525.                 }
  1526.                 else
  1527.                 {
  1528.                     throw new Exception("PS3MAPI not connected!");
  1529.                 }
  1530.             }
  1531.             internal static void Module_Unload(uint pid, int prx_id)
  1532.             {
  1533.                 if (IsConnected)
  1534.                 {
  1535.                     SendCommand("MODULE UNLOAD " + pid.ToString() + " " + prx_id.ToString());
  1536.                     switch (eResponseCode)
  1537.                     {
  1538.                         case PS3MAPI_ResponseCode.RequestSuccessful:
  1539.                         case PS3MAPI_ResponseCode.CommandOK:
  1540.                             break;
  1541.                         default:
  1542.                             Fail();
  1543.                             break;
  1544.                     }
  1545.                 }
  1546.                 else
  1547.                 {
  1548.                     throw new Exception("PS3MAPI not connected!");
  1549.                 }
  1550.             }
  1551.             //----------------------------------------------------------------------------------------
  1552.             internal static void Fail()
  1553.             {
  1554.                 Fail(new Exception("[" + eResponseCode.ToString() + "] " + sResponse));
  1555.             }
  1556.             internal static void Fail(Exception e)
  1557.             {
  1558.                 Disconnect();
  1559.                 throw e;
  1560.             }
  1561.             internal static void SetBinaryMode(bool bMode)
  1562.             {
  1563.                 SendCommand("TYPE" + ((bMode) ? " I" : " A"));
  1564.                 switch (eResponseCode)
  1565.                 {
  1566.                     case PS3MAPI_ResponseCode.RequestSuccessful:
  1567.                     case PS3MAPI_ResponseCode.CommandOK:
  1568.                         break;
  1569.                     default:
  1570.                         Fail();
  1571.                         break;
  1572.                 }
  1573.             }
  1574.             internal static void OpenDataSocket()
  1575.             {
  1576.                 string[] pasv;
  1577.                 string sServer;
  1578.                 int iPort;
  1579.                 Connect();
  1580.                 SendCommand("PASV");
  1581.                 if (eResponseCode != PS3MAPI_ResponseCode.EnteringPassiveMode)
  1582.                 {
  1583.                      Fail();
  1584.                 }
  1585.                 try
  1586.                 {
  1587.                      int i1, i2;
  1588.                      i1 = sResponse.IndexOf('(') + 1;
  1589.                      i2 = sResponse.IndexOf(')') - i1;
  1590.                      pasv = sResponse.Substring(i1, i2).Split(',');
  1591.                  }
  1592.                  catch (Exception)
  1593.                  {
  1594.                      Fail(new Exception("Malformed PASV response: " + sResponse));
  1595.                      throw new Exception("Malformed PASV response: " + sResponse);
  1596.                  }
  1597.  
  1598.                  if (pasv.Length < 6)
  1599.                  {
  1600.                      Fail(new Exception("Malformed PASV response: " + sResponse));
  1601.                  }
  1602.  
  1603.                  sServer = String.Format("{0}.{1}.{2}.{3}", pasv[0], pasv[1], pasv[2], pasv[3]);
  1604.                  iPort = (int.Parse(pasv[4]) << 8) + int.Parse(pasv[5]);
  1605.                  try
  1606.                  {
  1607.                      CloseDataSocket();
  1608.                      data_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  1609.                      data_ipEndPoint = new IPEndPoint(Dns.GetHostByName(sServerIP).AddressList[0], iPort);
  1610.                      data_sock.Connect(data_ipEndPoint);
  1611.                  }
  1612.                  catch (Exception e)
  1613.                  {
  1614.                      throw new Exception("Failed to connect for data transfer: " + e.Message);
  1615.                  }
  1616.             }
  1617.             internal static void ConnectDataSocket()
  1618.             {
  1619.                 if (data_sock != null)      // already connected (always so if passive mode)
  1620.                     return;
  1621.                 try
  1622.                 {
  1623.                     data_sock = listening_sock.Accept();    // Accept is blocking
  1624.                     listening_sock.Close();
  1625.                     listening_sock = null;
  1626.                     if (data_sock == null)
  1627.                     {
  1628.                         throw new Exception("Winsock error: " +
  1629.                             Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));
  1630.                     }
  1631.                 }
  1632.                 catch (Exception ex)
  1633.                 {
  1634.                     throw new Exception("Failed to connect for data transfer: " + ex.Message);
  1635.                 }
  1636.             }
  1637.             internal static void CloseDataSocket()
  1638.             {
  1639.                 if (data_sock != null)
  1640.                 {
  1641.                     if (data_sock.Connected)
  1642.                     {
  1643.                         data_sock.Close();
  1644.                     }
  1645.                     data_sock = null;
  1646.                 }
  1647.                 data_ipEndPoint = null;
  1648.             }
  1649.             internal static void ReadResponse()
  1650.             {
  1651.                 string sBuffer;
  1652.                 sMessages = "";
  1653.                 while (true)
  1654.                 {
  1655.                     sBuffer = GetLineFromBucket();
  1656.                     if (Regex.Match(sBuffer, "^[0-9]+ ").Success)
  1657.                     {
  1658.                         sResponse = sBuffer.Substring(4).Replace("\r", "").Replace("\n", "");
  1659.                         eResponseCode = (PS3MAPI_ResponseCode)int.Parse(sBuffer.Substring(0, 3));
  1660.                         sLog = sLog + "RESPONSE CODE: " + eResponseCode.ToString() + Environment.NewLine;
  1661.                         sLog = sLog + "RESPONSE MSG: " + sResponse + Environment.NewLine + Environment.NewLine;
  1662.                         break;
  1663.                     }
  1664.                     else
  1665.                     {
  1666.                         sMessages += Regex.Replace(sBuffer, "^[0-9]+-", "") + "\n";
  1667.                     }
  1668.                 }        
  1669.             }
  1670.             internal static void SendCommand(string sCommand)
  1671.             {
  1672.                 sLog = sLog + "COMMAND: " + sCommand + Environment.NewLine;
  1673.                 Connect();
  1674.                 Byte[] byCommand = Encoding.ASCII.GetBytes((sCommand + "\r\n").ToCharArray());
  1675.                 main_sock.Send(byCommand, byCommand.Length, 0);
  1676.                 ReadResponse();
  1677.             }
  1678.             internal static void FillBucket()
  1679.             {
  1680.                 Byte[] bytes = new Byte[512];
  1681.                 long lBytesRecieved;
  1682.                 int iMilliSecondsPassed = 0;
  1683.                 while (main_sock.Available < 1)
  1684.                 {
  1685.                     System.Threading.Thread.Sleep(50);
  1686.                     iMilliSecondsPassed += 50;
  1687.  
  1688.                     if (iMilliSecondsPassed > Timeout) // Prevents infinite loop
  1689.                     {
  1690.                         Fail(new Exception("Timed out waiting on server to respond."));
  1691.                     }
  1692.                 }
  1693.                 while (main_sock.Available > 0)
  1694.                 {
  1695.                     // gives any further data not yet received, a small chance to arrive
  1696.                     lBytesRecieved = main_sock.Receive(bytes, 512, 0);
  1697.                     sBucket += Encoding.ASCII.GetString(bytes, 0, (int)lBytesRecieved);
  1698.                     System.Threading.Thread.Sleep(50);
  1699.                 }
  1700.             }
  1701.             internal static string GetLineFromBucket()
  1702.             {
  1703.                 string sBuffer = "";
  1704.                 int i = sBucket.IndexOf('\n');
  1705.  
  1706.                 while (i < 0)
  1707.                 {
  1708.                     FillBucket();
  1709.                     i = sBucket.IndexOf('\n');
  1710.                 }
  1711.  
  1712.                 sBuffer = sBucket.Substring(0, i);
  1713.                 sBucket = sBucket.Substring(i + 1);
  1714.  
  1715.                 return sBuffer;
  1716.             }
  1717.         }
  1718.         #endregion PS3MAPI_Client_Server
  1719.  
  1720.     }
  1721. }
  1722. //BaSs_HaXoR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement