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

Contents of /trunk/RomCheater/Main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 415 - (show annotations) (download)
Fri Jun 22 08:33:51 2012 UTC (7 years, 8 months ago) by william
File size: 22037 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22