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

Contents of /trunk/RomCheater/Main.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22