ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.PluginFramework/Core/ConfigPlugin.cs
Revision: 682
Committed: Mon Jun 17 08:27:34 2013 UTC (9 years, 5 months ago) by william
File size: 7150 byte(s)
Log Message:
+ fix-up null plugins

File Contents

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