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