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_memoryview.AcceptedPlugin = this.ConfigPlugin; |
70 |
m_memoryview.AcceptedProcess = SelectedProcess; |
71 |
|
72 |
m_memsearcher.AcceptedPlugin = this.ConfigPlugin; |
73 |
m_memsearcher.AcceptedProcess = SelectedProcess; |
74 |
m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion); |
75 |
|
76 |
m_peviewer.AcceptedPlugin = this.ConfigPlugin; |
77 |
m_peviewer.AcceptedProcess = SelectedProcess; |
78 |
m_peviewer.OnPEDataUpdated += new BaseEventHandler<PEViewerDataUpdatedEvent>(OnPEDataUpdated); |
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.Activate(); |
166 |
} |
167 |
private void SetupMemoryViewWindowHandler() |
168 |
{ |
169 |
if (m_memoryview == null) return; |
170 |
m_memoryview.Shown += new EventHandler(AddDockToWindowList); |
171 |
m_memoryview.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
172 |
m_memoryview.Activate(); |
173 |
} |
174 |
private void SetupPIDSelectorWindowHandler() |
175 |
{ |
176 |
if (m_PIDSelector == null) return; |
177 |
m_PIDSelector.Shown += new EventHandler(AddDockToWindowList); |
178 |
m_PIDSelector.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
179 |
m_PIDSelector.Activate(); |
180 |
} |
181 |
private void SetupDataTypeConverterWindowHandler() |
182 |
{ |
183 |
if (m_typeconverter == null) return; |
184 |
m_typeconverter.Shown += new EventHandler(AddDockToWindowList); |
185 |
m_typeconverter.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
186 |
m_typeconverter.Activate(); |
187 |
} |
188 |
private void SetupMemorySearchWindowHandler() |
189 |
{ |
190 |
if (m_memsearcher == null) return; |
191 |
m_memsearcher.Shown += new EventHandler(AddDockToWindowList); |
192 |
m_memsearcher.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
193 |
m_memsearcher.Activate(); |
194 |
} |
195 |
private void SetupPEViewerWindowHandler() |
196 |
{ |
197 |
if (m_peviewer == null) return; |
198 |
m_peviewer.Shown += new EventHandler(AddDockToWindowList); |
199 |
m_peviewer.FormClosed += new FormClosedEventHandler(RemoveDockFromWindowList); |
200 |
m_peviewer.Activate(); |
201 |
} |
202 |
#endregion |
203 |
public void ShowDocks() |
204 |
{ |
205 |
ShowLogWindow(); |
206 |
SetupLogWindowHandler(); |
207 |
//ShowAboutBox(); |
208 |
ShowRamDump(); |
209 |
SetupRamDumpWindowHandler(); |
210 |
ShowMemoryView(); |
211 |
SetupMemoryViewWindowHandler(); |
212 |
ShowPidSelector(); |
213 |
SetupPIDSelectorWindowHandler(); |
214 |
ShowDataTypeConverter(); |
215 |
SetupDataTypeConverterWindowHandler(); |
216 |
ShowMemorySearch(); |
217 |
SetupMemorySearchWindowHandler(); |
218 |
ShowPEViewer(); |
219 |
SetupPEViewerWindowHandler(); |
220 |
} |
221 |
public void ShowLogWindow() |
222 |
{ |
223 |
if (m_LogWindow == null || m_LogWindow.IsDisposed) { m_LogWindow = new FloatingLogWindow(); } |
224 |
LoggerInstance = m_LogWindow.Logwriter; |
225 |
LoggerInstance.CreateNewLog(false); |
226 |
m_LogWindow.Show(dockPanel, DockState.DockBottom); |
227 |
} |
228 |
public void ShowAboutBox() |
229 |
{ |
230 |
if (m_AboutBox == null || m_AboutBox.IsDisposed) { m_AboutBox = new FloatingAboutBox(); } |
231 |
m_AboutBox.ShowDialog(); |
232 |
} |
233 |
public void ShowRamDump() |
234 |
{ |
235 |
load_plugins(); |
236 |
m_RamDump = new FloatingRamDumperDialog(ConfigPlugin); |
237 |
m_RamDump.AcceptedProcess = SelectedProcess; |
238 |
m_RamDump.Show(dockPanel); |
239 |
} |
240 |
public void ShowMemoryView() |
241 |
{ |
242 |
load_plugins(); |
243 |
m_memoryview = new FloatingMemoryView(ConfigPlugin); |
244 |
m_memoryview.AcceptedProcess = SelectedProcess; |
245 |
m_memoryview.Show(dockPanel); |
246 |
} |
247 |
public void ShowPidSelector() |
248 |
{ |
249 |
load_plugins(); |
250 |
//List<Process> procs = ConfigPlugin.ValidProcessesForPlugin; |
251 |
m_PIDSelector = new PIDSelector(ConfigPlugin); |
252 |
m_PIDSelector.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(OnProcessChanged); |
253 |
m_PIDSelector.Show(dockPanel); |
254 |
} |
255 |
public void ShowDataTypeConverter() |
256 |
{ |
257 |
if (m_typeconverter == null || m_typeconverter.IsDisposed) { m_typeconverter = new FloatingDataTypeConverter(); } |
258 |
m_typeconverter.Show(dockPanel, DockState.DockRightAutoHide); |
259 |
} |
260 |
public void ShowMemorySearch() |
261 |
{ |
262 |
load_plugins(); |
263 |
m_memsearcher = new FloatingMemorySearcher(ConfigPlugin); |
264 |
m_memsearcher.AcceptedProcess = SelectedProcess; |
265 |
m_memsearcher.OnBrowseMemoryRegion += new BaseEventHandler<BrowseMemoryRegionEvent>(OnBrowseMemoryRegion); |
266 |
m_memsearcher.Show(dockPanel); |
267 |
} |
268 |
public void ShowPEViewer() |
269 |
{ |
270 |
load_plugins(); |
271 |
m_peviewer = new FloatingPEViewer(ConfigPlugin); |
272 |
m_peviewer.AcceptedProcess = SelectedProcess; |
273 |
m_peviewer.OnPEDataUpdated +=new BaseEventHandler<PEViewerDataUpdatedEvent>(OnPEDataUpdated); |
274 |
m_peviewer.Show(dockPanel); |
275 |
} |
276 |
|
277 |
#endregion |
278 |
|
279 |
|
280 |
public Main() : this(false) { } |
281 |
public Main(bool no_console_redirect) |
282 |
{ |
283 |
InitializeComponent(); |
284 |
#if SHOW_DEBUG_MENU |
285 |
mnuDebug.Visible = true; |
286 |
#else |
287 |
mnuDebug.Visible = false; |
288 |
#endif |
289 |
load_loggerflags(); |
290 |
SetupDocks(); |
291 |
LoggerInstance = m_LogWindow.Logwriter; |
292 |
LoggerInstance.CreateNewLog(false); |
293 |
logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name); |
294 |
load_plugins(); |
295 |
if (no_console_redirect) |
296 |
LoggerInstance.RedirectConsoleOutput = false; |
297 |
|
298 |
|
299 |
|
300 |
} |
301 |
private void load_loggerflags() |
302 |
{ |
303 |
logger.SetLoggingFlags(Logging.Properties.Settings.Default.LoggingFlags); |
304 |
#if FORCE_ALL_LOGGING_FLAGS |
305 |
logger.SetLoggingFlags(loggerflags.ALL); |
306 |
#endif |
307 |
} |
308 |
private void load_plugins() |
309 |
{ |
310 |
loader = new PluginLoader(); |
311 |
loader.LoadPlugins(); |
312 |
|
313 |
ConfigPlugin = loader.GetConfigPlugin(RomCheater.Properties.Settings.Default.LastConfigPlugin); |
314 |
if (ConfigPlugin != null) |
315 |
logger.Info.WriteLine("Loaded Config Plugin: {0}", ConfigPlugin.ToString()); |
316 |
InputPlugin = loader.GetInputPlugin(RomCheater.Properties.Settings.Default.LastInputPlugin); |
317 |
if (InputPlugin != null) |
318 |
logger.Info.WriteLine("Loaded Input Plugin: {0}", InputPlugin.ToString()); |
319 |
WindowPlugin = loader.GetWindowPlugin(RomCheater.Properties.Settings.Default.LastWindowPlugin); |
320 |
if (WindowPlugin != null) |
321 |
logger.Info.WriteLine("Loaded Window Plugin: {0}", WindowPlugin.ToString()); |
322 |
|
323 |
m_PIDSelector.AcceptedPlugin = ConfigPlugin; |
324 |
m_RamDump.AcceptedPlugin = ConfigPlugin; |
325 |
m_memoryview.AcceptedPlugin = ConfigPlugin; |
326 |
m_memsearcher.AcceptedPlugin = ConfigPlugin; |
327 |
|
328 |
if (this.SelectedProcess != null) |
329 |
{ |
330 |
m_RamDump.AcceptedProcess = SelectedProcess; |
331 |
m_memoryview.AcceptedProcess = SelectedProcess; |
332 |
m_memsearcher.AcceptedProcess = SelectedProcess; |
333 |
} |
334 |
|
335 |
} |
336 |
|
337 |
private void mnuItemExit_Click(object sender, EventArgs e) |
338 |
{ |
339 |
this.Close(); |
340 |
} |
341 |
|
342 |
|
343 |
private void Main_Load(object sender, EventArgs e) |
344 |
{ |
345 |
|
346 |
} |
347 |
|
348 |
private void mnuItemConfig_Click(object sender, EventArgs e) |
349 |
{ |
350 |
RomCheaterConfigDialog dlg = new RomCheaterConfigDialog(loader); |
351 |
dlg.ShowDialog(); |
352 |
logger.ForceLog.Info.WriteLine("LoggingFlags = 0x{0:x4} ({1})", logger.GetLoggingFlags().Value, logger.GetLoggingFlags().Name); |
353 |
// reload plugins |
354 |
load_plugins(); |
355 |
|
356 |
} |
357 |
|
358 |
private void mnuItemOpenProcess_Click(object sender, EventArgs e) |
359 |
{ |
360 |
////List<Process> procs = ConfigPlugin.ValidProcessesForPlugin; |
361 |
//PIDSelector selector = new PIDSelector(ConfigPlugin); |
362 |
//selector.ShowDialog(); |
363 |
} |
364 |
|
365 |
private void Main_Shown(object sender, EventArgs e) |
366 |
{ |
367 |
//dockPanel.SuspendLayout(true); |
368 |
//ShowDocks(); |
369 |
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config"); |
370 |
if (File.Exists(configFile)) |
371 |
{ |
372 |
try |
373 |
{ |
374 |
dockPanel.LoadFromXml(configFile, m_deserializeDockContent); |
375 |
SetupDockWindowHandler(); |
376 |
} |
377 |
catch (Exception) |
378 |
{ |
379 |
this.Controls.Remove(dockPanel); |
380 |
dockPanel = new DockPanel(); |
381 |
dockPanel.Dock = DockStyle.Fill; |
382 |
dockPanel.DocumentStyle = DocumentStyle.DockingWindow; |
383 |
this.Controls.Add(dockPanel); |
384 |
ShowDocks(); |
385 |
} |
386 |
} |
387 |
else |
388 |
{ |
389 |
ShowDocks(); |
390 |
} |
391 |
|
392 |
//dockPanel.ResumeLayout(true, true); |
393 |
} |
394 |
|
395 |
private void mnuItemShowLogWindow_Click(object sender, EventArgs e) |
396 |
{ |
397 |
ShowLogWindow(); |
398 |
} |
399 |
|
400 |
private void mnuItemHelpAbout_Click(object sender, EventArgs e) |
401 |
{ |
402 |
ShowAboutBox(); |
403 |
} |
404 |
|
405 |
private void mnuItemShowRamDumpDialog_Click(object sender, EventArgs e) |
406 |
{ |
407 |
ShowRamDump(); |
408 |
} |
409 |
|
410 |
private void mnuItemShowPIDSelector_Click(object sender, EventArgs e) |
411 |
{ |
412 |
ShowPidSelector(); |
413 |
} |
414 |
private void mnuItemShowMemoryView_Click(object sender, EventArgs e) |
415 |
{ |
416 |
ShowMemoryView(); |
417 |
} |
418 |
private void Main_FormClosing(object sender, FormClosingEventArgs e) |
419 |
{ |
420 |
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config"); |
421 |
if (m_bSaveLayout) |
422 |
dockPanel.SaveAsXml(configFile); |
423 |
else if (File.Exists(configFile)) |
424 |
File.Delete(configFile); |
425 |
} |
426 |
|
427 |
private void mnuTestExeParse_Click(object sender, EventArgs e) |
428 |
{ |
429 |
PEReader reader = new PEReader(@"C:\Windows\System32\notepad.exe"); |
430 |
} |
431 |
|
432 |
private void mnuItemFindMaxNonNegativeHexValue_Click(object sender, EventArgs e) |
433 |
{ |
434 |
|
435 |
//uint start = 0xf0000000; |
436 |
uint end = uint.MaxValue; |
437 |
//for (uint i = start; i < end; i++) |
438 |
//{ |
439 |
ulong value = Convert.ToUInt64(end.ToString(), 16); |
440 |
//} |
441 |
} |
442 |
|
443 |
private void mnuItemShowDataTypeConverter_Click(object sender, EventArgs e) |
444 |
{ |
445 |
ShowDataTypeConverter(); |
446 |
} |
447 |
|
448 |
private void mnuItemShowMemorySearch_Click(object sender, EventArgs e) |
449 |
{ |
450 |
ShowMemorySearch(); |
451 |
} |
452 |
|
453 |
private void mnuItemDateShift_Click(object sender, EventArgs e) |
454 |
{ |
455 |
DateTime t = DateTime.Now; |
456 |
int year = Convert.ToInt32(t.ToString("yy")); |
457 |
int y = year << 9; |
458 |
int m = t.Month << 5; |
459 |
int d = t.Day; |
460 |
|
461 |
ushort v1 = (ushort)(y | m | d); |
462 |
|
463 |
t = t.AddDays(1); |
464 |
year = Convert.ToInt32(t.ToString("yy")); |
465 |
y = year << 9; |
466 |
m = t.Month << 5; |
467 |
d = t.Day; |
468 |
ushort v2 = (ushort)(y | m | d); |
469 |
|
470 |
t = t.AddDays(1); |
471 |
year = Convert.ToInt32(t.ToString("yy")); |
472 |
y = year << 9; |
473 |
m = t.Month << 5; |
474 |
d = t.Day; |
475 |
ushort v3 = (ushort)(y | m | d); |
476 |
|
477 |
t = new DateTime(9999,12,31); |
478 |
year = Convert.ToInt32(t.ToString("yy")); |
479 |
y = year << 9; |
480 |
m = t.Month << 5; |
481 |
d = t.Day; |
482 |
ushort v4 = (ushort)(y | m | d); |
483 |
} |
484 |
|
485 |
private void mnuItemShowPEViewer_Click(object sender, EventArgs e) |
486 |
{ |
487 |
ShowPEViewer(); |
488 |
} |
489 |
|
490 |
|
491 |
} |
492 |
} |