1 |
#region Logging Defines |
2 |
// include this any class or method that required logging, and comment-out what is not needed |
3 |
|
4 |
#region Enabled logging levels |
5 |
#define LOGGING_ENABLE_INFO |
6 |
#define LOGGING_ENABLE_WARN |
7 |
#define LOGGING_ENABLE_DEBUG |
8 |
//#define LOGGING_ENABLE_VERBOSEDEBUG |
9 |
#define LOGGING_ENABLE_ERROR |
10 |
//#define LOGGING_ENABLE_VERBOSEERROR |
11 |
#define LOGGING_ENABLE_PROFILER |
12 |
#endregion |
13 |
#endregion |
14 |
//#define DISABLE_VERBOSE_LOGGING_FOR_PERFORMANCE_BOOST // when defined will disable verbose logging for performance speed-up |
15 |
//#define DISALLOW_VERBOSE_LOGGING // when defined will disallow verbose logging for performance reasons |
16 |
#define ALLOW_SYSTEM_PROCESSES_TO_BE_INLUCDED // when defined system processes will be included in process list |
17 |
using System; |
18 |
using System.Collections.Generic; |
19 |
using System.Linq; |
20 |
using System.Text; |
21 |
using System.Diagnostics; |
22 |
using libWin32.Win32.Threading; |
23 |
using RomCheater.Logging; |
24 |
using System.IO; |
25 |
using RomCheater.Interfaces; |
26 |
using Enterprise.Logging; |
27 |
using System.ComponentModel; |
28 |
|
29 |
namespace RomCheater.PluginFramework.Core |
30 |
{ |
31 |
/// <summary> |
32 |
/// Base class for all configuration plugins |
33 |
/// </summary> |
34 |
public abstract class ConfigPlugin : PluginBase, IConfigPlugin, IAcceptsReadOnlyMemoryRange |
35 |
{ |
36 |
public ConfigPlugin() : this(false) { } |
37 |
public ConfigPlugin(bool doinit) : base() { SetupConfig(); if (doinit) init(); } |
38 |
|
39 |
private IWebBrowserProvider _WebBrowserProvider; |
40 |
public IWebBrowserProvider WebBrowserProvider |
41 |
{ |
42 |
get |
43 |
{ |
44 |
return _WebBrowserProvider; |
45 |
} |
46 |
set |
47 |
{ |
48 |
_WebBrowserProvider = value; |
49 |
} |
50 |
} |
51 |
public override bool IsGenericPlugin { get { return false; } } |
52 |
public override bool IsNullPlugin { get { return false; } } |
53 |
|
54 |
private void SetupConfig() |
55 |
{ |
56 |
this.ValidProcessesForPlugin = new List<ProcContainer>(); |
57 |
MemoryRangeStart = 0; |
58 |
MemoryRangeSize = int.MaxValue; |
59 |
} |
60 |
|
61 |
public override void Reload(bool silent) |
62 |
{ |
63 |
if (!silent) |
64 |
gLog.Debug.WriteLine(" Loading config for {0}...", this.ToString()); |
65 |
init(silent); |
66 |
if (!silent) |
67 |
gLog.Debug.WriteLine(" Loaded config for {0}.", this.ToString()); |
68 |
} |
69 |
private void init() { init(false); } |
70 |
private void init(bool silent) |
71 |
{ |
72 |
// loggerflags flags = logger.GetLoggingFlags(); |
73 |
|
74 |
//#if DISABLE_VERBOSE_LOGGING_FOR_PERFORMANCE_BOOST |
75 |
// ushort performance_flags = flags.Value; |
76 |
// if (flags.HasFlag(loggerflags.VERBOSE_DEBUG)) |
77 |
// { |
78 |
// performance_flags = (ushort)(performance_flags &~ loggerflags.VERBOSE_DEBUG.Value); |
79 |
// } |
80 |
// if (flags.HasFlag(loggerflags.VERBOSE_ERROR)) |
81 |
// { |
82 |
// performance_flags = (ushort)(performance_flags &~ loggerflags.VERBOSE_ERROR.Value); |
83 |
// } |
84 |
//#endif |
85 |
// logger.SetLoggingFlags(performance_flags); |
86 |
|
87 |
List<ProcContainer> proc_list = new List<ProcContainer>(); |
88 |
foreach (Process proc in Process.GetProcesses()) |
89 |
{ |
90 |
string proc_name = proc.ProcessName.ToLower(); |
91 |
try |
92 |
{ |
93 |
bool isSystem = false; |
94 |
string proc_user = libWin32.Win32.Threading.ThreadControl.GetProcessOwner(proc.Handle, out isSystem).ToLower(); |
95 |
#if !ALLOW_SYSTEM_PROCESSES_TO_BE_INLUCDED |
96 |
if (isSystem) |
97 |
{ |
98 |
#if !DISALLOW_VERBOSE_LOGGING |
99 |
if(!silent) |
100 |
logger.VerboseDebug.WriteLine(" not adding process {0} because it is a system process", proc_name); |
101 |
#endif |
102 |
continue; |
103 |
} |
104 |
#endif |
105 |
ProcContainer container = null; |
106 |
try |
107 |
{ |
108 |
container = new ProcContainer(proc); |
109 |
#if !DISALLOW_VERBOSE_LOGGING |
110 |
if(!silent) |
111 |
gLog.Verbose.Debug.WriteLine(" adding process {0} ", proc_name); |
112 |
#endif |
113 |
proc_list.Add(container); |
114 |
} |
115 |
catch (FileNotFoundException) { } |
116 |
#if !DISALLOW_VERBOSE_LOGGING |
117 |
catch (Exception ex) |
118 |
{ |
119 |
//throw; |
120 |
if(!silent) |
121 |
gLog.Verbose.Error.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
122 |
//continue; |
123 |
} |
124 |
#else |
125 |
catch (Exception) { } |
126 |
#endif |
127 |
} |
128 |
catch (System.ComponentModel.Win32Exception ex) |
129 |
{ |
130 |
if (!((uint)ex.ErrorCode == 0x80004005)) |
131 |
{ |
132 |
#if !DISALLOW_VERBOSE_LOGGING |
133 |
if(!silent) |
134 |
gLog.Verbose.Error.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
135 |
#endif |
136 |
} |
137 |
//continue; |
138 |
} |
139 |
#if !DISALLOW_VERBOSE_LOGGING |
140 |
catch (Exception ex) |
141 |
{ |
142 |
if(!silent) |
143 |
gLog.Verbose.Error.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
144 |
//continue; |
145 |
} |
146 |
#else |
147 |
catch (Exception) { } |
148 |
#endif |
149 |
} |
150 |
proc_list = proc_list.OrderBy(p => p.Name).ToList(); |
151 |
Predicate<ProcContainer> predicate = new Predicate<ProcContainer>(InternalIsNotValidProcess); |
152 |
proc_list.RemoveAll(predicate); |
153 |
ValidProcessesForPlugin = proc_list; |
154 |
//logger.SetLoggingFlags(flags); // reset flags |
155 |
} |
156 |
|
157 |
private bool InternalIsNotValidProcess(ProcContainer p) |
158 |
{ |
159 |
bool internal_valid = true; |
160 |
// determinie if process is valid (for 32/64 bit) |
161 |
try |
162 |
{ |
163 |
var proc_module = p.ProcessInfo.MainModule; |
164 |
} |
165 |
catch (Win32Exception ex) |
166 |
{ |
167 |
if (ex.Message.ToLower() == "A 32 bit processes cannot access modules of a 64 bit process.".ToLower()) |
168 |
{ |
169 |
if (IntPtr.Size != 8) |
170 |
{ |
171 |
// our process is 32-bit |
172 |
gLog.Warn.WriteLine("Cannout open 64-bit process: '{0}' -- use the 64-bit version RomCheater", p.ProcessInfo.ProcessName); |
173 |
internal_valid = false; |
174 |
} |
175 |
} |
176 |
} |
177 |
if (internal_valid) { return IsNotValidProcess(p); } |
178 |
return true; // we are saying that the process IS NOT valid |
179 |
} |
180 |
protected abstract bool IsNotValidProcess(ProcContainer p); |
181 |
|
182 |
#region IConfigPlugin Members |
183 |
public List<ProcContainer> ValidProcessesForPlugin { get; protected set; } |
184 |
public override Guid ID |
185 |
{ |
186 |
get { return new Guid(); } |
187 |
} |
188 |
public override string Name |
189 |
{ |
190 |
get |
191 |
{ |
192 |
return "Unknown Config Plugin"; |
193 |
} |
194 |
} |
195 |
public override string Description |
196 |
{ |
197 |
get |
198 |
{ |
199 |
return ""; |
200 |
} |
201 |
} |
202 |
#endregion |
203 |
#region IAcceptsReadOnlyMemoryRange members |
204 |
public virtual ulong MemoryRangeStart { get; protected set; } |
205 |
public virtual ulong MemoryRangeSize { get; protected set; } |
206 |
#endregion |
207 |
|
208 |
#region ISearchInProgress members |
209 |
private ISearchInProgress sip = null; |
210 |
public bool SearchInProgess { get { return (sip == null) ? false : sip.SearchInProgess; } } |
211 |
public Guid SearchGuid { get { return (sip == null) ? Guid.Empty : sip.SearchGuid; } } |
212 |
#endregion |
213 |
|
214 |
#region IAcceptsMemorySearch Members |
215 |
public void SetMemorySearchReference(ISearchInProgress sip) |
216 |
{ |
217 |
this.sip = sip; |
218 |
} |
219 |
#endregion |
220 |
} |
221 |
} |