1 |
//#define DISABLE_VERBOSE_LOGGING_FOR_PERFORMANCE_BOOST // when defined will disable verbose logging for performance speed-up |
2 |
#define DISALLOW_VERBOSE_LOGGING // when defined will disallow verbose logging for performance reasons |
3 |
#define ALLOW_SYSTEM_PROCESSES_TO_BE_INLUCDED // when defined system processes will be included in process list |
4 |
using System; |
5 |
using System.Collections.Generic; |
6 |
using System.Linq; |
7 |
using System.Text; |
8 |
using RomCheater.PluginFramework.Interfaces; |
9 |
using System.Diagnostics; |
10 |
using libWin32.Win32.Threading; |
11 |
using RomCheater.Logging; |
12 |
using System.IO; |
13 |
|
14 |
namespace RomCheater.PluginFramework.Core |
15 |
{ |
16 |
/// <summary> |
17 |
/// Base class for all configuration plugins |
18 |
/// </summary> |
19 |
public abstract class ConfigPlugin : PluginBase, IConfigPlugin, IAcceptsReadOnlyMemoryRange |
20 |
{ |
21 |
public ConfigPlugin() : this(false) { } |
22 |
public ConfigPlugin(bool doinit) : base() { SetupConfig(); if (doinit) init(); } |
23 |
|
24 |
private void SetupConfig() |
25 |
{ |
26 |
this.ValidProcessesForPlugin = new List<ProcContainer>(); |
27 |
MemoryRangeStart = 0; |
28 |
MemoryRangeSize = int.MaxValue; |
29 |
} |
30 |
|
31 |
public override void Reload(bool silent) |
32 |
{ |
33 |
if (!silent) |
34 |
logger.Debug.WriteLine(" Loading config for {0}...", this.ToString()); |
35 |
init(silent); |
36 |
if (!silent) |
37 |
logger.Debug.WriteLine(" Loaded config for {0}.", this.ToString()); |
38 |
} |
39 |
private void init() { init(false); } |
40 |
private void init(bool silent) |
41 |
{ |
42 |
// loggerflags flags = logger.GetLoggingFlags(); |
43 |
|
44 |
//#if DISABLE_VERBOSE_LOGGING_FOR_PERFORMANCE_BOOST |
45 |
// ushort performance_flags = flags.Value; |
46 |
// if (flags.HasFlag(loggerflags.VERBOSE_DEBUG)) |
47 |
// { |
48 |
// performance_flags = (ushort)(performance_flags &~ loggerflags.VERBOSE_DEBUG.Value); |
49 |
// } |
50 |
// if (flags.HasFlag(loggerflags.VERBOSE_ERROR)) |
51 |
// { |
52 |
// performance_flags = (ushort)(performance_flags &~ loggerflags.VERBOSE_ERROR.Value); |
53 |
// } |
54 |
//#endif |
55 |
// logger.SetLoggingFlags(performance_flags); |
56 |
|
57 |
List<ProcContainer> proc_list = new List<ProcContainer>(); |
58 |
foreach (Process proc in Process.GetProcesses()) |
59 |
{ |
60 |
string proc_name = proc.ProcessName.ToLower(); |
61 |
try |
62 |
{ |
63 |
bool isSystem = false; |
64 |
string proc_user = ThreadControl.GetProcessOwner(proc.Handle, out isSystem).ToLower(); |
65 |
#if !ALLOW_SYSTEM_PROCESSES_TO_BE_INLUCDED |
66 |
if (isSystem) |
67 |
{ |
68 |
#if !DISALLOW_VERBOSE_LOGGING |
69 |
if(!silent) |
70 |
logger.VerboseDebug.WriteLine(" not adding process {0} because it is a system process", proc_name); |
71 |
#endif |
72 |
continue; |
73 |
} |
74 |
#endif |
75 |
ProcContainer container = null; |
76 |
try |
77 |
{ |
78 |
container = new ProcContainer(proc); |
79 |
#if !DISALLOW_VERBOSE_LOGGING |
80 |
if(!silent) |
81 |
logger.VerboseDebug.WriteLine(" adding process {0} ", proc_name); |
82 |
#endif |
83 |
proc_list.Add(container); |
84 |
} |
85 |
catch (FileNotFoundException) { } |
86 |
#if !DISALLOW_VERBOSE_LOGGING |
87 |
catch (Exception ex) |
88 |
{ |
89 |
//throw; |
90 |
if(!silent) |
91 |
logger.VerboseError.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
92 |
//continue; |
93 |
} |
94 |
#else |
95 |
catch (Exception) { } |
96 |
#endif |
97 |
} |
98 |
catch (System.ComponentModel.Win32Exception ex) |
99 |
{ |
100 |
if (!((uint)ex.ErrorCode == 0x80004005)) |
101 |
{ |
102 |
#if !DISALLOW_VERBOSE_LOGGING |
103 |
if(!silent) |
104 |
logger.VerboseError.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
105 |
#endif |
106 |
} |
107 |
//continue; |
108 |
} |
109 |
#if !DISALLOW_VERBOSE_LOGGING |
110 |
catch (Exception ex) |
111 |
{ |
112 |
if(!silent) |
113 |
logger.VerboseError.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
114 |
//continue; |
115 |
} |
116 |
#else |
117 |
catch (Exception) { } |
118 |
#endif |
119 |
} |
120 |
proc_list = proc_list.OrderBy(p => p.Name).ToList(); |
121 |
Predicate<ProcContainer> predicate = new Predicate<ProcContainer>(IsNotValidProcess); |
122 |
proc_list.RemoveAll(predicate); |
123 |
ValidProcessesForPlugin = proc_list; |
124 |
//logger.SetLoggingFlags(flags); // reset flags |
125 |
} |
126 |
|
127 |
protected abstract bool IsNotValidProcess(ProcContainer p); |
128 |
|
129 |
#region IConfigPlugin Members |
130 |
public List<ProcContainer> ValidProcessesForPlugin { get; protected set; } |
131 |
public override Guid ID |
132 |
{ |
133 |
get { return new Guid(); } |
134 |
} |
135 |
public override string Name |
136 |
{ |
137 |
get |
138 |
{ |
139 |
return "Unknown Config Plugin"; |
140 |
} |
141 |
} |
142 |
public override string Description |
143 |
{ |
144 |
get |
145 |
{ |
146 |
return ""; |
147 |
} |
148 |
} |
149 |
#endregion |
150 |
#region IAcceptsReadOnlyMemoryRange members |
151 |
public virtual uint MemoryRangeStart { get; protected set; } |
152 |
public virtual uint MemoryRangeSize { get; protected set; } |
153 |
#endregion |
154 |
|
155 |
#region ISearchInProgress members |
156 |
private ISearchInProgress sip = null; |
157 |
public bool SearchInProgess { get { return (sip == null) ? false : sip.SearchInProgess; } } |
158 |
#endregion |
159 |
|
160 |
#region IAcceptsMemorySearch Members |
161 |
public void SetMemorySearchReference(ISearchInProgress sip) |
162 |
{ |
163 |
this.sip = sip; |
164 |
} |
165 |
#endregion |
166 |
} |
167 |
} |