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

Contents of /trunk/RomCheater/Main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 408 - (show annotations) (download)
Thu Jun 21 18:10:21 2012 UTC (8 years, 5 months ago) by william
File size: 21634 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22