ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Main.cs
Revision: 437
Committed: Tue May 28 18:08:23 2013 UTC (10 years, 6 months ago) by william
File size: 22518 byte(s)
Log Message:
+ current wip for custom usercontrol plugins

File Contents

# Content
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 ShowMemorySearch();
248 SetupMemorySearchWindowHandler();
249 ShowPEViewer();
250 SetupPEViewerWindowHandler();
251 ShowRVACalculator();
252 SetupRVACalculatorWindowHandler();
253 }
254 public void ShowLogWindow()
255 {
256 if (m_LogWindow == null || m_LogWindow.IsDisposed) { m_LogWindow = new FloatingLogWindow(); }
257 LoggerInstance = m_LogWindow.Logwriter;
258 LoggerInstance.CreateNewLog(false);
259 m_LogWindow.Show(dockPanel, DockState.DockBottom);
260 }
261 public void ShowAboutBox()
262 {
263 if (m_AboutBox == null || m_AboutBox.IsDisposed) { m_AboutBox = new FloatingAboutBox(); }
264 m_AboutBox.ShowDialog();
265 }
266 public void ShowRamDump()
267 {
268 load_plugins_silent();
269 m_RamDump = new FloatingRamDumperDialog(ConfigPlugin);
270 m_RamDump.AcceptedProcess = SelectedProcess;
271 m_RamDump.Show(dockPanel);
272 }
273 public void ShowMemoryView()
274 {
275 load_plugins_silent();
276 m_memoryview = new FloatingMemoryView(ConfigPlugin);
277 m_memoryview.AcceptedProcess = SelectedProcess;
278 m_memoryview.Show(dockPanel);
279 }
280 public void ShowPidSelector()
281 {
282 load_plugins_silent();
283 //List<Process> procs = ConfigPlugin.ValidProcessesForPlugin;
284 m_PIDSelector = new FloatingPIDSelector(ConfigPlugin);
285 m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged);
286 m_PIDSelector.Show(dockPanel);
287 }
288 public void ShowDataTypeConverter()
289 {
290 if (m_typeconverter == null || m_typeconverter.IsDisposed) { m_typeconverter = new FloatingDataTypeConverter(); }
291 m_typeconverter.Show(dockPanel, DockState.DockRightAutoHide);
292 }
293 public void ShowMemorySearch()
294 {
295 load_plugins_silent();
296 m_memsearcher = new FloatingMemorySearcher(ConfigPlugin);
297 m_memsearcher.AcceptedProcess = SelectedProcess;
298 m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion);
299 m_memsearcher.Show(dockPanel);
300 }
301 public void ShowPEViewer()
302 {
303 load_plugins_silent();
304 m_peviewer = new FloatingPEViewer(ConfigPlugin);
305 m_peviewer.AcceptedProcess = SelectedProcess;
306 m_peviewer.OnPEDataUpdated += new BaseEventHandler<PEViewerDataUpdatedEventArgs>(OnPEDataUpdated);
307 m_peviewer.Show(dockPanel);
308 }
309 public void ShowRVACalculator()
310 {
311 load_plugins_silent();
312 m_rvacalc = new FloatingRVACalculator(ConfigPlugin);
313 m_rvacalc.AcceptedProcess = SelectedProcess;
314 m_rvacalc.Show(dockPanel, DockState.DockRightAutoHide);
315 }
316 #endregion
317
318
319 public Main() : this(false) { }
320 public Main(bool no_console_redirect)
321 {
322 InitializeComponent();
323 #if SHOW_DEBUG_MENU
324 mnuDebug.Visible = true;
325 #else
326 mnuDebug.Visible = false;
327 #endif
328 load_loggerflags();
329 SetupDocks();
330 LoggerInstance = m_LogWindow.Logwriter;
331 LoggerInstance.CreateNewLog(false);
332 logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name);
333 load_plugins();
334 if (no_console_redirect)
335 LoggerInstance.RedirectConsoleOutput = false;
336
337
338
339 }
340 private void load_loggerflags()
341 {
342 logger.SetLoggingFlags(Logging.Properties.Settings.Default.LoggingFlags);
343 #if FORCE_ALL_LOGGING_FLAGS
344 logger.SetLoggingFlags(loggerflags.ALL);
345 #endif
346 }
347 private void load_plugins() { load_plugins(false); }
348 private void load_plugins_silent() { load_plugins(true); }
349 private void load_plugins(bool silent)
350 {
351 loader = new PluginLoader();
352 loader.LoadPlugins(silent);
353
354 ConfigPlugin = loader.GetConfigPlugin(RomCheater.Properties.Settings.Default.LastConfigPlugin);
355 if (ConfigPlugin != null && !silent)
356 logger.Info.WriteLine("Loaded Config Plugin: {0}", ConfigPlugin.ToString());
357 InputPlugin = loader.GetInputPlugin(RomCheater.Properties.Settings.Default.LastInputPlugin);
358 if (InputPlugin != null && !silent)
359 logger.Info.WriteLine("Loaded Input Plugin: {0}", InputPlugin.ToString());
360 WindowPlugin = loader.GetWindowPlugin(RomCheater.Properties.Settings.Default.LastWindowPlugin);
361 if (WindowPlugin != null && !silent)
362 logger.Info.WriteLine("Loaded Window Plugin: {0}", WindowPlugin.ToString());
363
364
365 UserControlPlugins = new List<IUserControlPlugin>(loader.LoadedUserControlPlugins);
366 if (UserControlPlugins != null && !silent && UserControlPlugins.Count > 0)
367 {
368 foreach (var t in UserControlPlugins)
369 {
370 logger.Info.WriteLine("Loaded UserControl Plugin: {0}", t.ToString());
371 }
372 }
373
374
375
376 m_PIDSelector.AcceptedPlugin = ConfigPlugin;
377 m_RamDump.AcceptedPlugin = ConfigPlugin;
378 m_memoryview.AcceptedPlugin = ConfigPlugin;
379 m_memsearcher.AcceptedPlugin = ConfigPlugin;
380 m_PIDSelector.AcceptedPlugin = ConfigPlugin;
381 if (this.SelectedProcess != null)
382 {
383 m_RamDump.AcceptedProcess = SelectedProcess;
384 m_memoryview.AcceptedProcess = SelectedProcess;
385 m_memsearcher.AcceptedProcess = SelectedProcess;
386 }
387
388 }
389
390 private void mnuItemExit_Click(object sender, EventArgs e)
391 {
392 this.Close();
393 }
394
395
396 private void Main_Load(object sender, EventArgs e)
397 {
398
399 }
400
401 private void mnuItemConfig_Click(object sender, EventArgs e)
402 {
403 RomCheaterConfigDialog dlg = new RomCheaterConfigDialog(loader);
404 dlg.ShowDialog();
405 logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name);
406 // reload plugins
407 load_plugins(true);
408 }
409
410 private void mnuItemOpenProcess_Click(object sender, EventArgs e)
411 {
412 ////List<Process> procs = ConfigPlugin.ValidProcessesForPlugin;
413 //PIDSelector selector = new PIDSelector(ConfigPlugin);
414 //selector.ShowDialog();
415 }
416
417 private void Main_Shown(object sender, EventArgs e)
418 {
419 //dockPanel.SuspendLayout(true);
420 //ShowDocks();
421 string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
422 if (File.Exists(configFile))
423 {
424 try
425 {
426 dockPanel.LoadFromXml(configFile, m_deserializeDockContent);
427 SetupDockWindowHandler();
428 }
429 catch (Exception)
430 {
431 this.Controls.Remove(dockPanel);
432 dockPanel = new DockPanel();
433 dockPanel.Dock = DockStyle.Fill;
434 dockPanel.DocumentStyle = DocumentStyle.DockingWindow;
435 this.Controls.Add(dockPanel);
436 ShowDocks();
437 }
438 }
439 else
440 {
441 ShowDocks();
442 }
443
444 //dockPanel.ResumeLayout(true, true);
445 }
446
447 private void mnuItemShowLogWindow_Click(object sender, EventArgs e)
448 {
449 ShowLogWindow();
450 }
451
452 private void mnuItemHelpAbout_Click(object sender, EventArgs e)
453 {
454 ShowAboutBox();
455 }
456
457 private void mnuItemShowRamDumpDialog_Click(object sender, EventArgs e)
458 {
459 ShowRamDump();
460 }
461
462 private void mnuItemShowPIDSelector_Click(object sender, EventArgs e)
463 {
464 ShowPidSelector();
465 }
466 private void mnuItemShowMemoryView_Click(object sender, EventArgs e)
467 {
468 ShowMemoryView();
469 }
470 private void Main_FormClosing(object sender, FormClosingEventArgs e)
471 {
472 string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
473 if (m_bSaveLayout)
474 dockPanel.SaveAsXml(configFile);
475 else if (File.Exists(configFile))
476 File.Delete(configFile);
477 }
478
479 private void mnuTestExeParse_Click(object sender, EventArgs e)
480 {
481 IPEDData peData = PEDataWrapper.GetPEData((IAcceptsProcessAndConfig)this);
482 }
483
484 private void mnuItemFindMaxNonNegativeHexValue_Click(object sender, EventArgs e)
485 {
486
487 //uint start = 0xf0000000;
488 uint end = uint.MaxValue;
489 //for (uint i = start; i < end; i++)
490 //{
491 ulong value = Convert.ToUInt64(end.ToString(), 16);
492 //}
493 }
494
495 private void mnuItemShowDataTypeConverter_Click(object sender, EventArgs e)
496 {
497 ShowDataTypeConverter();
498 }
499
500 private void mnuItemShowMemorySearch_Click(object sender, EventArgs e)
501 {
502 ShowMemorySearch();
503 }
504
505 private void mnuItemDateShift_Click(object sender, EventArgs e)
506 {
507 DateTime t = DateTime.Now;
508 int year = Convert.ToInt32(t.ToString("yy"));
509 int y = year << 9;
510 int m = t.Month << 5;
511 int d = t.Day;
512
513 ushort v1 = (ushort)(y | m | d);
514
515 t = t.AddDays(1);
516 year = Convert.ToInt32(t.ToString("yy"));
517 y = year << 9;
518 m = t.Month << 5;
519 d = t.Day;
520 ushort v2 = (ushort)(y | m | d);
521
522 t = t.AddDays(1);
523 year = Convert.ToInt32(t.ToString("yy"));
524 y = year << 9;
525 m = t.Month << 5;
526 d = t.Day;
527 ushort v3 = (ushort)(y | m | d);
528
529 t = new DateTime(9999,12,31);
530 year = Convert.ToInt32(t.ToString("yy"));
531 y = year << 9;
532 m = t.Month << 5;
533 d = t.Day;
534 ushort v4 = (ushort)(y | m | d);
535 }
536
537 private void mnuItemShowPEViewer_Click(object sender, EventArgs e)
538 {
539 ShowPEViewer();
540 }
541
542 private void mnuItemShowRVACalculator_Click(object sender, EventArgs e)
543 {
544 ShowRVACalculator();
545 }
546 }
547 }