Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-// All source code is protected under the GNU GENERAL PUBLIC LICENSE
- // C# + Android Java Library for CCAPI Socket Connection:
- // Source: https://github.com/Mr-Smithy-x
- // Credits: Mr-Smithy-x (Creator of CCAndroid, RTE for GTA V on Android)
- // Check out his YouTube! -> http://youtube.com/xDudek13lx
- //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-
- //====> JControlConsoleSample/Form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using JControlConsole;
- namespace JControlConsoleSample
- {
- public partial class Form1 : Form, OnRequest
- {
- public Form1()
- {
- InitializeComponent();
- }
- JControlConsoleServer jccs;
- private void button1_Click(object sender, EventArgs e)
- {
- jccs = new JControlConsoleServer(this, 1337);
- this.Text = "Accepting Connections At: " + jccs.getIP() + ":" + jccs.getPort().ToString();
- jccs.Initialize();
- }
- public ResponseTemplate sendResponse(OrganizedRequest organized)
- {
- string message = "";
- string response = "";
- switch (organized.getRequest())
- {
- case "console":
- switch (organized.getPlayer())
- {
- case "Connect":
- message = GlobalAPI.Connect(organized.getParam(0)).getReturnStr();
- response = "Connected";
- return new ResponseTemplate(response, message, null);
- case "Attach":
- message = GlobalAPI.Attach().getReturnStr();
- response = "Attached";
- return new ResponseTemplate(response, message, null);
- case "ShutDown":
- message = GlobalAPI.sendBoot(PS3Lib.CCAPI.RebootFlags.ShutDown).getReturnStr();
- response = "Turned Off";
- return new ResponseTemplate(response, message, null);
- case "SoftReboot":
- message = GlobalAPI.sendBoot(PS3Lib.CCAPI.RebootFlags.SoftReboot).getReturnStr();
- response = "Soft Rebooted";
- return new ResponseTemplate(response, message, null);
- case "HardBoot":
- message = GlobalAPI.sendBoot(PS3Lib.CCAPI.RebootFlags.HardReboot).getReturnStr();
- response = "Hard Rebooted";
- return new ResponseTemplate(response, message, null);
- case "GetConsoles":
- message = "Consoles Loaded";
- response = "Consoles";
- return new ResponseTemplate(response, message, GlobalAPI.getConsoles());
- case "Notify":
- message = GlobalAPI.Notify(organized.getParam(0)).getReturnStr();
- response = "Notified";//
- return new ResponseTemplate(response, message, null);
- case "Buzzer":
- message = GlobalAPI.RingBuzzer().getReturnStr();
- response = "Buzzed";
- return new ResponseTemplate(response, message, null);
- }
- break;
- }
- return null;
- }
- }
- }
- //===>
- //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-
- //##############################################################################
- //JControlConsole--->
- //ParseRequest.cs
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace JControlConsole
- {
- public class ParseRequest
- {
- /// <summary>
- /// Makes the json request after filling the parameters
- /// </summary>
- /// <param name="res">Stands for response, The Android app will do based on the response</param>
- /// <param name="mes">Stands for message, The Android app will pop a message up showing this message</param>
- /// <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>
- /// <returns>This Returns the json String</returns>
- public static string MakeJsonWithPair(string res, string mes,List<KeyValuePair<string, string>> pair)
- {
- ResponseTemplate c = new ResponseTemplate(res, mes, pair);
- return JsonConvert.SerializeObject(c, Formatting.Indented);
- }
- /// <summary>
- /// Makes the json request using the template.
- /// </summary>
- /// <param name="resTemp"></param>
- /// <returns>Returns the Json String of this response template</returns>
- public static string MakeJson(ResponseTemplate resTemp)
- {
- return JsonConvert.SerializeObject(resTemp, Formatting.Indented);
- }
- /// <summary>
- /// 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
- /// </summary>
- /// <param name="Request">The Android Request Json</param>
- /// <param name="sock">The Socket passed from the JControlSample</param>
- /// <param name="gets">This will be pointed to the main ui through the JControlControl</param>
- /// <returns></returns>
- public static ResponseTemplate getResponse(string Request, Socket sock, OnRequest gets)
- {
- if (Request == null || sock == null || gets == null) return null;
- try
- {
- JObject j = (JObject)JsonConvert.DeserializeObject(Request);
- string request = j["request"].ToString();
- string player = j["data"]["player"].ToString();
- string p1 = j["params"]["p1"].ToString();
- string p2 = j["params"]["p2"].ToString();
- string p3 = j["params"]["p3"].ToString();
- string p4 = j["params"]["p4"].ToString();
- string p5 = j["params"]["p5"].ToString();
- string ip = sock.RemoteEndPoint.ToString();
- OrganizedRequest Organized = new OrganizedRequest(request, player, new string[] { p1, p2, p3, p4, p5 });
- 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");
- return gets.sendResponse(Organized);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- return null;
- }
- }
- }
- }
- //##############################################################################
- //JControlConsoleServer.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace JControlConsole
- {
- public class JControlConsoleServer
- {
- private string ip;
- private int port;
- private TcpListener tcpListener;
- private OnRequest getResponse;
- /// <summary>
- /// Initializes the connection
- /// </summary>
- /// <param name="getResponse">After Implementing OnRequest in your form, use "this" in this parameter</param>
- /// <param name="port">Set the port you want it to start listening for request</param>
- public JControlConsoleServer(OnRequest getResponse,int port)
- {
- this.getResponse = getResponse;
- this.ip = WhatsMyIP();
- this.port = port;
- }
- /// <summary>
- /// Initializes the connection
- /// </summary>
- /// <param name="getResponse">After Implementing OnRequest in your form, use "this" in this parameter</param>
- /// <param name="ip">Set a special ip you want to listen on</param>
- /// <param name="port">Set the port number you want it to listen to</param>
- public JControlConsoleServer(OnRequest getResponse, string ip, int port)
- {
- this.getResponse = getResponse;
- this.ip = ip;
- this.port = port;
- }
- /// <summary>
- /// Gets the port number
- /// </summary>
- /// <returns>the port number (int) </returns>
- public int getPort()
- {
- return port;
- }
- /// <summary>
- /// get the ip its listening on
- /// </summary>
- /// <returns>get the ip</returns>
- public string getIP()
- {
- return ip;
- }
- /// <summary>
- /// Gets the computers ip its listening on;
- /// </summary>
- /// <returns>the ip</returns>
- public string WhatsMyIP()
- {
- IPHostEntry host;
- string localIP = "?";
- host = Dns.GetHostEntry(Dns.GetHostName());
- foreach (IPAddress ip in host.AddressList)
- {
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- localIP = ip.ToString();
- }
- }
- return localIP;
- }
- /// <summary>
- /// Starts listening to the ip and the port
- /// </summary>
- public void Initialize()
- {
- Start();
- }
- private void StartAccept()
- {
- Console.WriteLine("Accepting using Async");
- tcpListener.BeginAcceptTcpClient(HandleAsyncConnection, tcpListener);
- }
- private void HandleAsyncConnection(IAsyncResult res)
- {
- StartAccept();
- TcpClient client = tcpListener.EndAcceptTcpClient(res);
- Console.WriteLine("Connection accepted from " + client.Client.RemoteEndPoint);
- byte[] b = new byte[1024];
- int k = client.Client.Receive(b);
- char cc = ' ';
- string test = null;
- Console.WriteLine("Recieved...");
- for (int i = 0; i < k; i++)
- {
- // Console.Write(Convert.ToChar(b[i]));
- cc = Convert.ToChar(b[i]);
- test += cc.ToString();
- }
- if (String.IsNullOrEmpty(test)) Console.WriteLine("Data Recieved From The Android Was Empty: {0}", test);
- Console.WriteLine("Data Recieved From Client: {0}", test);
- try
- {
- if (test != null)
- {
- ResponseTemplate resTemp = ParseRequest.getResponse(test, client.Client, getResponse);
- if (resTemp != null)
- {
- string str = ParseRequest.MakeJson(resTemp);
- Console.WriteLine(str);
- client.Client.Send(ASCIIEncoding.ASCII.GetBytes(str));
- }
- else
- {
- resTemp = new ResponseTemplate("ParseError", "You made connection to the app but we don't speak the same language :(", null);
- string str = ParseRequest.MakeJson(resTemp);
- Console.WriteLine(str);
- client.Client.Send(ASCIIEncoding.ASCII.GetBytes(str));
- }
- }
- }
- catch (Exception ex) { Console.WriteLine(ex.StackTrace); client.Client.Close(); }
- client.Client.Close();
- }
- private void Start()
- {
- try
- {
- IPAddress ipAd = IPAddress.Parse(ip);
- tcpListener = new TcpListener(ipAd, port);
- tcpListener.Start();
- Console.WriteLine("The server is running at port {0}...", port);
- Console.WriteLine("The local End point is: {0}", tcpListener.LocalEndpoint);
- Console.WriteLine("Waiting for a connection.....");
- StartAccept();
- Console.ReadLine();
- }
- catch (Exception e)
- {
- Console.WriteLine("Error..... " + e.StackTrace);
- }
- }
- }
- }
- //##############################################################################
- //GetResponse.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace JControlConsole
- {
- /// <summary>
- /// This is the handler that is passes info from the thread to the main UI and back
- /// It is also where you read the content to tell the server what to do to the ps3
- /// </summary>
- public interface OnRequest
- {
- /// <summary>
- /// 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
- /// </summary>
- /// <param name="organized">The Android Request</param>
- /// <returns>Response Template</returns>
- ResponseTemplate sendResponse(OrganizedRequest organized);
- }
- }
- //PresetClasses.cs
- using PS3Lib;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace JControlConsole
- {
- /// <summary>
- /// I left you with a little preset, to connect to the console and attach shutdown
- /// </summary>
- public class GlobalAPI
- {
- public static PS3API ps3Api = new PS3API(SelectAPI.ControlConsole);
- public static List<KeyValuePair<string, string>> getConsoles()
- {
- List<KeyValuePair<string, string>> Consoles = new List<KeyValuePair<string, string>>();
- List<CCAPI.ConsoleInfo> consoles = ps3Api.CCAPI.GetConsoleList();
- foreach (CCAPI.ConsoleInfo c in consoles)
- {
- Consoles.Add(new KeyValuePair<string, string>(c.Name, c.Ip));
- }
- return Consoles;
- }
- public static SpecialReturn Notify(string message, CCAPI.NotifyIcon icon = CCAPI.NotifyIcon.CAUTION){
- ps3Api.CCAPI.Notify(icon,message);
- return new SpecialReturn(1,"sent notification");
- }
- public static SpecialReturn RingBuzzer(CCAPI.BuzzerMode buzzer = CCAPI.BuzzerMode.Single)
- {
- ps3Api.CCAPI.RingBuzzer(buzzer);
- return new SpecialReturn(1, "Ringed Buzzer");
- }
- public static SpecialReturn Connect(string ip = null)
- {
- if (ip != null)
- {
- if (ps3Api.ConnectTarget(ip) == true)
- {
- return new SpecialReturn("Connected!");
- }
- else
- {
- return new SpecialReturn("Not Connected!");
- }
- }
- else
- {
- if (ps3Api.ConnectTarget() == true)
- {
- return new SpecialReturn("Connected!");
- }
- else
- {
- return new SpecialReturn("Not Connected!");
- }
- }
- }
- public static SpecialReturn Attach()
- {
- if (ps3Api.AttachProcess() == true)
- {
- return new SpecialReturn(1,"Attached!");
- }
- else
- {
- return new SpecialReturn(1,"Could not attach?");
- }
- }
- public static SpecialReturn Disconnect()
- {
- ps3Api.DisconnectTarget();
- return new SpecialReturn(1,"Disconnected");
- }
- public static SpecialReturn sendBoot(CCAPI.RebootFlags boot)
- {
- if (boot == CCAPI.RebootFlags.SoftReboot)
- {
- ps3Api.CCAPI.ShutDown(CCAPI.RebootFlags.SoftReboot);
- return new SpecialReturn(1,"SoftBoot Initiated!");
- }
- else if (boot == CCAPI.RebootFlags.HardReboot)
- {
- ps3Api.CCAPI.ShutDown(CCAPI.RebootFlags.HardReboot);
- return new SpecialReturn(1,"HardBoot Initiated!");
- }
- else if (boot == CCAPI.RebootFlags.ShutDown)
- {
- ps3Api.CCAPI.ShutDown(CCAPI.RebootFlags.ShutDown);
- return new SpecialReturn(1,"Shutdown Initiated!");
- }
- else
- {
- return new SpecialReturn(0,"Could Not Understand");
- }
- }
- }
- }
- //##############################################################################
- //SpecialReturn.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace JControlConsole
- {
- /// <summary>
- /// returns 2 types, maybe useful for true and false and a message to display
- /// </summary>
- public class SpecialReturn
- {
- int returnInt;
- string returnStr;
- public int getReturnedInt(){
- return returnInt;
- }
- public string getReturnStr(){
- return returnStr;
- }
- public SpecialReturn(int returnInt)
- {
- this.returnInt = returnInt;
- }
- public SpecialReturn(string returnStr)
- {
- this.returnStr = returnStr;
- }
- public SpecialReturn(int retInt, string retStr)
- {
- this.returnInt = retInt;
- this.returnStr = retStr;
- }
- }
- }
- //JControlConsole--->
- //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-
- // Me and this guy happened to start working on Android Real Time Modding around the same time. (:P)
- // I did Real Time Modding over Android for TMAPI; while, he did Real Time Modding for Android for CCAPI
- //~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-~~~-#-//
- //BaSs_HaXoR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement