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

Contents of /trunk/RomCheater/Main.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22