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

Contents of /trunk/RomCheater/Main.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 438 - (show annotations) (download)
Tue May 28 18:22:07 2013 UTC (7 years, 11 months ago) by william
File size: 22550 byte(s)
+ fix issue with docks not displaying properly when they have not been setup

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

  ViewVC Help
Powered by ViewVC 1.1.22