Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ************************************************* //
- // --- Copyright (c) 2014 iMCS Productions --- //
- // ************************************************* //
- // PS3Lib v4 By FM|T iMCSx //
- // //
- // Features v4.3 : //
- // - Support CCAPI v2.5 C# by iMCSx //
- // - Popup better form with icon //
- // - CCAPI Consoles List Popup French/English //
- // - CCAPI Get Console Info //
- // - CCAPI Get Console List //
- // - CCAPI Get Number Of Consoles //
- // - Get Console Name TMAPI/CCAPI //
- // //
- // Credits : FM|T Enstone , Buc-ShoTz //
- // //
- // Follow me : //
- // //
- // FrenchModdingTeam.com //
- // Youtube.com/iMCSx //
- // Twitter.com/iMCSx //
- // Facebook.com/iMCSx //
- // //
- // ************************************************* //
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace PS3Lib
- {
- public class CCAPI
- {
- public CCAPI()
- {
- if (GetDllVersion() < 250)
- {
- MessageBox.Show("Invalid CCAPI version.\n\nPlease install the version 2.50.", "Re-install CCAPI v2.50", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi15initTargetCommsEv", CallingConvention = CallingConvention.Cdecl)]
- private static extern int initTargetComms();
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi16closeTargetCommsEv", CallingConvention = CallingConvention.Cdecl)]
- private static extern int closeTargetComms();
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14connectConsoleEPKc", CallingConvention = CallingConvention.Cdecl)]
- private static extern int connectConsole(string targetIP);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi17disconnectConsoleEi", CallingConvention = CallingConvention.Cdecl)]
- private static extern int disconnectConsole(int connectionID);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14getProcessListEiPjS0_", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getProcessList(int connectionID, ref uint numberProcesses, IntPtr processIdPtr);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14getProcessNameEijPc", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getProcessName(int connectionID, uint processID, IntPtr strPtr);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi16getProcessMemoryEijyjPh", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getProcessMemory(int connectionID, uint processID, ulong offset, uint size, byte[] buffOut);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi16setProcessMemoryEijyjPh", CallingConvention = CallingConvention.Cdecl)]
- private static extern int setProcessMemory(int connectionID, uint processID, ulong offset, uint size, byte[] buffIn);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi8shutdownEii", CallingConvention = CallingConvention.Cdecl)]
- private static extern int shutdown(int connectionId, int mode);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi10ringBuzzerEii", CallingConvention = CallingConvention.Cdecl)]
- private static extern int ringBuzzer(int connectionId, int type);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi13setConsoleLedEiii", CallingConvention = CallingConvention.Cdecl)]
- private static extern int setConsoleLed(int connectionId, int color, int status);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi15getFirmwareInfoEiPiS0_PyS0_", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getFirmwareInfo(int connectionId, ref int firmware, ref int ccapi, ref ulong systable, ref int consoleType);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi14getTemperatureEiPj", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getTemperature(int connectionId, IntPtr tempPtr);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi12setConsoleIDEiPh", CallingConvention = CallingConvention.Cdecl)]
- private static extern int setConsoleID(int connectionId, byte[] consoleID);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi6notifyEiiPw", CallingConvention = CallingConvention.Cdecl)]
- private static extern int notify(int connectionId, int mode, [MarshalAsAttribute(UnmanagedType.LPWStr)] string msgWChar);
- [DllImport("CCAPI.dll", EntryPoint = "_ZN5CcApi13getDllVersionEv", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getDllVersion();
- [DllImport("CCAPI.dll", EntryPoint = "_ZN8Settings19getNumberOfConsolesEv", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getNumberOfConsoles();
- [DllImport("CCAPI.dll", EntryPoint = "_ZN8Settings14getConsoleInfoEiPcS0_", CallingConvention = CallingConvention.Cdecl)]
- private static extern int getConsoleInfo(int index, IntPtr ptrN, IntPtr ptrI);
- public enum NotifyIcon
- {
- INFO,
- CAUTION,
- FRIEND,
- SLIDER,
- WRONGWAY,
- DIALOG,
- DIALOGSHADOW,
- TEXT,
- POINTER,
- GRAB,
- HAND,
- PEN,
- FINGER,
- ARROW,
- ARROWRIGHT,
- PROGRESS
- }
- public enum ConsoleType
- {
- CEX = 1,
- DEX = 2,
- TOOL = 3
- }
- public enum ProcessType
- {
- VSH,
- SYS_AGENT,
- CURRENTGAME
- }
- public enum RebootFlags
- {
- ShutDown = 1,
- SoftReboot = 2,
- HardReboot = 3
- }
- public enum BuzzerMode
- {
- Continuous,
- Single,
- Double
- }
- public enum LedColor
- {
- Green = 1,
- Red = 2
- }
- public enum LedMode
- {
- Off,
- On,
- Blink
- }
- private TargetInfo pInfo = new TargetInfo();
- private class CCAPIGlobalPointer
- {
- private IntPtr _intPtr = IntPtr.Zero;
- public CCAPIGlobalPointer(IntPtr intPtr)
- {
- _intPtr = intPtr;
- }
- ~CCAPIGlobalPointer()
- { if (_intPtr != IntPtr.Zero)
- Marshal.FreeHGlobal(_intPtr); }
- public IntPtr GetPtr()
- {
- return _intPtr;
- }
- }
- private IntPtr ReadDataFromUnBufPtr<T>(IntPtr unBuf, ref T storage)
- {
- storage = (T)Marshal.PtrToStructure(unBuf, typeof(T));
- return new IntPtr(unBuf.ToInt64() + Marshal.SizeOf((T)storage));
- }
- private class System
- {
- public static int
- connectionID = -1;
- public static uint
- processID = 0;
- public static uint[]
- processIDs;
- }
- /// <summary>Get informations from your target.</summary>
- public class TargetInfo
- {
- public int
- Firmware = 0,
- CCAPI = 0,
- ConsoleType = 0,
- TempCell = 0,
- TempRSX = 0;
- public ulong
- SysTable = 0;
- }
- /// <summary>Get Info for targets.</summary>
- public class ConsoleInfo
- {
- public string
- Name,
- Ip;
- }
- public Extension Extension
- {
- get { return new Extension(SelectAPI.ControlConsole); }
- }
- private void CompleteInfo(ref TargetInfo Info, int fw, int ccapi, ulong sysTable, int consoleType, int tempCELL, int tempRSX)
- {
- Info.Firmware = fw;
- Info.CCAPI = ccapi;
- Info.SysTable = sysTable;
- Info.ConsoleType = consoleType;
- Info.TempCell = tempCELL;
- Info.TempRSX = tempRSX;
- }
- /// <summary>Return true if a ccapi function return a good integer.</summary>
- public bool SUCCESS(int Void)
- {
- if (Void >= 0)
- return true;
- else return false;
- }
- /// <summary>Connect your console by console list.</summary>
- public bool ConnectTarget()
- {
- return new PS3API.ConsoleList(new PS3API(SelectAPI.ControlConsole)).Show();
- }
- /// <summary>Connect your console by ip address.</summary>
- public int ConnectTarget(string targetIP)
- {
- if (SUCCESS(System.connectionID))
- disconnectConsole(System.connectionID);
- initTargetComms();
- System.connectionID = connectConsole(targetIP);
- if (System.connectionID == -7)
- MessageBox.Show("Invalid version between CCAPI.dll and the version installed on your ps3.\n\nPlease re-install the same version.", "Invalid Version", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return System.connectionID;
- }
- /// <summary>Disconnect your console.</summary>
- public int DisconnectTarget()
- {
- return disconnectConsole(System.connectionID);
- }
- /// <summary>Attach the default process (Current Game).</summary>
- public int AttachProcess()
- {
- int result = -1; System.processID = 0;
- result = GetProcessList(out System.processIDs);
- if (SUCCESS(result) && System.processIDs.Length > 0)
- {
- for (int i = 0; i < System.processIDs.Length; i++)
- {
- string name = String.Empty;
- result = GetProcessName(System.processIDs[i], out name);
- if (!SUCCESS(result))
- break;
- if (!name.Contains("flash"))
- {
- System.processID = System.processIDs[i];
- break;
- }
- else result = -1;
- }
- if(System.processID == 0)
- System.processID = System.processIDs[System.processIDs.Length-1];
- }
- else result = -1;
- return result;
- }
- /// <summary>Attach your desired process.</summary>
- public int AttachProcess(ProcessType procType)
- {
- int result = -1; System.processID = 0;
- result = GetProcessList(out System.processIDs);
- if (result >= 0 && System.processIDs.Length > 0)
- {
- for (int i = 0; i < System.processIDs.Length; i++)
- {
- string name = String.Empty;
- result = GetProcessName(System.processIDs[i], out name);
- if (result < 0)
- break;
- if(procType == ProcessType.VSH && name.Contains("vsh"))
- {
- System.processID = System.processIDs[i]; break;
- }
- else if (procType == ProcessType.SYS_AGENT && name.Contains("agent"))
- {
- System.processID = System.processIDs[i]; break;
- }
- else if (procType == ProcessType.CURRENTGAME && !name.Contains("flash"))
- {
- System.processID = System.processIDs[i]; break;
- }
- }
- if (System.processID == 0)
- System.processID = System.processIDs[System.processIDs.Length - 1];
- }
- else result = -1;
- return result;
- }
- /// <summary>Attach your desired process.</summary>
- public int AttachProcess(uint process)
- {
- int result = -1;
- uint[] procs = new uint[64];
- result = GetProcessList(out procs);
- if(SUCCESS(result))
- {
- for (int i = 0; i < procs.Length; i++)
- {
- if (procs[i] == process)
- {
- result = 0;
- System.processID = process;
- break;
- }
- else result = -1;
- }
- }
- return result;
- }
- /// <summary>Get a list of all processes available.</summary>
- public int GetProcessList(out uint[] processIds)
- {
- uint numOfProcs = 0; int result = -1;
- CCAPIGlobalPointer uPtr = new CCAPIGlobalPointer(Marshal.AllocHGlobal((int) (4*0x40)));
- result = getProcessList(System.connectionID, ref numOfProcs, uPtr.GetPtr());
- processIds = new uint[numOfProcs];
- if (SUCCESS(result))
- {
- IntPtr unBuf = uPtr.GetPtr();
- for (uint i = 0; i < numOfProcs; i++)
- unBuf = ReadDataFromUnBufPtr<uint>(unBuf, ref processIds[i]);
- }
- return result;
- }
- /// <summary>Get the process name of your choice.</summary>
- public int GetProcessName(uint processId, out string name)
- {
- IntPtr ptr = Marshal.AllocHGlobal((int)(0x211)); int result = -1;
- result = getProcessName(System.connectionID, processId, ptr);
- name = String.Empty;
- if(SUCCESS(result))
- name = Marshal.PtrToStringAnsi(ptr);
- return result;
- }
- /// <summary>Return the current process attached. Use this function only if you called AttachProcess before.</summary>
- public uint GetAttachedProcess()
- {
- return System.processID;
- }
- /// <summary>Set memory to offset (uint).</summary>
- public int SetMemory(uint offset, byte[] buffer)
- {
- return setProcessMemory(System.connectionID, System.processID, (ulong)offset, (uint)buffer.Length, buffer);
- }
- /// <summary>Set memory to offset (ulong).</summary>
- public int SetMemory(ulong offset, byte[] buffer)
- {
- return setProcessMemory(System.connectionID, System.processID, offset, (uint)buffer.Length, buffer);
- }
- /// <summary>Set memory to offset (string hex).</summary>
- public int SetMemory(ulong offset, string hexadecimal, EndianType Type = EndianType.BigEndian)
- {
- byte[] Entry = StringToByteArray(hexadecimal);
- if (Type == EndianType.LittleEndian)
- Array.Reverse(Entry);
- return setProcessMemory(System.connectionID, System.processID, offset, (uint)Entry.Length, Entry);
- }
- /// <summary>Get memory from offset (uint).</summary>
- public int GetMemory(uint offset, byte[] buffer)
- {
- return getProcessMemory(System.connectionID, System.processID, (ulong)offset, (uint)buffer.Length, buffer);
- }
- /// <summary>Get memory from offset (ulong).</summary>
- public int GetMemory(ulong offset, byte[] buffer)
- {
- return getProcessMemory(System.connectionID, System.processID, offset, (uint)buffer.Length, buffer);
- }
- /// <summary>Like Get memory but this function return directly the buffer from the offset (uint).</summary>
- public byte[] GetBytes(uint offset, uint length)
- {
- byte[] buffer = new byte[length];
- GetMemory(offset, buffer);
- return buffer;
- }
- /// <summary>Like Get memory but this function return directly the buffer from the offset (ulong).</summary>
- public byte[] GetBytes(ulong offset, uint length)
- {
- byte[] buffer = new byte[length];
- GetMemory(offset, buffer);
- return buffer;
- }
- /// <summary>Display the notify message on your PS3.</summary>
- public int Notify(NotifyIcon icon, string message)
- {
- return notify(System.connectionID, (int)icon, message);
- }
- /// <summary>Display the notify message on your PS3.</summary>
- public int Notify(int icon, string message)
- {
- return notify(System.connectionID, icon, message);
- }
- /// <summary>You can shutdown the console or just reboot her according the flag selected.</summary>
- public int ShutDown(RebootFlags flag)
- {
- return shutdown(System.connectionID, (int)flag);
- }
- /// <summary>Your console will emit a song.</summary>
- public int RingBuzzer(BuzzerMode flag)
- {
- return ringBuzzer(System.connectionID, (int)flag);
- }
- /// <summary>Change leds for your console.</summary>
- public int SetConsoleLed(LedColor color, LedMode mode)
- {
- return setConsoleLed(System.connectionID, (int)color, (int)mode);
- }
- private int GetTargetInfo()
- {
- int result = -1; int[] sysTemp = new int[2];
- int fw = 0, ccapi = 0, consoleType = 0; ulong sysTable = 0;
- result = getFirmwareInfo(System.connectionID, ref fw, ref ccapi, ref sysTable, ref consoleType);
- if (result >= 0)
- {
- CCAPIGlobalPointer uPtr = new CCAPIGlobalPointer(Marshal.AllocHGlobal((int)(2 * 4)));
- result = getTemperature(System.connectionID, uPtr.GetPtr());
- if (result >= 0)
- {
- IntPtr unBuf = uPtr.GetPtr();
- for (uint i = 0; i < 2; i++)
- unBuf = ReadDataFromUnBufPtr<int>(unBuf, ref sysTemp[i]);
- CompleteInfo(ref pInfo, fw, ccapi, sysTable, consoleType, sysTemp[0], sysTemp[1]);
- }
- }
- return result;
- }
- /// <summary>Get informations of your console and store them into TargetInfo class.</summary>
- public int GetTargetInfo(out TargetInfo Info)
- {
- Info = new TargetInfo();
- int result = -1; int[] sysTemp = new int[2];
- int fw = 0, ccapi = 0, consoleType = 0; ulong sysTable = 0;
- result = getFirmwareInfo(System.connectionID, ref fw, ref ccapi, ref sysTable, ref consoleType);
- if (result >= 0)
- {
- CCAPIGlobalPointer uPtr = new CCAPIGlobalPointer(Marshal.AllocHGlobal((int)(2 * 4)));
- result = getTemperature(System.connectionID, uPtr.GetPtr());
- if (result >= 0)
- {
- IntPtr unBuf = uPtr.GetPtr();
- for (uint i = 0; i < 2; i++)
- unBuf = ReadDataFromUnBufPtr<int>(unBuf, ref sysTemp[i]);
- CompleteInfo(ref Info, fw, ccapi, sysTable, consoleType, sysTemp[0], sysTemp[1]);
- CompleteInfo(ref pInfo, fw, ccapi, sysTable, consoleType, sysTemp[0], sysTemp[1]);
- }
- }
- return result;
- }
- /// <summary>Return the current firmware of your console in string format.</summary>
- public string GetFirmwareVersion()
- {
- if (pInfo.Firmware == 0)
- GetTargetInfo();
- string ver = pInfo.Firmware.ToString("X8");
- string char1 = ver.Substring(1, 1) + ".";
- string char2 = ver.Substring(3, 1);
- string char3 = ver.Substring(4, 1);
- return char1 + char2 + char3;
- }
- /// <summary>Return the current temperature of your system in string.</summary>
- public string GetTemperatureCELL()
- {
- if (pInfo.TempCell == 0)
- GetTargetInfo(out pInfo);
- return pInfo.TempCell.ToString() + " C";
- }
- /// <summary>Return the current temperature of your system in string.</summary>
- public string GetTemperatureRSX()
- {
- if (pInfo.TempRSX == 0)
- GetTargetInfo(out pInfo);
- return pInfo.TempRSX.ToString() + " C";
- }
- /// <summary>Return the type of your firmware in string format.</summary>
- public string GetFirmwareType()
- {
- if (pInfo.ConsoleType.ToString() == "")
- GetTargetInfo(out pInfo);
- string type = String.Empty;
- if (pInfo.ConsoleType == (int)ConsoleType.CEX)
- type = "CEX";
- else if (pInfo.ConsoleType == (int)ConsoleType.DEX)
- type = "DEX";
- else if (pInfo.ConsoleType == (int)ConsoleType.TOOL)
- type = "TOOL";
- return type;
- }
- /// <summary>Clear informations into the DLL (PS3Lib).</summary>
- public void ClearTargetInfo()
- {
- pInfo = new TargetInfo();
- }
- /// <summary>Set a new ConsoleID in real time. (string)</summary>
- public int SetConsoleID(string consoleID)
- {
- string newCID = String.Empty;
- if (consoleID.Length >= 32)
- newCID = consoleID.Substring(0, 32);
- return setConsoleID(System.connectionID, StringToByteArray(newCID));
- }
- /// <summary>Set a new ConsoleID in real time. (bytes)</summary>
- public int SetConsoleID(byte[] consoleID)
- {
- return setConsoleID(System.connectionID, consoleID);
- }
- /// <summary>Return CCAPI Version.</summary>
- public int GetDllVersion()
- {
- return getDllVersion();
- }
- /// <summary>Return a list of informations for each console available.</summary>
- public List<ConsoleInfo> GetConsoleList()
- {
- List<ConsoleInfo> data = new List<ConsoleInfo>();
- int targetCount = getNumberOfConsoles();
- IntPtr name = Marshal.AllocHGlobal((int)(256)),
- ip = Marshal.AllocHGlobal((int)(256));
- for (int i = 0; i < targetCount; i++)
- {
- ConsoleInfo Info = new ConsoleInfo();
- getConsoleInfo(i, name, ip);
- Info.Name = Marshal.PtrToStringAnsi(name);
- Info.Ip = Marshal.PtrToStringAnsi(ip);
- data.Add(Info);
- }
- Marshal.FreeHGlobal(name);
- Marshal.FreeHGlobal(ip);
- return data;
- }
- internal static byte[] StringToByteArray(string hex)
- {
- string replace = hex.Replace("0x", "");
- string Stringz = replace.Insert(replace.Length - 1, "0");
- int Odd = replace.Length;
- bool Nombre;
- if (Odd % 2 == 0)
- Nombre = true;
- else
- Nombre = false;
- try
- {
- if (Nombre == true)
- {
- return Enumerable.Range(0, replace.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(replace.Substring(x, 2), 16))
- .ToArray();
- }
- else
- {
- return Enumerable.Range(0, replace.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(Stringz.Substring(x, 2), 16))
- .ToArray();
- }
- }
- catch { throw new ArgumentException("Value not possible.", "Byte Array"); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement