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 |
using System; |
4 |
using System.Collections.Generic; |
5 |
using System.Linq; |
6 |
using System.Text; |
7 |
using RomCheater.PluginFramework.Interfaces; |
8 |
using System.Diagnostics; |
9 |
using libWin32.Win32.Threading; |
10 |
using RomCheater.Logging; |
11 |
using System.IO; |
12 |
|
13 |
namespace RomCheater.PluginFramework.Core |
14 |
{ |
15 |
/// <summary> |
16 |
/// Base class for all configuration plugins |
17 |
/// </summary> |
18 |
public abstract class ConfigPlugin : PluginBase, IConfigPlugin |
19 |
{ |
20 |
public ConfigPlugin() : this(false) { } |
21 |
public ConfigPlugin(bool doinit) : base() { this.ValidProcessesForPlugin = new List<ProcContainer>(); if(doinit) init(); } |
22 |
|
23 |
public override void Reload() |
24 |
{ |
25 |
logger.Debug.WriteLine(" Loading config for {0}...", this.ToString()); |
26 |
init(); |
27 |
logger.Debug.WriteLine(" Loaded config for {0}.", this.ToString()); |
28 |
} |
29 |
private void init() |
30 |
{ |
31 |
// loggerflags flags = logger.GetLoggingFlags(); |
32 |
|
33 |
//#if DISABLE_VERBOSE_LOGGING_FOR_PERFORMANCE_BOOST |
34 |
// ushort performance_flags = flags.Value; |
35 |
// if (flags.HasFlag(loggerflags.VERBOSE_DEBUG)) |
36 |
// { |
37 |
// performance_flags = (ushort)(performance_flags &~ loggerflags.VERBOSE_DEBUG.Value); |
38 |
// } |
39 |
// if (flags.HasFlag(loggerflags.VERBOSE_ERROR)) |
40 |
// { |
41 |
// performance_flags = (ushort)(performance_flags &~ loggerflags.VERBOSE_ERROR.Value); |
42 |
// } |
43 |
//#endif |
44 |
// logger.SetLoggingFlags(performance_flags); |
45 |
|
46 |
List<ProcContainer> proc_list = new List<ProcContainer>(); |
47 |
foreach (Process proc in Process.GetProcesses()) |
48 |
{ |
49 |
string proc_name = proc.ProcessName.ToLower(); |
50 |
try |
51 |
{ |
52 |
bool isSystem = false; |
53 |
string proc_user = ThreadControl.GetProcessOwner(proc.Handle, out isSystem).ToLower(); |
54 |
if (isSystem) |
55 |
{ |
56 |
#if !DISALLOW_VERBOSE_LOGGING |
57 |
logger.VerboseDebug.WriteLine(" not adding process {0} because it is a system process", proc_name); |
58 |
#endif |
59 |
continue; |
60 |
} |
61 |
ProcContainer container = null; |
62 |
try |
63 |
{ |
64 |
container = new ProcContainer(proc); |
65 |
#if !DISALLOW_VERBOSE_LOGGING |
66 |
logger.VerboseDebug.WriteLine(" adding process {0} ", proc_name); |
67 |
#endif |
68 |
proc_list.Add(container); |
69 |
} |
70 |
catch (FileNotFoundException) { } |
71 |
catch (Exception ex) |
72 |
{ |
73 |
//throw; |
74 |
#if !DISALLOW_VERBOSE_LOGGING |
75 |
logger.VerboseError.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
76 |
#endif |
77 |
//continue; |
78 |
} |
79 |
} |
80 |
catch (System.ComponentModel.Win32Exception ex) |
81 |
{ |
82 |
if (!((uint)ex.ErrorCode == 0x80004005)) |
83 |
{ |
84 |
#if !DISALLOW_VERBOSE_LOGGING |
85 |
logger.VerboseError.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
86 |
#endif |
87 |
} |
88 |
//continue; |
89 |
} |
90 |
catch (Exception ex) |
91 |
{ |
92 |
#if !DISALLOW_VERBOSE_LOGGING |
93 |
logger.VerboseError.WriteLine(" not adding process {0} because it thew an exception [{1}]", proc_name, ex.ToString()); |
94 |
#endif |
95 |
//continue; |
96 |
} |
97 |
} |
98 |
proc_list = proc_list.OrderBy(p => p.Name).ToList(); |
99 |
Predicate<ProcContainer> predicate = new Predicate<ProcContainer>(IsNotValidProcess); |
100 |
proc_list.RemoveAll(predicate); |
101 |
ValidProcessesForPlugin = proc_list; |
102 |
//logger.SetLoggingFlags(flags); // reset flags |
103 |
} |
104 |
|
105 |
protected abstract bool IsNotValidProcess(ProcContainer p); |
106 |
|
107 |
#region IConfigPlugin Members |
108 |
public List<ProcContainer> ValidProcessesForPlugin { get; protected set; } |
109 |
public override Guid ID |
110 |
{ |
111 |
get { return new Guid(); } |
112 |
} |
113 |
public override string Name |
114 |
{ |
115 |
get |
116 |
{ |
117 |
return "Unknown Config Plugin"; |
118 |
} |
119 |
} |
120 |
public override string Description |
121 |
{ |
122 |
get |
123 |
{ |
124 |
return ""; |
125 |
} |
126 |
} |
127 |
#endregion |
128 |
} |
129 |
} |