ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.CorePlugins/Config/PCSX2Config.cs
Revision: 92
Committed: Wed May 9 21:42:21 2012 UTC (11 years ago) by william
File size: 6683 byte(s)
Log Message:

File Contents

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