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

Contents of /trunk/RomCheater/Main.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

1 #define FORCE_ALL_LOGGING_FLAGS // when defined will force logging flags to ALL
2 #define SHOW_DEBUG_MENU // when defined will show the debug menu or else it will be hidden
3 using System;
4 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 using RomCheater.Logging;
12 using RomCheater.Properties;
13 using RomCheater.UserSettingsSupport;
14 using RomCheater.PluginFramework.Core;
15 using System.Diagnostics;
16 using RomCheater.PluginFramework.Interfaces;
17 using WeifenLuo.WinFormsUI.Docking;
18 using RomCheater.Docking;
19 using System.IO;
20 using Sojaner.MemoryScanner;
21 using RomCheater.PluginFramework.Events;
22
23 namespace RomCheater
24 {
25 public partial class Main : Form
26 {
27 private bool m_bSaveLayout = true;
28 private Process SelectedProcess = null;
29 private DeserializeDockContent m_deserializeDockContent;
30 private FloatingLogWindow m_LogWindow = new FloatingLogWindow();
31 private FloatingAboutBox m_AboutBox = new FloatingAboutBox();
32 private FloatingRamDumperDialog m_RamDump = new FloatingRamDumperDialog();
33 private PIDSelector m_PIDSelector = new PIDSelector();
34 private FloatingMemoryView m_memoryview = new FloatingMemoryView();
35 private FloatingDataTypeConverter m_typeconverter = new FloatingDataTypeConverter();
36 private FloatingMemorySearcher m_memsearcher = new FloatingMemorySearcher();
37 private FloatingPEViewer m_peviewer = new FloatingPEViewer();
38 //private bool log_window_expanded = false;
39 //private double log_window_splitter_default_position = 1.4045;
40 PluginLoader loader = null;
41 IConfigPlugin ConfigPlugin = null;
42 IInputPlugin InputPlugin = null;
43 IWindowPlugin WindowPlugin = null;
44 static Main() { SettingSubscriber.AddSubscriber(new Main(), Settings.Default); }
45 private const string t = "RomCheater";
46 #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 private void OnProcessChanged(ProcessChangedEventArgs e)
57 {
58 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 SelectedProcess = Process.GetProcessById(e.ProcessID);
66 m_RamDump.AcceptedPlugin = this.ConfigPlugin;
67 m_RamDump.AcceptedProcess = SelectedProcess;
68
69 m_memoryview.AcceptedPlugin = this.ConfigPlugin;
70 m_memoryview.AcceptedProcess = SelectedProcess;
71
72 m_memsearcher.AcceptedPlugin = this.ConfigPlugin;
73 m_memsearcher.AcceptedProcess = SelectedProcess;
74 m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion);
75
76 m_peviewer.AcceptedPlugin = this.ConfigPlugin;
77 m_peviewer.AcceptedProcess = SelectedProcess;
78
79 }
80 private void OnBrowseMemoryRegion(BrowseMemoryRegionEvent e)
81 {
82 m_memoryview.BrowseMemoryRegion(e.MemoryRegion);
83 }
84
85 #region Dock Support
86 void AddDockToWindowList(object sender, EventArgs e)
87 {
88 DockContent dc;
89 TypeBinder.Bind(sender, out dc);
90 ToolStripMenuItem tsmi = new ToolStripMenuItem(dc.Text);
91 tsmi.Name = dc.Name;
92 tsmi.Tag = dc;
93 tsmi.Click += new EventHandler(tsmi_Click);
94 mnuWindows.DropDownItems.Add(tsmi);
95 }
96 void tsmi_Click(object sender, EventArgs e)
97 {
98 ToolStripMenuItem tsmi;
99 TypeBinder.Bind(sender, out tsmi);
100 DockContent dc;
101 TypeBinder.Bind(tsmi.Tag, out dc);
102 dc.Activate();
103 }
104 void RemoveDockFromWindowList(object sender, FormClosedEventArgs e)
105 {
106 DockContent dc;
107 TypeBinder.Bind(sender, out dc);
108 mnuWindows.DropDownItems.RemoveByKey(dc.Name);
109 }
110 private IDockContent GetContentFromPersistString(string persistString)
111 {
112 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 if (persistString == typeof(FloatingPEViewer).ToString()) { return m_peviewer; }
119 else { return null; }
120 }
121 public void SetupDocks()
122 {
123 m_LogWindow = new FloatingLogWindow();
124 m_AboutBox = new FloatingAboutBox();
125 m_RamDump = new FloatingRamDumperDialog();
126 m_PIDSelector = new PIDSelector();
127 m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged);
128 m_memoryview = new FloatingMemoryView();
129 m_typeconverter = new FloatingDataTypeConverter();
130 m_memsearcher = new FloatingMemorySearcher();
131 m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion);
132 m_peviewer = new FloatingPEViewer();
133 m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
134 }
135 #region SetupDockWindowHandler support
136 public void SetupDockWindowHandler()
137 {
138 SetupLogWindowHandler();
139 SetupRamDumpWindowHandler();
140 SetupMemoryViewWindowHandler();
141 SetupPIDSelectorWindowHandler();
142 SetupDataTypeConverterWindowHandler();
143 SetupMemorySearchWindowHandler();
144 SetupPEViewerWindowHandler();
145 }
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 m_LogWindow.Activate();
152 }
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 m_RamDump.Activate();
159 }
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 m_memoryview.Activate();
166 }
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 m_PIDSelector.Activate();
173 }
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 m_typeconverter.Activate();
180 }
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 m_memsearcher.Activate();
187 }
188 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 #endregion
196 public void ShowDocks()
197 {
198 ShowLogWindow();
199 SetupLogWindowHandler();
200 //ShowAboutBox();
201 ShowRamDump();
202 SetupRamDumpWindowHandler();
203 ShowMemoryView();
204 SetupMemoryViewWindowHandler();
205 ShowPidSelector();
206 SetupPIDSelectorWindowHandler();
207 ShowDataTypeConverter();
208 SetupDataTypeConverterWindowHandler();
209 ShowMemorySearch();
210 SetupMemorySearchWindowHandler();
211 ShowPEViewer();
212 SetupPEViewerWindowHandler();
213 }
214 public void ShowLogWindow()
215 {
216 if (m_LogWindow == null || m_LogWindow.IsDisposed) { m_LogWindow = new FloatingLogWindow(); }
217 LoggerInstance = m_LogWindow.Logwriter;
218 LoggerInstance.CreateNewLog(false);
219 m_LogWindow.Show(dockPanel, DockState.DockBottom);
220 }
221 public void ShowAboutBox()
222 {
223 if (m_AboutBox == null || m_AboutBox.IsDisposed) { m_AboutBox = new FloatingAboutBox(); }
224 m_AboutBox.ShowDialog();
225 }
226 public void ShowRamDump()
227 {
228 load_plugins();
229 m_RamDump = new FloatingRamDumperDialog(ConfigPlugin);
230 m_RamDump.AcceptedProcess = SelectedProcess;
231 m_RamDump.Show(dockPanel);
232 }
233 public void ShowMemoryView()
234 {
235 load_plugins();
236 m_memoryview = new FloatingMemoryView(ConfigPlugin);
237 m_memoryview.AcceptedProcess = SelectedProcess;
238 m_memoryview.Show(dockPanel);
239 }
240 public void ShowPidSelector()
241 {
242 load_plugins();
243 //List<Process> procs = ConfigPlugin.ValidProcessesForPlugin;
244 m_PIDSelector = new PIDSelector(ConfigPlugin);
245 m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged);
246 m_PIDSelector.Show(dockPanel);
247 }
248 public void ShowDataTypeConverter()
249 {
250 if (m_typeconverter == null || m_typeconverter.IsDisposed) { m_typeconverter = new FloatingDataTypeConverter(); }
251 m_typeconverter.Show(dockPanel, DockState.DockRightAutoHide);
252 }
253 public void ShowMemorySearch()
254 {
255 load_plugins();
256 m_memsearcher = new FloatingMemorySearcher(ConfigPlugin);
257 m_memsearcher.AcceptedProcess = SelectedProcess;
258 m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion);
259 m_memsearcher.Show(dockPanel);
260 }
261 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
269 #endregion
270
271
272 public Main() : this(false) { }
273 public Main(bool no_console_redirect)
274 {
275 InitializeComponent();
276 #if SHOW_DEBUG_MENU
277 mnuDebug.Visible = true;
278 #else
279 mnuDebug.Visible = false;
280 #endif
281 load_loggerflags();
282 SetupDocks();
283 LoggerInstance = m_LogWindow.Logwriter;
284 LoggerInstance.CreateNewLog(false);
285 logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name);
286 load_plugins();
287 if (no_console_redirect)
288 LoggerInstance.RedirectConsoleOutput = false;
289
290
291
292 }
293 private void load_loggerflags()
294 {
295 logger.SetLoggingFlags(Logging.Properties.Settings.Default.LoggingFlags);
296 #if FORCE_ALL_LOGGING_FLAGS
297 logger.SetLoggingFlags(loggerflags.ALL);
298 #endif
299 }
300 private void load_plugins()
301 {
302 loader = new PluginLoader();
303 loader.LoadPlugins();
304
305 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 m_PIDSelector.AcceptedPlugin = ConfigPlugin;
316 m_RamDump.AcceptedPlugin = ConfigPlugin;
317 m_memoryview.AcceptedPlugin = ConfigPlugin;
318 m_memsearcher.AcceptedPlugin = ConfigPlugin;
319
320 if (this.SelectedProcess != null)
321 {
322 m_RamDump.AcceptedProcess = SelectedProcess;
323 m_memoryview.AcceptedProcess = SelectedProcess;
324 m_memsearcher.AcceptedProcess = SelectedProcess;
325 }
326
327 }
328
329 private void mnuItemExit_Click(object sender, EventArgs e)
330 {
331 this.Close();
332 }
333
334
335 private void Main_Load(object sender, EventArgs e)
336 {
337
338 }
339
340 private void mnuItemConfig_Click(object sender, EventArgs e)
341 {
342 RomCheaterConfigDialog dlg = new RomCheaterConfigDialog(loader);
343 dlg.ShowDialog();
344 logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name);
345 // reload plugins
346 load_plugins();
347
348 }
349
350 private void mnuItemOpenProcess_Click(object sender, EventArgs e)
351 {
352 ////List<Process> procs = ConfigPlugin.ValidProcessesForPlugin;
353 //PIDSelector selector = new PIDSelector(ConfigPlugin);
354 //selector.ShowDialog();
355 }
356
357 private void Main_Shown(object sender, EventArgs e)
358 {
359 //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 SetupDockWindowHandler();
368 }
369 catch (Exception)
370 {
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 }
386
387 private void mnuItemShowLogWindow_Click(object sender, EventArgs e)
388 {
389 ShowLogWindow();
390 }
391
392 private void mnuItemHelpAbout_Click(object sender, EventArgs e)
393 {
394 ShowAboutBox();
395 }
396
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 private void mnuItemShowMemoryView_Click(object sender, EventArgs e)
407 {
408 ShowMemoryView();
409 }
410 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
419 private void mnuTestExeParse_Click(object sender, EventArgs e)
420 {
421 PEReader reader = new PEReader(@"C:\Windows\System32\notepad.exe");
422 }
423
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
435 private void mnuItemShowDataTypeConverter_Click(object sender, EventArgs e)
436 {
437 ShowDataTypeConverter();
438 }
439
440 private void mnuItemShowMemorySearch_Click(object sender, EventArgs e)
441 {
442 ShowMemorySearch();
443 }
444
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
477 private void mnuItemShowPEViewer_Click(object sender, EventArgs e)
478 {
479 ShowPEViewer();
480 }
481
482
483 }
484 }

  ViewVC Help
Powered by ViewVC 1.1.22