ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.CorePlugins/Config/PCSX2Config.cs
(Generate patch)

Comparing trunk/RomCheater.CorePlugins/Config/PCSX2Config.cs (file contents):
Revision 147 by william, Sun May 27 23:05:18 2012 UTC vs.
Revision 389 by william, Tue Jun 19 12:47:26 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines