/[RomCheater]/trunk/RomCheater.CorePlugins/Config/PCSX2Config.cs
ViewVC logotype

Contents of /trunk/RomCheater.CorePlugins/Config/PCSX2Config.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 98 - (show annotations) (download)
Wed May 9 23:10:12 2012 UTC (9 years, 4 months ago) by william
File size: 7670 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using RomCheater.PluginFramework.Core;
6 using System.IO;
7 using System.Diagnostics;
8 using RomCheater.Logging;
9
10 namespace RomCheater.CorePlugins.Config
11 {
12 public sealed class PCSX2Config : ConfigPlugin
13 {
14 #region Configuration Memvers
15 const string PCXS2_PROCESS_MAGIC = "pcsx2";
16 const string PCXS2_LOOKUP_MAGIC_001 = "pcsx2";
17 const string PCXS2_LOOKUP_MAGIC_002 = "compiled on";
18
19 const uint VTLB_VADDR_OFFSET_DEBUG = 0x20000000;
20 #endregion
21
22 public PCSX2Config() : base(false) { }
23 public PCSX2Config(bool doinit) : base(doinit) { }
24
25
26 protected override bool IsNotValidProcess(ProcContainer p)
27 {
28 bool isDebug = false;
29 uint VTLB_RELEASE_OFFSET = 0;
30 bool notvalid = !IsValidPSX2Process(p.ProcessInfo.Id, out isDebug, out VTLB_RELEASE_OFFSET);
31 if (notvalid)
32 {
33 logger.Debug.WriteLine(" Not Allowing process {0} to be added because it was filterd out", p.Name);
34 }
35 else
36 {
37 logger.Debug.WriteLine(" Allowing process {0} to be added", p.Name);
38 }
39 return notvalid;
40 }
41 #region IPluginBase Members
42 public override Guid ID
43 {
44 get
45 {
46 return new GuidGenerator(typeof(PCSX2Config).FullName).Guid;
47 }
48 }
49 public override string Name
50 {
51 get { return "PCSX2 Configuration Plugin"; }
52 }
53 public override string Description
54 {
55 get { return "This plugin provides a configuration suitable for use with PCSX2"; }
56 }
57 #endregion
58
59 private static bool IsValidPSX2Process(int pid, out bool isDebug, out uint VTLB_RELEASE_OFFSET)
60 {
61 bool isValid = false;
62 isDebug = false;
63 VTLB_RELEASE_OFFSET = 0;
64 try
65 {
66 Process p = Process.GetProcessById(pid);
67 string filename = p.MainModule.FileName;
68
69 FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
70 BinaryReader r = new BinaryReader(fs);
71 byte[] exe_check = new byte[] {
72 0x4D, 0x5A, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
73 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
76 0x0E, 0x1F, 0xBA, 0x0E, 0x00, 0xB4, 0x09, 0xCD, 0x21, 0xB8, 0x01, 0x4C, 0xCD, 0x21, 0x54, 0x68,
77 0x69, 0x73, 0x20, 0x70, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x20, 0x63, 0x61, 0x6E, 0x6E, 0x6F,
78 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6E, 0x20, 0x69, 0x6E, 0x20, 0x44, 0x4F, 0x53, 0x20,
79 0x6D, 0x6F, 0x64, 0x65, 0x2E
80 };
81
82 r.BaseStream.Seek(0, SeekOrigin.Begin);
83 byte[] data = r.ReadBytes(exe_check.Length);
84 bool isExe = false;
85 for (int i = 0; i < exe_check.Length; i++) { if (exe_check[i] == data[i]) { isExe = true; } else { isExe = false; } }
86 r.Close();
87 if (!isExe)
88 {
89 logger.Debug.WriteLine(" Process: {0} is not a valid executable file", filename);
90 isValid = false;
91 }
92 else
93 {
94 // check for valid pcsx2 exe
95 fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
96 StreamReader sr = new StreamReader(fs);
97 string found_string = sr.ReadToEnd();
98 if (found_string.ToLower().Contains(PCXS2_LOOKUP_MAGIC_001.ToLower()) && found_string.ToLower().Contains(PCXS2_LOOKUP_MAGIC_002.ToLower()))
99 {
100 isValid = true;
101 logger.Debug.WriteLine(" Process: {0} found MAGIC LOOKUP {1} and {2}", filename, PCXS2_LOOKUP_MAGIC_001.ToLower(), PCXS2_LOOKUP_MAGIC_002.ToLower());
102 }
103 sr.Close();
104
105 // check for debug
106 fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
107 sr = new StreamReader(fs);
108 found_string = sr.ReadToEnd();
109 // DebugBreak should only be present in debug build version(s) of pcsx2
110 if (found_string.ToLower().Contains("DebugBreak".ToLower()))
111 {
112 isDebug = true;
113 logger.Debug.WriteLine(" Process: {0} found DebugBreak", filename);
114 }
115 sr.Close();
116 }
117 //this.IsDebugBuild = isDebug;
118
119 // run check to see if we have a later version with offset fixed at: 0x20000000
120
121
122 byte[] entrypoint_data = new byte[]{
123 0x01, 0x80, 0x1A, 0x3C, 0x78, 0x53, 0x59, 0xFF, 0x00, 0x68, 0x19, 0x40,
124 0x01, 0x80, 0x1A, 0x3C, 0x7C, 0x00, 0x39, 0x33, 0x21, 0xD0, 0x59, 0x03,
125 0x40, 0x53, 0x5A, 0x8F, 0x01, 0x80, 0x19, 0x3C, 0x08, 0x00, 0x40, 0x03,
126 0x78, 0x53, 0x39, 0xDF
127 };
128
129 //uint address = ramdumper.VTLB_VADDR_OFFSET_DEBUG;
130 Sojaner.MemoryScanner.ProcessMemoryReader reader = new Sojaner.MemoryScanner.ProcessMemoryReader();
131 reader.ReadProcess = p;
132 reader.OpenProcess();
133 int bytesReadSize;
134 byte[] check_data = reader.ReadProcessMemory((IntPtr)VTLB_VADDR_OFFSET_DEBUG, (uint)entrypoint_data.Length, out bytesReadSize);
135
136 isDebug = check_data.SequenceEqual(entrypoint_data);
137 logger.Debug.WriteLine(" Process: {0} isDebug: {1}", filename, isDebug.ToString());
138 //List<uint> addresses = new List<uint>();
139 if (!isDebug)
140 {
141
142 for (int i = 0x1000; i < 0x2000;i++ )
143 {
144 try
145 {
146 uint t = ((uint)i << 16) | 0x5000;
147 check_data = reader.ReadProcessMemory((IntPtr)t, (uint)entrypoint_data.Length, out bytesReadSize);
148 isValid = check_data.SequenceEqual(entrypoint_data);
149 if (isValid)
150 {
151 //addresses.Add(t);
152 VTLB_RELEASE_OFFSET = t;
153 logger.Debug.WriteLine(" Process: {0} found release build entrypoint at 0x{1:x8}", filename, t);
154 break; //use only the first valid value
155 }
156 }
157 catch { }
158 }
159
160 }
161 reader.CloseHandle();
162 return isValid;
163 }
164 catch (System.ComponentModel.Win32Exception) { }
165 catch (Exception) { }
166 return isValid;
167 }
168 }
169 }

  ViewVC Help
Powered by ViewVC 1.1.22