Advertisement
BaSs_HaXoR

JControlConsole Source Code [Android RTE CCAPI]

Jan 11th, 2015
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.79 KB | None | 0 0
  1. //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-// All source code is protected under the GNU GENERAL PUBLIC LICENSE
  2. // C# + Android Java Library for CCAPI Socket Connection:
  3. // Source: https://github.com/Mr-Smithy-x
  4. // Credits: Mr-Smithy-x (Creator of CCAndroid, RTE for GTA V on Android)
  5. // Check out his YouTube! -> http://youtube.com/xDudek13lx
  6. //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-
  7.  
  8. //====> JControlConsoleSample/Form1.cs
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using JControlConsole;
  19.  
  20. namespace JControlConsoleSample
  21. {
  22.     public partial class Form1 : Form, OnRequest
  23.     {
  24.         public Form1()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.         JControlConsoleServer jccs;
  30.         private void button1_Click(object sender, EventArgs e)
  31.         {
  32.             jccs = new JControlConsoleServer(this, 1337);
  33.             this.Text = "Accepting Connections At: " + jccs.getIP() + ":" + jccs.getPort().ToString();
  34.             jccs.Initialize();
  35.  
  36.         }
  37.  
  38.         public ResponseTemplate sendResponse(OrganizedRequest organized)
  39.         {
  40.             string message = "";
  41.             string response = "";
  42.             switch (organized.getRequest())
  43.             {
  44.                 case "console":
  45.                     switch (organized.getPlayer())
  46.                     {
  47.  
  48.                         case "Connect":
  49.                             message = GlobalAPI.Connect(organized.getParam(0)).getReturnStr();
  50.                             response = "Connected";
  51.                             return new ResponseTemplate(response, message, null);
  52.                         case "Attach":
  53.                             message = GlobalAPI.Attach().getReturnStr();
  54.                             response = "Attached";
  55.                             return new ResponseTemplate(response, message, null);
  56.                         case "ShutDown":
  57.                             message = GlobalAPI.sendBoot(PS3Lib.CCAPI.RebootFlags.ShutDown).getReturnStr();
  58.                             response = "Turned Off";
  59.                             return new ResponseTemplate(response, message, null);
  60.                         case "SoftReboot":
  61.                             message = GlobalAPI.sendBoot(PS3Lib.CCAPI.RebootFlags.SoftReboot).getReturnStr();
  62.                             response = "Soft Rebooted";
  63.                             return new ResponseTemplate(response, message, null);
  64.                         case "HardBoot":
  65.                             message = GlobalAPI.sendBoot(PS3Lib.CCAPI.RebootFlags.HardReboot).getReturnStr();
  66.                             response = "Hard Rebooted";
  67.                             return new ResponseTemplate(response, message, null);
  68.                         case "GetConsoles":
  69.                             message = "Consoles Loaded";
  70.                             response = "Consoles";
  71.                             return new ResponseTemplate(response, message, GlobalAPI.getConsoles());
  72.                         case "Notify":
  73.                             message = GlobalAPI.Notify(organized.getParam(0)).getReturnStr();
  74.                             response = "Notified";//
  75.                             return new ResponseTemplate(response, message, null);
  76.                         case "Buzzer":
  77.                             message = GlobalAPI.RingBuzzer().getReturnStr();
  78.                             response = "Buzzed";
  79.                             return new ResponseTemplate(response, message, null);
  80.  
  81.                     }
  82.                     break;
  83.             }
  84.             return null;
  85.  
  86.         }
  87.        
  88.     }
  89. }
  90. //===>
  91. //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-
  92.  
  93. //##############################################################################
  94. //JControlConsole--->
  95. //ParseRequest.cs
  96. using Newtonsoft.Json;
  97. using Newtonsoft.Json.Linq;
  98. using System;
  99. using System.Collections.Generic;
  100. using System.Linq;
  101. using System.Net.Sockets;
  102. using System.Text;
  103. using System.Threading.Tasks;
  104.  
  105. namespace JControlConsole
  106. {
  107.     public class ParseRequest
  108.     {
  109.         /// <summary>
  110.         /// Makes the json request after filling the parameters
  111.         /// </summary>
  112.         /// <param name="res">Stands for response, The Android app will do based on the response</param>
  113.         /// <param name="mes">Stands for message, The Android app will pop a message up showing this message</param>
  114.         /// <param name="pair">Stands for pair, If this is filled, the android app will put this in a json array for the android to parse</param>
  115.         /// <returns>This Returns the json String</returns>
  116.         public static string MakeJsonWithPair(string res, string mes,List<KeyValuePair<string, string>> pair)
  117.         {
  118.             ResponseTemplate c = new ResponseTemplate(res, mes, pair);
  119.             return JsonConvert.SerializeObject(c, Formatting.Indented);
  120.         }
  121.         /// <summary>
  122.         /// Makes the json request using the template.
  123.         /// </summary>
  124.         /// <param name="resTemp"></param>
  125.         /// <returns>Returns the Json String of this response template</returns>
  126.         public static string MakeJson(ResponseTemplate resTemp)
  127.         {
  128.             return JsonConvert.SerializeObject(resTemp, Formatting.Indented);
  129.         }
  130.  
  131.         /// <summary>
  132.         /// This parses the json that the android sent to the server, this will parse it in contents and send the parsed content to the main UI, for TODO to take place
  133.         /// </summary>
  134.         /// <param name="Request">The Android Request Json</param>
  135.         /// <param name="sock">The Socket passed from the JControlSample</param>
  136.         /// <param name="gets">This will be pointed to the main ui through the JControlControl</param>
  137.         /// <returns></returns>
  138.         public static ResponseTemplate getResponse(string Request, Socket sock, OnRequest gets)
  139.         {
  140.             if (Request == null || sock == null || gets == null) return null;
  141.             try
  142.             {
  143.                 JObject j = (JObject)JsonConvert.DeserializeObject(Request);
  144.                 string request = j["request"].ToString();
  145.                 string player = j["data"]["player"].ToString();
  146.                 string p1 = j["params"]["p1"].ToString();
  147.                 string p2 = j["params"]["p2"].ToString();
  148.                 string p3 = j["params"]["p3"].ToString();
  149.                 string p4 = j["params"]["p4"].ToString();
  150.                 string p5 = j["params"]["p5"].ToString();
  151.                 string ip = sock.RemoteEndPoint.ToString();
  152.                 OrganizedRequest Organized = new OrganizedRequest(request, player, new string[] { p1, p2, p3, p4, p5 });
  153.                 Console.WriteLine("---------------\nRequest: {0}\nPlayer: {1}\nParam 1: {2}\nParam 2: {3} \nParam 3: {4}\nParam 4: {5}\nParam 5: {6}\n From: {7}", Organized.getRequest(), Organized.getPlayer(),Organized.getParam(0),Organized.getParam(1),Organized.getParam(2), Organized.getParam(3), Organized.getParam(4), ip + "\n----------------\n");
  154.                 return gets.sendResponse(Organized);
  155.  
  156.             }
  157.             catch (Exception ex)
  158.             {
  159.                 Console.WriteLine(ex);
  160.                 return null;
  161.             }
  162.         }
  163.  
  164.     }
  165. }
  166.  
  167. //##############################################################################
  168. //JControlConsoleServer.cs
  169. using System;
  170. using System.Collections.Generic;
  171. using System.Linq;
  172. using System.Net;
  173. using System.Net.Sockets;
  174. using System.Text;
  175. using System.Threading.Tasks;
  176. using System.Threading;
  177.  
  178. namespace JControlConsole
  179. {
  180.     public class JControlConsoleServer
  181.     {
  182.         private string ip;
  183.         private int port;
  184.         private TcpListener tcpListener;
  185.         private OnRequest getResponse;
  186.  
  187.         /// <summary>
  188.         /// Initializes the connection
  189.         /// </summary>
  190.         /// <param name="getResponse">After Implementing OnRequest in your form, use "this" in this parameter</param>
  191.         /// <param name="port">Set the port you want it to start listening for request</param>
  192.         public JControlConsoleServer(OnRequest getResponse,int port)
  193.         {
  194.             this.getResponse = getResponse;
  195.             this.ip = WhatsMyIP();
  196.             this.port = port;
  197.         }
  198.         /// <summary>
  199.         /// Initializes the connection
  200.         /// </summary>
  201.         /// <param name="getResponse">After Implementing OnRequest in your form, use "this" in this parameter</param>
  202.         /// <param name="ip">Set a special ip you want to listen on</param>
  203.         /// <param name="port">Set the port number you want it to listen to</param>
  204.         public JControlConsoleServer(OnRequest getResponse, string ip, int port)
  205.         {
  206.             this.getResponse = getResponse;
  207.             this.ip = ip;
  208.             this.port = port;
  209.         }
  210.         /// <summary>
  211.         /// Gets the port number
  212.         /// </summary>
  213.         /// <returns>the port number (int) </returns>
  214.         public int getPort()
  215.         {
  216.             return port;
  217.         }
  218.         /// <summary>
  219.         /// get the ip its listening on
  220.         /// </summary>
  221.         /// <returns>get the ip</returns>
  222.         public string getIP()
  223.         {
  224.             return ip;
  225.         }
  226.         /// <summary>
  227.         /// Gets the computers ip its listening on;
  228.         /// </summary>
  229.         /// <returns>the ip</returns>
  230.         public string WhatsMyIP()
  231.         {
  232.             IPHostEntry host;
  233.             string localIP = "?";
  234.             host = Dns.GetHostEntry(Dns.GetHostName());
  235.             foreach (IPAddress ip in host.AddressList)
  236.             {
  237.                 if (ip.AddressFamily == AddressFamily.InterNetwork)
  238.                 {
  239.                     localIP = ip.ToString();
  240.                 }
  241.             }
  242.             return localIP;
  243.         }
  244.         /// <summary>
  245.         /// Starts listening to the ip and the port
  246.         /// </summary>
  247.         public void Initialize()
  248.         {
  249.             Start();
  250.         }
  251.         private void StartAccept()
  252.         {
  253.             Console.WriteLine("Accepting using Async");
  254.             tcpListener.BeginAcceptTcpClient(HandleAsyncConnection, tcpListener);
  255.  
  256.         }
  257.         private void HandleAsyncConnection(IAsyncResult res)
  258.         {
  259.             StartAccept();
  260.             TcpClient client = tcpListener.EndAcceptTcpClient(res);
  261.             Console.WriteLine("Connection accepted from " + client.Client.RemoteEndPoint);
  262.             byte[] b = new byte[1024];
  263.             int k = client.Client.Receive(b);
  264.             char cc = ' ';
  265.             string test = null;
  266.             Console.WriteLine("Recieved...");
  267.             for (int i = 0; i < k; i++)
  268.             {
  269.             //  Console.Write(Convert.ToChar(b[i]));
  270.                 cc = Convert.ToChar(b[i]);
  271.                 test += cc.ToString();
  272.             }
  273.             if (String.IsNullOrEmpty(test)) Console.WriteLine("Data Recieved From The Android Was Empty: {0}", test);
  274.             Console.WriteLine("Data Recieved From Client: {0}", test);
  275.             try
  276.             {
  277.                 if (test != null)
  278.                 {
  279.                     ResponseTemplate resTemp = ParseRequest.getResponse(test, client.Client, getResponse);
  280.                     if (resTemp != null)
  281.                     {
  282.                         string str = ParseRequest.MakeJson(resTemp);
  283.                         Console.WriteLine(str);
  284.                         client.Client.Send(ASCIIEncoding.ASCII.GetBytes(str));
  285.                     }
  286.                     else
  287.                     {
  288.                         resTemp = new ResponseTemplate("ParseError", "You made connection to the app but we don't speak the same language :(", null);
  289.                         string str = ParseRequest.MakeJson(resTemp);
  290.                         Console.WriteLine(str);
  291.                         client.Client.Send(ASCIIEncoding.ASCII.GetBytes(str));
  292.                     }
  293.                 }
  294.             }
  295.             catch (Exception ex) { Console.WriteLine(ex.StackTrace); client.Client.Close(); }
  296.             client.Client.Close();
  297.         }
  298.         private void Start()
  299.         {
  300.             try
  301.             {
  302.                 IPAddress ipAd = IPAddress.Parse(ip);
  303.                 tcpListener = new TcpListener(ipAd, port);
  304.                 tcpListener.Start();
  305.                 Console.WriteLine("The server is running at port {0}...", port);
  306.                 Console.WriteLine("The local End point is: {0}", tcpListener.LocalEndpoint);
  307.                 Console.WriteLine("Waiting for a connection.....");
  308.                 StartAccept();
  309.                 Console.ReadLine();
  310.             }
  311.             catch (Exception e)
  312.             {
  313.                 Console.WriteLine("Error..... " + e.StackTrace);
  314.             }
  315.         }
  316.  
  317.     }
  318. }
  319.  
  320. //##############################################################################
  321. //GetResponse.cs
  322. using System;
  323. using System.Collections.Generic;
  324. using System.Linq;
  325. using System.Net.Sockets;
  326. using System.Text;
  327. using System.Threading.Tasks;
  328.  
  329. namespace JControlConsole
  330. {
  331.     /// <summary>
  332.     /// This is the handler that is passes info from the thread to the main UI and back
  333.     /// It is also where you read the content to tell the server what to do to the ps3
  334.     /// </summary>
  335.     public interface OnRequest
  336.     {
  337.         /// <summary>
  338.         /// Based on the organized request you need to use switch & case to tell the app what to do to the ps3, i left you with a sample
  339.         /// </summary>
  340.         /// <param name="organized">The Android Request</param>
  341.         /// <returns>Response Template</returns>
  342.         ResponseTemplate sendResponse(OrganizedRequest organized);
  343.     }
  344. }
  345.  
  346.  
  347. //PresetClasses.cs
  348. using PS3Lib;
  349. using System;
  350. using System.Collections.Generic;
  351. using System.Linq;
  352. using System.Text;
  353. using System.Threading.Tasks;
  354.  
  355. namespace JControlConsole
  356. {
  357.     /// <summary>
  358.     /// I left you with a little preset, to connect to the console and attach shutdown
  359.     /// </summary>
  360.     public class GlobalAPI
  361.     {
  362.         public static PS3API ps3Api = new PS3API(SelectAPI.ControlConsole);
  363.  
  364.         public static List<KeyValuePair<string, string>> getConsoles()
  365.         {
  366.             List<KeyValuePair<string, string>> Consoles = new List<KeyValuePair<string, string>>();
  367.             List<CCAPI.ConsoleInfo> consoles = ps3Api.CCAPI.GetConsoleList();
  368.             foreach (CCAPI.ConsoleInfo c in consoles)
  369.             {
  370.                 Consoles.Add(new KeyValuePair<string, string>(c.Name, c.Ip));
  371.             }
  372.             return Consoles;
  373.         }
  374.  
  375.         public static SpecialReturn Notify(string message, CCAPI.NotifyIcon icon = CCAPI.NotifyIcon.CAUTION){
  376.             ps3Api.CCAPI.Notify(icon,message);
  377.             return new SpecialReturn(1,"sent notification");
  378.         }
  379.  
  380.         public static SpecialReturn RingBuzzer(CCAPI.BuzzerMode buzzer = CCAPI.BuzzerMode.Single)
  381.         {
  382.             ps3Api.CCAPI.RingBuzzer(buzzer);
  383.             return new SpecialReturn(1, "Ringed Buzzer");
  384.         }
  385.         public static SpecialReturn Connect(string ip = null)
  386.         {
  387.            
  388.             if (ip != null)
  389.             {
  390.                 if (ps3Api.ConnectTarget(ip) == true)
  391.                 {
  392.  
  393.                     return new SpecialReturn("Connected!");
  394.                 }
  395.                 else
  396.                 {
  397.                     return new SpecialReturn("Not Connected!");
  398.                 }
  399.             }
  400.             else
  401.             {
  402.                 if (ps3Api.ConnectTarget() == true)
  403.                 {
  404.  
  405.                     return new SpecialReturn("Connected!");
  406.                 }
  407.                 else
  408.                 {
  409.                     return new SpecialReturn("Not Connected!");
  410.                 }
  411.             }
  412.            
  413.            
  414.         }
  415.         public static SpecialReturn Attach()
  416.         {
  417.  
  418.             if (ps3Api.AttachProcess() == true)
  419.             {
  420.                 return new SpecialReturn(1,"Attached!");
  421.             }
  422.             else
  423.             {
  424.                 return new SpecialReturn(1,"Could not attach?");
  425.             }
  426.  
  427.  
  428.         }
  429.  
  430.         public static SpecialReturn Disconnect()
  431.         {
  432.             ps3Api.DisconnectTarget();
  433.             return new SpecialReturn(1,"Disconnected");
  434.         }
  435.        
  436.         public static SpecialReturn sendBoot(CCAPI.RebootFlags boot)
  437.         {
  438.  
  439.             if (boot == CCAPI.RebootFlags.SoftReboot)
  440.             {
  441.                 ps3Api.CCAPI.ShutDown(CCAPI.RebootFlags.SoftReboot);
  442.                 return new SpecialReturn(1,"SoftBoot Initiated!");
  443.             }
  444.             else if (boot == CCAPI.RebootFlags.HardReboot)
  445.             {
  446.  
  447.                 ps3Api.CCAPI.ShutDown(CCAPI.RebootFlags.HardReboot);
  448.                 return new SpecialReturn(1,"HardBoot Initiated!");
  449.             }
  450.             else if (boot == CCAPI.RebootFlags.ShutDown)
  451.             {
  452.  
  453.                 ps3Api.CCAPI.ShutDown(CCAPI.RebootFlags.ShutDown);
  454.                 return new SpecialReturn(1,"Shutdown Initiated!");
  455.             }
  456.             else
  457.             {
  458.                 return new SpecialReturn(0,"Could Not Understand");
  459.             }
  460.  
  461.         }
  462.     }
  463. }
  464.  
  465. //##############################################################################
  466. //SpecialReturn.cs
  467. using System;
  468. using System.Collections.Generic;
  469. using System.Linq;
  470. using System.Text;
  471. using System.Threading.Tasks;
  472.  
  473. namespace JControlConsole
  474. {
  475.     /// <summary>
  476.     /// returns 2 types, maybe useful for true and false and a message to display
  477.     /// </summary>
  478.     public class SpecialReturn
  479.     {
  480.         int returnInt;
  481.         string returnStr;
  482.         public int getReturnedInt(){
  483.             return returnInt;
  484.         }
  485.         public string getReturnStr(){
  486.             return returnStr;
  487.         }
  488.         public SpecialReturn(int returnInt)
  489.         {
  490.             this.returnInt = returnInt;
  491.         }
  492.         public SpecialReturn(string returnStr)
  493.         {
  494.             this.returnStr = returnStr;
  495.         }
  496.         public SpecialReturn(int retInt, string retStr)
  497.         {
  498.             this.returnInt = retInt;
  499.             this.returnStr = retStr;
  500.         }
  501.     }
  502. }
  503. //JControlConsole--->
  504.  
  505. //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-
  506. // Me and this guy happened to start working on Android Real Time Modding around the same time. (:P)
  507. // I did Real Time Modding over Android for TMAPI; while, he did Real Time Modding for Android for CCAPI
  508. //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-//
  509. //BaSs_HaXoR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement