/[RomCheater]/trunk/RomCheater/Main.cs
ViewVC logotype

Annotation of /trunk/RomCheater/Main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 318 - (hide annotations) (download)
Tue Jun 5 17:57:37 2012 UTC (9 years, 3 months ago) by william
File size: 19398 byte(s)
+ add support for displaying PE Struct data into property grid (read-only)

1 william 99 #define FORCE_ALL_LOGGING_FLAGS // when defined will force logging flags to ALL
2 william 158 #define SHOW_DEBUG_MENU // when defined will show the debug menu or else it will be hidden
3 william 99 using System;
4 william 5 using System.Collections.Generic;
5     using System.ComponentModel;
6     using System.Data;
7     using System.Drawing;
8     using System.Linq;
9     using System.Text;
10     using System.Windows.Forms;
11 william 17 using RomCheater.Logging;
12 william 34 using RomCheater.Properties;
13     using RomCheater.UserSettingsSupport;
14 william 83 using RomCheater.PluginFramework.Core;
15 william 86 using System.Diagnostics;
16 william 87 using RomCheater.PluginFramework.Interfaces;
17 william 144 using WeifenLuo.WinFormsUI.Docking;
18 william 146 using RomCheater.Docking;
19 william 152 using System.IO;
20 william 159 using Sojaner.MemoryScanner;
21 william 196 using RomCheater.PluginFramework.Events;
22 william 5
23     namespace RomCheater
24     {
25 william 13 public partial class Main : Form
26 william 5 {
27 william 152 private bool m_bSaveLayout = true;
28 william 196 private Process SelectedProcess = null;
29 william 144 private DeserializeDockContent m_deserializeDockContent;
30     private FloatingLogWindow m_LogWindow = new FloatingLogWindow();
31 william 147 private FloatingAboutBox m_AboutBox = new FloatingAboutBox();
32 william 148 private FloatingRamDumperDialog m_RamDump = new FloatingRamDumperDialog();
33     private PIDSelector m_PIDSelector = new PIDSelector();
34 william 196 private FloatingMemoryView m_memoryview = new FloatingMemoryView();
35 william 218 private FloatingDataTypeConverter m_typeconverter = new FloatingDataTypeConverter();
36 william 227 private FloatingMemorySearcher m_memsearcher = new FloatingMemorySearcher();
37 william 318 private FloatingPEViewer m_peviewer = new FloatingPEViewer();
38 william 144 //private bool log_window_expanded = false;
39     //private double log_window_splitter_default_position = 1.4045;
40 william 86 PluginLoader loader = null;
41 william 87 IConfigPlugin ConfigPlugin = null;
42     IInputPlugin InputPlugin = null;
43     IWindowPlugin WindowPlugin = null;
44 william 146 static Main() { SettingSubscriber.AddSubscriber(new Main(), Settings.Default); }
45 william 20 private const string t = "RomCheater";
46 william 17 #region LogWriterSupport
47     static LogWriter _LoggerInstance;
48     static LogWriter LoggerInstance
49     {
50     get { return _LoggerInstance; }
51     set { _LoggerInstance = value; }
52     }
53     #endregion
54    
55    
56 william 196 private void OnProcessChanged(ProcessChangedEventArgs e)
57 william 151 {
58 william 228 if (m_memsearcher.SearchInProgess)
59     {
60     DialogResult result = MessageBox.Show("Do you want to start a new search with the selected process?", "A memory search is currently in progress", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
61     if (result != DialogResult.Yes || result != DialogResult.OK) { return; }
62     m_memsearcher = null; //free memory used by the memory searcher
63     m_memsearcher = new FloatingMemorySearcher();
64     }
65 william 153 SelectedProcess = Process.GetProcessById(e.ProcessID);
66 william 196 m_RamDump.AcceptedPlugin = this.ConfigPlugin;
67 william 245 m_RamDump.AcceptedProcess = SelectedProcess;
68 william 196
69 william 244 m_memoryview.AcceptedPlugin = this.ConfigPlugin;
70 william 196 m_memoryview.AcceptedProcess = SelectedProcess;
71 william 227
72 william 244 m_memsearcher.AcceptedPlugin = this.ConfigPlugin;
73 william 227 m_memsearcher.AcceptedProcess = SelectedProcess;
74 william 275 m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion);
75 william 318
76     m_peviewer.AcceptedPlugin = this.ConfigPlugin;
77     m_peviewer.AcceptedProcess = SelectedProcess;
78    
79 william 151 }
80 william 275 private void OnBrowseMemoryRegion(BrowseMemoryRegionEvent e)
81     {
82 william 276 m_memoryview.BrowseMemoryRegion(e.MemoryRegion);
83 william 275 }
84 william 151
85 william 144 #region Dock Support
86 william 313 void AddDockToWindowList(object sender, EventArgs e)
87     {
88     DockContent dc;
89 william 315 TypeBinder.Bind(sender, out dc);
90 william 314 ToolStripMenuItem tsmi = new ToolStripMenuItem(dc.Text);
91     tsmi.Name = dc.Name;
92     tsmi.Tag = dc;
93 william 315 tsmi.Click += new EventHandler(tsmi_Click);
94 william 314 mnuWindows.DropDownItems.Add(tsmi);
95 william 313 }
96 william 314 void tsmi_Click(object sender, EventArgs e)
97     {
98     ToolStripMenuItem tsmi;
99 william 315 TypeBinder.Bind(sender, out tsmi);
100 william 314 DockContent dc;
101 william 315 TypeBinder.Bind(tsmi.Tag, out dc);
102 william 314 dc.Activate();
103     }
104 william 313 void RemoveDockFromWindowList(object sender, FormClosedEventArgs e)
105     {
106     DockContent dc;
107 william 315 TypeBinder.Bind(sender, out dc);
108 william 314 mnuWindows.DropDownItems.RemoveByKey(dc.Name);
109 william 313 }
110 william 144 private IDockContent GetContentFromPersistString(string persistString)
111     {
112 william 228 if (persistString == typeof(FloatingLogWindow).ToString()) { return m_LogWindow; }
113     if (persistString == typeof(FloatingRamDumperDialog).ToString()) { return m_RamDump; }
114     if (persistString == typeof(PIDSelector).ToString()) { return m_PIDSelector; }
115     if (persistString == typeof(FloatingMemoryView).ToString()) { return m_memoryview; }
116     if (persistString == typeof(FloatingDataTypeConverter).ToString()) { return m_typeconverter; }
117     if (persistString == typeof(FloatingMemorySearcher).ToString()) { return m_memsearcher; }
118 william 318 if (persistString == typeof(FloatingPEViewer).ToString()) { return m_peviewer; }
119 william 228 else { return null; }
120 william 144 }
121     public void SetupDocks()
122     {
123     m_LogWindow = new FloatingLogWindow();
124 william 147 m_AboutBox = new FloatingAboutBox();
125 william 148 m_RamDump = new FloatingRamDumperDialog();
126     m_PIDSelector = new PIDSelector();
127 william 196 m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged);
128     m_memoryview = new FloatingMemoryView();
129 william 218 m_typeconverter = new FloatingDataTypeConverter();
130 william 227 m_memsearcher = new FloatingMemorySearcher();
131 william 275 m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion);
132 william 318 m_peviewer = new FloatingPEViewer();
133 william 144 m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
134     }
135 william 313 #region SetupDockWindowHandler support
136     public void SetupDockWindowHandler()
137     {
138     SetupLogWindowHandler();
139     SetupRamDumpWindowHandler();
140     SetupMemoryViewWindowHandler();
141     SetupPIDSelectorWindowHandler();
142     SetupDataTypeConverterWindowHandler();
143     SetupMemorySearchWindowHandler();
144 william 318 SetupPEViewerWindowHandler();
145 william 313 }
146     private void SetupLogWindowHandler()
147     {
148     if (m_LogWindow == null) return;
149     m_LogWindow.Shown += new EventHandler(AddDockToWindowList);
150     m_LogWindow.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList);
151 william 314 m_LogWindow.Activate();
152 william 313 }
153     private void SetupRamDumpWindowHandler()
154     {
155     if (m_RamDump == null) return;
156     m_RamDump.Shown += new EventHandler(AddDockToWindowList);
157     m_RamDump.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList);
158 william 314 m_RamDump.Activate();
159 william 313 }
160     private void SetupMemoryViewWindowHandler()
161     {
162     if (m_memoryview == null) return;
163     m_memoryview.Shown += new EventHandler(AddDockToWindowList);
164     m_memoryview.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList);
165 william 314 m_memoryview.Activate();
166 william 313 }
167     private void SetupPIDSelectorWindowHandler()
168     {
169     if (m_PIDSelector == null) return;
170     m_PIDSelector.Shown += new EventHandler(AddDockToWindowList);
171     m_PIDSelector.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList);
172 william 314 m_PIDSelector.Activate();
173 william 313 }
174     private void SetupDataTypeConverterWindowHandler()
175     {
176     if (m_typeconverter == null) return;
177     m_typeconverter.Shown += new EventHandler(AddDockToWindowList);
178     m_typeconverter.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList);
179 william 314 m_typeconverter.Activate();
180 william 313 }
181     private void SetupMemorySearchWindowHandler()
182     {
183     if (m_memsearcher == null) return;
184     m_memsearcher.Shown += new EventHandler(AddDockToWindowList);
185     m_memsearcher.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList);
186 william 314 m_memsearcher.Activate();
187 william 313 }
188 william 318 private void SetupPEViewerWindowHandler()
189     {
190     if (m_peviewer == null) return;
191     m_peviewer.Shown += new EventHandler(AddDockToWindowList);
192     m_peviewer.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList);
193     m_peviewer.Activate();
194     }
195 william 313 #endregion
196 william 144 public void ShowDocks()
197     {
198     ShowLogWindow();
199 william 313 SetupLogWindowHandler();
200 william 152 //ShowAboutBox();
201 william 148 ShowRamDump();
202 william 313 SetupRamDumpWindowHandler();
203 william 196 ShowMemoryView();
204 william 313 SetupMemoryViewWindowHandler();
205 william 148 ShowPidSelector();
206 william 313 SetupPIDSelectorWindowHandler();
207 william 218 ShowDataTypeConverter();
208 william 313 SetupDataTypeConverterWindowHandler();
209 william 227 ShowMemorySearch();
210 william 313 SetupMemorySearchWindowHandler();
211 william 318 ShowPEViewer();
212     SetupPEViewerWindowHandler();
213 william 144 }
214     public void ShowLogWindow()
215 william 218 {
216     if (m_LogWindow == null || m_LogWindow.IsDisposed) { m_LogWindow = new FloatingLogWindow(); }
217 william 263 LoggerInstance = m_LogWindow.Logwriter;
218 william 264 LoggerInstance.CreateNewLog(false);
219 william 313 m_LogWindow.Show(dockPanel, DockState.DockBottom);
220 william 144 }
221 william 147 public void ShowAboutBox()
222     {
223 william 218 if (m_AboutBox == null || m_AboutBox.IsDisposed) { m_AboutBox = new FloatingAboutBox(); }
224 william 148 m_AboutBox.ShowDialog();
225 william 147 }
226 william 148 public void ShowRamDump()
227     {
228 william 218 load_plugins();
229 william 153 m_RamDump = new FloatingRamDumperDialog(ConfigPlugin);
230 william 201 m_RamDump.AcceptedProcess = SelectedProcess;
231 william 313 m_RamDump.Show(dockPanel);
232     }
233 william 196 public void ShowMemoryView()
234     {
235 william 218 load_plugins();
236 william 196 m_memoryview = new FloatingMemoryView(ConfigPlugin);
237 william 201 m_memoryview.AcceptedProcess = SelectedProcess;
238 william 196 m_memoryview.Show(dockPanel);
239     }
240 william 148 public void ShowPidSelector()
241     {
242 william 218 load_plugins();
243 william 148 //List<Process> procs = ConfigPlugin.ValidProcessesForPlugin;
244     m_PIDSelector = new PIDSelector(ConfigPlugin);
245 william 196 m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged);
246 william 148 m_PIDSelector.Show(dockPanel);
247     }
248 william 218 public void ShowDataTypeConverter()
249     {
250     if (m_typeconverter == null || m_typeconverter.IsDisposed) { m_typeconverter = new FloatingDataTypeConverter(); }
251 william 313 m_typeconverter.Show(dockPanel, DockState.DockRightAutoHide);
252 william 218 }
253 william 227 public void ShowMemorySearch()
254     {
255     load_plugins();
256     m_memsearcher = new FloatingMemorySearcher(ConfigPlugin);
257     m_memsearcher.AcceptedProcess = SelectedProcess;
258 william 275 m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion);
259 william 227 m_memsearcher.Show(dockPanel);
260     }
261 william 318 public void ShowPEViewer()
262     {
263     load_plugins();
264     m_peviewer = new FloatingPEViewer(ConfigPlugin);
265     m_peviewer.AcceptedProcess = SelectedProcess;
266     m_peviewer.Show(dockPanel);
267     }
268 william 313
269 william 144 #endregion
270    
271    
272 william 117 public Main() : this(false) { }
273     public Main(bool no_console_redirect)
274 william 5 {
275     InitializeComponent();
276 william 158 #if SHOW_DEBUG_MENU
277     mnuDebug.Visible = true;
278     #else
279     mnuDebug.Visible = false;
280     #endif
281 william 83 load_loggerflags();
282 william 144 SetupDocks();
283     LoggerInstance = m_LogWindow.Logwriter;
284 william 23 LoggerInstance.CreateNewLog(false);
285 william 112 logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name);
286 william 87 load_plugins();
287 william 117 if (no_console_redirect)
288     LoggerInstance.RedirectConsoleOutput = false;
289 william 158
290    
291 william 144
292 william 5 }
293 william 83 private void load_loggerflags()
294     {
295 william 111 logger.SetLoggingFlags(Logging.Properties.Settings.Default.LoggingFlags);
296 william 99 #if FORCE_ALL_LOGGING_FLAGS
297     logger.SetLoggingFlags(loggerflags.ALL);
298     #endif
299 william 83 }
300 william 87 private void load_plugins()
301     {
302     loader = new PluginLoader();
303     loader.LoadPlugins();
304 william 83
305 william 87 ConfigPlugin = loader.GetConfigPlugin(RomCheater.Properties.Settings.Default.LastConfigPlugin);
306     if (ConfigPlugin != null)
307     logger.Info.WriteLine("Loaded Config Plugin: {0}", ConfigPlugin.ToString());
308     InputPlugin = loader.GetInputPlugin(RomCheater.Properties.Settings.Default.LastInputPlugin);
309     if (InputPlugin != null)
310     logger.Info.WriteLine("Loaded Input Plugin: {0}", InputPlugin.ToString());
311     WindowPlugin = loader.GetWindowPlugin(RomCheater.Properties.Settings.Default.LastWindowPlugin);
312     if (WindowPlugin != null)
313     logger.Info.WriteLine("Loaded Window Plugin: {0}", WindowPlugin.ToString());
314    
315 william 153 m_PIDSelector.AcceptedPlugin = ConfigPlugin;
316     m_RamDump.AcceptedPlugin = ConfigPlugin;
317 william 267 m_memoryview.AcceptedPlugin = ConfigPlugin;
318     m_memsearcher.AcceptedPlugin = ConfigPlugin;
319    
320 william 153 if (this.SelectedProcess != null)
321 william 267 {
322 william 153 m_RamDump.AcceptedProcess = SelectedProcess;
323 william 267 m_memoryview.AcceptedProcess = SelectedProcess;
324     m_memsearcher.AcceptedProcess = SelectedProcess;
325     }
326 william 152
327 william 87 }
328    
329 william 14 private void mnuItemExit_Click(object sender, EventArgs e)
330     {
331     this.Close();
332     }
333 william 16
334 william 17
335     private void Main_Load(object sender, EventArgs e)
336 william 144 {
337 william 152
338 william 17 }
339 william 63
340     private void mnuItemConfig_Click(object sender, EventArgs e)
341     {
342 william 86 RomCheaterConfigDialog dlg = new RomCheaterConfigDialog(loader);
343 william 63 dlg.ShowDialog();
344 william 270 logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name);
345 william 92 // reload plugins
346     load_plugins();
347 william 270
348 william 63 }
349 william 69
350     private void mnuItemOpenProcess_Click(object sender, EventArgs e)
351     {
352 william 148 ////List<Process> procs = ConfigPlugin.ValidProcessesForPlugin;
353     //PIDSelector selector = new PIDSelector(ConfigPlugin);
354     //selector.ShowDialog();
355 william 69 }
356 william 104
357 william 144 private void Main_Shown(object sender, EventArgs e)
358 william 104 {
359 william 152 //dockPanel.SuspendLayout(true);
360     //ShowDocks();
361     string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
362     if (File.Exists(configFile))
363     {
364     try
365     {
366     dockPanel.LoadFromXml(configFile, m_deserializeDockContent);
367 william 313 SetupDockWindowHandler();
368 william 152 }
369 william 177 catch (Exception)
370 william 152 {
371     this.Controls.Remove(dockPanel);
372     dockPanel = new DockPanel();
373     dockPanel.Dock = DockStyle.Fill;
374     dockPanel.DocumentStyle = DocumentStyle.DockingWindow;
375     this.Controls.Add(dockPanel);
376     ShowDocks();
377     }
378     }
379     else
380     {
381     ShowDocks();
382     }
383    
384     //dockPanel.ResumeLayout(true, true);
385 william 104 }
386 william 144
387     private void mnuItemShowLogWindow_Click(object sender, EventArgs e)
388     {
389     ShowLogWindow();
390     }
391 william 147
392     private void mnuItemHelpAbout_Click(object sender, EventArgs e)
393     {
394     ShowAboutBox();
395     }
396 william 148
397     private void mnuItemShowRamDumpDialog_Click(object sender, EventArgs e)
398     {
399     ShowRamDump();
400     }
401    
402     private void mnuItemShowPIDSelector_Click(object sender, EventArgs e)
403     {
404     ShowPidSelector();
405     }
406 william 201 private void mnuItemShowMemoryView_Click(object sender, EventArgs e)
407     {
408     ShowMemoryView();
409     }
410 william 152 private void Main_FormClosing(object sender, FormClosingEventArgs e)
411     {
412     string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
413     if (m_bSaveLayout)
414     dockPanel.SaveAsXml(configFile);
415     else if (File.Exists(configFile))
416     File.Delete(configFile);
417     }
418 william 158
419     private void mnuTestExeParse_Click(object sender, EventArgs e)
420     {
421 william 159 PEReader reader = new PEReader(@"C:\Windows\System32\notepad.exe");
422 william 158 }
423 william 201
424     private void mnuItemFindMaxNonNegativeHexValue_Click(object sender, EventArgs e)
425     {
426    
427     //uint start = 0xf0000000;
428     uint end = uint.MaxValue;
429     //for (uint i = start; i < end; i++)
430     //{
431     ulong value = Convert.ToUInt64(end.ToString(), 16);
432     //}
433     }
434 william 218
435     private void mnuItemShowDataTypeConverter_Click(object sender, EventArgs e)
436     {
437     ShowDataTypeConverter();
438     }
439 william 227
440     private void mnuItemShowMemorySearch_Click(object sender, EventArgs e)
441     {
442     ShowMemorySearch();
443     }
444 william 260
445     private void mnuItemDateShift_Click(object sender, EventArgs e)
446     {
447     DateTime t = DateTime.Now;
448     int year = Convert.ToInt32(t.ToString("yy"));
449     int y = year << 9;
450     int m = t.Month << 5;
451     int d = t.Day;
452    
453     ushort v1 = (ushort)(y | m | d);
454    
455     t = t.AddDays(1);
456     year = Convert.ToInt32(t.ToString("yy"));
457     y = year << 9;
458     m = t.Month << 5;
459     d = t.Day;
460     ushort v2 = (ushort)(y | m | d);
461    
462     t = t.AddDays(1);
463     year = Convert.ToInt32(t.ToString("yy"));
464     y = year << 9;
465     m = t.Month << 5;
466     d = t.Day;
467     ushort v3 = (ushort)(y | m | d);
468    
469     t = new DateTime(9999,12,31);
470     year = Convert.ToInt32(t.ToString("yy"));
471     y = year << 9;
472     m = t.Month << 5;
473     d = t.Day;
474     ushort v4 = (ushort)(y | m | d);
475     }
476 william 318
477     private void mnuItemShowPEViewer_Click(object sender, EventArgs e)
478     {
479     ShowPEViewer();
480     }
481    
482 william 201
483 william 5 }
484     }

  ViewVC Help
Powered by ViewVC 1.1.22